mirror of
https://codeberg.org/Melon-Bread/Comets.gd.git
synced 2024-11-28 15:08:20 -05:00
73 lines
1.8 KiB
GDScript
73 lines
1.8 KiB
GDScript
extends CharacterBody2D
|
|
|
|
signal crashed
|
|
|
|
const BULLET = preload("res://scenes/bullet.tscn")
|
|
|
|
@export var accleration := 100.0
|
|
@export var max_speed := 400.0
|
|
@export var rotation_speed := 250.0
|
|
|
|
var viewport_size : Vector2
|
|
|
|
var can_shoot := true
|
|
var can_warp := true
|
|
|
|
func _ready() -> void:
|
|
viewport_size = get_viewport_rect().size
|
|
|
|
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)
|
|
|
|
if Input.is_action_pressed("ui_cancel") and can_warp:
|
|
warp()
|
|
|
|
# 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, 0.5)
|
|
|
|
## Rotation
|
|
rotate(deg_to_rad(rotation_direction * rotation_speed * delta))
|
|
move_and_slide()
|
|
|
|
# Screen Wrap
|
|
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:
|
|
var bullet = BULLET.instantiate()
|
|
bullet.position = pos
|
|
bullet.rotation = rot
|
|
$Projectiles.add_child(bullet)
|
|
$ShootSound.play()
|
|
can_shoot = false
|
|
$ShotCooldown.start()
|
|
|
|
func warp() -> void:
|
|
var random_position := Vector2.ZERO
|
|
random_position.x = randf_range(0, viewport_size.x)
|
|
random_position.y = randf_range(0, viewport_size.y)
|
|
position = random_position
|
|
can_warp = false
|
|
$WarpCooldown.start()
|
|
|
|
func crash() -> void:
|
|
crashed.emit()
|
|
$AnimationPlayer.play("crash")
|
|
|
|
|
|
func _on_shot_cooldown_timeout() -> void:
|
|
can_shoot = true
|
|
|
|
func _on_warp_cooldown_timeout() -> void:
|
|
can_warp = true
|