109 lines
3.9 KiB
GDScript
109 lines
3.9 KiB
GDScript
extends Node2D
|
|
|
|
# TODO: rewrite movement system to be impulse based
|
|
|
|
# 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"
|
|
@export var dash_recharge_timer : Timer
|
|
@export_range(1, 10, 1) var dashes_count : int = 3
|
|
|
|
@onready var ship = get_parent()
|
|
@onready var double_tap_timer = Timer.new()
|
|
@onready var dash_timer = Timer.new()
|
|
|
|
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
|
|
var dash_direction_left = true
|
|
var is_double_tapping = false
|
|
var is_dashing = false
|
|
|
|
func _ready():
|
|
dash_recharge_timer.timeout.connect(dash_recharge)
|
|
add_child(double_tap_timer)
|
|
add_child(dash_timer)
|
|
double_tap_timer.one_shot = true
|
|
double_tap_timer.timeout.connect(not_double_tapping)
|
|
dash_timer.one_shot = true
|
|
dash_timer.timeout.connect(not_dashing)
|
|
|
|
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")
|
|
# проверка на рывок
|
|
var left_released = Input.is_action_just_released("rotateleft")
|
|
var right_released = Input.is_action_just_released("rotateright")
|
|
if (left_released or right_released):
|
|
if !is_double_tapping and !is_dashing:
|
|
double_tap_timer.start(0.25)
|
|
is_double_tapping = true
|
|
print("first tap")
|
|
else:
|
|
dash_direction_left = left_released
|
|
is_dashing = true
|
|
is_double_tapping = false
|
|
dash_timer.start(0.1)
|
|
double_tap_timer.stop()
|
|
print("second tap")
|
|
|
|
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
|
|
var additional_rotation = 0
|
|
var additional_speed = 0
|
|
if is_dashing and ship is MainShip:
|
|
print("wanna dash")
|
|
additional_speed = 500
|
|
additional_rotation = deg_to_rad(-90) if dash_direction_left else deg_to_rad(90)
|
|
ship.velocity = Vector2.from_angle(ship.rotation + additional_rotation) * (speed + additional_speed)
|
|
ship.move_and_slide()
|
|
|
|
if Input.is_action_just_released("alternatemovement") and ship is MainShip:
|
|
alternative_movement = !alternative_movement
|
|
|
|
func dash_recharge():
|
|
pass
|
|
|
|
func not_double_tapping():
|
|
is_double_tapping = false
|
|
|
|
func not_dashing():
|
|
is_dashing = false
|