crabs-game/scripts/game1/PlayerController.gd

55 lines
1.0 KiB
GDScript

extends RigidBody2D
var dir = Vector2(0, 0)
var sleep = true
var input_right = false
var input_left = false
var input_up = false
var input_down = false
func _input(event):
if sleep: return
if event.is_action_pressed('left'):
input_left = true
if event.is_action_released('left'):
input_left = false
if event.is_action_pressed('right'):
input_right = true
if event.is_action_released('right'):
input_right = false
if event.is_action_pressed('up'):
input_up = true
if event.is_action_released('up'):
input_up = false
if event.is_action_pressed('down'):
input_down = true
if event.is_action_released('down'):
input_down = false
var x = 0
var y = 0
if input_left: x -= 1
if input_right: x += 1
if input_up: y -= 1
if input_down: y += 1
dir = Vector2(x, y)
func _process(delta):
if linear_velocity.length_squared() < 1:
$Sprite.play('default')
elif linear_velocity.x > 0:
$Sprite.play('right')
else:
$Sprite.play('left')
func _physics_process(delta : float):
apply_central_impulse(dir.normalized() * 10)