aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/entity.c6
-rw-r--r--src/entity.h2
-rw-r--r--src/player.c10
-rw-r--r--src/settings.c1
-rw-r--r--src/settings.h3
-rw-r--r--src/world.c50
6 files changed, 61 insertions, 11 deletions
diff --git a/src/entity.c b/src/entity.c
index 2242878..90ee641 100644
--- a/src/entity.c
+++ b/src/entity.c
@@ -115,6 +115,12 @@ bool entityCanBeSelected(EntityId id)
return entityEntries[id].canBeSelected;
}
+float getEntityDistance(Entity entity, Vector3 position)
+{
+ return Vector3Distance(entity.position, position)
+ - (Vector3Distance(entity.box.min, entity.box.max) / 2.0);
+}
+
InteractionCommand interactWithEntity(Entity* entity, Game* game,
Selection selection)
{
diff --git a/src/entity.h b/src/entity.h
index d791c51..65e5b2a 100644
--- a/src/entity.h
+++ b/src/entity.h
@@ -86,6 +86,8 @@ void placeEntityOnGround(Entity* entity, const World* world);
bool entityIsPlace(EntityId id);
bool entityCanBeSelected(EntityId id);
+float getEntityDistance(Entity entity, Vector3 position);
+
InteractionCommand interactWithEntity(Entity* entity, Game* game,
Selection selection);
diff --git a/src/player.c b/src/player.c
index 36f5efe..a153443 100644
--- a/src/player.c
+++ b/src/player.c
@@ -83,12 +83,10 @@ void updatePlayerMovement(Player* player, Game* game)
camera->target = Vector3Add(player->position, player->direction);
}
-bool playerCanEntityBeSelected(Player* player, Entity* entity)
+bool playerCanEntityBeSelected(Player* player, Entity entity)
{
- float maxDistance = PLAYER_MAX_SELECT_DISTANCE;
- maxDistance += Vector3Distance(entity->box.min, entity->box.max) / 2.0;
-
- return Vector3Distance(player->position, entity->position) <= maxDistance;
+ return getEntityDistance(entity, player->position)
+ <= PLAYER_MAX_SELECT_DISTANCE;
}
void playerInteractWithEntity(Player* player, Entity* entity, Game* game)
@@ -98,7 +96,7 @@ void playerInteractWithEntity(Player* player, Entity* entity, Game* game)
void playerUpdateSelectedEntity(Player* player, Entity* entity, Game* game)
{
- if (!playerCanEntityBeSelected(player, entity))
+ if (!playerCanEntityBeSelected(player, *entity))
{
return;
}
diff --git a/src/settings.c b/src/settings.c
index 5356ffc..31b70c1 100644
--- a/src/settings.c
+++ b/src/settings.c
@@ -13,6 +13,7 @@ Settings defaultSettings()
.edgeDetectionFactor = 0.11,
.gamma = 0.6,
.colorCount = 11.0,
+ .maxUpdateDistance = 100.0,
.mouseSpeed = 0.1,
.forwardKey = KEY_W,
.backwardKey = KEY_S,
diff --git a/src/settings.h b/src/settings.h
index c399fcc..36d311f 100644
--- a/src/settings.h
+++ b/src/settings.h
@@ -24,6 +24,9 @@ typedef struct {
float gamma;
float colorCount;
+ // Update.
+ float maxUpdateDistance;
+
// Controls.
float mouseSpeed;
KeyboardKey forwardKey;
diff --git a/src/world.c b/src/world.c
index 5ffec66..6f2e093 100644
--- a/src/world.c
+++ b/src/world.c
@@ -509,13 +509,13 @@ void generateWorldSamanthasPlace(World* world, const Assets* assets,
Vector3 center = Vector3Scale(world->size, 0.5);
Entity spot = createEntity(SAMANTHAS_SPOT, center);
world->entities[*placeId] = spot;
+ world->places[*placeId] = *placeId;
*placeId = *placeId + 1;
// Samantha.
Entity samantha = createEntity(SAMANTHA, Vector3Add(center,
SAMANTHA_OFFSET));
world->entities[*placeId] = samantha;
- *placeId = *placeId + 1;
// Trashcans yippee!
Vector3 trashPosition = (Vector3){-SAMANTHAS_SPOT,
@@ -824,15 +824,57 @@ void drawBVHDebug(BVHNode bvh, int level, int selected)
}
}
+float getBVHNodeDistance(BVHNode node, Vector3 position)
+{
+ return Vector3Distance(node.position, position)
+ - (Vector3Distance(node.box.min, node.box.max) / 2.0);
+}
+
+void updateNearByEntites(World* world, BVHNode node, Game* game)
+{
+ if (getBVHNodeDistance(node, game->player.position)
+ > game->settings.maxUpdateDistance)
+ {
+ return;
+ }
+
+ // Leaf node.
+ if (node.branchCount == 0)
+ {
+ for (int index = 0; index < BVH_MAX; ++index)
+ {
+ if (node.entities[index] != -1)
+ {
+ Entity* entity = &world->entities[node.entities[index]];
+
+ if (!entityIsPlace(entity->id))
+ {
+ updateEntity(entity, game);
+ }
+ }
+ }
+ }
+ else // Subtree.
+ {
+ for (int index = 0; index < node.branchCount; ++index)
+ {
+ updateNearByEntites(world, *node.branches[index], game);
+ }
+ }
+}
+
void updateWorld(World* world, Game* game)
{
DrawModel(world->heightmap, Vector3Zero(), 1.0, WHITE);
- for (int index = 0; index < WORLD_ENTITY_MAX; ++index)
+ // Update places.
+ for (int index = 0; index < WORLD_PLACE_COUNT; ++index)
{
- updateEntity(&world->entities[index], game);
+ updateEntity(&world->entities[world->places[index]], game);
}
+ updateNearByEntites(world, world->bvh, game);
+
DrawMeshInstanced(game->assets.models[UTILITY_POLE_MODEL].meshes[0],
game->assets.models[UTILITY_POLE_MODEL].materials[0],
world->utilityPoleTransforms,
@@ -993,8 +1035,6 @@ void castRayBVH(const World* world, BVHNode node, Ray ray, bool allowAll,
closest, closestDistance);
}
}
-
- return;
}
WorldUID castRayAtWorld(const World* world, Ray ray, bool allowAll,