Advertisement
Guest User

Player.gd

a guest
Mar 15th, 2021
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.37 KB | None | 0 0
  1. extends KinematicBody2D
  2.  
  3. const UP = Vector2(0, -1)
  4.  
  5. const STOP_SLOPE = 1 * Globals.UNIT_SIZE
  6. var velocity = Vector2()
  7. var move_speed = 7.5 * Globals.UNIT_SIZE
  8. var gravity = 0
  9. var max_jump_velocity = 0
  10. var min_jump_velocity = 0
  11. var max_jump_height = 3.25 * Globals.UNIT_SIZE
  12. var min_jump_height = 0.8 * Globals.UNIT_SIZE
  13. var jump_duration = 0.5
  14. var is_grounded
  15. # Player stats
  16. var health = 0
  17. var health_max = 100
  18. #from variables Script
  19. signal health_changed(health)
  20.  
  21. export(int) var max_health = 100
  22.  
  23. onready var raycasts = $Raycasts
  24. onready var anim_player = $AnimationPlayer
  25.  
  26. func _ready():
  27. gravity = 2 * max_jump_height / pow(jump_duration, 2)
  28. max_jump_velocity = -sqrt(2 * gravity * max_jump_height)
  29. min_jump_velocity = -sqrt(2 * gravity * min_jump_height)
  30. #from variables script
  31. health = max_health
  32. emit_signal("health_changed", health)
  33.  
  34. func take_damage(amount):
  35. health -= amount
  36. health = max(0, health)
  37. emit_signal("health_changed", health)
  38.  
  39.  
  40. func heal(amount):
  41. health += amount
  42. health = max(health, max_health)
  43. emit_signal("health_changed", health)
  44.  
  45.  
  46. func _physics_process(delta):
  47. _get_input()
  48. velocity.y += gravity * delta
  49.  
  50. velocity = move_and_slide(velocity, UP, STOP_SLOPE)
  51. #to prevent multiple jumps
  52. is_grounded = _check_is_grounded()
  53.  
  54. _assign_animation()
  55.  
  56.  
  57. func _input(event):
  58. if event.is_action_pressed("jump") && is_grounded:
  59. $JumpSound.play()
  60. if is_grounded:
  61. velocity.y = max_jump_velocity
  62.  
  63.  
  64. if event.is_action_released("jump") && velocity.y < min_jump_velocity:
  65. velocity.y = min_jump_velocity
  66.  
  67.  
  68. func _get_input():
  69. var move_direction = -int(Input.is_action_pressed("move_left")) + int(Input.is_action_pressed("move_right"))
  70. velocity.x = move_speed * move_direction
  71. if move_direction !=0:
  72. $Sprite.scale.x = move_direction
  73.  
  74.  
  75.  
  76. # 0.05 is too low
  77. func _get_h_weight():
  78. return 0.2 if is_grounded else 0.1
  79.  
  80. func _check_is_grounded():
  81. for raycast in $Raycasts.get_children():
  82. if raycast.is_colliding():
  83. return true
  84.  
  85. #if loop completes then raycast was not detected
  86. return false
  87.  
  88. func _assign_animation():
  89. var anim = "idle"
  90.  
  91. if !is_grounded:
  92. anim = "jump"
  93. elif velocity.x != 0:
  94. anim = "running"
  95.  
  96. if Input.is_action_pressed("attack"):
  97. anim = "attack"
  98.  
  99. if health <= 0:
  100. anim = "dead"
  101. set_physics_process(false)
  102.  
  103. if anim_player.assigned_animation != anim:
  104. anim_player.play(anim)
  105.  
  106. func knockback(var enemypos):
  107. velocity.y = min_jump_velocity
  108. var direction = 1
  109. if direction > 0:
  110. velocity.x = -800
  111. elif direction < 0:
  112. velocity.x = 800
  113.  
  114. Input.action_release("move_left")
  115. Input.action_release("move_right")
  116. $AnimationPlayer.play("stagger")
  117. yield($AnimationPlayer, "animation_finished")
  118.  
  119.  
  120. func _on_PHitbox_area_entered(area):
  121. if area.name == "Hitbox":
  122. take_damage(15)
  123. anim_player.play("stagger")
  124. emit_signal("health_changed", health)
  125. if area.name == "FBHitbox":
  126. take_damage(25)
  127. emit_signal("health_changed", health)
  128. if health <=0:
  129. yield($AnimationPlayer, "animation_finished")
  130. get_tree().reload_current_scene()
  131. else:
  132. pass
  133.  
  134. func _boss_dead(life):
  135. if life <= 0:
  136. $Win.play
  137.  
  138. func _on_Health_Up_health_changed(area):
  139. if area.name == "Health_Up":
  140. heal(10)
  141. emit_signal("health_changed", health)
  142.  
  143.  
  144. func _on_PHitbox_body_entered(body):
  145. if body.name == "Enemy":
  146. knockback(position)
  147.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement