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.
 
 

141 lines
3.5 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. # called every physics step (60 times a second)
  21. func _physics_process (delta):
  22. var input := Vector3()
  23. # gravity
  24. vel.y -= gravity * delta
  25. if Controller.player_input_enabled :
  26. # movement inputs
  27. if Input.is_action_pressed("move_forward"):
  28. input.z += 1
  29. if Input.is_action_pressed("move_backward"):
  30. input.z -= 1
  31. if Input.is_action_pressed("move_left"):
  32. input.x += 1
  33. if Input.is_action_pressed("move_right"):
  34. input.x -= 1
  35. input = input.normalized()
  36. # get the relative direction
  37. var dir := (transform.basis.z * input.z + transform.basis.x * input.x)
  38. # apply the direction to our velocity
  39. if not is_on_floor() and dir.length_squared() == 0:
  40. vel.x = lerp(vel.x, 0, 0.02)
  41. vel.z = lerp(vel.z, 0, 0.02)
  42. elif Input.is_action_pressed("run"):
  43. vel.x = dir.x * runSpeed
  44. vel.z = dir.z * runSpeed
  45. else:
  46. vel.x = dir.x * walkSpeed
  47. vel.z = dir.z * walkSpeed
  48. # jump input
  49. if Input.is_action_pressed("jump") and is_on_floor():
  50. vel.y = jumpForce
  51. else:
  52. vel.x = 0
  53. vel.z = 0
  54. if is_on_floor():
  55. if anim.current_animation != "Punch":
  56. if input.x != 0 || input.z != 0:
  57. if Controller.player_input_enabled and Input.is_action_pressed("run"):
  58. anim.play("Run")
  59. else:
  60. anim.play("Walk")
  61. else:
  62. anim.play("Idle")
  63. else:
  64. anim.play("RecieveHit")
  65. # move along the current velocity
  66. var previous_vel = vel
  67. vel = move_and_slide(vel, Vector3.UP, true, 4, deg2rad(30))
  68. "debug for water"
  69. if translation.y <= -5.75:
  70. translation.y = -5.75
  71. vel.y = 0
  72. if is_on_wall():
  73. vel.y = min(previous_vel.y, vel.y)
  74. # called when we collect a coin
  75. func give_gold (amount):
  76. var gold = Controller.player_infos.gold
  77. gold += amount
  78. Controller.player_infos.gold = gold
  79. # called when an enemy deals damage to us
  80. func take_damage (damageToTake):
  81. Controller.player_infos.curHp -= damageToTake
  82. # if our health is 0, die
  83. if Controller.player_infos.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. #
  95. # # set the last attack time to now
  96. # lastAttackTime = OS.get_ticks_msec()
  97. #
  98. # # play the animation
  99. # anim.stop()
  100. # anim.play("Punch")
  101. #
  102. # # is the ray cast colliding with an enemy?
  103. # if attackCast.is_colliding():
  104. # if attackCast.get_collider().has_method("take_damage"):
  105. # attackCast.get_collider().take_damage(damage)
  106. # if attackCast.get_collider().has_signal("on_interact"):
  107. # attackCast.get_collider().emit_signal("on_interact")
  108. # called when we press the interact button
  109. func try_interact():
  110. # is the ray cast colliding with an enemy?
  111. if interactCast.is_colliding():
  112. # play the animation
  113. anim.stop()
  114. anim.play("Idle")
  115. if interactCast.get_collider().has_signal("on_interact"):
  116. interactCast.get_collider().emit_signal("on_interact")