Kenney 2025-09-16 18:05:20 +07:00
commit a8113ca3ed
1 changed files with 35 additions and 44 deletions

@ -2,6 +2,7 @@ extends CharacterBody3D
@export_subgroup("Properties")
@export var movement_speed = 5
@export_range(0, 100) var number_of_jumps:int = 2
@export var jump_strength = 8
@export_subgroup("Weapons")
@ -25,8 +26,7 @@ var gravity := 0.0
var previously_floored := false
var jump_single := true
var jump_double := true
var jumps_remaining:int
var container_offset = Vector3(1.2, -1.1, -2.75)
@ -60,7 +60,7 @@ func _process(delta):
handle_gravity(delta)
# Movement
var applied_velocity: Vector3
movement_velocity = transform.basis * movement_velocity # Move forward
@ -71,13 +71,7 @@ func _process(delta):
velocity = applied_velocity
move_and_slide()
# Rotation
camera.rotation.z = lerp_angle(camera.rotation.z, -input_mouse.x * 25 * delta, delta * 5)
camera.rotation.x = lerp_angle(camera.rotation.x, rotation_target.x, delta * 25)
rotation.y = lerp_angle(rotation.y, rotation_target.y, delta * 25)
# Rotation
container.position = lerp(container.position, container_offset - (basis.inverse() * applied_velocity / 30), delta * 10)
# Movement sound
@ -107,13 +101,10 @@ func _process(delta):
func _input(event):
if event is InputEventMouseMotion and mouse_captured:
input_mouse = event.relative / mouse_sensitivity
rotation_target.y -= event.relative.x / mouse_sensitivity
rotation_target.x -= event.relative.y / mouse_sensitivity
handle_rotation(event.relative.x, event.relative.y, false)
func handle_controls(_delta):
func handle_controls(delta):
# Mouse capture
@ -128,17 +119,13 @@ func handle_controls(_delta):
input_mouse = Vector2.ZERO
# Movement
var input := Input.get_vector("move_left", "move_right", "move_forward", "move_back")
movement_velocity = Vector3(input.x, 0, input.y).normalized() * movement_speed
# Rotation
# Handle Controller Rotation
var rotation_input := Input.get_vector("camera_right", "camera_left", "camera_down", "camera_up")
rotation_target -= Vector3(-rotation_input.y, -rotation_input.x, 0).limit_length(1.0) * gamepad_sensitivity
rotation_target.x = clamp(rotation_target.x, deg_to_rad(-90), deg_to_rad(90))
if rotation_input:
handle_rotation(rotation_input.x, rotation_input.y, true, delta)
# Shooting
@ -148,39 +135,43 @@ func handle_controls(_delta):
if Input.is_action_just_pressed("jump"):
if jump_single or jump_double:
Audio.play("sounds/jump_a.ogg, sounds/jump_b.ogg, sounds/jump_c.ogg")
if jump_double:
gravity = -jump_strength
jump_double = false
if(jump_single): action_jump()
if jumps_remaining:
action_jump()
# Weapon switching
action_weapon_toggle()
# Camera rotation
func handle_rotation(xRot: float, yRot: float, isController: bool, delta: float = 0.0):
if isController:
rotation_target -= Vector3(-yRot, -xRot, 0).limit_length(1.0) * gamepad_sensitivity
rotation_target.x = clamp(rotation_target.x, deg_to_rad(-90), deg_to_rad(90))
camera.rotation.x = lerp_angle(camera.rotation.x, rotation_target.x, delta * 25)
rotation.y = lerp_angle(rotation.y, rotation_target.y, delta * 25)
else:
rotation_target += (Vector3(-yRot, -xRot, 0) / mouse_sensitivity)
rotation_target.x = clamp(rotation_target.x, deg_to_rad(-90), deg_to_rad(90))
camera.rotation.x = rotation_target.x;
rotation.y = rotation_target.y;
# Handle gravity
func handle_gravity(delta):
gravity += 20 * delta
if gravity > 0 and is_on_floor():
jump_single = true
jumps_remaining = number_of_jumps
gravity = 0
# Jumping
func action_jump():
gravity = -jump_strength
jump_single = false;
jump_double = true;
func action_jump():
Audio.play("sounds/jump_a.ogg, sounds/jump_b.ogg, sounds/jump_c.ogg")
gravity = -jump_strength
jumps_remaining -= 1
# Shooting
@ -192,10 +183,6 @@ func action_shoot():
Audio.play(weapon.sound_shoot)
container.position.z += 0.25 # Knockback of weapon visual
camera.rotation.x += 0.025 # Knockback of camera
movement_velocity += Vector3(0, 0, weapon.knockback) # Knockback
# Set muzzle flash position, play animation
muzzle.play("default")
@ -234,7 +221,11 @@ func action_shoot():
get_tree().root.add_child(impact_instance)
impact_instance.position = raycast.get_collision_point() + (raycast.get_collision_normal() / 10)
impact_instance.look_at(camera.global_transform.origin, Vector3.UP, true)
impact_instance.look_at(camera.global_transform.origin, Vector3.UP, true)
container.position.z += 0.25 # Knockback of weapon visual
camera.rotation.x += 0.025 # Knockback of camera
movement_velocity += Vector3(0, 0, weapon.knockback) # Knockback
# Toggle between available weapons (listed in 'weapons')