Rewrote entity system

This commit is contained in:
2025-05-09 22:33:51 -05:00
parent a69ec56a2d
commit f465880044
29 changed files with 372 additions and 284 deletions

View 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;

View File

@@ -0,0 +1 @@
uid://cjfcpi7sxentf

View File

@@ -0,0 +1,77 @@
class_name EntityMovement extends Node
enum MovementType {
PLAYER_INPUT,
STILL,
NPC_RANDOM_LOOK
};
@export var characterBody:CharacterBody3D = null;
@export var entity:Entity = null;
@export var entityDirection:EntityDirection = null;
@export var speed:float = 200;
@export var runSpeed:float = 400;
@export var friction:float = 8.5;
@export var gravity:float = 30;
@export var movementType:MovementType = MovementType.STILL;
@export var randomLookMinTime:float = 1.5;
@export var randomLookMaxTime:float = 4.0;
var randomLookTimer:float = 0.0;
func _physics_process(delta:float) -> void:
if !entity || entity.isPaused() || !characterBody:
return;
# Update movement
_updateVelocity(delta)
# Gravity and friction
if !characterBody.is_on_floor():
characterBody.velocity.y -= gravity * delta;
else:
characterBody.velocity += -(characterBody.velocity * friction * delta);
# if velocity.length() != 0:
# _updateTileData();
# Update character controller.
characterBody.move_and_slide();
func _updateVelocity(delta:float):
match movementType:
MovementType.PLAYER_INPUT:
_updatePlayerInput(delta);
MovementType.STILL:
_updateStill(delta);
MovementType.NPC_RANDOM_LOOK:
_updateNPCRandomLook(delta);
func _updatePlayerInput(delta:float) -> void:
var dir:Vector2 = Input.get_vector("left", "right", "up", "down");
var moveSpeed:float;
if Input.is_action_pressed("run"):
moveSpeed = runSpeed;
else:
moveSpeed = speed;
if dir.x != 0 or dir.y != 0:
characterBody.velocity.x = dir.x * moveSpeed * delta;
characterBody.velocity.z = dir.y * moveSpeed * delta;
if entityDirection:
entityDirection.updateDirectionFromMovement(dir);
func _updateStill(delta:float):
pass
func _updateNPCRandomLook(delta:float):
if !entityDirection:
return
randomLookTimer -= delta;
if randomLookTimer <= 0:
randomLookTimer = randf_range(randomLookMinTime, randomLookMaxTime);
entityDirection.direction = randi_range(0, 3);

View File

@@ -0,0 +1 @@
uid://c5nfs0m6ua4eb

View File

@@ -0,0 +1 @@
uid://dp6itsmbl0ucn