extends KinematicBody # stats var curHp : int = 10 var maxHp : int = 10 var damage : int = 1 var gold : int = 0 var attackRate : float = 0.3 var lastAttackTime : int = 0 # physics var airborneSpeed : float = 3.0 var walkSpeed : float = 5.0 var runSpeed : float = 10.0 var jumpForce : float = 10.0 var gravity : float = 15.0 var vel := Vector3() # components onready var camera = get_node("CameraOrbit") onready var interactCast = get_node("InteractRayCast") onready var anim = get_node("Casual2_Female/AnimationPlayer") export var ui_path : NodePath onready var ui = get_node(ui_path) # called when the node is initialized func _ready (): # initialize the UI ui.update_health_bar(curHp, maxHp) ui.update_gold_text(gold) func _process(delta): # attack input if Controller.player_input_enabled and is_on_floor() and Input.is_action_just_pressed("interact"): try_interact() # called every physics step (60 times a second) func _physics_process (delta): var input := Vector3() # gravity vel.y -= gravity * delta if Controller.player_input_enabled : # movement inputs if Input.is_action_pressed("move_forward"): input.z += 1 if Input.is_action_pressed("move_backward"): input.z -= 1 if Input.is_action_pressed("move_left"): input.x += 1 if Input.is_action_pressed("move_right"): input.x -= 1 input = input.normalized() # get the relative direction var dir := (transform.basis.z * input.z + transform.basis.x * input.x) # apply the direction to our velocity if not is_on_floor(): vel.x = lerp(vel.x, dir.x * airborneSpeed, .5 * delta) vel.z = lerp(vel.z, dir.z * airborneSpeed, .5 * delta) elif Input.is_action_pressed("run"): vel.x = dir.x * runSpeed vel.z = dir.z * runSpeed else: vel.x = dir.x * walkSpeed vel.z = dir.z * walkSpeed # jump input if Input.is_action_pressed("jump") and is_on_floor(): vel.y = jumpForce else: vel.x = 0 vel.z = 0 if is_on_floor(): if anim.current_animation != "Punch": if input.x != 0 || input.z != 0: if Controller.player_input_enabled and Input.is_action_pressed("run"): anim.play("Run") else: anim.play("Walk") else: anim.play("Idle") else: anim.play("RecieveHit") # move along the current velocity var previous_vel = vel vel = move_and_slide(vel, Vector3.UP, true, 4, deg2rad(30)) if is_on_wall(): vel.y = min(previous_vel.y, vel.y) # called when we collect a coin func give_gold (amount): gold += amount # update the UI ui.update_gold_text(gold) # called when an enemy deals damage to us func take_damage (damageToTake): curHp -= damageToTake # update the UI ui.update_health_bar(curHp, maxHp) # if our health is 0, die if curHp <= 0: die() # called when our health reaches 0 func die (): # reload the scene get_tree().reload_current_scene() ## called when we press the attack button #func try_attack(): # # if we're not ready to attack, return # if OS.get_ticks_msec() - lastAttackTime < attackRate * 1000: # return # # # set the last attack time to now # lastAttackTime = OS.get_ticks_msec() # # # play the animation # anim.stop() # anim.play("Punch") # # # is the ray cast colliding with an enemy? # if attackCast.is_colliding(): # if attackCast.get_collider().has_method("take_damage"): # attackCast.get_collider().take_damage(damage) # if attackCast.get_collider().has_signal("on_interact"): # attackCast.get_collider().emit_signal("on_interact") # called when we press the interact button func try_interact(): # is the ray cast colliding with an enemy? if interactCast.is_colliding(): # play the animation anim.stop() anim.play("Idle") if interactCast.get_collider().has_signal("on_interact"): interactCast.get_collider().emit_signal("on_interact")