starcheese/spaceship.gd

61 lines
1.8 KiB
GDScript3
Raw Normal View History

2024-10-01 16:43:34 +02:00
extends CharacterBody2D
@export var acceleration: float = 400.0
@export var max_speed: float = 1500.0
@export var rotation_speed: float = 3.0
@export var friction: float = 0.99
2024-11-12 15:05:38 +01:00
@onready var laser := $WeaponsSystem/mining_beam_turret/mining_beam
2024-10-01 16:43:34 +02:00
2024-10-03 18:12:33 +01:00
var weapons_system: WeaponsSystem
var event_bus: EventBus
2024-10-01 16:43:34 +02:00
func _ready():
2024-10-03 18:12:33 +01:00
weapons_system = get_node("/root/Main/WeaponsSystem")
event_bus = get_node("/root/Main/MainEventBus")
event_bus.subscribe_to(get_node("/root/Main/InputEventScope"), "KeyboardInputEvent", Callable(self, "handle_keyboard_input"))
2024-11-12 15:05:38 +01:00
2024-10-01 16:43:34 +02:00
func get_spaceship_orientation_vector() -> Vector2:
return -global_transform.y
func handle_movement(delta: float) -> void:
var thrust = Vector2.ZERO
if Input.is_action_pressed("ui_up"):
2024-11-12 15:05:38 +01:00
thrust = Vector2(1, 0).rotated(rotation) * acceleration * delta
2024-10-01 16:43:34 +02:00
velocity += thrust
if Input.is_action_pressed("ui_down"):
2024-11-12 15:05:38 +01:00
thrust = Vector2(-1, 0).rotated(rotation) * acceleration * delta
2024-10-01 16:43:34 +02:00
velocity += thrust
if Input.is_action_pressed("ui_left"):
rotation -= rotation_speed * delta
if Input.is_action_pressed("ui_right"):
rotation += rotation_speed * delta
velocity = velocity.clamp(Vector2(-max_speed, -max_speed), Vector2(max_speed, max_speed))
velocity *= friction
var collision = move_and_collide(velocity * delta)
if collision:
print("Collided with: ", collision.get_collider().name)
var collision_normal = collision.get_normal()
velocity = velocity.slide(collision_normal)
2024-11-12 15:05:38 +01:00
func _unhandled_input(event: InputEvent) -> void:
if not event.is_action("fire_weapon"):
return
laser.is_casting = event.is_action_pressed("fire_weapon")
2024-10-03 18:12:33 +01:00
func handle_keyboard_input(event: KeyboardInputEvent):
2024-11-12 15:05:38 +01:00
if event.action == "ui_fire":
if event.pressed:
2024-10-03 18:12:33 +01:00
weapons_system.fire_all()
else:
weapons_system.cease_fire_all()
2024-10-01 16:43:34 +02:00
func _process(delta: float) -> void:
handle_movement(delta)