]> git.lizzy.rs Git - shadowclad.git/blob - src/game/level.c
Implement first stage of obstacle detection
[shadowclad.git] / src / game / level.c
1 #include "level.h"
2
3 #include <stdlib.h>
4 #include <GL/gl.h>
5
6 #include "engine/logger.h"
7 #include "engine/scene.h"
8 #include "engine/tga.h"
9
10 #include "player.h"
11
12 BlockGrid levelGrid;
13
14 static Block blockEmpty = { .type = BLOCKTYPE_SPACE,
15                             .solid = NULL };
16 static Block blockWall01 = { .type = BLOCKTYPE_OBSTACLE_X | BLOCKTYPE_OBSTACLE_Z,
17                              .solid = NULL };
18
19 static Transform playerSpawnTransform;
20
21 static void buildLevelFromImage(const TgaImage* image);
22
23
24
25 void initLevel() {
26         playerSpawnTransform = identity();
27         translate(&playerSpawnTransform, (Vector) { -BLOCKGRID_CELL_SIZE,
28                                                     0.0f,
29                                                     -BLOCKGRID_CELL_SIZE });
30
31         blockWall01.solid = importSolid("assets/wall01.3ds");
32
33         buildLevelFromImage(readTga("assets/level01.tga"));
34
35         Scene* levelScene = newScene();
36
37         for (size_t z = 0; z < levelGrid.depth; ++z) {
38                 for (size_t x = 0; x < levelGrid.width; ++x) {
39                         Scene* blockScene = newScene();
40                         translate(&blockScene->transform, (Vector) {
41                                 (x * BLOCKGRID_CELL_SIZE) + (BLOCKGRID_CELL_SIZE * 0.5f),
42                                 0.0f,
43                                 (z * BLOCKGRID_CELL_SIZE) + (BLOCKGRID_CELL_SIZE * 0.5f) });
44                         blockScene->solid = getBlockFromGrid(levelGrid, x, z)->solid;
45                         insertChildScene(levelScene, blockScene);
46                 }
47         }
48
49         currentScene = levelScene;
50 }
51
52 void startLevel() {
53         spawnPlayer(playerSpawnTransform);
54 }
55
56 static void buildLevelFromImage(const TgaImage* image) {
57         if (image == NULL) {
58                 logError("Null image received, cannot build level");
59                 return;
60         }
61
62         if (image->header.imageBpp != 32) {
63                 logError("Invalid level image format (%d bpp)", image->header.imageBpp);
64                 return;
65         }
66
67         BlockGrid newGrid = { .width = image->header.imageWidth,
68                               .depth = image->header.imageHeight,
69                               .blocks = malloc(image->header.imageWidth
70                                                * image->header.imageHeight
71                                                * sizeof(Block*)) };
72
73         for (size_t row = 0; row < newGrid.depth; ++row) {
74                 for (size_t x = 0; x < newGrid.width; ++x) {
75                         // Flip the image vertically due to (0, 0) being bottom left
76                         size_t z = newGrid.depth - row - 1;
77
78                         uint32_t pixelColorARGB = ((uint32_t*) image->bytes)[(row * newGrid.width) + x];
79                         Block* block;
80                         switch (pixelColorARGB) {
81                                 case 0xFFFF0000:
82                                         block = &blockWall01;
83                                         break;
84                                 case 0xFF00FFFF:
85                                         block = &blockEmpty;
86                                         playerSpawnTransform = identity();
87                                         translate(&playerSpawnTransform, (Vector) {
88                                                 (x * BLOCKGRID_CELL_SIZE) + (BLOCKGRID_CELL_SIZE * 0.5f),
89                                                 0.0f,
90                                                 (z * BLOCKGRID_CELL_SIZE) + (BLOCKGRID_CELL_SIZE * 0.5f) });
91                                         break;
92                                 default:
93                                         block = &blockEmpty;
94                                         break;
95                         }
96                         setBlockInGrid(newGrid, x, z, block);
97                 }
98         }
99
100         levelGrid = newGrid;
101 }
102
103 static inline size_t nonNegative(long n) {
104         return n < 0 ? 0u : n;
105 }
106
107 GridLocation gridLocationFromPosition(Vector pos) {
108         Vector scaledPos = scaleVector(pos, 1.0f / BLOCKGRID_CELL_SIZE);
109         return (GridLocation) { .x = nonNegative(scaledPos.x),
110                                 .z = nonNegative(scaledPos.z) };
111 }
112
113 Obstacle getObstacles(GridLocation loc) {
114         Obstacle result = OBSTACLE_NONE;
115         if (loc.x == 0) {
116                 result |= OBSTACLE_XN | OBSTACLE_XN_ZP | OBSTACLE_XN_ZN;
117         }
118         if (loc.x >= levelGrid.width - 1) {
119                 result |= OBSTACLE_XP | OBSTACLE_XP_ZP | OBSTACLE_XP_ZN;
120         }
121         if (loc.z == 0) {
122                 result |= OBSTACLE_ZN | OBSTACLE_XP_ZN | OBSTACLE_XN_ZN;
123         }
124         if (loc.z >= levelGrid.depth - 1) {
125                 result |= OBSTACLE_ZP | OBSTACLE_XP_ZP | OBSTACLE_XN_ZP;
126         }
127         if (!(result & OBSTACLE_XP)
128             && getBlockFromGrid(levelGrid, loc.x + 1, loc.z)->type & BLOCKTYPE_OBSTACLE_X) {
129                 result |= OBSTACLE_XP;
130         }
131         if (!(result & OBSTACLE_XN)
132             && getBlockFromGrid(levelGrid, loc.x - 1, loc.z)->type & BLOCKTYPE_OBSTACLE_X) {
133                 result |= OBSTACLE_XN;
134         }
135         if (!(result & OBSTACLE_ZP)
136             && getBlockFromGrid(levelGrid, loc.x, loc.z + 1)->type & BLOCKTYPE_OBSTACLE_Z) {
137                 result |= OBSTACLE_ZP;
138         }
139         if (!(result & OBSTACLE_ZN)
140             && getBlockFromGrid(levelGrid, loc.x, loc.z - 1)->type & BLOCKTYPE_OBSTACLE_Z) {
141                 result |= OBSTACLE_ZN;
142         }
143         if (!(result & OBSTACLE_XP_ZP)
144             && getBlockFromGrid(levelGrid, loc.x + 1, loc.z + 1)->type
145                & (BLOCKTYPE_OBSTACLE_X | BLOCKTYPE_OBSTACLE_Z)) {
146                 result |= OBSTACLE_XP_ZP;
147         }
148         if (!(result & OBSTACLE_XP_ZN)
149             && getBlockFromGrid(levelGrid, loc.x + 1, loc.z - 1)->type
150                & (BLOCKTYPE_OBSTACLE_X | BLOCKTYPE_OBSTACLE_Z)) {
151                 result |= OBSTACLE_XP_ZN;
152         }
153         if (!(result & OBSTACLE_XN_ZP)
154             && getBlockFromGrid(levelGrid, loc.x - 1, loc.z + 1)->type
155                & (BLOCKTYPE_OBSTACLE_X | BLOCKTYPE_OBSTACLE_Z)) {
156                 result |= OBSTACLE_XN_ZP;
157         }
158         if (!(result & OBSTACLE_XN_ZN)
159             && getBlockFromGrid(levelGrid, loc.x - 1, loc.z - 1)->type
160                & (BLOCKTYPE_OBSTACLE_X | BLOCKTYPE_OBSTACLE_Z)) {
161                 result |= OBSTACLE_XN_ZN;
162         }
163         return result;
164 }