Guest User

combat_component.gd

a guest
May 10th, 2026
66
0
198 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.39 KB | None | 0 0
  1. extends Node
  2.  
  3. # =========================================================
  4. # STATS
  5. # =========================================================
  6. @export var max_health = 100
  7. @export var hitstun_duration = 0.2
  8.  
  9. var current_health = 0
  10. var is_stunned = false
  11. var stun_timer = 0.0
  12.  
  13. var body: CharacterBody2D
  14.  
  15.  
  16. # =========================================================
  17. # INIT
  18. # =========================================================
  19. func _ready():
  20. print("\n[COMBAT] READY:", get_parent())
  21.  
  22. body = get_parent()
  23. current_health = max_health
  24.  
  25. print("[COMBAT] HP:", current_health)
  26.  
  27.  
  28. # =========================================================
  29. # FRAME LOOP
  30. # =========================================================
  31. func _process(delta):
  32.  
  33. if is_stunned:
  34. stun_timer -= delta
  35. print("[COMBAT] stun_timer =", stun_timer)
  36.  
  37. if stun_timer <= 0:
  38. is_stunned = false
  39. print("[COMBAT] HITSTUN END")
  40.  
  41.  
  42. # =========================================================
  43. # DAMAGE
  44. # =========================================================
  45. func take_damage(amount, knockback_dir := Vector2.ZERO, knockback_force := 0.0):
  46. print("\n[COMBAT] DAMAGE RECEIVED:", amount)
  47.  
  48. current_health -= amount
  49. print("[COMBAT] HP:", current_health)
  50.  
  51. apply_hitstun()
  52.  
  53. if current_health <= 0:
  54. die()
  55.  
  56.  
  57. # =========================================================
  58. # HITSTUN
  59. # =========================================================
  60. func apply_hitstun():
  61. is_stunned = true
  62. stun_timer = hitstun_duration
  63. print("[COMBAT] HITSTUN START:", hitstun_duration)
  64.  
  65.  
  66. # =========================================================
  67. # KNOCKBACK DEBUG SAFE
  68. # =========================================================
  69. func apply_knockback(dir: Vector2, force: float):
  70. if body == null:
  71. print("[COMBAT] No body reference")
  72. return
  73.  
  74. print("[COMBAT] Knockback attempted")
  75.  
  76. if "knockback_velocity" in body:
  77. body.knockback_velocity = dir * force
  78. elif "external_velocity" in body:
  79. body.external_velocity = dir * force
  80.  
  81.  
  82. # =========================================================
  83. # DEATH
  84. # =========================================================
  85. func die():
  86. print("[COMBAT] DIE")
  87. get_parent().queue_free()
  88.  
  89.  
  90. # =========================================================
  91. # STATE CHECK
  92. # =========================================================
  93. func can_act() -> bool:
  94. return not is_stunned
  95.  
Advertisement
Add Comment
Please, Sign In to add comment