extends CharacterBody3D const SPEED = 5.0 const MOUSE_SENSE = 0.5 @onready var animation_tree : AnimationTree = $Player_HUD/Sword_UI/Sword_Sprite/AnimationPlayer/AnimationTree @export var health = 100 var can_swing = true var dead = false signal blocking signal attacking func _ready(): Input.mouse_mode = Input.MOUSE_MODE_CAPTURED $Player_HUD/Sword_UI/Sword_Sprite/AnimationPlayer.animation_finished.connect(done_swinging) $Player_HUD/Deathscreen/Panel/Button.button_up.connect(restart) add_to_group("Player") $Player_HUD/Sword_UI/Player_Health.value = health func _input(event): if dead: return if event is InputEventMouseMotion: rotation_degrees.y -= event.relative.x * MOUSE_SENSE rotation_degrees.x -= event.relative.y * MOUSE_SENSE if Input.is_action_just_pressed("Swing"): swing() if Input.is_action_just_pressed("Restart"): restart() func _physics_process(delta: float) -> void: update_animation_parameters() if not is_on_floor(): velocity += get_gravity() * delta $Player_HUD/Sword_UI/Player_Health.value = health if velocity.x or velocity.y != 0: $Player_Camera/AnimationPlayer.play("Viewbob") # Get the input direction and handle the movement/deceleration. # As good practice, you should replace UI actions with custom gameplay actions var input_dir := Input.get_vector("move_left", "move_right", "move_forwards", "move_backwards") var direction := (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized() if direction: velocity.x = direction.x * SPEED velocity.z = direction.z * SPEED else: velocity.x = move_toward(velocity.x, 0, SPEED) velocity.z = move_toward(velocity.z, 0, SPEED) move_and_slide() func swing(): if !can_swing: return can_swing = false emit_signal("attacking") $Swing_Sound.play() if $Player_Hitscan.is_colliding() and $Player_Hitscan.get_collider().has_method("kill"): $Player_Hitscan.get_collider().kill() func done_swinging(): can_swing = true func restart(): get_tree().reload_current_scene() func kill(): $Player_HUD/Sword_UI/Hurt.visible = true $Player_HUD/Sword_UI/Hurt/Hurt_Flash_Timer.start() if health == 0: dead = true $Player_HUD/Deathscreen.show() Input.mouse_mode = Input.MOUSE_MODE_VISIBLE $Player_Hurtbox.disabled = true else: return func _on_hurt_flash_timer_timeout() -> void: $Player_HUD/Sword_UI/Hurt.visible = false func block(): emit_signal("blocking") $Player_HUD/Sword_UI/AnimationPlayer/Parrying_Timer.start() func update_animation_parameters(): if Input.is_action_just_pressed("Block"): animation_tree["parameters/conditions/Blocking"] = true animation_tree["parameters/conditions/Idle"] = false animation_tree["parameters/conditions/Swinging"] = false else: animation_tree["parameters/conditions/Idle"] = true animation_tree["parameters/conditions/Blocking"] = false animation_tree["parameters/conditions/Swinging"] = false if Input.is_action_just_pressed("Swing"): animation_tree["parameters/conditions/Swinging"] = true animation_tree["parameters/conditions/Blocking"] = false animation_tree["parameters/conditions/Idle"] = false else: animation_tree["parameters/conditions/Idle"] = true animation_tree["parameters/conditions/Blocking"] = false animation_tree["parameters/conditions/Swinging"] = false