53 lines
1.5 KiB
GDScript
53 lines
1.5 KiB
GDScript
extends Area2D
|
|
|
|
@export var speed = 400
|
|
@export var score : int = 0
|
|
@export var bullet_scene: PackedScene
|
|
|
|
@export var life_max : float = 30
|
|
@onready var life : float = life_max
|
|
|
|
var screen_size
|
|
|
|
# Called when the node enters the scene tree for the first time.
|
|
func _ready() -> void:
|
|
screen_size = get_viewport_rect().size
|
|
$ShootTimer.timeout.connect(_on_shoot_timer_timeout)
|
|
|
|
|
|
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
|
func _process(delta: float) -> void:
|
|
var velocity = Vector2.ZERO # The player's movement vector.
|
|
if Input.is_action_pressed("move_right"):
|
|
velocity.x += 1
|
|
if Input.is_action_pressed("move_left"):
|
|
velocity.x -= 1
|
|
if Input.is_action_pressed("move_down"):
|
|
velocity.y += 1
|
|
if Input.is_action_pressed("move_up"):
|
|
velocity.y -= 1
|
|
if velocity.length() > 0:
|
|
velocity = velocity.normalized() * speed
|
|
rotation = velocity.angle() #velocity.angle() + PI/2
|
|
position += velocity * delta
|
|
#position = position.clamp(Vector2.ZERO, screen_size)
|
|
|
|
func _on_shoot_timer_timeout():
|
|
_shoot(Vector2(0, 40))
|
|
_shoot(Vector2(0, -40))
|
|
|
|
func _shoot(socket : Vector2):
|
|
var bullet = bullet_scene.instantiate()
|
|
bullet.position = position + socket.rotated(self.rotation)
|
|
bullet.rotation = self.rotation + PI/2 * sign(socket.y)
|
|
bullet.instigator = self
|
|
get_parent().add_child(bullet)
|
|
|
|
func take_damage(damage : float):
|
|
life -= damage
|
|
if life <= 0:
|
|
#get_tree().reload_current_scene()
|
|
get_tree().paused = true
|
|
$GameOverScreen.show()
|
|
|