Files
ld58/code/game/impl/world.c

82 lines
2.7 KiB
C
Raw Normal View History

#include "../world.h"
#include "../npc.h"
#include "../player.h"
2025-10-05 22:35:34 +01:00
#include "../aabb.h"
#include <SDL3/SDL_events.h>
2025-10-06 01:10:44 +01:00
#include <SDL3/SDL_keycode.h>
#include <SDL3/SDL_oldnames.h>
2025-10-06 00:38:10 +01:00
#include "../map.h"
2025-10-05 16:05:57 +01:00
void UpdateWorld(F32 delta, World *world)
{
2025-10-05 21:05:29 +01:00
UpdateBandit(delta, &world->bandit, world);
UpdateNPCs(delta, world);
2025-10-05 18:05:01 +01:00
PlayerUpdate(delta, &world->player);
}
2025-10-05 16:05:57 +01:00
void UpdateNPCs(F32 delta, World *world)
{
for (U32 i = 0; i < world->npcCount; i++)
{
NPC *npc = &world->npcs[i];
UpdateNPC(delta, npc, world);
2025-10-06 00:48:41 +01:00
if (
world->player.controls.shot && AABB_Slab(world->player.pos, world->player.shotPos, npc->collision) && npc->currentArea == world->player.currentArea)
{
printf("You shot %*.s\n", Sv(world->npcs[i].name));
2025-10-05 21:05:29 +01:00
}
}
}
2025-10-05 16:05:57 +01:00
void ProcessEvents(SDL_Event *event, World *world)
{
2025-10-05 18:05:01 +01:00
PlayerInput(event, &world->player);
2025-10-06 01:10:44 +01:00
if(event->type == SDL_EVENT_KEY_DOWN && event->key.key == SDLK_F5){
SaveWorld(world->arena, world);
}
}
2025-10-05 16:49:18 +01:00
void RenderWorld(World *world, D_Context *draw) {
2025-10-06 00:38:10 +01:00
World_Tile tileTypes[] = {dirt, middlePath, middlePathEdgeTop, middlePathEdgeRight, middlePathEdgeBottom, middlePathEdgeLeft, middlePathCornerTopLeft, middlePathCornerTopRight, middlePathCornerBottomRight, middlePathCornerTurnBottomLeft};
for (int i = 0; i < 4800; i++)
{
D_Rect(draw, (F32) (i % 96), (F32) (i / 96), .texture = tileTypes[map[i]].tile, .angle = (F32) tileTypes[map[i]].rotation);
2025-10-06 00:38:10 +01:00
}
for(U32 i = 0; i < world->npcCount; i++) {
2025-10-05 21:05:29 +01:00
NPC npc = world->npcs[i];
if(npc.currentArea == world->player.currentArea) {
V2f drawPos = AABB_Centre(npc.collision);
D_Rect(draw, drawPos.x, drawPos.y, .texture = 1);
}
}
if(world->bandit.currentArea == world->player.currentArea) {
V2f drawPos = AABB_Centre(world->bandit.collision);
D_Rect(draw, drawPos.x, drawPos.y, .texture = 9);
2025-10-05 16:49:18 +01:00
}
D_Rect(draw, world->player.pos.x, world->player.pos.y, .texture = 1);
}
2025-10-06 01:10:44 +01:00
void SaveWorld(M_Arena *arena, World *world) {
printf("Saving world\n");
2025-10-06 01:49:10 +01:00
OS_Handle file = FS_FileOpen(S("world.sgdat"), FS_ACCESS_WRITE);
FS_FileWrite(file, world, sizeof(World)+sizeof(NavMesh), 0);
FS_FileWrite(file, world->navMesh, sizeof(World)+sizeof(NavMesh), sizeof(World));
FS_FileClose(file);
printf("Saved world :)\n");
2025-10-06 01:10:44 +01:00
}
2025-10-06 01:49:10 +01:00
World *LoadWorld(M_Arena *arena) {
printf("loading world\n");
2025-10-06 11:31:35 +01:00
OS_Handle file = FS_FileOpen(S("world.sgdat"), FS_ACCESS_READ);
2025-10-06 01:49:10 +01:00
World *world = M_ArenaPush(arena, World);
NavMesh *navMesh = M_ArenaPush(arena, NavMesh);
FS_FileRead(file, world, sizeof(World), 0);
FS_FileRead(file, navMesh, sizeof(NavMesh), sizeof(World));
FS_FileClose(file);
world->navMesh = navMesh;
world->arena = arena;
world->player.world = world;
printf("loaded world\n");
return world;
2025-10-06 01:10:44 +01:00
}