revo-jailbreak/objects/enemy/eel.gd
2023-06-13 03:25:17 -04:00

59 lines
2 KiB
GDScript

extends "res://objects/enemy/enemy.gd"
const SEGMENT_LENGTH: float = 4.0
const BASE_LENGTH: float = 20.0 # combined length of head and tail
export var segments: int = 8
export var speed: float = 32.0
export var wave_length: float = 8.0
export var wave_amplitude: float = 4.0
onready var sector: Vector2 = Game.get_sector(global_position)
onready var hitbox: Area2D = $Path2D/PathFollow2D/Hitbox
onready var first_segment: CollisionShape2D = $"%Segment"
onready var segment_start: Position2D = $"%SegmentStart"
onready var path_follow = $Path2D/PathFollow2D
func _ready() -> void:
hitbox.remove_child(first_segment)
for i in segments:
var new_segment = first_segment.duplicate()
new_segment.position.x = segment_start.position.x + SEGMENT_LENGTH * float(i)
hitbox.add_child(new_segment)
$Path2D/PathFollow2D/Hitbox/TailShape.position.x = segment_start.position.x + SEGMENT_LENGTH * segments
_wave_segments()
func _physics_process(delta: float) -> void:
# move
# global_position.x += speed * delta * sign(scale.x)
path_follow.set_offset(path_follow.get_offset() + speed * delta)
# make segments wibble
_wave_segments()
# check for wrapping
var sector_rect = Rect2(sector * Game.resolution - Vector2(8.0, 0.0), Game.resolution + Vector2(16.0, 0.0))
var total_length = BASE_LENGTH + (SEGMENT_LENGTH * float(segments))
var eel_rect = Rect2(global_position + Vector2(-2.0, 4.0), Vector2(total_length, 0.0))
if sign(scale.x) == 1.0:
eel_rect.position.x -= total_length
if not sector_rect.intersects(eel_rect):
global_position.x -= (sector_rect.size.x + total_length) * sign(scale.x)
func _wave_segments() -> void:
for segment in hitbox.get_children():
if segment is Node2D:
segment.position.y = sin(segment.global_position.x / wave_length) * wave_amplitude
func die() -> void:
for segment in hitbox.get_children():
if segment is Node2D:
var death_particles = DeathParticles.instance()
death_particles.global_position = segment.global_position
death_particles.emitting = true
get_parent().add_child(death_particles)
.die()