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.
 
 

136 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():
  40. vel.x = lerp(vel.x, dir.x * airborneSpeed, .5 * delta)
  41. vel.z = lerp(vel.z, dir.z * airborneSpeed, .5 * delta)
  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. if is_on_wall():
  69. vel.y = min(previous_vel.y, vel.y)
  70. # called when we collect a coin
  71. func give_gold (amount):
  72. var gold = Controller.player_infos.gold
  73. gold += amount
  74. Controller.player_infos.gold = gold
  75. # called when an enemy deals damage to us
  76. func take_damage (damageToTake):
  77. Controller.player_infos.curHp -= damageToTake
  78. # if our health is 0, die
  79. if Controller.player_infos.curHp <= 0:
  80. die()
  81. # called when our health reaches 0
  82. func die ():
  83. # reload the scene
  84. get_tree().reload_current_scene()
  85. ## called when we press the attack button
  86. #func try_attack():
  87. # # if we're not ready to attack, return
  88. # if OS.get_ticks_msec() - lastAttackTime < attackRate * 1000:
  89. # return
  90. #
  91. # # set the last attack time to now
  92. # lastAttackTime = OS.get_ticks_msec()
  93. #
  94. # # play the animation
  95. # anim.stop()
  96. # anim.play("Punch")
  97. #
  98. # # is the ray cast colliding with an enemy?
  99. # if attackCast.is_colliding():
  100. # if attackCast.get_collider().has_method("take_damage"):
  101. # attackCast.get_collider().take_damage(damage)
  102. # if attackCast.get_collider().has_signal("on_interact"):
  103. # attackCast.get_collider().emit_signal("on_interact")
  104. # called when we press the interact button
  105. func try_interact():
  106. # is the ray cast colliding with an enemy?
  107. if interactCast.is_colliding():
  108. # play the animation
  109. anim.stop()
  110. anim.play("Idle")
  111. if interactCast.get_collider().has_signal("on_interact"):
  112. interactCast.get_collider().emit_signal("on_interact")