Merge branch 'main' of yibble.dev:bulmanator/ld58
Fixed conflicts Added "code" directory for include to make it easier to include core headers Stopped warnings (probably cl specific)
This commit is contained in:
18
code/game/aabb.h
Normal file
18
code/game/aabb.h
Normal file
@@ -0,0 +1,18 @@
|
||||
#if !defined(LD_GAME_AABB_H_)
|
||||
#define LD_GAME_AABB_H_
|
||||
|
||||
#include "../core/types.h"
|
||||
#include "../core/macros.h"
|
||||
|
||||
|
||||
typedef struct AABB AABB;
|
||||
struct AABB {
|
||||
V2f pos;
|
||||
V2f size;
|
||||
};
|
||||
|
||||
|
||||
function bool AABB_Collide(AABB a, AABB b);
|
||||
function bool AABB_Point(AABB a, V2f v);
|
||||
|
||||
#endif // LD_GAME_AABB_H_
|
||||
@@ -1,4 +1,3 @@
|
||||
|
||||
void G_ImagesLoad(G_State *game) {
|
||||
M_TempScope(0, 0) {
|
||||
FS_List assets = FS_PathList(temp.arena, S("assets"));
|
||||
@@ -193,3 +192,7 @@ void G_PipelinesLoad(G_State *game) {
|
||||
|
||||
Vk_PipelineCreate(basic);
|
||||
}
|
||||
|
||||
#include "impl/aabb.c"
|
||||
#include "impl/nav.c"
|
||||
#include "impl/player.c"
|
||||
|
||||
@@ -31,4 +31,10 @@ struct G_State {
|
||||
function void G_ImagesLoad(G_State *game);
|
||||
function void G_PipelinesLoad(G_State *game);
|
||||
|
||||
#include "aabb.h"
|
||||
#include "player.h"
|
||||
#include "nav.h"
|
||||
#include "npc.h"
|
||||
#include "world.h"
|
||||
|
||||
#endif // LD_GAME_CORE_H_
|
||||
|
||||
15
code/game/impl/aabb.c
Normal file
15
code/game/impl/aabb.c
Normal file
@@ -0,0 +1,15 @@
|
||||
#include "game/aabb.h"
|
||||
#include "core/types.h"
|
||||
|
||||
bool AABB_Collide(AABB a, AABB b) {
|
||||
bool collision_x = a.pos.x + a.size.x >= b.pos.x && b.pos.x + b.size.x >= a.pos.x;
|
||||
bool collision_y = a.pos.y + a.size.x >= b.pos.y && b.pos.y + b.size.y >= a.pos.y;
|
||||
return collision_x && collision_y;
|
||||
}
|
||||
|
||||
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;
|
||||
return collision_x && collision_y;
|
||||
}
|
||||
|
||||
101
code/game/impl/nav.c
Normal file
101
code/game/impl/nav.c
Normal file
@@ -0,0 +1,101 @@
|
||||
#include "game/nav.h"
|
||||
#include "core/types.h"
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#define MAX_UNFINISHED 128
|
||||
|
||||
typedef struct navSearchNodeState navSearchNodeState;
|
||||
struct navSearchNodeState{
|
||||
bool visited;
|
||||
U64 distance;
|
||||
U32 shortest;
|
||||
bool addedToUnvisited;
|
||||
};
|
||||
|
||||
typedef struct navSearchState navSearchState;
|
||||
struct navSearchState{
|
||||
navSearchNodeState nodeStates[NAV_MAX_NODES];
|
||||
};
|
||||
|
||||
navSearchState initState(U32 start, U32 meshSize) {
|
||||
navSearchState state = {};
|
||||
for(U32 i = 0; i < meshSize; i++) {
|
||||
state.nodeStates[i].visited = false;
|
||||
state.nodeStates[i].addedToUnvisited = false;
|
||||
// underflow to the max :)
|
||||
state.nodeStates[i].distance = U64_MAX;
|
||||
state.nodeStates[i].shortest = 0;
|
||||
}
|
||||
state.nodeStates[start].distance = 0;
|
||||
return state;
|
||||
}
|
||||
|
||||
U32 getLowestState(U32 unfinishedIndexes[128], U32 unfinishedCount, navSearchState state, U32 *offset) {
|
||||
U32 lowest = U32_MAX;
|
||||
U32 lowestI = U32_MAX;
|
||||
bool startFound = false;
|
||||
for(U32 i = *offset; i < unfinishedCount; i++) {
|
||||
navSearchNodeState checkNode = state.nodeStates[unfinishedIndexes[i]];
|
||||
if(checkNode.visited) {
|
||||
if(!startFound) {
|
||||
*offset = i;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
startFound = true;
|
||||
if (lowest > checkNode.distance) {
|
||||
lowest = cast(U32) checkNode.distance;
|
||||
lowestI = unfinishedIndexes[i];
|
||||
}
|
||||
}
|
||||
return lowestI;
|
||||
}
|
||||
|
||||
// Generate a path to follow between the start and end node.
|
||||
NavPath Nav_Path(NavMesh mesh, U32 start, U32 end) {
|
||||
navSearchState state = initState(start, mesh.nodeCount);
|
||||
U32 unfinishedCount = 1;
|
||||
U32 unfinishedIndexes[NAV_MAX_NODES] = {start};
|
||||
// I don't want to spend time removing items from
|
||||
// the unfinished nodes, so when checking for a lowest
|
||||
// if I find the first N items have been checked, I'll mark
|
||||
// an offset to skip the first N items.
|
||||
U32 unfinishedOffset = 0;
|
||||
U32 lowestNodeIndex = start;
|
||||
bool found = false;
|
||||
while(!found) {
|
||||
for(int connectionI = 0 ; connectionI < mesh.nodes[lowestNodeIndex].connectionCount; connectionI++) {
|
||||
NavConnection connection = mesh.nodes[lowestNodeIndex].connections[connectionI];
|
||||
navSearchNodeState *testNode = &state.nodeStates[connection.NodeIndex];
|
||||
if(testNode->visited) {continue;}
|
||||
U32 distance = cast(U32) (state.nodeStates[lowestNodeIndex].distance + connection.Cost);
|
||||
distance += cast(U32) (mesh.nodes[end].pos.x - mesh.nodes[connection.NodeIndex].pos.x);
|
||||
distance += cast(U32) (mesh.nodes[end].pos.y - mesh.nodes[connection.NodeIndex].pos.y);
|
||||
if(testNode->distance > distance) {
|
||||
testNode->distance = distance;
|
||||
testNode->shortest = lowestNodeIndex;
|
||||
}
|
||||
if(!testNode->addedToUnvisited) {
|
||||
unfinishedIndexes[unfinishedCount] = connection.NodeIndex;
|
||||
unfinishedCount++;
|
||||
testNode->addedToUnvisited = true;
|
||||
}
|
||||
}
|
||||
state.nodeStates[lowestNodeIndex].visited = true;
|
||||
lowestNodeIndex = getLowestState(unfinishedIndexes, unfinishedCount, state, &unfinishedOffset);
|
||||
if(lowestNodeIndex == end) {
|
||||
found = true;
|
||||
}
|
||||
}
|
||||
NavPath res_path = {0};
|
||||
U32 index = end;
|
||||
while(index!=start) {
|
||||
res_path.indexes[res_path.nodeCount] = index;
|
||||
res_path.nodeCount++;
|
||||
index = state.nodeStates[index].shortest;
|
||||
}
|
||||
res_path.indexes[res_path.nodeCount] = start;
|
||||
res_path.nodeCount++;
|
||||
return res_path;
|
||||
}
|
||||
22
code/game/impl/player.c
Normal file
22
code/game/impl/player.c
Normal file
@@ -0,0 +1,22 @@
|
||||
#include "../player.h"
|
||||
|
||||
void PlayerUpdate(SDL_Event *event, Player *player)
|
||||
{
|
||||
SDL_KeyboardEvent key = event->key;
|
||||
if (key.key == SDLK_W)
|
||||
{
|
||||
player->pos.y += 10;
|
||||
}
|
||||
if (key.key == SDLK_A)
|
||||
{
|
||||
player->pos.x -= 10;
|
||||
}
|
||||
if (key.key == SDLK_D)
|
||||
{
|
||||
player->pos.x += 10;
|
||||
}
|
||||
if (key.key == SDLK_S)
|
||||
{
|
||||
player->pos.y -= 10;
|
||||
}
|
||||
}
|
||||
40
code/game/nav.h
Normal file
40
code/game/nav.h
Normal file
@@ -0,0 +1,40 @@
|
||||
#if !defined(LD_GAME_NAV_H_)
|
||||
#define LD_GAME_NAV_H_
|
||||
|
||||
#include "core/types.h"
|
||||
#include "core/macros.h"
|
||||
|
||||
#define NAV_MAX_PATH 1024
|
||||
#define NAV_MAX_CONNECTIONS 8
|
||||
#define NAV_MAX_NODES 4096
|
||||
|
||||
typedef struct NavNode NavNode;
|
||||
|
||||
typedef struct NavConnection NavConnection;
|
||||
struct NavConnection{
|
||||
F32 Cost;
|
||||
U32 NodeIndex;
|
||||
};
|
||||
|
||||
struct NavNode {
|
||||
V2f pos;
|
||||
U8 connectionCount;
|
||||
NavConnection connections[NAV_MAX_CONNECTIONS];
|
||||
};
|
||||
|
||||
typedef struct NavPath NavPath;
|
||||
struct NavPath {
|
||||
U32 nodeCount;
|
||||
U32 indexes[NAV_MAX_PATH];
|
||||
};
|
||||
|
||||
typedef struct NavMesh NavMesh;
|
||||
struct NavMesh{
|
||||
U32 nodeCount;
|
||||
NavNode nodes[NAV_MAX_NODES];
|
||||
};
|
||||
|
||||
|
||||
function NavPath Nav_Path(NavMesh mesh, U32 start, U32 end);
|
||||
|
||||
#endif
|
||||
10
code/game/npc.h
Normal file
10
code/game/npc.h
Normal file
@@ -0,0 +1,10 @@
|
||||
#if !defined(LD_GAME_NPC_H_)
|
||||
#define LD_GAME_NPC_H_
|
||||
#include "aabb.h"
|
||||
|
||||
typedef struct NPC NPC;
|
||||
struct NPC {
|
||||
AABB collision;
|
||||
};
|
||||
|
||||
#endif // LD_GAME_NPC_H_
|
||||
17
code/game/player.h
Normal file
17
code/game/player.h
Normal file
@@ -0,0 +1,17 @@
|
||||
#if !defined(LD_GAME_PLAYER_H_)
|
||||
#define LD_GAME_PLAYER_H_
|
||||
|
||||
#include "../core/types.h"
|
||||
#include "../core/macros.h"
|
||||
|
||||
#include <SDL3/SDL_events.h>
|
||||
|
||||
typedef struct Player Player;
|
||||
struct Player
|
||||
{
|
||||
V2f pos;
|
||||
};
|
||||
|
||||
function void PlayerUpdate(SDL_Event *event, Player *player);
|
||||
|
||||
#endif // LD_GAME_PLAYER_H_
|
||||
10
code/game/world.h
Normal file
10
code/game/world.h
Normal file
@@ -0,0 +1,10 @@
|
||||
#if !defined(LD_GAME_WORLD_H_)
|
||||
#define LD_GAME_WORLD_H_
|
||||
|
||||
typedef struct World World;
|
||||
struct World {
|
||||
NPC npcs[128];
|
||||
U32 npcCount;
|
||||
};
|
||||
|
||||
#endif // LD_GAME_WORLD_H_
|
||||
Reference in New Issue
Block a user