67 lines
2.0 KiB
GDScript
67 lines
2.0 KiB
GDScript
#@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
|