flappy-bird/Player.gd

73 lines
1.5 KiB
GDScript3
Raw Permalink Normal View History

2020-01-14 22:20:17 +01:00
extends Area2D
2024-04-16 12:44:28 +02:00
@export var speed = 3 # How fast the player will move (pixels/sec).
@export var jump_speed = 4
@export var speed_inc = 0.3
2020-01-14 22:20:17 +01:00
2020-04-08 12:40:43 +02:00
signal start # The bird starts
signal passed # The bird passes a Tower
signal hit # The bird is hit/flies off screen
var tower_locations = []
2020-01-14 22:20:17 +01:00
var screen_size
2020-01-15 18:48:07 +01:00
var playing = false
2020-01-14 22:20:17 +01:00
var speed_y = 0
2024-04-16 12:44:28 +02:00
func start_game():
2020-04-09 18:30:56 +02:00
tower_locations = []
screen_size = get_viewport_rect().size
position = Vector2(0, screen_size.y / 2)
speed_y = 0
update_rotation()
2020-01-14 22:20:17 +01:00
# Called when the node enters the scene tree for the first time.
func _ready():
screen_size = get_viewport_rect().size
2024-04-18 15:53:19 +02:00
# Called every frame
func _process(_delta):
2020-01-14 22:20:17 +01:00
if Input.is_action_just_pressed("ui_accept"):
2020-01-15 18:48:07 +01:00
if not playing:
2024-04-16 12:44:28 +02:00
start_game()
2020-04-08 12:40:43 +02:00
emit_signal("start")
2020-01-15 18:48:07 +01:00
speed_y = 0
playing = true
$CollisionShape2D.set_deferred("disabled", false)
2020-01-14 22:20:17 +01:00
speed_y = min(speed_y, 0) - 1 * jump_speed
2020-01-15 18:48:07 +01:00
if not playing:
return
2020-04-08 12:40:43 +02:00
2024-04-18 15:53:19 +02:00
# Adjust the birds position
2020-01-15 18:48:07 +01:00
speed_y += speed_inc
position += Vector2(speed, speed_y * speed)
2020-01-14 22:20:17 +01:00
2020-04-08 12:40:43 +02:00
var global = get_global_transform().get_origin()
2020-04-09 18:30:56 +02:00
while global.x > tower_locations[0]:
2020-04-08 12:40:43 +02:00
emit_signal("passed")
tower_locations.pop_front()
2020-02-08 11:36:48 +01:00
2024-04-18 15:53:19 +02:00
# Rotate the bird
2020-04-09 18:30:56 +02:00
update_rotation()
2020-01-15 18:48:07 +01:00
2020-01-29 21:51:40 +01:00
if position.y > screen_size.y:
2020-01-15 18:48:07 +01:00
emit_signal("hit")
playing = false
2020-04-09 18:30:56 +02:00
func update_rotation():
rotation = atan(speed_y / speed)
2020-01-15 14:43:08 +01:00
2024-04-18 15:53:19 +02:00
# Hit detection
func _on_Player_body_entered(_body):
2020-01-15 18:48:07 +01:00
playing = false
2024-04-16 12:44:28 +02:00
start_game()
2020-04-09 18:30:56 +02:00
2020-01-15 14:43:08 +01:00
emit_signal("hit")
2020-04-08 12:40:43 +02:00
$CollisionShape2D.set_deferred("disabled", true)
func _on_Tower_spawn(x):
tower_locations.append(x)