Rewrote entity system
This commit is contained in:
78
scripts/Entity/Component/EntityDirection.gd
Normal file
78
scripts/Entity/Component/EntityDirection.gd
Normal file
@@ -0,0 +1,78 @@
|
||||
@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;
|
Reference in New Issue
Block a user