You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

95 lines
2.7 KiB
GDScript

extends Area2D
enum PlayerState { NORMAL, DASHING, STANDING };
enum PlayerDirection { RIGHT, LEFT, DOWN, UP };
export var speed = 200 # How fast the player will move (pixels/sec).
var screen_size # Size of the game window.
var state = PlayerState.NORMAL;
var direction = PlayerDirection.RIGHT;
var last_process_velocity = Vector2()
func start(pos):
position = pos
show()
func _ready():
screen_size = get_viewport_rect().size
func processNormal(delta):
var velocity = Vector2() # The player's movement vector.
if Input.is_key_pressed(KEY_SHIFT):
state = PlayerState.DASHING
return
if Input.is_action_pressed("ui_right"):
velocity.x += 1
direction = PlayerDirection.RIGHT;
if Input.is_action_pressed("ui_left"):
velocity.x -= 1
direction = PlayerDirection.LEFT;
if Input.is_action_pressed("ui_down"):
velocity.y += 1
direction = PlayerDirection.DOWN;
if Input.is_action_pressed("ui_up"):
velocity.y -= 1
direction = PlayerDirection.UP;
if velocity.length() > 0:
velocity = velocity.normalized() * speed
$AnimatedSprite.play()
position += velocity * delta
position.x = clamp(position.x, 0, screen_size.x)
position.y = clamp(position.y, 0, screen_size.y)
if velocity.x > 0 or velocity.y != 0:
$AnimatedSprite.animation = "forward"
direction = PlayerDirection.RIGHT;
elif velocity.x < 0:
$AnimatedSprite.animation = "backward"
direction = PlayerDirection.LEFT;
else:
$AnimatedSprite.animation = "idle"
last_process_velocity = velocity
func processDash(delta):
$AnimatedSprite.animation = "dash"
$AnimatedSprite.speed_scale = 2.5
var velocity = last_process_velocity
velocity = velocity.normalized() * speed
if velocity.length() > 0:
velocity = velocity.normalized() * speed
$AnimatedSprite.play()
position += velocity * delta * 1.8
position.x = clamp(position.x, 0, screen_size.x)
position.y = clamp(position.y, 0, screen_size.y)
$AnimatedSprite.flip_h = velocity.x < 0
func processStanding(delta):
$AnimatedSprite.animation = "standing up"
$AnimatedSprite.speed_scale = 1
var velocity = Vector2(0.0, 0.0)
position += velocity.normalized() * delta
position.x = clamp(position.x, 0, screen_size.x)
position.y = clamp(position.y, 0, screen_size.y)
$AnimatedSprite.flip_h = last_process_velocity.x < 0
func _process(delta):
if state == PlayerState.NORMAL:
processNormal(delta)
elif state == PlayerState.DASHING:
processDash(delta)
else:
processStanding(delta)
func _on_AnimatedSprite_animation_finished():
if $AnimatedSprite.animation == "dash": # Replace with function body.
state = PlayerState.STANDING
$AnimatedSprite.speed_scale = 1
elif $AnimatedSprite.animation == "standing up":
state = PlayerState.NORMAL
$AnimatedSprite.flip_h = false