feat: Added portals
This commit is contained in:
@@ -11,7 +11,7 @@ bool AABB_Collide(AABB a, AABB b)
|
||||
bool AABB_Point(AABB a, V2f v)
|
||||
{
|
||||
bool collision_x = a.pos.x + a.size.x >= v.x && a.pos.x <= v.x;
|
||||
bool collision_y = a.pos.x + a.size.y >= v.y && a.pos.y <= v.y;
|
||||
bool collision_y = a.pos.y + a.size.y >= v.y && a.pos.y <= v.y;
|
||||
return collision_x && collision_y;
|
||||
}
|
||||
|
||||
@@ -45,4 +45,4 @@ bool AABB_Circle(F32 rad, V2f radOrigin, AABB a)
|
||||
F32 xSq = (Abs(aCentre.x) - Abs(radOrigin.x)) * (Abs(aCentre.x) - Abs(radOrigin.x));
|
||||
F32 ySq = (Abs(aCentre.y) - Abs(radOrigin.y)) * (Abs(aCentre.y) - Abs(radOrigin.y));
|
||||
return SDL_sqrt(xSq + ySq) < rad;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,6 +10,11 @@ V2f shootTowards(Bandit *bandit, V2f target, Random* r)
|
||||
}
|
||||
|
||||
void UpdateBandit(F32 delta, Bandit *bandit, World *world) {
|
||||
for(int i = 0; i < world->portalCount; i++) {
|
||||
if(AABB_Collide(world->portals[0].box, bandit->collision)) {
|
||||
bandit->currentArea = world->portals[0].area;
|
||||
}
|
||||
}
|
||||
if (
|
||||
world->player.controls.shot && AABB_Slab(world->player.collision.pos, world->player.shotPos, bandit->collision) && bandit->currentArea == world->player.currentArea)
|
||||
{
|
||||
|
||||
@@ -4,6 +4,11 @@
|
||||
#include "core/math.h"
|
||||
|
||||
void UpdateNPC(F32 delta, NPC *npc, World *world) {
|
||||
for(int i = 0; i < world->portalCount; i++) {
|
||||
if(AABB_Collide(world->portals[0].box, npc->collision)) {
|
||||
npc->currentArea = world->portals[0].area;
|
||||
}
|
||||
}
|
||||
switch (npc->mode) {
|
||||
case NPC_ACTION_WAITING:
|
||||
npc->waitTime+=delta;
|
||||
|
||||
@@ -165,6 +165,11 @@ void PlayerUpdate(F32 delta, Player *player) {
|
||||
dir = V2f_Scale(NormaliseV2F(dir), PLAYER_SPEED*delta);
|
||||
player->collision.pos.x += dir.x;
|
||||
player->collision.pos.y += dir.y;
|
||||
for(int i = 0; i < player->world->portalCount; i++) {
|
||||
if(AABB_Collide(player->world->portals[0].box, player->collision)) {
|
||||
player->currentArea = player->world->portals[0].area;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void PlayerDraw(D_Context *draw, Player *player) {
|
||||
|
||||
@@ -64,16 +64,6 @@ void RenderWorld(World *world, D_Context *draw) {
|
||||
);
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < world->navMesh->nodeCount; i++) {
|
||||
NavNode n = world->navMesh->nodes[i];
|
||||
D_Rect(
|
||||
draw,
|
||||
n.pos.x,
|
||||
n.pos.y,
|
||||
.texture = 0,
|
||||
.scale = 0.2f,
|
||||
);
|
||||
}
|
||||
for(U32 i = 0; i < world->npcCount; i++) {
|
||||
NPC npc = world->npcs[i];
|
||||
if(npc.currentArea == world->player.currentArea) {
|
||||
@@ -127,6 +117,11 @@ void SaveWorld(M_Arena *arena, World *world) {
|
||||
FS_FileWrite(file, world->map, sizeof(U32)*WORLD_MAP_MAX, offset);
|
||||
offset += sizeof(U32)*WORLD_MAP_MAX;
|
||||
|
||||
FS_FileWrite(file, &world->portalCount, sizeof(U32), offset);
|
||||
offset += sizeof(U32);
|
||||
|
||||
FS_FileWrite(file, world->hitboxes, sizeof(World_Portal)*WORLD_PORTAL_MAX, offset);
|
||||
|
||||
FS_FileClose(file);
|
||||
printf("Saved world :)\n");
|
||||
}
|
||||
@@ -175,6 +170,12 @@ void LoadWorld(M_Arena *arena, World *world) {
|
||||
FS_FileRead(file, world->map, sizeof(U32)*WORLD_MAP_MAX, offset);
|
||||
offset += sizeof(U32)*WORLD_MAP_MAX;
|
||||
|
||||
FS_FileRead(file, &world->portalCount, sizeof(U32), offset);
|
||||
offset += sizeof(U32);
|
||||
|
||||
world->portals = M_ArenaPush(arena, World_Portal, .count=WORLD_PORTAL_MAX);
|
||||
FS_FileRead(file, world->hitboxes, sizeof(World_Portal)*WORLD_PORTAL_MAX, offset);
|
||||
|
||||
FS_FileClose(file);
|
||||
printf("loaded world\n");
|
||||
}
|
||||
@@ -195,14 +196,10 @@ void GenerateNavMesh(M_Arena *arena, World *world) {
|
||||
if(skip) {continue;}
|
||||
world->navMesh->nodes[world->navMesh->nodeCount].pos.x = x;
|
||||
world->navMesh->nodes[world->navMesh->nodeCount].pos.y = y;
|
||||
U32 cost = 20;
|
||||
if(Str8_Equal(world->tileTypes[world->map[i]].tag, S("path_middle"), STR8_EQUAL_IGNORE_CASE)) {
|
||||
cost = 10;
|
||||
}
|
||||
world->navMesh->nodes[world->navMesh->nodeCount].connectionCount = 0;
|
||||
for(int nx = -1; nx < 2; nx++){
|
||||
for(int ny = -1; ny < 2; ny++){
|
||||
if((nx==ny) && nx==0) {continue;}
|
||||
if(nx==0 && ny==0) {continue;}
|
||||
if(x+nx < 0 || x+nx > 95) {
|
||||
continue;
|
||||
}
|
||||
@@ -216,14 +213,42 @@ void GenerateNavMesh(M_Arena *arena, World *world) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(skip) {continue;}
|
||||
U32 index = x+nx + (y+ny)*96;
|
||||
U32 nCount = world->navMesh->nodeCount;
|
||||
S32 index = -1;
|
||||
for(int ohgod = 0; ohgod < nCount; ohgod++) {
|
||||
if(world->navMesh->nodes[ohgod].pos.x == nx+x&& world->navMesh->nodes[ohgod].pos.y == y+ny) {
|
||||
index=ohgod;
|
||||
break;
|
||||
}
|
||||
}
|
||||
F32 cost = 20;
|
||||
if(ny+nx == 2) {
|
||||
cost = 40;
|
||||
};
|
||||
if(Str8_Equal(world->tileTypes[world->map[i]].tag, S("path_middle"), STR8_EQUAL_IGNORE_CASE)) {
|
||||
cost = 1;
|
||||
if(Abs(ny+nx) == 2) {
|
||||
cost = 4;
|
||||
};
|
||||
}
|
||||
if(index < 0) {continue;}
|
||||
skip |= index > nCount;
|
||||
if(skip) {continue;}
|
||||
world->navMesh->nodes[nCount].connections[world->navMesh->nodes[nCount].connectionCount].NodeIndex = index;
|
||||
world->navMesh->nodes[nCount].connections[world->navMesh->nodes[nCount].connectionCount].Cost = cost;
|
||||
world->navMesh->nodes[index].connections[world->navMesh->nodes[index].connectionCount].NodeIndex = nCount;
|
||||
world->navMesh->nodes[index].connections[world->navMesh->nodes[index].connectionCount].Cost = cost;
|
||||
world->navMesh->nodes[nCount].connectionCount++;
|
||||
world->navMesh->nodes[index].connectionCount++;
|
||||
}
|
||||
}
|
||||
world->navMesh->nodeCount++;
|
||||
}
|
||||
for(int i = 0; i < world->npcCount; i++) {
|
||||
world->npcs[i].mode = NPC_ACTION_WAITING;
|
||||
world->npcs[i].maxWaitTime = Random_F32(&world->random, 20, 140);
|
||||
world->npcs[i].waitTime = 0;
|
||||
world->npcs[i].currentNavNode=0;
|
||||
world->npcs[i].pathIndex = 0;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user