Comets.gd/scripts/ship.gd

42 lines
1.1 KiB
GDScript3
Raw Normal View History

2024-05-10 20:42:22 -04:00
extends RigidBody2D
2024-05-09 22:42:55 -04:00
2024-05-10 20:42:22 -04:00
@export var engine_power := 16000.0
@export var spin_power := 200000.0
2024-05-09 22:42:55 -04:00
2024-05-10 20:42:22 -04:00
@onready var screensize = self.get_viewport_rect().size
2024-05-09 22:42:55 -04:00
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-10 20:42:22 -04:00
var thrust := Vector2.ZERO
var rotation_direction := 0
var can_shoot := false
2024-05-09 22:42:55 -04:00
2024-05-10 20:42:22 -04:00
func shoot_gun(pos : Vector2, dir : Vector2, rot : float) -> void:
var bullet = BULLET.instantiate()
bullet.position = pos
bullet.direction = dir
bullet.rotation = rot
add_child(bullet)
can_shoot = false
$ShotCooldown.start()
2024-05-09 22:42:55 -04:00
2024-05-10 20:42:22 -04:00
func get_input() -> void:
thrust = Vector2.ZERO
if Input.is_action_pressed("ui_up"):
thrust = self.transform.x * engine_power
rotation_direction = Input.get_axis("ui_left", "ui_right")
2024-05-09 22:42:55 -04:00
2024-05-10 20:42:22 -04:00
func _integrate_forces(state: PhysicsDirectBodyState2D) -> void:
var xform := state.transform
xform.origin.x = wrapf(xform.origin.x, 0 , screensize.x)
xform.origin.y = wrapf(xform.origin.y , 0, screensize.y)
state.transform = xform
func _physics_process(delta: float) -> void:
get_input()
self.constant_force = thrust * delta
self.constant_torque = rotation_direction * spin_power * delta
func _on_shot_cooldown_timeout() -> void:
can_shoot = true