Files
seeding-planets/entities/player/scripts/player_sprite.gd

113 lines
2.8 KiB
GDScript

@tool
extends AnimatedSprite2D
class_name PlayerSprite
const ORIENTATION_TIME = 0.1
enum ROrient {
FRONT,
FRONT_RIGHT,
FRONT_LEFT,
BACK,
BACK_RIGHT,
BACK_LEFT,
RIGHT,
LEFT,
}
@export var wanted_orientation : ROrient
var orientation : ROrient : set = set_orientation
var time_since_last_orientation = 10.
func _process(delta):
time_since_last_orientation += delta
if time_since_last_orientation > ORIENTATION_TIME and wanted_orientation != orientation:
time_since_last_orientation = 0.
if is_closest_clockwise(orientation, wanted_orientation):
orientation = clock_wise(orientation)
else :
orientation = counter_clock_wise(orientation)
func set_orientation(o : ROrient = orientation):
orientation = o
if is_node_ready() and animation != orientation_to_str(o):
play(orientation_to_str(o))
static func orientation_to_str(o : ROrient) -> String:
match o:
ROrient.FRONT:
return "front"
ROrient.FRONT_RIGHT:
return "front_right"
ROrient.FRONT_LEFT:
return "front_left"
ROrient.BACK:
return "back"
ROrient.BACK_RIGHT:
return "back_right"
ROrient.BACK_LEFT:
return "back_left"
ROrient.RIGHT:
return "right"
ROrient.LEFT:
return "left"
_:
return "front"
static func clock_wise(o : ROrient) -> ROrient:
match o:
ROrient.FRONT:
return ROrient.FRONT_LEFT
ROrient.FRONT_LEFT:
return ROrient.LEFT
ROrient.LEFT:
return ROrient.BACK_LEFT
ROrient.BACK_LEFT:
return ROrient.BACK
ROrient.BACK:
return ROrient.BACK_RIGHT
ROrient.BACK_RIGHT:
return ROrient.RIGHT
ROrient.RIGHT:
return ROrient.FRONT_RIGHT
ROrient.FRONT_RIGHT:
return ROrient.FRONT
_:
return ROrient.FRONT
static func counter_clock_wise(o : ROrient) -> ROrient:
match o:
ROrient.FRONT:
return ROrient.FRONT_RIGHT
ROrient.FRONT_RIGHT:
return ROrient.RIGHT
ROrient.RIGHT:
return ROrient.BACK_RIGHT
ROrient.BACK_RIGHT:
return ROrient.BACK
ROrient.BACK:
return ROrient.BACK_LEFT
ROrient.BACK_LEFT:
return ROrient.LEFT
ROrient.LEFT:
return ROrient.FRONT_LEFT
ROrient.FRONT_LEFT:
return ROrient.FRONT
_:
return ROrient.FRONT
static func is_closest_clockwise(
current : ROrient,
wanted : ROrient,
):
var move := current
var step := 0
while move != wanted:
step += 1
move = clock_wise(move)
return step < 4