Comets.gd/scripts/ship.old.gd

51 lines
1.4 KiB
GDScript

extends Area2D
@export var max_speed := 1200.0
@export var steering_factor := 3.0
const BULLET = preload("res://scenes/bullet.tscn")
var velocity := Vector2.ZERO
var gun_direction := Vector2.ZERO
var can_shoot := true
func _process(delta: float) -> void:
# TODO: Maybe look into tank controls like classic Astroids
var direction := Input.get_vector("ui_left", "ui_right", "ui_up", "ui_down")
if direction.length() > 1.0:
direction.normalized()
if direction.length() != 0.0:
gun_direction = direction
var desired_velocity := max_speed * direction
var steering := desired_velocity - velocity
velocity += steering * steering_factor * delta
position += velocity * delta
if velocity.length() > 0.0:
# TODO: Have the CollisionShape toate with the sprite
$Sprite2D.rotation = velocity.angle()
print("Angle: " + str($Sprite2D.rotation))
var viewport_size := get_viewport_rect().size
position.x = wrap(position.x, 0, viewport_size.x)
position.y = wrap(position.y, 0,viewport_size.y)
if Input.is_action_pressed("ui_accept") and can_shoot:
fire_gun($Gun.position, $Sprite2D.rotation, velocity.angle())
func fire_gun(pos : Vector2, dir : Vector2, rot : float) -> void:
var bullet = BULLET.instantiate()
bullet.position = pos
bullet.direction = dir
bullet.rotation = rot
add_child(bullet)
can_shoot = false
$ShotCooldown.start()
func _on_timer_timeout() -> void:
can_shoot = true