Skip to content

Commit

Permalink
Begin to work on ship editor
Browse files Browse the repository at this point in the history
  • Loading branch information
SirLynix committed Dec 9, 2023
1 parent 199d1c8 commit 4644417
Show file tree
Hide file tree
Showing 6 changed files with 807 additions and 0 deletions.
62 changes: 62 additions & 0 deletions include/CommonLib/Ship.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Copyright (C) 2023 Jérôme "Lynix" Leclercq ([email protected])
// This file is part of the "This Space Of Mine" project
// For conditions of distribution and use, see copyright notice in Config.hpp

#pragma once

#ifndef TSOM_COMMONLIB_SHIP_HPP
#define TSOM_COMMONLIB_SHIP_HPP

#include <CommonLib/ChunkContainer.hpp>
#include <CommonLib/Direction.hpp>
#include <CommonLib/Export.hpp>
#include <NazaraUtils/FunctionRef.hpp>
#include <memory>
#include <vector>

namespace tsom
{
class BlockLibrary;

class TSOM_COMMONLIB_API Ship : public ChunkContainer
{
public:
Ship(BlockLibrary& blockManager, const Nz::Vector3ui& gridSize, float tileSize);
Ship(const Ship&) = delete;
Ship(Ship&&) = delete;
~Ship() = default;

Chunk& AddChunk(const Nz::Vector3ui& indices, const Nz::FunctionRef<void(BlockIndex* blocks)>& initCallback = nullptr);

inline Nz::Vector3f GetCenter() const override;
inline Chunk* GetChunk(std::size_t chunkIndex) override;
inline const Chunk* GetChunk(std::size_t chunkIndex) const override;
inline Chunk& GetChunk(const Nz::Vector3ui& indices) override;
inline const Chunk& GetChunk(const Nz::Vector3ui& indices) const override;
inline Nz::Vector3ui GetChunkIndices(std::size_t chunkIndex) const;
inline std::size_t GetChunkCount() const override;

void RemoveChunk(const Nz::Vector3ui& indices);

Ship& operator=(const Ship&) = delete;
Ship& operator=(Ship&&) = delete;

static constexpr unsigned int ChunkSize = 32;

private:
void SetupChunks(BlockLibrary& blockManager);

struct ChunkData
{
std::unique_ptr<Chunk> chunk;

NazaraSlot(Chunk, OnBlockUpdated, onUpdated);
};

std::vector<ChunkData> m_chunks;
};
}

#include <CommonLib/Ship.inl>

#endif // TSOM_COMMONLIB_SHIP_HPP
47 changes: 47 additions & 0 deletions include/CommonLib/Ship.inl
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright (C) 2023 Jérôme "Lynix" Leclercq ([email protected])
// This file is part of the "This Space Of Mine" project
// For conditions of distribution and use, see copyright notice in Config.hpp

namespace tsom
{
inline Nz::Vector3f Ship::GetCenter() const
{
return Nz::Vector3f::Zero();
}

inline Chunk* Ship::GetChunk(std::size_t chunkIndex)
{
return m_chunks[chunkIndex].chunk.get();
}

inline const Chunk* Ship::GetChunk(std::size_t chunkIndex) const
{
return m_chunks[chunkIndex].chunk.get();
}

inline Chunk& Ship::GetChunk(const Nz::Vector3ui& indices)
{
return *m_chunks[GetChunkIndex(indices)].chunk;
}

inline const Chunk& Ship::GetChunk(const Nz::Vector3ui& indices) const
{
return *m_chunks[GetChunkIndex(indices)].chunk;
}

inline Nz::Vector3ui Ship::GetChunkIndices(std::size_t chunkIndex) const
{
Nz::Vector3ui indices;
indices.x = chunkIndex % ChunkSize;
indices.y = (chunkIndex / ChunkSize) % ChunkSize;
indices.z = chunkIndex / (ChunkSize * ChunkSize);

return indices;
}

inline std::size_t Ship::GetChunkCount() const
{
return m_chunks.size();
}
}

83 changes: 83 additions & 0 deletions src/CommonLib/Ship.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// Copyright (C) 2023 Jérôme "Lynix" Leclercq ([email protected])
// This file is part of the "This Space Of Mine" project
// For conditions of distribution and use, see copyright notice in Config.hpp

#include <CommonLib/Ship.hpp>
#include <CommonLib/BlockLibrary.hpp>
#include <CommonLib/FlatChunk.hpp>
#include <fmt/format.h>
#include <PerlinNoise.hpp>
#include <random>

namespace tsom
{
Ship::Ship(BlockLibrary& blockManager, const Nz::Vector3ui& gridSize, float tileSize) :
ChunkContainer(gridSize, tileSize)
{
m_chunks.resize(m_chunkCount.x * m_chunkCount.y * m_chunkCount.z);

SetupChunks(blockManager);
}

Chunk& Ship::AddChunk(const Nz::Vector3ui& indices, const Nz::FunctionRef<void(BlockIndex* blocks)>& initCallback)
{
std::size_t index = GetChunkIndex(indices);
assert(!m_chunks[index].chunk);
m_chunks[index].chunk = std::make_unique<FlatChunk>(*this, indices, Nz::Vector3ui{ ChunkSize }, m_tileSize);

if (initCallback)
m_chunks[index].chunk->InitBlocks(initCallback);

m_chunks[index].onUpdated.Connect(m_chunks[index].chunk->OnBlockUpdated, [this](Chunk* chunk, const Nz::Vector3ui& /*indices*/, BlockIndex /*newBlock*/)
{
OnChunkUpdated(this, chunk);
});

OnChunkAdded(this, m_chunks[index].chunk.get());

return *m_chunks[index].chunk;
}

void Ship::RemoveChunk(const Nz::Vector3ui& indices)
{
std::size_t index = GetChunkIndex(indices);
assert(m_chunks[index].chunk);

OnChunkRemove(this, m_chunks[index].chunk.get());

m_chunks[index].chunk = nullptr;
m_chunks[index].onUpdated.Disconnect();
}

void Ship::SetupChunks(BlockLibrary& blockManager)
{
constexpr unsigned int freespace = 5;

BlockIndex hullIndex = blockManager.GetBlockIndex("hull");
if (hullIndex == InvalidBlockIndex)
return;

Chunk& chunk = AddChunk({ 0, 0, 0 });

constexpr unsigned int boxSize = 5;
Nz::Vector3ui startPos = chunk.GetSize() / 2 - Nz::Vector3ui(boxSize / 2);

for (unsigned int z = 0; z < boxSize; ++z)
{
for (unsigned int y = 0; y < boxSize; ++y)
{
for (unsigned int x = 0; x < boxSize; ++x)
{
if (x != 0 && x != boxSize - 1 &&
y != 0 && y != boxSize - 1 &&
z != 0 && z != boxSize - 1)
{
continue;
}

chunk.UpdateBlock(startPos + Nz::Vector3ui{ x, y, z }, hullIndex);
}
}
}
}
}
Loading

0 comments on commit 4644417

Please sign in to comment.