got four-direction movement working
This commit is contained in:
1 parent
f9e52d23e5
commit
5c5c510a2a
12 files changed
+504
-107
No files matched your search
@@ -0,0 +1,16 @@
|
||||
@tool
|
||||
class_name DebugLayer
|
||||
extends CanvasLayer
|
||||
|
||||
@export var variables_to_track: Array[String]
|
||||
|
||||
var debug_label_scene:= preload("res://scenes/debug_label.tscn")
|
||||
|
||||
@onready var v_box_container: VBoxContainer = $VBoxContainer
|
||||
|
||||
func _ready() -> void:
|
||||
for variable in variables_to_track:
|
||||
var new_debug_label:= debug_label_scene.instantiate()
|
||||
new_debug_label.target = get_parent()
|
||||
new_debug_label.variable_name = variable
|
||||
v_box_container.add_child(new_debug_label)
|
||||
@@ -0,0 +1 @@
|
||||
uid://dy34r8x6ofyaa
|
||||
@@ -1,28 +0,0 @@
|
||||
#@tool
|
||||
#@icon
|
||||
#class_name
|
||||
extends CharacterBody2D
|
||||
## Documentation comments
|
||||
|
||||
## Signals
|
||||
## Enums
|
||||
## Constants
|
||||
## @export variables
|
||||
@export var speed: int = 10
|
||||
## Regular variables
|
||||
var input_vector: Vector2
|
||||
## @onready variables
|
||||
|
||||
## Overridden built-in virtual methods
|
||||
#func _init() -> void:
|
||||
#func _enter_tree() -> void:
|
||||
#func _ready() -> void:
|
||||
#func _process(delta: float) -> void:
|
||||
func _physics_process(_delta: float) -> void:
|
||||
input_vector = Input.get_vector("left", "right", "up", "down")
|
||||
velocity = input_vector * speed
|
||||
move_and_slide()
|
||||
## Remaining virtual methods
|
||||
## Overridden custom methods
|
||||
## Remaining methods
|
||||
## Subclasses
|
||||
@@ -0,0 +1,66 @@
|
||||
#@tool
|
||||
#@icon
|
||||
#class_name
|
||||
extends CharacterBody2D
|
||||
## Documentation comments
|
||||
|
||||
enum FaceDirection {LEFT = 0, RIGHT = 1, UP = 2, DOWN = 3}
|
||||
enum State {IDLE, WALK}
|
||||
|
||||
const SPEED: int = 48
|
||||
## @export variables
|
||||
|
||||
## Regular variables
|
||||
var current_direction: FaceDirection = FaceDirection.DOWN:
|
||||
set = set_direction
|
||||
var current_direction_string: String = "down"
|
||||
|
||||
var current_state: State = State.IDLE:
|
||||
set = set_state
|
||||
var current_state_string: String = "idle"
|
||||
|
||||
var input_vector: Vector2
|
||||
|
||||
## @onready variables
|
||||
@onready var player_sprite: AnimatedSprite2D = $PlayerSprite
|
||||
|
||||
## Overridden built-in virtual methods
|
||||
#func _init() -> void:
|
||||
#func _enter_tree() -> void:
|
||||
#func _ready() -> void:
|
||||
#func _process(delta: float) -> void:
|
||||
func _physics_process(_delta: float) -> void:
|
||||
input_vector = Vector2.ZERO
|
||||
if Input.is_action_pressed("left"):
|
||||
input_vector = Vector2.LEFT
|
||||
current_direction = FaceDirection.LEFT
|
||||
current_state = State.WALK
|
||||
elif Input.is_action_pressed("right"):
|
||||
input_vector = Vector2.RIGHT
|
||||
current_direction = FaceDirection.RIGHT
|
||||
current_state = State.WALK
|
||||
elif Input.is_action_pressed("up"):
|
||||
input_vector = Vector2.UP
|
||||
current_direction = FaceDirection.UP
|
||||
current_state = State.WALK
|
||||
elif Input.is_action_pressed("down"):
|
||||
input_vector = Vector2.DOWN
|
||||
current_direction = FaceDirection.DOWN
|
||||
current_state = State.WALK
|
||||
else: current_state = State.IDLE
|
||||
velocity = input_vector * SPEED
|
||||
move_and_slide()
|
||||
## Remaining virtual methods
|
||||
## Overridden custom methods
|
||||
## Remaining methods
|
||||
func set_direction(new_direction: FaceDirection) -> void:
|
||||
current_direction = new_direction
|
||||
current_direction_string = str(FaceDirection.keys()[new_direction]).to_lower()
|
||||
print_debug("New direction is %s" % current_direction_string)
|
||||
|
||||
func set_state(new_state: State) -> void:
|
||||
current_state = new_state
|
||||
current_state_string = str(State.keys()[new_state]).to_lower()
|
||||
var animation_string = current_state_string + "_" + current_direction_string
|
||||
player_sprite.play(animation_string)
|
||||
## Subclasses
|
||||
File renamed without changes.
Reference in new issue
Block a user