Advertisement
chris33556

most recent enemy script

Mar 5th, 2023
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.28 KB | None | 0 0
  1. extends KinematicBody
  2.  
  3. var move_speed = 2
  4. var attack_range = .1
  5. var stopping_distance = 1.9
  6. var health := 50
  7. var foo : int = 10
  8. var is_floored = false
  9. var target_player
  10. var isAttacking = true
  11. var player_animations
  12. var player_state
  13.  
  14.  
  15.  
  16. export (int) var damage := 10
  17.  
  18.  
  19. onready var attack_timer = $attack_timer
  20.  
  21. onready var anim_player = self.get_node("AnimatedSprite3D")
  22.  
  23. var state = states.IDLE
  24.  
  25. enum states{
  26. IDLE
  27. SEEK
  28. ATTACK_1
  29. STUNNED
  30. KNOCKBACK
  31. FINALKNOCKBACK
  32. FLOORED
  33. GETUP
  34. DISAPEAR
  35. }
  36.  
  37. func _ready():
  38. target_player = get_tree().current_scene.get_node("player")
  39. player_animations = target_player.get_node("AnimatedSprite3D")
  40. func _process(delta):
  41. match state:
  42. states.IDLE:
  43. $CollisionShape.disabled = false
  44. anim_player.play("idle")
  45. var direction_to_player = target_player.global_transform.origin - global_transform.origin
  46. var distance_to_player = direction_to_player.length()
  47. if distance_to_player < stopping_distance:
  48. return true
  49. state = states.SEEK
  50.  
  51. states.SEEK:
  52. anim_player.play("walking")
  53. var direction_to_player = target_player.global_transform.origin - global_transform.origin
  54. var distance_to_player = direction_to_player.length()
  55. direction_to_player = direction_to_player.normalized()
  56.  
  57. # check if the enemy is to tyield(get_tree().create_timer(0.7), "timeout")he left or right of the player
  58. if global_transform.origin.x < target_player.global_transform.origin.x:
  59. # enemy is to the left of the player, flip the sprite3d horizontally
  60. anim_player.set_scale(Vector3(-1, 1, 1))
  61. else:
  62. # enemy is to the right of the player, flip the sprite3d back to the opposite direction
  63. anim_player.set_scale(Vector3(1, 1, 1))
  64.  
  65. if distance_to_player > stopping_distance:
  66. var velocity = direction_to_player * move_speed
  67. move_and_slide(velocity)
  68.  
  69. if distance_to_player <= attack_range:
  70. state = states.ATTACK
  71.  
  72. else:
  73. state = states.IDLE
  74.  
  75. states.STUNNED:
  76. anim_player.play("stunned")
  77. yield(get_tree().create_timer(0.6), "timeout")
  78. $CollisionShape.disabled = false
  79. states.KNOCKBACK:
  80. anim_player.play("knockback")
  81. yield(get_tree().create_timer(0.5), "timeout")
  82. $CollisionShape.disabled = true
  83. state = states.FLOORED
  84.  
  85. states.FINALKNOCKBACK:
  86. anim_player.play("knockback")
  87. yield(get_tree().create_timer(0.5), "timeout")
  88. $CollisionShape.disabled = true
  89. state = states.DISAPEAR
  90.  
  91. states.FLOORED:
  92. anim_player.play("ontheFloor")
  93. yield(get_tree().create_timer(0.7), "timeout")
  94. $CollisionShape.disabled = true
  95. state = states.GETUP
  96.  
  97. states.GETUP:
  98. anim_player.play("getting_up")
  99. $CollisionShape.disabled = true
  100. state = states.IDLE
  101.  
  102. states.DISAPEAR:
  103. anim_player.play("queueFree")
  104. $CollisionShape.disabled = true
  105. yield(get_tree().create_timer(0.7), "timeout")
  106. queue_free()
  107.  
  108. states.ATTACK_1:
  109. anim_player.play("attack_1")
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120. # THIS IS THE ENEMYS HITCOUNT BAR SO THAT ITS HEALTH CAN DEPLETE ON PLAYERS ATTACKS----
  121. func Enemyknockback(damage: int):
  122. health -= damage
  123. print("enemy was hit!, current health: " + str(health))
  124. #player.health -= 10 # Subtract 10 health points from the player
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131. # THIS IS THE PLAYERS KNOCKBACK FUNCTION .INCLUDES ENEMYS REGISTERED ATTACKS AND EFFECTS
  132. func _on_EnemyHitBox_body_entered(player_body):
  133. #var PlayerStunTimer = target_player.PlayerStunTimer()
  134. if player_body.has_method("playerknockback"):
  135. player_body.playerknockback(damage)
  136. if player_body.health > 0:
  137. if $EnemyHitBox/CollisionShape.disabled == false:
  138. state = states.ATTACK_1
  139. #if PlayerStunTimer > 0:
  140. #player_animations.play("stunned")
  141. #print("got hit!")
  142. #player_body.emit_signal("PlayerStunned")
  143.  
  144. # alternative code player
  145. player_body.state = player_body.states.STUNNED
  146. #yield(get_tree().create_timer(0.3), "timeout")
  147. #player_body.state = player_body.states.IDLE
  148.  
  149. if player_body.health <= 0:
  150. $EnemyHitBox/CollisionShape.disabled = true
  151.  
  152. func _on_StunTimer_timeout():
  153. player_animations.play("idle")
  154. state = states.IDLE
  155.  
  156.  
  157.  
  158.  
  159. func _on_AnimatedSprite3D_animation_finished():
  160. if anim_player.animation == "attack_1":
  161. state = states.IDLE
  162.  
  163.  
  164.  
  165.  
  166.  
  167.  
  168.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement