crabs-game/scripts/game1/PlayerController.gd

56 lines
1.1 KiB
GDScript3
Raw Permalink Normal View History

extends RigidBody2D
var dir = Vector2(0, 0)
2020-08-19 18:59:29 +02:00
var sleep = true
var input_right = false
var input_left = false
var input_up = false
var input_down = false
func _input(event):
2020-08-19 18:59:29 +02:00
if sleep: return
if event.is_action_pressed('left'):
2020-08-19 18:59:29 +02:00
input_left = true
if event.is_action_released('left'):
2020-08-19 18:59:29 +02:00
input_left = false
if event.is_action_pressed('right'):
2020-08-19 18:59:29 +02:00
input_right = true
if event.is_action_released('right'):
2020-08-19 18:59:29 +02:00
input_right = false
if event.is_action_pressed('up'):
2020-08-19 18:59:29 +02:00
input_up = true
if event.is_action_released('up'):
2020-08-19 18:59:29 +02:00
input_up = false
if event.is_action_pressed('down'):
2020-08-19 18:59:29 +02:00
input_down = true
if event.is_action_released('down'):
2020-08-19 18:59:29 +02:00
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
2020-08-19 18:59:29 +02:00
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):
2020-08-20 12:01:19 +02:00
if sleep: return
apply_central_impulse(dir.normalized() * 10)