SchildDerStaerke/scripts/player.gd

80 lines
2.6 KiB
GDScript

extends Unit
class_name Player
@export var mouse_sensitivity := 0.006
@export var rotation_speed := 24.0
var attacks := ["1h_slice_diagonal","1h_slice_horizontal","1h_attack_chop"]
var blocking := false
@onready var spring_arm := $SpringArm3D
@export var gold := 0
signal gold_changed(current_gold)
func _ready() -> void:
died.connect(_on_died)
func _on_died() -> void:
print("Game Over!")
func add_gold(gold_amount: int) -> void:
gold += gold_amount
gold_changed.emit(gold)
func _physics_process(delta: float) -> void:
velocity.y += -gravity * delta
get_move_input(delta)
move_and_slide()
if velocity.length() > 1.0:
model.rotation.y = lerp_angle(model.rotation.y, spring_arm.rotation.y, rotation_speed * delta)
func _input(event: InputEvent) -> void:
#move camera
if event is InputEventMouseMotion:
spring_arm.rotation.x -= event.relative.y * mouse_sensitivity
spring_arm.rotation_degrees.x = clamp(spring_arm.rotation_degrees.x, -90.0, 30.0)
spring_arm.rotation.y -= event.relative.x * mouse_sensitivity
#attack
if event.is_action_pressed("attack"):
if enough_stamina_available(attack_cost):
use_stamina(attack_cost)
if blocking: anim_state.travel("Block_Attack")
else: anim_state.travel(attacks.pick_random())
#block
if Input.is_action_just_pressed("block"): blocking = true
if Input.is_action_just_released("block"): blocking = false
anim_tree.set("parameters/conditions/blocking", blocking)
anim_tree.set("parameters/conditions/not_blocking", !blocking)
#jump
if is_on_floor() and Input.is_action_just_pressed("jump"):
if enough_stamina_available(jump_cost):
use_stamina(jump_cost)
velocity.y = jump_speed
jumping = true
anim_tree.set("parameters/conditions/grounded", false)
# We just hit the floor after being in the air
if is_on_floor() and not last_floor:
jumping = false
anim_tree.set("parameters/conditions/grounded", true)
# We're in the air, but we didn't jump
if not is_on_floor() and not jumping:
anim_state.travel("Jump_Idle")
anim_tree.set("parameters/conditions/grounded", false)
anim_tree.set("parameters/conditions/jumping", jumping)
last_floor = is_on_floor()
func get_move_input(delta: float) -> void:
var vy = velocity.y
velocity.y = 0
var input = Input.get_vector("left", "right", "forward", "back")
var dir = Vector3(input.x, 0, input.y).rotated(Vector3.UP, spring_arm.rotation.y)
if blocking: dir = Vector3.ZERO
velocity = lerp(velocity, dir * speed, acceleration * delta)
var vl = velocity * model.transform.basis
anim_tree.set("parameters/IWR/blend_position", Vector2(vl.x, -vl.z) / speed)
velocity.y = vy