Advertisement
chris33556

enemy_script_03_03_23

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