Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- extends KinematicBody
- export var speed = 2
- var path = []
- var cur_path_idx = 0
- var target = null
- var velocity = Vector3.ZERO
- var threshold = .1
- var attacking = false
- var facing_left = false
- var knockback = Vector3.RIGHT
- var tween = Tween.new()
- onready var anim_player = $AnimatedSprite3D
- onready var nav = get_parent()
- onready var AttackTimer = $AttackTimer
- var player
- func _ready():
- yield(owner, "ready")
- target = owner.player
- player = get_tree().current_scene.get_node("player")
- func _physics_process(delta):
- if path.size() > 0:
- move_to_target()
- func move_to_target():
- if attacking:
- anim_player.play("punch")
- else:
- if cur_path_idx >= path.size():
- return
- # check if the enemy is to the left or right of the player
- if global_transform.origin.x < target.global_transform.origin.x:
- # enemy is to the left of the player, flip the sprite3d horizontally
- anim_player.set_scale(Vector3(-1, 1, 1))
- else:
- # enemy is to the right of the player, flip the sprite3d back to the opposite direction
- anim_player.set_scale(Vector3(1, 1, 1))
- if global_transform.origin.distance_to(path[cur_path_idx]) < 1.2:
- cur_path_idx += 1
- anim_player.play("idle")
- else:
- var direction = path[cur_path_idx] - global_transform.origin
- anim_player.play("walking")
- velocity = direction.normalized() * speed
- move_and_slide(velocity, Vector3.UP)
- func get_target_path(target_pos):
- path = nav.get_simple_path(global_transform.origin, target_pos)
- cur_path_idx = 0
- func _on_Timer_timeout():
- get_target_path(target.global_transform.origin)
- func _on_AttackTimer_timeout():
- attacking = false
- func _on_AnimatedSprite3D_animation_finished():
- if $AnimatedSprite3D.animation == "stunned":
- $AnimatedSprite3D.play("idle")
- func _on_AreaEnemy_area_entered(area):
- if area.is_in_group("fistPunch"):
- attacking = false
- $AnimatedSprite3D.play("stunned")
- func _on_EnemyHitBox_body_entered(body):
- var player_anim = player.get_node("AnimatedSprite3D")
- if body.get_name() == "player":
- AttackTimer.start()
- attacking = true
- player_anim.play("stunned")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement