90 lines
2.5 KiB
C
90 lines
2.5 KiB
C
#include "../world.h"
|
|
#include "../npc.h"
|
|
#include "../player.h"
|
|
#include "../aabb.h"
|
|
#include <SDL3/SDL_events.h>
|
|
#include <SDL3/SDL_keycode.h>
|
|
#include <SDL3/SDL_oldnames.h>
|
|
|
|
void UpdateWorld(F32 delta, World *world)
|
|
{
|
|
UpdateBandit(delta, &world->bandit, world);
|
|
UpdateNPCs(delta, world);
|
|
PlayerUpdate(delta, &world->player);
|
|
}
|
|
|
|
void UpdateNPCs(F32 delta, World *world)
|
|
{
|
|
for (U32 i = 0; i < world->npcCount; i++)
|
|
{
|
|
NPC *npc = &world->npcs[i];
|
|
UpdateNPC(delta, npc, world);
|
|
if(
|
|
world->player.controls.shot
|
|
&& AABB_Point(npc->collision, world->player.shotPos)
|
|
&& npc->currentArea == world->player.currentArea
|
|
) {
|
|
printf("You shot %*.s\n", Sv(world->npcs[i].name));
|
|
}
|
|
}
|
|
}
|
|
|
|
void ProcessEvents(SDL_Event *event, World *world)
|
|
{
|
|
PlayerInput(event, &world->player);
|
|
if(event->type == SDL_EVENT_KEY_DOWN && event->key.key == SDLK_F5){
|
|
SaveWorld(world->arena, world);
|
|
}
|
|
}
|
|
|
|
void RenderWorld(World *world, D_Context *draw) {
|
|
for(U32 i = 0; i < world->npcCount; i++) {
|
|
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);
|
|
D_Rect(draw, drawPos.x, drawPos.y, .texture = 0, .dim = npc.collision.size, .flags = D_RECT_IGNORE_ASPECT);
|
|
}
|
|
}
|
|
if(world->bandit.currentArea == world->player.currentArea) {
|
|
V2f drawPos = AABB_Centre(world->bandit.collision);
|
|
D_Rect(draw, drawPos.x, drawPos.y, .texture = 9);
|
|
D_Rect(draw, drawPos.x, drawPos.y, .texture = 0, .dim = world->bandit.collision.size, .flags = D_RECT_IGNORE_ASPECT);
|
|
}
|
|
D_Rect(draw, world->player.pos.x, world->player.pos.y, .texture = 1);
|
|
}
|
|
|
|
void G_WorldDraw(G_State *game, World *world) {
|
|
D_Context *draw = &game->draw;
|
|
|
|
(void) world;
|
|
|
|
for (F32 y = -128; y < 128; y += 1.1f) {
|
|
for (F32 x = -128; x < 128; x += 1.1f) {
|
|
|
|
U32 ux = (U32) x;
|
|
U32 uy = (U32) y;
|
|
|
|
U32 tid = 15;
|
|
if ((ux % 11) == 0 || ((uy % 7) == 0)) {
|
|
tid = 16;
|
|
}
|
|
|
|
D_Rect(draw, x, y, .texture = tid);
|
|
}
|
|
}
|
|
}
|
|
|
|
void SaveWorld(M_Arena *arena, World *world) {
|
|
printf("Saving world\n");
|
|
World *saveWorld = M_ArenaPush(arena, World);
|
|
NavMesh *saveNavMesh = M_ArenaPush(arena, NavMesh);
|
|
M_CopySize(saveWorld, world, sizeof(World));
|
|
M_CopySize(saveNavMesh, world->navMesh, sizeof(NavMesh));
|
|
OS_Handle file =FS_FileOpen(S("world.sgdat"), FS_ACCESS_WRITE);
|
|
FS_FileWrite(file, saveWorld, sizeof(World)+sizeof(NavMesh), 0);
|
|
}
|
|
|
|
World *load(Str8 levelData) {
|
|
}
|