From 8e4d2d8ca9398ccccd34a4136b0769adf2b6d854 Mon Sep 17 00:00:00 2001 From: ObeseTermite Date: Wed, 9 Oct 2024 22:35:06 -0700 Subject: [PATCH] Made movement really smooth --- scenes/player.tscn | 16 ++---------- scripts/player.gd | 62 ++++++++++++++++++++++++---------------------- 2 files changed, 35 insertions(+), 43 deletions(-) diff --git a/scenes/player.tscn b/scenes/player.tscn index c755337..8cfec65 100644 --- a/scenes/player.tscn +++ b/scenes/player.tscn @@ -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 diff --git a/scripts/player.gd b/scripts/player.gd index ba88fe7..8bdd60b 100644 --- a/scripts/player.gd +++ b/scripts/player.gd @@ -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)