65 lines
1.8 KiB
GDScript
65 lines
1.8 KiB
GDScript
extends RigidBody2D
|
|
@export var speed = 400
|
|
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 _physics_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
|
|
apply_central_force(velocity)
|
|
|
|
var map_size = Vector2(1000, 1000)
|
|
var velocity2 = Vector2.ZERO
|
|
if position.x <= 0 or position.x >= map_size.x:
|
|
velocity2.x = -linear_velocity.x
|
|
if position.y <= 0 or position.y >= map_size.y:
|
|
velocity2.y = -linear_velocity.y
|
|
if velocity2.length():
|
|
linear_velocity = velocity2
|
|
|
|
|
|
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()
|
|
|
|
func add_score(score_to_add : int):
|
|
self.score += score_to_add
|
|
$SFXGetCoin.play()
|