Made movement really smooth
This commit is contained in:
parent
6951a80fdb
commit
8e4d2d8ca9
|
@ -10,21 +10,9 @@ script = ExtResource("1_44r7b")
|
|||
texture = ExtResource("1_5amrr")
|
||||
centered = false
|
||||
|
||||
[node name="UpCast" type="RayCast2D" parent="."]
|
||||
[node name="MoveRay" type="RayCast2D" parent="."]
|
||||
position = Vector2(8, 8)
|
||||
target_position = Vector2(0, -16)
|
||||
|
||||
[node name="LeftCast" type="RayCast2D" parent="."]
|
||||
position = Vector2(8, 8)
|
||||
target_position = Vector2(-16, 0)
|
||||
|
||||
[node name="DownCast" type="RayCast2D" parent="."]
|
||||
position = Vector2(8, 8)
|
||||
target_position = Vector2(0, 16)
|
||||
|
||||
[node name="RightCast" type="RayCast2D" parent="."]
|
||||
position = Vector2(8, 8)
|
||||
target_position = Vector2(16, 0)
|
||||
target_position = Vector2(0, 0)
|
||||
|
||||
[node name="MoveTimer" type="Timer" parent="."]
|
||||
one_shot = true
|
||||
|
|
|
@ -4,13 +4,13 @@ const TILE_SIZE : int = 16
|
|||
|
||||
const MOVE_TIME : float = 0.2
|
||||
|
||||
enum {UP,LEFT,DOWN,RIGHT,NONE = -1}
|
||||
|
||||
@onready var DirectionCast : Array[RayCast2D] = [$UpCast,$LeftCast,$DownCast,$RightCast]
|
||||
|
||||
@onready var MoveTimer : Timer = $MoveTimer
|
||||
|
||||
var direction : int
|
||||
@onready var MoveRay : RayCast2D = $MoveRay
|
||||
|
||||
var direction : Vector2i
|
||||
|
||||
var last_direction : Vector2i
|
||||
|
||||
# Called when the node enters the scene tree for the first time.
|
||||
func _ready() -> void:
|
||||
|
@ -19,40 +19,44 @@ func _ready() -> void:
|
|||
|
||||
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
||||
func _process(delta: float) -> void:
|
||||
direction = NONE
|
||||
|
||||
direction = Vector2i(0,0)
|
||||
|
||||
if Input.is_action_pressed("ui_left"):
|
||||
direction = LEFT
|
||||
direction.x -= 1
|
||||
if Input.is_action_just_pressed("ui_left"):
|
||||
move()
|
||||
|
||||
last_direction = Vector2i(-1,0)
|
||||
move(false)
|
||||
if Input.is_action_pressed("ui_right"):
|
||||
direction = RIGHT
|
||||
direction.x += 1
|
||||
if Input.is_action_just_pressed("ui_right"):
|
||||
move()
|
||||
|
||||
last_direction = Vector2i(1,0)
|
||||
move(false)
|
||||
if Input.is_action_pressed("ui_up"):
|
||||
direction = UP
|
||||
direction.y -= 1
|
||||
if Input.is_action_just_pressed("ui_up"):
|
||||
move()
|
||||
|
||||
last_direction = Vector2i(0,-1)
|
||||
move(false)
|
||||
if Input.is_action_pressed("ui_down"):
|
||||
direction = DOWN
|
||||
direction.y += 1
|
||||
if Input.is_action_just_pressed("ui_down"):
|
||||
move()
|
||||
last_direction = Vector2i(0,1)
|
||||
move(false)
|
||||
|
||||
if MoveTimer.is_stopped():
|
||||
move()
|
||||
|
||||
move(false)
|
||||
|
||||
func move() -> void:
|
||||
if direction == LEFT and not DirectionCast[LEFT].is_colliding():
|
||||
position.x -= TILE_SIZE
|
||||
if direction == RIGHT and not DirectionCast[RIGHT].is_colliding():
|
||||
position.x += TILE_SIZE
|
||||
if direction == UP and not DirectionCast[UP].is_colliding():
|
||||
position.y -= TILE_SIZE
|
||||
if direction == DOWN and not DirectionCast[DOWN].is_colliding():
|
||||
position.y += TILE_SIZE
|
||||
func move(repeated : bool) -> void:
|
||||
if last_direction.x:
|
||||
direction.y = 0
|
||||
if last_direction.y:
|
||||
direction.x = 0
|
||||
|
||||
MoveRay.target_position = Vector2(TILE_SIZE * direction)
|
||||
MoveRay.force_raycast_update()
|
||||
|
||||
if not MoveRay.is_colliding():
|
||||
position += Vector2(TILE_SIZE * direction)
|
||||
elif not repeated:
|
||||
move(true)
|
||||
|
||||
MoveTimer.start(MOVE_TIME)
|
||||
|
|
Loading…
Reference in a new issue