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