Skip to content

Commit bb5bcc6

Browse files
Add procedural map generation and block rendering system
Introduces a chunk-based map system with procedural terrain generation using noise, new block types, and instanced rendering. Adds new shaders and models for blocks, updates actor and player logic to interact with terrain height, and refactors map and player structures for extensibility. Also includes utility random function and various resource files for blocks and shaders.
1 parent 6c68ae0 commit bb5bcc6

File tree

19 files changed

+401
-46
lines changed

19 files changed

+401
-46
lines changed

include/borealis/gfx/model.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ namespace brl
4141
static GfxMesh* GetPrimitive(GfxPrimitiveType type);
4242

4343
GfxSubMesh* GetSubMesh(int index) { return subMeshes[index]; }
44+
int GetSubMeshCount() { return subMeshCount; }
4445

4546
GfxMesh() = default;
4647
GfxMesh(GfxAttribBuffer* buffer);

include/borealis/util/random.hpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#if !defined(RANDOM_HPP)
2+
#define RANDOM_HPP
3+
4+
#include "borealis/util/util.h"
5+
6+
namespace brl
7+
{
8+
int random(int min, int max) {
9+
return min + (std::rand() % (max - min + 1));
10+
}
11+
} // namespace brl
12+
13+
14+
15+
#endif // RANDOM_HPP
507 KB
Binary file not shown.
507 KB
Binary file not shown.
312 Bytes
Binary file not shown.
-33.7 KB
Binary file not shown.

test/include/map.h

Lines changed: 80 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,15 @@
44
#include <borealis/ecs/entity.hpp>
55
#include <borealis/gfx/gfx.hpp>
66

7+
#include "actor.h"
78
#include "borealis/gfx/sprite.hpp"
89
#include "borealis/util/input.hpp"
910

11+
struct PlayerController;
12+
struct MapChunk;
13+
class FastNoiseLite;
14+
struct MapController;
15+
1016
struct MapObject : brl::EcsEntity
1117
{
1218

@@ -31,19 +37,54 @@ struct MiniMapController : brl::EcsEntity
3137
};
3238

3339
#define MAP_CHUNK_SIZE 16
40+
#define MAP_MAX_HEIGHT 16
41+
#define MAP_BLOCK_SPACING 1.0f
42+
#define MAP_CHUNK_BLOCK_COUNT MAP_CHUNK_SIZE*MAP_CHUNK_SIZE * MAP_MAX_HEIGHT
43+
44+
struct MapBlock
45+
{
46+
int type = 0;
47+
48+
private:
49+
friend MapChunk;
50+
int relativeBlocks = 0;
51+
};
3452

3553
struct MapChunk : brl::EcsEntity
3654
{
37-
struct MapBlock
38-
{
39-
int type = 0;
40-
};
4155

42-
MapBlock* blocks = new MapBlock[pow(MAP_CHUNK_SIZE, 3)];
56+
57+
MapBlock* blocks = new MapBlock[MAP_CHUNK_BLOCK_COUNT];
4358

4459
MapChunk();
4560

4661
void lateUpdate() override;
62+
63+
private:
64+
friend MapController;
65+
66+
int mapIndexX, mapIndexY;
67+
MapController* _mapController;
68+
69+
int* heights = new int[MAP_CHUNK_SIZE * MAP_CHUNK_SIZE];
70+
71+
MapBlock* GetBlock(int x, int y, int z)
72+
{
73+
return &blocks[x + (y * MAP_CHUNK_SIZE) + (z * (MAP_CHUNK_SIZE * MAP_CHUNK_SIZE))];
74+
}
75+
76+
void initialize(FastNoiseLite noiseGen);
77+
void postInitializeCheck();
78+
79+
};
80+
81+
struct BlockData
82+
{
83+
std::vector<brl::GfxMaterial*> materials;
84+
int meshType;
85+
glm::mat4 offset = glm::mat4(1.0);
86+
87+
BlockData* clone();
4788
};
4889

4990
struct MapController : brl::EcsEntity
@@ -54,6 +95,40 @@ struct MapController : brl::EcsEntity
5495

5596
void update() override;
5697

98+
BlockData& GetBlockData(int index)
99+
{
100+
return dataBlocks[index];
101+
}
102+
103+
int GetHeight(int x, int z);
104+
105+
private:
106+
friend MapChunk;
107+
friend ActorBehaviour;
108+
MapChunk** chunks;
109+
int chunkCountX, chunkCountY;
110+
111+
std::vector<BlockData> dataBlocks;
112+
std::vector<brl::GfxModel*> blockModels;
113+
114+
static MapController* Instance;
115+
116+
117+
MapChunk* GetChunk(int x, int y);
118+
119+
MapBlock* GetBlock(int x, int y, int z)
120+
{
121+
int chunkX = x / 16;
122+
int chunkZ = z / 16;
123+
124+
int relativeChunkX = x % 16;
125+
int relativeChunkZ = z % 16;
126+
127+
return GetChunk(chunkX, chunkZ)->GetBlock(relativeChunkX, y, relativeChunkZ);
128+
129+
}
130+
131+
57132
};
58133

59134
#endif // MAP_H

test/include/player.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@
33
#include <borealis/ecs/entity.hpp>
44
#include <borealis/gfx/gfx.hpp>
55

6+
#include "actor.h"
67
#include "borealis/gfx/sprite.hpp"
78
#include "borealis/util/input.hpp"
89

9-
struct PlayerEntity : brl::EcsEntity
10+
struct PlayerEntity : ActorBehaviour
1011
{
1112
PlayerEntity();
1213

13-
virtual void handleAttack(glm::vec3 dir, float power);
14+
void handleAttack(glm::vec3 dir, float power) override;
1415

1516
bool isGuarding = false;
1617

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
BLK
29.4 KB
Binary file not shown.

0 commit comments

Comments
 (0)