Files
Dawn-Godot/scripts/Entity/Component/EntityDirection.gd
2025-05-09 22:33:51 -05:00

79 lines
1.9 KiB
GDScript

@tool
class_name EntityDirection extends Node
enum Direction {
SOUTH,
WEST,
NORTH,
EAST,
}
@export var meshInstance:MeshInstance3D = null:
set(newInstance):
meshInstance = newInstance;
_updateMaterial();
@export var direction = Direction.SOUTH:
set(newDirection):
direction = newDirection;
_updateMaterial();
func _ready() -> void:
_updateMaterial();
func _updateMaterial() -> void:
if !meshInstance:
return
for i in range(meshInstance.get_surface_override_material_count()):
var material:ShaderMaterial = meshInstance.get_surface_override_material(i)
if !material:
continue
material.set_shader_parameter("direction", direction)
func getDirectionVector() -> Vector3:
match direction:
Direction.NORTH:
return Vector3(0, 0, -1);
Direction.SOUTH:
return Vector3(0, 0, 1);
Direction.WEST:
return Vector3(-1, 0, 0);
Direction.EAST:
return Vector3(1, 0, 0);
_:
assert(false, "Invalid direction");
return Vector3(0, 0, 0);
func updateDirectionFromMovement(movement:Vector2) -> void:
if movement.x >= abs(movement.y) and(
movement.y == 0 or
(movement.y > 0 and direction != Direction.SOUTH) or
(movement.y < 0 and direction != Direction.NORTH)
):
direction = Direction.EAST;
elif (movement.x <= -abs(movement.y) and (
movement.y == 0 or
(movement.y > 0 and direction != Direction.SOUTH) or
(movement.y < 0 and direction != Direction.NORTH)
)):
direction = Direction.WEST;
elif (movement.y > 0):
direction = Direction.SOUTH;
elif (movement.y < 0):
direction = Direction.NORTH;
# func getDirectionToFace(position:Vector3) -> Direction:
# var diff = position - self.position;
# if abs(diff.x) > abs(diff.z):
# if diff.x > 0:
# return Direction.EAST;
# else:
# return Direction.WEST;
# else:
# if diff.z > 0:
# return Direction.SOUTH;
# else:
# return Direction.NORTH;
# return Direction.SOUTH;