Almost fixed entities

This commit is contained in:
2025-05-12 21:00:13 -05:00
parent 828ff03e56
commit a575b8a47d
13 changed files with 158 additions and 79 deletions

View File

@@ -18,6 +18,8 @@ enum Direction {
direction = newDirection;
_updateMaterial();
@export var characterBody:CharacterBody3D = null
func _ready() -> void:
_updateMaterial();
@@ -63,16 +65,19 @@ func updateDirectionFromMovement(movement:Vector2) -> void:
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;
func getDirectionToFace(position:Vector3) -> Direction:
if !characterBody:
return Direction.SOUTH;
var diff = position - characterBody.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

@@ -1,3 +1,10 @@
class_name EntityInteractable extends Node
signal entityOnInteract(entity:EntityInteractor);
@export var entityDirection:EntityDirection = null
@export var entity:Entity = null
@export var characterBody:CharacterBody3D = null;
signal onInteract(interactor:EntityInteractor, interactable:EntityInteractable)
func interact(interactor:EntityInteractor) -> void:
onInteract.emit(interactor, self)

View File

@@ -1,4 +1,59 @@
class_name EntityInteractor extends Node
func tryInteract() -> EntityInteractable:
return null
enum InteractorType {
PLAYER_INPUT,
UNDECIDED
}
@export var interactorType:InteractorType = InteractorType.UNDECIDED;
@export var entityDirection:EntityDirection = null;
@export var characterBody:CharacterBody3D = null;
@export var interactRange = 0.7;
@export var entity:Entity = null
@export_flags_3d_physics() var interactLayers = 2;
func interactWith(interactable:EntityInteractable) -> void:
interactable.interact(self)
pass
func getRaycastInteractable() -> EntityInteractable:
if !entityDirection or !characterBody:
return null
var rayDirection = entityDirection.getDirectionVector()
var query = PhysicsRayQueryParameters3D.create(
characterBody.position,
characterBody.position + (rayDirection * interactRange)
)
query.collide_with_bodies = true;
query.collide_with_areas = true;
query.collision_mask = interactLayers;
query.exclude = [ characterBody ];
var result = characterBody.get_world_3d().direct_space_state.intersect_ray(query)
if !result or !result.collider:
return null
var coll:Node3D = result.collider;
var interactable:EntityInteractable = coll.find_child("EntityInteractable")
return interactable
func _process(delta: float) -> void:
match interactorType:
InteractorType.PLAYER_INPUT:
_processPlayerInput()
InteractorType.UNDECIDED:
_processUndecided()
_:
assert(false, "Invalid interactor type")
func _processPlayerInput() -> void:
if !Input.is_action_just_pressed("interact"):
return
var interactable:EntityInteractable = getRaycastInteractable()
if interactable:
self.interactWith(interactable)
func _processUndecided() -> void:
pass