tool extends "res://objects/enemy/enemy.gd" export var walk_speed = 25.0 export var left_boundary = 0.0 setget _set_left_boundary export var right_boundary = 0.0 setget _set_right_boundary export var direction = 1.0 export var idle_turns = 0 export var turn_time = 0.5 export var shoot_time = 1.0 onready var sprite = $AnimatedSprite onready var shoot_position = $"%ShootPosition" onready var shoot_cast = $"%ShootCast" onready var graphics_cast = $"%GraphicsCast" onready var shoot_line = $"%ShootLine" onready var sparks = $SparkParticles var shooting = false var turns = 0 func _ready(): if Engine.editor_hint: update() return # convert boundaries into actual coordinate positions left_boundary = position.x - left_boundary * 8.0 right_boundary = position.x + right_boundary * 8.0 # make facing match direction sprite.scale.x = direction # make animation speed sync to walk speed sprite.speed_scale = inverse_lerp(0.0, 25.0, walk_speed) func _physics_process(delta): if Engine.editor_hint: return if !shooting: # check for player in raycast var collider = shoot_cast.get_collider() if collider != null && collider.is_in_group("player"): # kill player and enter shooting state temporarily collider.get_parent().die() shooting = true get_tree().create_timer(0.05, false).connect("timeout", self, "_stop_shoot") sprite.play("shoot") # check other raycast to find collision passing through player graphics_cast.force_raycast_update() var hit_position = graphics_cast.to_global(Vector2(256.0, 0.0)) if graphics_cast.is_colliding(): hit_position = graphics_cast.get_collision_point() # set line point to point of collision var hit_x = shoot_line.to_local(hit_position).x shoot_line.points[1].x = hit_x # shoot_line.visible = true # get_tree().create_timer(0.05, false).connect("timeout", shoot_line, "set_visible", [false], CONNECT_DEFERRED) # move and play sparks sparks.global_position = hit_position sparks.emitting = true return # if there aren't turns, walk around if turns == 0: sprite.play("walk") position.x += direction * walk_speed * delta if position.x <= left_boundary: position.x = left_boundary direction = 1.0 _do_turn() elif position.x >= right_boundary: position.x = right_boundary direction = -1.0 _do_turn() func _do_turn(): if shooting: get_tree().create_timer(turn_time, false).connect("timeout", self, "_do_turn") return sprite.play("idle") # still pause with zero "turns" if idle_turns <= 0: # set turns to -1 to stop walking turns = -1 # wait for turn_tie yield(get_tree().create_timer(turn_time, false), "timeout") sprite.scale.x *= -1.0 # wait a bit after turning get_tree().create_timer(turn_time, false).connect("timeout", self, "set", ["turns", 0]) return sprite.scale.x *= -1.0 if turns < idle_turns * 2: turns += 1 get_tree().create_timer(turn_time, false).connect("timeout", self, "_do_turn") else: get_tree().create_timer(turn_time, false).connect("timeout", self, "set", ["turns", 0]) # turns = 0 func _stop_shoot(): shooting = false shoot_line.visible = false if turns == 0: sprite.play("walk") else: sprite.play("idle") # editor debug drawing func _draw(): if Engine.editor_hint: var left = -left_boundary * 8.0 var right = right_boundary * 8.0 + 8.0 draw_line( Vector2(left, 0.0), Vector2(right, 0.0), Color(0.4, 0.2, 0.6, 0.75), 1.01, false ) draw_line( Vector2(left, 2.0), Vector2(left, -2.0), Color(0.4, 0.2, 0.6, 0.75), 1.01, false ) draw_line( Vector2(right, 2.0), Vector2(right, -2.0), Color(0.4, 0.2, 0.6, 0.75), 1.01, false ) func _set_left_boundary(value): left_boundary = value if Engine.editor_hint: update() func _set_right_boundary(value): right_boundary = value if Engine.editor_hint: update()