getting-started/flappy-bird/Player.gd
2024-04-18 16:02:58 +02:00

72 lines
2.1 KiB
GDScript

extends Area2D
# Signals
signal start # The bird starts
signal passed # The bird passes a Tower
signal hit # The bird is hit/flies off screen
var screen_size # The screen size, we want this at a later point
# Speed variables
@export var speed = 3 # How fast the player will move (pixels/sec).
@export var jump_speed = 4 # Speed at which the bird gains height after jumping
@export var speed_inc = 0.3 # Speed at which the bird falls
var speed_y = 0 # Y speed of the bird
var playing = false # State of the game
func start_game():
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():
# We want the screen size to check if something is off screen
screen_size = get_viewport_rect().size
# Called every frame
func _process(_delta):
# Every frame we want to move the bird
# The player can influence the movement by pressing space
# An Area2D can have a position, split between x speed and y speed
# Right now we want to bird to move at a set pace so x is constant
# It's up to you to change the y accordingly.
# Check if space is pressed
if Input.is_action_just_pressed("ui_accept"):
# Player pressed space -> Move the bird up
# Space is pressed, change the y speed so it gains height
# TODO 1
# When we're not playing and space is hit then the game starts -> emit the right signal
# TODO 2
pass
# Potential space are handled
# Decrease the y speed
# TODO 1
# Emit signal when a pipe is passed
# TODO 3
# Update the position with the new x and y speed
position += Vector2(speed, speed_y * speed)
# Rotate the bird
update_rotation()
# Rotate the bird
func update_rotation():
rotation = atan(speed_y / speed)
# We want to trigger this function when the bird hits a tower
# Connect the function with the Area2D signal 'body_entered(body: Node2D)'
# Do this by pressing on the Area2D, go to the 'Node' tab on the right side and right clicking on the desired signal.
func _on_Player_body_entered(_body):
# Set the right variables and emit the right signals so the game restarts
# TODO 2