59 lines
2.4 KiB
GDScript
59 lines
2.4 KiB
GDScript
extends Node2D
|
|
|
|
class_name ShipEngine
|
|
|
|
@export var max_speed : float = 200
|
|
@export var max_turbo_speed : float = 300
|
|
@export var acceleration : float = 50
|
|
@export var fuel_consumption : float = 100
|
|
@export var rotation_speed : int = 90
|
|
@export var id : String = "starterengine"
|
|
@onready var ship = get_parent()
|
|
var hull
|
|
var speed = 0
|
|
var min_speed = max_speed / -4
|
|
var turbo_enabled = false
|
|
var alternative_movement = false
|
|
var destination_angle : float
|
|
var destination_difference : float
|
|
|
|
func _physics_process(delta):
|
|
hull = ship.hull
|
|
modulate = ship.material.get_shader_parameter('color')
|
|
var turbo_input = Input.get_action_raw_strength("turbo")
|
|
var acceleration_input = Input.get_axis("deccelerate", "accelerate") if ship is MainShip else 1.0
|
|
var rotation_input = Input.get_axis("rotateleft","rotateright")
|
|
if destination_angle - ship.rotation_degrees == clamp(destination_angle - ship.rotation_degrees, -180, 180):
|
|
destination_difference = destination_angle - ship.rotation_degrees
|
|
else:
|
|
destination_difference = ship.rotation_degrees - destination_angle
|
|
if destination_difference != clamp(destination_difference, -1, 1):
|
|
ship.rotation_degrees += sign(destination_difference) * rotation_speed * delta
|
|
else:
|
|
ship.rotation_degrees = destination_angle
|
|
if alternative_movement:
|
|
destination_angle += rotation_input * rotation_speed * delta
|
|
if destination_angle > 180: destination_angle = -180
|
|
if destination_angle < -180: destination_angle = 180
|
|
if Vector2.ZERO.distance_to(global_position) >= 5800:
|
|
destination_angle = rad_to_deg(global_position.angle_to_point(Vector2.ZERO))
|
|
elif ship is MainShip:
|
|
destination_angle = rad_to_deg(ship.global_position.angle_to_point(get_global_mouse_position()))
|
|
turbo_enabled = clamp(turbo_input * hull.fuel, 0, 1) if ship is MainShip else (ship.state == "runaway" and hull.fuel > 0)
|
|
if !turbo_enabled:
|
|
speed = clamp(speed + acceleration_input * acceleration * delta, min_speed, max(max_speed, speed))
|
|
if speed > max_speed:
|
|
speed -= acceleration * delta if acceleration_input != -1 else 0
|
|
else:
|
|
if hull.fuel > 0:
|
|
speed = clamp(speed + acceleration * delta, min_speed, max_turbo_speed)
|
|
if speed > max_speed:
|
|
hull.fuel -= fuel_consumption * delta
|
|
if hull.fuel < 0:
|
|
hull.fuel = 0
|
|
|
|
ship.velocity = Vector2.from_angle(ship.rotation) * speed
|
|
ship.move_and_slide()
|
|
|
|
if Input.is_action_just_released("alternatemovement") and ship is MainShip:
|
|
alternative_movement = !alternative_movement
|