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 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 attackCast = get_node("AttackRayCast") 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 is_on_floor() and Input.is_action_just_pressed("attack"): try_attack() # called every physics step (60 times a second) func _physics_process (delta): vel.x = 0 vel.z = 0 var input := Vector3() # 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 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 # gravity vel.y -= gravity * delta # jump input if Input.is_action_pressed("jump") and is_on_floor(): vel.y = jumpForce if is_on_floor(): if anim.current_animation != "Punch": if input.x != 0 || input.z != 0: if 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 vel = move_and_slide(vel, Vector3.UP, true, 4, deg2rad(89)) # 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)