Added player
This commit is contained in:
23
game-of-life-test/player.gd
Normal file
23
game-of-life-test/player.gd
Normal file
@@ -0,0 +1,23 @@
|
||||
extends Area2D
|
||||
|
||||
@export var speed = 400 # How fast the player will move (pixels/sec).
|
||||
var screen_size # Size of the game window.
|
||||
|
||||
func _ready() -> void:
|
||||
screen_size = get_viewport_rect().size
|
||||
|
||||
func _process(delta):
|
||||
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 velocity.length() > 0:
|
||||
velocity = velocity.normalized() * speed
|
||||
$AnimatedSprite2D.play()
|
||||
else:
|
||||
$AnimatedSprite2D.stop()
|
||||
|
||||
position += velocity * delta
|
||||
position = position.clamp(Vector2.ZERO, screen_size)
|
||||
Reference in New Issue
Block a user