crabs-game/scripts/game1/PlayerController.gd

41 lines
748 B
GDScript3
Raw Permalink Normal View History

extends RigidBody2D
var dir = Vector2(0, 0)
func _input(event):
var x = 0
var y = 0
if event.is_action_pressed('left'):
x -= 1
if event.is_action_released('left'):
x += 1
if event.is_action_pressed('right'):
x += 1
if event.is_action_released('right'):
x -= 1
if event.is_action_pressed('up'):
y -= 1
if event.is_action_released('up'):
y += 1
if event.is_action_pressed('down'):
y += 1
if event.is_action_released('down'):
y -= 1
dir.x += x
dir.y += 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)