2024-05-12 00:07:06 -04:00
|
|
|
extends CharacterBody2D
|
2024-05-09 22:42:55 -04:00
|
|
|
|
2024-05-12 01:17:58 -04:00
|
|
|
# TODO: Add the ability to "warp"
|
|
|
|
# TODO: Tweak speed and shoot time values
|
|
|
|
|
2024-05-10 20:42:22 -04:00
|
|
|
const BULLET = preload("res://scenes/bullet.tscn")
|
2024-05-09 22:42:55 -04:00
|
|
|
|
2024-05-12 00:07:06 -04:00
|
|
|
@export var accleration := 90.0
|
|
|
|
@export var max_speed := 350.0
|
|
|
|
@export var rotation_speed := 200.0
|
2024-05-09 22:42:55 -04:00
|
|
|
|
2024-05-12 00:07:06 -04:00
|
|
|
var can_shoot := true
|
|
|
|
|
|
|
|
func _physics_process(delta: float) -> void:
|
|
|
|
# Controls
|
|
|
|
var accleration_direction := Vector2(0, Input.get_axis("ui_up", "ui_down"))
|
|
|
|
var rotation_direction := Input.get_axis("ui_left", "ui_right")
|
|
|
|
|
|
|
|
if Input.is_action_pressed("ui_accept") and can_shoot:
|
|
|
|
fire_gun($Gun.global_position, self.global_rotation)
|
|
|
|
|
|
|
|
# Movement Logic
|
|
|
|
## Accleration
|
|
|
|
velocity += accleration_direction.rotated(rotation) * accleration * delta
|
|
|
|
velocity.limit_length(max_speed)
|
|
|
|
### Slowly stops movement if no accleration
|
|
|
|
if accleration_direction.y == 0:
|
|
|
|
velocity = velocity.move_toward(Vector2.ZERO, 1)
|
|
|
|
|
|
|
|
## Rotation
|
|
|
|
rotate(deg_to_rad(rotation_direction * rotation_speed * delta))
|
|
|
|
move_and_slide()
|
|
|
|
|
|
|
|
# Screen Wrap
|
|
|
|
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)
|
|
|
|
|
|
|
|
func fire_gun(pos : Vector2, rot : float) -> void:
|
2024-05-10 20:42:22 -04:00
|
|
|
var bullet = BULLET.instantiate()
|
|
|
|
bullet.position = pos
|
|
|
|
bullet.rotation = rot
|
2024-05-12 00:07:06 -04:00
|
|
|
$Projectiles.add_child(bullet)
|
2024-05-12 00:47:14 -04:00
|
|
|
$ShootSounds.play()
|
2024-05-10 20:42:22 -04:00
|
|
|
can_shoot = false
|
|
|
|
$ShotCooldown.start()
|
2024-05-09 22:42:55 -04:00
|
|
|
|
2024-05-12 01:03:18 -04:00
|
|
|
func crash():
|
2024-05-12 01:17:58 -04:00
|
|
|
# TODO: Maybe emit a signal that the main game node listens for "game over" state
|
2024-05-12 01:03:18 -04:00
|
|
|
$AnimationPlayer.play("crash")
|
|
|
|
|
|
|
|
|
2024-05-10 20:42:22 -04:00
|
|
|
func _on_shot_cooldown_timeout() -> void:
|
|
|
|
can_shoot = true
|