flappy-bird/Player.gd
2024-04-16 12:44:28 +02:00

70 lines
1.5 KiB
GDScript

extends Area2D
@export var speed = 3 # How fast the player will move (pixels/sec).
@export var jump_speed = 4
@export var speed_inc = 0.3
signal start # The bird starts
signal passed # The bird passes a Tower
signal hit # The bird is hit/flies off screen
var tower_locations = []
var screen_size
var playing = false
var speed_y = 0
func start_game():
tower_locations = []
screen_size = get_viewport_rect().size
position = Vector2(0, screen_size.y / 2)
speed_y = 0
update_rotation()
# Called when the node enters the scene tree for the first time.
func _ready():
screen_size = get_viewport_rect().size
# x_offset = get_global_transform().get_origin().x
func _process(delta):
if Input.is_action_just_pressed("ui_accept"):
if not playing:
start_game()
emit_signal("start")
speed_y = 0
playing = true
$CollisionShape2D.set_deferred("disabled", false)
speed_y = min(speed_y, 0) - 1 * jump_speed
if not playing:
return
speed_y += speed_inc
position += Vector2(speed, speed_y * speed)
var global = get_global_transform().get_origin()
while global.x > tower_locations[0]:
emit_signal("passed")
tower_locations.pop_front()
update_rotation()
if position.y > screen_size.y:
emit_signal("hit")
print(position)
playing = false
func update_rotation():
rotation = atan(speed_y / speed)
func _on_Player_body_entered(body):
playing = false
start_game()
emit_signal("hit")
$CollisionShape2D.set_deferred("disabled", true)
func _on_Tower_spawn(x):
tower_locations.append(x)