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.
 
 

127 lines
3.0 KiB

  1. extends KinematicBody
  2. class_name Player
  3. var attackRate : float = 0.3
  4. var lastAttackTime : int = 0
  5. # physics
  6. var airborneSpeed : float = 3.0
  7. var walkSpeed : float = 5.0
  8. var runSpeed : float = 10.0
  9. var jumpForce : float = 10.0
  10. var gravity : float = 15.0
  11. var vel := Vector3()
  12. # components
  13. onready var camera = get_node("CameraOrbit")
  14. onready var interactCast = get_node("InteractRayCast")
  15. onready var anim = get_node("Casual2_Female/AnimationPlayer")
  16. func _process(delta):
  17. # attack input
  18. if Controller.player_input_enabled and is_on_floor() and Input.is_action_just_pressed("interact"):
  19. try_interact()
  20. for area in $TouchableArea.get_overlapping_areas():
  21. try_touch(area)
  22. # called every physics step (60 times a second)
  23. func _physics_process (delta):
  24. var input := Vector3()
  25. # gravity
  26. vel.y -= gravity * delta
  27. if Controller.player_input_enabled :
  28. # movement inputs
  29. if Input.is_action_pressed("move_forward"):
  30. input.z += 1
  31. if Input.is_action_pressed("move_backward"):
  32. input.z -= 1
  33. if Input.is_action_pressed("move_left"):
  34. input.x += 1
  35. if Input.is_action_pressed("move_right"):
  36. input.x -= 1
  37. input = input.normalized()
  38. # get the relative direction
  39. var dir := (transform.basis.z * input.z + transform.basis.x * input.x)
  40. # apply the direction to our velocity
  41. if not is_on_floor() and dir.length_squared() == 0:
  42. vel.x = lerp(vel.x, 0, 0.02)
  43. vel.z = lerp(vel.z, 0, 0.02)
  44. elif Input.is_action_pressed("run"):
  45. vel.x = dir.x * runSpeed
  46. vel.z = dir.z * runSpeed
  47. else:
  48. vel.x = dir.x * walkSpeed
  49. vel.z = dir.z * walkSpeed
  50. # jump input
  51. if Input.is_action_pressed("jump") and is_on_floor():
  52. vel.y = jumpForce
  53. else:
  54. vel.x = 0
  55. vel.z = 0
  56. if is_on_floor():
  57. if anim.current_animation != "Punch":
  58. if input.x != 0 || input.z != 0:
  59. if Controller.player_input_enabled and Input.is_action_pressed("run"):
  60. anim.play("Run")
  61. else:
  62. anim.play("Walk")
  63. else:
  64. anim.play("Idle")
  65. else:
  66. anim.play("RecieveHit")
  67. # move along the current velocity
  68. var previous_vel = vel
  69. vel = move_and_slide(vel, Vector3.UP, true, 4, deg2rad(30))
  70. "debug for water"
  71. if translation.y <= -5.75:
  72. translation.y = -5.75
  73. vel.y = 0
  74. if is_on_wall():
  75. vel.y = min(previous_vel.y, vel.y)
  76. # called when we collect a coin
  77. func give_gold (amount):
  78. var gold = Controller.player_infos.gold
  79. gold += amount
  80. Controller.player_infos.gold = gold
  81. # called when an enemy deals damage to us
  82. func take_damage (damageToTake):
  83. Controller.player_infos.curHp -= damageToTake
  84. # if our health is 0, die
  85. if Controller.player_infos.curHp <= 0:
  86. die()
  87. # called when our health reaches 0
  88. func die ():
  89. # reload the scene
  90. get_tree().reload_current_scene()
  91. func try_touch(area):
  92. if area.has_method("on_touch"):
  93. area.on_touch(self)
  94. # called when we press the interact button
  95. func try_interact():
  96. # is the ray cast colliding with an enemy?
  97. for area in $InteractorArea.get_overlapping_areas():
  98. # play the animation
  99. anim.stop()
  100. anim.play("Idle")
  101. if area.has_method("on_interact"):
  102. area.on_interact()