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.
 
 

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