Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- extends KinematicBody2D
- export(int) var Speed: int = 40
- export(int) var reposition_delay: float = 1.0
- export(int) var idle_step_distance: int = 20
- export(int) var attack_radius: int = 150
- export(int) var knockback_threshold: int = 5
- var velocity: Vector2 = Vector2.ZERO
- var path: Array = []
- var levelnav: Navigation2D = null
- var player = null
- onready var soft_collision = $SoftCollision
- var knockbackforce = Vector2()
- var knockback_decay = 20
- var rng
- var wait_timer = false
- var made_a_path = false
- var waituntilldone = true
- var CurrentPath = Vector2.ZERO
- onready var raycast = $RayCast2D
- var hit_count = 0
- var is_running_away = false
- enum {
- IDLE,
- ATTACK
- }
- var State = ATTACK
- var randompos = Vector2.ZERO
- func _ready():
- rng = RandomNumberGenerator.new()
- yield(get_tree(),"idle_frame")
- var tree = get_tree()
- if tree.has_group("LevelNavigation"):
- levelnav = tree.get_nodes_in_group("LevelNavigation")[0]
- player = tree.get_nodes_in_group("Player")[0]
- func _physics_process(delta):
- $Line2D.global_position = Vector2.ZERO
- $Line2D.points = path
- if knockbackforce.length() > 7:
- velocity = knockbackforce
- knockbackforce += Vector2(0, 0) - knockbackforce / 3
- if not knockbackforce.length() > 14:
- $Sprite.self_modulate = Color(1, 1, 1)
- else:
- if wait_timer and waituntilldone:
- waituntilldone = false
- $Timer.start(reposition_delay)
- wait_timer = false
- made_a_path = false
- print("starting timer")
- else:
- _navigate(delta)
- if soft_collision.is_colliding():
- velocity += soft_collision.get_push_vector() * delta * 400
- if soft_collision.touching_wall().size() > 0:
- wait_timer = true
- velocity = move_and_slide(velocity)
- func _navigate(delta):
- if path.size() > 1 and made_a_path and not wait_timer:
- velocity = global_position.direction_to(path[1]) * Speed + knockbackforce
- if global_position.distance_to(path[1]) < 3:
- path.pop_front()
- elif soft_collision.touching_wall().size() > 0 and not wait_timer:
- wait_timer = true
- elif path.size() > 1 and path.size() <= 3 and global_position.distance_to(path[0]) < 12 and not wait_timer:
- wait_timer = true
- else:
- velocity = lerp(velocity, Vector2.ZERO, 10 * delta)
- func _generate_path(randopos):
- if levelnav != null and player != null:
- path = levelnav.get_simple_path(global_position, randopos, false)
- $Line2D.points = path
- if path.size() > 0:
- CurrentPath = path[path.size() - 1]
- made_a_path = true
- wait_timer = false
- func _on_SoftCollision_area_entered(area):
- if "Bullet" in area.name:
- _knockback(-area.rotation, area.knockback)
- func _knockback(bulletrot, knockback):
- path = levelnav.get_simple_path(global_position, CurrentPath, false)
- $Line2D.points = path
- knockbackforce = Vector2(sin(bulletrot), cos(bulletrot)).normalized() * -knockback
- $Sprite.self_modulate = Color(100, 100, 100)
- func _on_Timer_timeout():
- waituntilldone = true
- rng.randomize()
- if player and levelnav:
- match State:
- IDLE:
- randompos = global_position + Vector2(rng.randi_range(-idle_step_distance, idle_step_distance), rng.randi_range(-idle_step_distance, idle_step_distance))
- randompos = levelnav.get_closest_point(randompos)
- ATTACK:
- randompos = player.global_position + Vector2(rng.randi_range(-attack_radius, attack_radius), rng.randi_range(-attack_radius, attack_radius))
- randompos = levelnav.get_closest_point(randompos)
- _generate_path(randompos)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement