Comets.gd/scripts/invader.gd

69 lines
1.6 KiB
GDScript3
Raw Permalink Normal View History

2024-05-14 20:08:32 -04:00
extends Area2D
# TODO: Make a random spawner for invaders
# TODO: Give points if shot
signal shot
const BULLET = preload("res://scenes/bullet.tscn")
@export var move_speed := 100.0
var movement_direction := Vector2.ZERO
var random_position := Vector2.ZERO
var can_shoot := false
var viewport_size : Vector2
func _ready() -> void:
viewport_size = get_viewport_rect().size
# I am sorry to who has to make sense of this
random_position.x = randi_range(1, 2)
if random_position.x == 2:
random_position.x = viewport_size.x-1
movement_direction.x = -1
else:
movement_direction.x = 1
2024-05-16 14:42:02 -04:00
random_position.y = randf_range(32, viewport_size.y + 32)
2024-05-14 20:08:32 -04:00
position = random_position
func _process(delta: float) -> void:
position += move_speed * movement_direction * delta
if can_shoot:
# FIXME: Proper shoot angle and not able to sudoku
shoot($GunMarker.global_position, randf_range(90.0, 92.0))
2024-05-14 20:08:32 -04:00
# "Die", if off screen
if position.x > viewport_size.x or position.x < 0:
queue_free()
func _on_flying_tone_finished() -> void:
$FlyingTone.play()
func _on_body_entered(body: Node2D) -> void:
if body.is_in_group("ship"):
body.crash()
func _on_area_entered(area: Area2D) -> void:
if area.is_in_group("bullet"):
shot.emit()
crash()
func shoot(pos : Vector2, rot : float) -> void:
var bullet := BULLET.instantiate()
bullet.position = pos
bullet.rotation = rot
bullet.life_span = 0.3
2024-05-16 14:05:08 -04:00
bullet.screen_wrap = false
2024-05-14 20:08:32 -04:00
$Projectiles.add_child(bullet)
$ShootSound.play()
can_shoot = false
$ShootTimer.start()
func crash() -> void:
$AnimationPlayer.play("crash")
func _on_shoot_timer_timeout() -> void:
can_shoot = true