Skip to content

Commit

Permalink
Add ship platforms
Browse files Browse the repository at this point in the history
  • Loading branch information
SirLynix committed Dec 23, 2023
1 parent 36f2778 commit 5825268
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 0 deletions.
1 change: 1 addition & 0 deletions include/CommonLib/Planet.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ namespace tsom
Nz::Vector3f ComputeUpDirection(const Nz::Vector3f& position) const;

void GenerateChunks(BlockLibrary& blockLibrary);
void GeneratePlatform(BlockLibrary& blockLibrary, Direction upDirection, const Nz::Vector3ui& platformCenter);

inline Nz::Vector3f GetCenter() const override;
inline Chunk* GetChunk(std::size_t chunkIndex) override;
Expand Down
59 changes: 59 additions & 0 deletions src/CommonLib/Planet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,65 @@ namespace tsom
}*/
}

void Planet::GeneratePlatform(BlockLibrary& blockLibrary, Direction upDirection, const Nz::Vector3ui& platformCenter)
{
constexpr int platformSize = 15;
constexpr unsigned int freeHeight = 10;

constexpr Nz::EnumArray<Direction, unsigned int> s_leftAxis = { 0, 0, 0, 1, 1, 0 };
constexpr Nz::EnumArray<Direction, unsigned int> s_forwardAxis = { 2, 1, 2, 0, 0, 1 };
constexpr Nz::EnumArray<Direction, unsigned int> s_upAxis = { 1, 2, 1, 2, 2, 2 };
constexpr Nz::EnumArray<Direction, int> s_leftDir = { -1, -1, -1, -1, 1, -1 };
constexpr Nz::EnumArray<Direction, int> s_forwardDir = { 1, 1, -1, -1, -1, -1 };
constexpr Nz::EnumArray<Direction, int> s_upDir = { 1, -1, -1, 1, -1, 1 };

Nz::Vector3ui coordinates = platformCenter;

unsigned int& xPos = coordinates[s_leftAxis[upDirection]];
xPos += s_leftDir[upDirection] * platformSize / 2;

unsigned int& yPos = coordinates[s_upAxis[upDirection]];

unsigned int& zPos = coordinates[s_forwardAxis[upDirection]];
zPos += -s_forwardDir[upDirection] * platformSize / 2;

BlockIndex borderBlockIndex = blockLibrary.GetBlockIndex("copper_block");
BlockIndex interiorBlockIndex = blockLibrary.GetBlockIndex("stone_bricks");

for (unsigned int y = 0; y < freeHeight; ++y)
{
unsigned int startingZ = zPos;
for (unsigned int z = 0; z < platformSize; ++z)
{
unsigned int startingX = xPos;
for (unsigned int x = 0; x < platformSize; ++x)
{
BlockIndex blockIndex;
if (y != 0)
blockIndex = EmptyBlockIndex;
else if (x == 0 || x == platformSize - 1)
blockIndex = borderBlockIndex;
else if (z == 0 || z == platformSize - 1)
blockIndex = borderBlockIndex;
else
blockIndex = interiorBlockIndex;

Nz::Vector3ui innerCoordinates;
Chunk& chunk = GetChunkByIndices(coordinates, &innerCoordinates);
chunk.UpdateBlock(innerCoordinates, blockIndex);

xPos += -s_leftDir[upDirection];
}

xPos = startingX;
zPos += s_forwardDir[upDirection];
}

yPos += s_upDir[upDirection];
zPos = startingZ;
}
}

void Planet::RemoveChunk(const Nz::Vector3ui& indices)
{
std::size_t index = GetChunkIndex(indices);
Expand Down
2 changes: 2 additions & 0 deletions src/ServerLib/ServerInstance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ namespace tsom

m_planet = std::make_unique<Planet>(Nz::Vector3ui(160), 2.f, 16.f, 9.81f);
m_planet->GenerateChunks(m_blockLibrary);
m_planet->GeneratePlatform(m_blockLibrary, tsom::Direction::Up, { 68, 109, 146 });
m_planet->GeneratePlatform(m_blockLibrary, tsom::Direction::Back, { 33, 148, 60 });

m_planetEntities = std::make_unique<ChunkEntities>(m_world, *m_planet, m_blockLibrary);
}
Expand Down

0 comments on commit 5825268

Please sign in to comment.