flappy-bird/Player.gd

73 lines
1.5 KiB
GDScript3
Raw Permalink Normal View History

2020-01-14 21:20:17 +00:00
extends Area2D
2024-04-16 10:44:28 +00: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 21:20:17 +00:00
2020-04-08 10:40:43 +00: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 21:20:17 +00:00
var screen_size
2020-01-15 17:48:07 +00:00
var playing = false
2020-01-14 21:20:17 +00:00
var speed_y = 0
2024-04-16 10:44:28 +00:00
func start_game():
2020-04-09 16:30:56 +00:00
tower_locations = []
screen_size = get_viewport_rect().size
position = Vector2(0, screen_size.y / 2)
speed_y = 0
update_rotation()
2020-01-14 21:20:17 +00:00
# Called when the node enters the scene tree for the first time.
func _ready():
screen_size = get_viewport_rect().size
2024-04-18 13:53:19 +00:00
# Called every frame
func _process(_delta):
2020-01-14 21:20:17 +00:00
if Input.is_action_just_pressed("ui_accept"):
2020-01-15 17:48:07 +00:00
if not playing:
2024-04-16 10:44:28 +00:00
start_game()
2020-04-08 10:40:43 +00:00
emit_signal("start")
2020-01-15 17:48:07 +00:00
speed_y = 0
playing = true
$CollisionShape2D.set_deferred("disabled", false)
2020-01-14 21:20:17 +00:00
speed_y = min(speed_y, 0) - 1 * jump_speed
2020-01-15 17:48:07 +00:00
if not playing:
return
2020-04-08 10:40:43 +00:00
2024-04-18 13:53:19 +00:00
# Adjust the birds position
2020-01-15 17:48:07 +00:00
speed_y += speed_inc
position += Vector2(speed, speed_y * speed)
2020-01-14 21:20:17 +00:00
2020-04-08 10:40:43 +00:00
var global = get_global_transform().get_origin()
2020-04-09 16:30:56 +00:00
while global.x > tower_locations[0]:
2020-04-08 10:40:43 +00:00
emit_signal("passed")
tower_locations.pop_front()
2020-02-08 10:36:48 +00:00
2024-04-18 13:53:19 +00:00
# Rotate the bird
2020-04-09 16:30:56 +00:00
update_rotation()
2020-01-15 17:48:07 +00:00
2020-01-29 20:51:40 +00:00
if position.y > screen_size.y:
2020-01-15 17:48:07 +00:00
emit_signal("hit")
playing = false
2020-04-09 16:30:56 +00:00
func update_rotation():
rotation = atan(speed_y / speed)
2020-01-15 13:43:08 +00:00
2024-04-18 13:53:19 +00:00
# Hit detection
func _on_Player_body_entered(_body):
2020-01-15 17:48:07 +00:00
playing = false
2024-04-16 10:44:28 +00:00
start_game()
2020-04-09 16:30:56 +00:00
2020-01-15 13:43:08 +00:00
emit_signal("hit")
2020-04-08 10:40:43 +00:00
$CollisionShape2D.set_deferred("disabled", true)
func _on_Tower_spawn(x):
tower_locations.append(x)