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.
 
 

164 lines
3.8 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. # internal
  17. var interactables := []
  18. var pushed_object
  19. func _process(delta):
  20. # attack input
  21. if Controller.player_input_enabled and is_on_floor():
  22. if Input.is_action_just_pressed("interact"):
  23. try_interact_pressed()
  24. if Input.is_action_just_released("interact"):
  25. try_interact_released()
  26. for area in $TouchableArea.get_overlapping_areas():
  27. try_touch(area)
  28. # called every physics step (60 times a second)
  29. func _physics_process (delta):
  30. var input := Vector3()
  31. # gravity
  32. vel.y -= gravity * delta
  33. if Controller.player_input_enabled :
  34. # movement inputs
  35. if Input.is_action_pressed("move_forward"):
  36. input.z += 1
  37. if Input.is_action_pressed("move_backward"):
  38. input.z -= 1
  39. if Input.is_action_pressed("move_left"):
  40. input.x += 1
  41. if Input.is_action_pressed("move_right"):
  42. input.x -= 1
  43. input = input.normalized()
  44. # get the relative direction
  45. var dir := (transform.basis.z * input.z + transform.basis.x * input.x)
  46. # apply the direction to our velocity
  47. if not is_on_floor() and dir.length_squared() == 0:
  48. vel.x = lerp(vel.x, 0, 0.02)
  49. vel.z = lerp(vel.z, 0, 0.02)
  50. elif Input.is_action_pressed("run"):
  51. vel.x = dir.x * runSpeed
  52. vel.z = dir.z * runSpeed
  53. else:
  54. vel.x = dir.x * walkSpeed
  55. vel.z = dir.z * walkSpeed
  56. # jump input
  57. if can_jump():
  58. vel.y = jumpForce
  59. else:
  60. vel.x = 0
  61. vel.z = 0
  62. if is_on_floor():
  63. if anim.current_animation != "Punch":
  64. if input.x != 0 || input.z != 0:
  65. if Controller.player_input_enabled and Input.is_action_pressed("run"):
  66. anim.play("Run")
  67. else:
  68. anim.play("Walk")
  69. else:
  70. anim.play("Idle")
  71. else:
  72. anim.play("RecieveHit")
  73. var previous_vel = vel
  74. # move pushed object first if there is one
  75. if pushed_object:
  76. var object_vel = pushed_object.move_and_slide(vel, Vector3.UP, true, 1, deg2rad(30))
  77. vel.x = object_vel.x
  78. vel.z = object_vel.z
  79. # move along the current velocity
  80. vel = move_and_slide(vel, Vector3.UP, true, 4, deg2rad(30))
  81. "debug for water"
  82. if translation.y <= -5.75:
  83. translation.y = -5.75
  84. vel.y = 0
  85. if is_on_wall():
  86. vel.y = min(previous_vel.y, vel.y)
  87. # called when we collect a coin
  88. func give_gold (amount):
  89. var gold = Controller.player_infos.gold
  90. gold += amount
  91. Controller.player_infos.gold = gold
  92. # called when an enemy deals damage to us
  93. func take_damage (damageToTake):
  94. Controller.player_infos.curHp -= damageToTake
  95. # if our health is 0, die
  96. if Controller.player_infos.curHp <= 0:
  97. die()
  98. # called when our health reaches 0
  99. func die ():
  100. # reload the scene
  101. get_tree().reload_current_scene()
  102. func try_touch(area):
  103. if area.has_method("on_touch"):
  104. area.on_touch(self)
  105. # called when we press the interact button
  106. func try_interact_pressed():
  107. # play the animation
  108. anim.stop()
  109. anim.play("Idle")
  110. for area in $InteractorArea.get_overlapping_areas():
  111. if area.has_method("on_interact_pressed"):
  112. interactables.push_back(area)
  113. area.on_interact_pressed(self)
  114. # called when we release the interact button
  115. func try_interact_released():
  116. # play the animation
  117. anim.stop()
  118. anim.play("Idle")
  119. for interactable in interactables:
  120. if interactable.has_method("on_interact_released"):
  121. interactable.on_interact_released(self)
  122. interactables.clear()
  123. func start_pushing(body):
  124. pushed_object = body
  125. func stop_pushing(body):
  126. pushed_object = null
  127. func can_jump():
  128. return pushed_object == null and Input.is_action_pressed("jump") and is_on_floor()