Advertisement
chris33556

enemy script33

Jan 8th, 2023
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.17 KB | None | 0 0
  1. extends KinematicBody
  2.  
  3. export var speed = 2
  4.  
  5. var path = []
  6. var cur_path_idx = 0
  7. var target = null
  8. var velocity = Vector3.ZERO
  9. var threshold = .1
  10. var attacking = false
  11. var facing_left = false
  12. var knockback = Vector3.RIGHT
  13. var tween = Tween.new()
  14.  
  15.  
  16. onready var anim_player = $AnimatedSprite3D
  17. onready var nav = get_parent()
  18. onready var AttackTimer = $AttackTimer
  19. var player
  20.  
  21. func _ready():
  22. yield(owner, "ready")
  23. target = owner.player
  24. player = get_tree().current_scene.get_node("player")
  25.  
  26. func _physics_process(delta):
  27. if path.size() > 0:
  28. move_to_target()
  29.  
  30.  
  31. func move_to_target():
  32. if attacking:
  33. anim_player.play("punch")
  34. else:
  35. if cur_path_idx >= path.size():
  36. return
  37.  
  38. # check if the enemy is to the left or right of the player
  39. if global_transform.origin.x < target.global_transform.origin.x:
  40. # enemy is to the left of the player, flip the sprite3d horizontally
  41. anim_player.set_scale(Vector3(-1, 1, 1))
  42. else:
  43. # enemy is to the right of the player, flip the sprite3d back to the opposite direction
  44. anim_player.set_scale(Vector3(1, 1, 1))
  45.  
  46.  
  47.  
  48.  
  49. if global_transform.origin.distance_to(path[cur_path_idx]) < 1.2:
  50. cur_path_idx += 1
  51. anim_player.play("idle")
  52.  
  53.  
  54.  
  55.  
  56. else:
  57. var direction = path[cur_path_idx] - global_transform.origin
  58. anim_player.play("walking")
  59. velocity = direction.normalized() * speed
  60. move_and_slide(velocity, Vector3.UP)
  61.  
  62. func get_target_path(target_pos):
  63. path = nav.get_simple_path(global_transform.origin, target_pos)
  64. cur_path_idx = 0
  65.  
  66. func _on_Timer_timeout():
  67. get_target_path(target.global_transform.origin)
  68.  
  69.  
  70.  
  71. func _on_AttackTimer_timeout():
  72. attacking = false
  73.  
  74.  
  75. func _on_AnimatedSprite3D_animation_finished():
  76. if $AnimatedSprite3D.animation == "stunned":
  77. $AnimatedSprite3D.play("idle")
  78.  
  79.  
  80. func _on_AreaEnemy_area_entered(area):
  81. if area.is_in_group("fistPunch"):
  82. attacking = false
  83. $AnimatedSprite3D.play("stunned")
  84.  
  85.  
  86. func _on_EnemyHitBox_body_entered(body):
  87. var player_anim = player.get_node("AnimatedSprite3D")
  88. if body.get_name() == "player":
  89. AttackTimer.start()
  90. attacking = true
  91.  
  92. player_anim.play("stunned")
  93.  
  94.  
  95.  
  96.  
  97.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement