You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

130 lines
2.9 KiB

  1. extends KinematicBody
  2. # stats
  3. var curHp : int = 10
  4. var maxHp : int = 10
  5. var damage : int = 1
  6. var gold : int = 0
  7. var attackRate : float = 0.3
  8. var lastAttackTime : int = 0
  9. # physics
  10. var walkSpeed : float = 5.0
  11. var runSpeed : float = 10.0
  12. var jumpForce : float = 10.0
  13. var gravity : float = 15.0
  14. var vel := Vector3()
  15. # components
  16. onready var camera = get_node("CameraOrbit")
  17. onready var attackCast = get_node("AttackRayCast")
  18. onready var anim = get_node("Casual2_Female/AnimationPlayer")
  19. export var ui_path : NodePath
  20. onready var ui = get_node(ui_path)
  21. # called when the node is initialized
  22. func _ready ():
  23. # initialize the UI
  24. ui.update_health_bar(curHp, maxHp)
  25. ui.update_gold_text(gold)
  26. func _process(delta):
  27. # attack input
  28. if is_on_floor() and Input.is_action_just_pressed("attack"):
  29. try_attack()
  30. # called every physics step (60 times a second)
  31. func _physics_process (delta):
  32. vel.x = 0
  33. vel.z = 0
  34. var input := Vector3()
  35. # movement inputs
  36. if Input.is_action_pressed("move_forward"):
  37. input.z += 1
  38. if Input.is_action_pressed("move_backward"):
  39. input.z -= 1
  40. if Input.is_action_pressed("move_left"):
  41. input.x += 1
  42. if Input.is_action_pressed("move_right"):
  43. input.x -= 1
  44. input = input.normalized()
  45. # get the relative direction
  46. var dir := (transform.basis.z * input.z + transform.basis.x * input.x)
  47. # apply the direction to our velocity
  48. if Input.is_action_pressed("run"):
  49. vel.x = dir.x * runSpeed
  50. vel.z = dir.z * runSpeed
  51. else:
  52. vel.x = dir.x * walkSpeed
  53. vel.z = dir.z * walkSpeed
  54. # gravity
  55. vel.y -= gravity * delta
  56. # jump input
  57. if Input.is_action_pressed("jump") and is_on_floor():
  58. vel.y = jumpForce
  59. if is_on_floor():
  60. if anim.current_animation != "Punch":
  61. if input.x != 0 || input.z != 0:
  62. if Input.is_action_pressed("run"):
  63. anim.play("Run")
  64. else:
  65. anim.play("Walk")
  66. else:
  67. anim.play("Idle")
  68. else:
  69. anim.play("RecieveHit")
  70. # move along the current velocity
  71. vel = move_and_slide(vel, Vector3.UP, true, 4, deg2rad(89))
  72. # called when we collect a coin
  73. func give_gold (amount):
  74. gold += amount
  75. # update the UI
  76. ui.update_gold_text(gold)
  77. # called when an enemy deals damage to us
  78. func take_damage (damageToTake):
  79. curHp -= damageToTake
  80. # update the UI
  81. ui.update_health_bar(curHp, maxHp)
  82. # if our health is 0, die
  83. if curHp <= 0:
  84. die()
  85. # called when our health reaches 0
  86. func die ():
  87. # reload the scene
  88. get_tree().reload_current_scene()
  89. # called when we press the attack button
  90. func try_attack():
  91. # if we're not ready to attack, return
  92. if OS.get_ticks_msec() - lastAttackTime < attackRate * 1000:
  93. return
  94. # set the last attack time to now
  95. lastAttackTime = OS.get_ticks_msec()
  96. # play the animation
  97. anim.stop()
  98. anim.play("Punch")
  99. # is the ray cast colliding with an enemy?
  100. if attackCast.is_colliding():
  101. if attackCast.get_collider().has_method("take_damage"):
  102. attackCast.get_collider().take_damage(damage)