diff --git a/SQCSim2021/SQCSim2021.vcxproj b/SQCSim2021/SQCSim2021.vcxproj index fe88180..35d96b4 100644 --- a/SQCSim2021/SQCSim2021.vcxproj +++ b/SQCSim2021/SQCSim2021.vcxproj @@ -151,6 +151,9 @@ + + + diff --git a/SQCSim2021/SQCSim2021.vcxproj.filters b/SQCSim2021/SQCSim2021.vcxproj.filters index 2a24100..f06cd67 100644 --- a/SQCSim2021/SQCSim2021.vcxproj.filters +++ b/SQCSim2021/SQCSim2021.vcxproj.filters @@ -45,4 +45,7 @@ Source Files + + + \ No newline at end of file diff --git a/SQCSim2021/blockarray3d.cpp b/SQCSim2021/blockarray3d.cpp index 50878f9..78b866a 100644 --- a/SQCSim2021/blockarray3d.cpp +++ b/SQCSim2021/blockarray3d.cpp @@ -1 +1,26 @@ #include "blockarray3d.h" + +BlockArray3d::BlockArray3d(int x, int y, int z) : m_x(x), m_y(y), m_z(z) { + m_blocks = new BlockType[x * y * z]; + Reset(BTYPE_AIR); +} + +BlockArray3d::BlockArray3d(BlockArray3d& ba) : m_x(ba.m_x), m_y(ba.m_y), m_z(ba.m_z) { + m_blocks = new BlockType[ba.m_x*ba.m_y*ba.m_z]; + + for (int index = 0; index < (m_x * m_y * m_z); ++index) + m_blocks[index] = ba.m_blocks[index]; +} + +BlockArray3d::~BlockArray3d() { delete[] m_blocks; } + +void BlockArray3d::Set(int x, int y, int z, BlockType type) { m_blocks[GetPosition(x, y, z)] = type; } + +BlockType BlockArray3d::Get(int x, int y, int z) const { return m_blocks[GetPosition(x, y, z)]; } + +void BlockArray3d::Reset(BlockType type) { + for (int index = 0; index < (m_x * m_y * m_z); ++index) + m_blocks[index] = type; +} + +int BlockArray3d::GetPosition(int x, int y, int z) const { return x + (z * m_x) + (y * m_z * m_x); } diff --git a/SQCSim2021/blockarray3d.h b/SQCSim2021/blockarray3d.h index 9d84ff7..f169cf2 100644 --- a/SQCSim2021/blockarray3d.h +++ b/SQCSim2021/blockarray3d.h @@ -5,16 +5,20 @@ class BlockArray3d { public: - BlockArray3d(); + BlockArray3d(int x, int y, int z); BlockArray3d(BlockArray3d& ba); - ~BlockArray3d(); + virtual ~BlockArray3d(); -protected: void Set(int x, int y, int z, BlockType type); BlockType Get(int x, int y, int z) const; void Reset(BlockType type); private: - int* arr; + BlockType* m_blocks; + int m_x; + int m_y; + int m_z; + + int GetPosition(int x, int y, int z) const; }; #endif \ No newline at end of file diff --git a/SQCSim2021/blockinfo.cpp b/SQCSim2021/blockinfo.cpp index 0bd7108..9205100 100644 --- a/SQCSim2021/blockinfo.cpp +++ b/SQCSim2021/blockinfo.cpp @@ -1 +1,13 @@ #include "blockinfo.h" + +BlockInfo::BlockInfo(BlockType type, const std::string& name): m_name(name), m_type(type) { m_durability = 0; } + +BlockInfo::~BlockInfo() { } + +BlockType BlockInfo::GetType() const { return m_type; } + +void BlockInfo::SetDurability(int durability) { m_durability = durability; } + +int BlockInfo::GetDurability() const { return m_durability; } + +void BlockInfo::Show() const { std::cout << "Name: " << m_name << "; Type " << m_type << "; Durability: " << m_durability << std::endl; } diff --git a/SQCSim2021/blockinfo.h b/SQCSim2021/blockinfo.h index 18f8f15..44542e8 100644 --- a/SQCSim2021/blockinfo.h +++ b/SQCSim2021/blockinfo.h @@ -2,6 +2,7 @@ #define BLOCKINFO_H__ #include "define.h" +#include class BlockInfo { public: @@ -13,6 +14,11 @@ public: void SetDurability(int durability); int GetDurability() const; void Show() const; + +private: + std::string m_name; + int m_durability; + BlockType m_type; }; #endif diff --git a/SQCSim2021/chunk.cpp b/SQCSim2021/chunk.cpp index 3a07abc..579e355 100644 --- a/SQCSim2021/chunk.cpp +++ b/SQCSim2021/chunk.cpp @@ -1 +1,13 @@ #include "chunk.h" + +Chunk::Chunk(): BlockArray3d(CHUNK_SIZE_X, CHUNK_SIZE_Y, CHUNK_SIZE_Z) {} + +Chunk::Chunk(Chunk& chk) : BlockArray3d(chk) {} + +Chunk::~Chunk() {} + +void Chunk::RemoveBlock(int x, int y, int z) { Set(x, y, z, BTYPE_AIR); } + +void Chunk::SetBlock(int x, int y, int z, BlockType type) { Set(x, y, z, type); } + +BlockType Chunk::GetBlock(int x, int y, int z) const { return Get(x, y, z); } diff --git a/SQCSim2021/chunk.h b/SQCSim2021/chunk.h index a93a601..4045b1a 100644 --- a/SQCSim2021/chunk.h +++ b/SQCSim2021/chunk.h @@ -7,7 +7,8 @@ class Chunk : public BlockArray3d { public: Chunk(); - ~Chunk(); + Chunk(Chunk& chk); + virtual ~Chunk(); void RemoveBlock(int x, int y, int z); void SetBlock(int x, int y, int z, BlockType type); diff --git a/SQCSim2021/main.cpp b/SQCSim2021/main.cpp index 0bf4d5b..ac7982a 100644 --- a/SQCSim2021/main.cpp +++ b/SQCSim2021/main.cpp @@ -1,5 +1,59 @@ #include "main.h" int main() { + + Chunk* testchunk = new Chunk(); + std::vector testblock; + + std::cout << "-Test 1-------BlockInfo----------" << std::endl; + + testblock.push_back(new BlockInfo(BTYPE_AIR, "The air in the sky")); + testblock.push_back(new BlockInfo(BTYPE_DIRT, "The dirt on the ground")); + testblock.push_back(new BlockInfo(BTYPE_GRASS, "The grass on the dirt")); + + testblock[1]->SetDurability(5); + testblock[2]->SetDurability(6); + + for (int index = 0; index < testblock.size(); ++index) + testblock[index]->Show(); + + std::cout << "GetDurability: " << testblock[0]->GetDurability() << " and " << testblock[1]->GetDurability() << " and " << testblock[2]->GetDurability() << std::endl; + std::cout << "GetType: " << testblock[0]->GetType() << " and " << testblock[1]->GetType() << " and " << testblock[2]->GetType() << std::endl; + + std::cout << std::endl; + std::cout << "-Test 2--------Chunk/BlockArray3D---------" << std::endl; + testchunk->SetBlock(1, 0, 0, BTYPE_DIRT); + testchunk->SetBlock(2, 0, 0, BTYPE_GRASS); + testblock.push_back(new BlockInfo(testchunk->GetBlock(0, 0, 0), "A new block in the chunk")); + testblock.push_back(new BlockInfo(testchunk->GetBlock(1, 0, 0), "A dirty block in the chunk")); + testblock.push_back(new BlockInfo(testchunk->GetBlock(2, 0, 0), "A grassy block in the chunk")); + + for (int index = 3; index < testblock.size(); ++index) + testblock[index]->Show(); + + std::cout << std::endl; + std::cout << "-Test 3--------Chunk Copy---------------" << std::endl; + Chunk testchunkcopy(*testchunk); + delete testchunk; + testblock.push_back(new BlockInfo(testchunkcopy.GetBlock(0, 0, 0), "A block in a copied chunk")); + testblock.push_back(new BlockInfo(testchunkcopy.GetBlock(1, 0, 0), "A dirty block in a copied chunk")); + testblock.push_back(new BlockInfo(testchunkcopy.GetBlock(1, 0, 0), "A grassy block in a copied chunk")); + + for (int index = 6; index < testblock.size(); ++index) + testblock[index]->Show(); + + std::cout << std::endl; + std::cout << "-Test 3--------Chunk Reset/Remove Block-----------" << std::endl; + testchunkcopy.Reset(BTYPE_GRASS); + testchunkcopy.RemoveBlock(3, 0, 0); + + testblock.empty(); + + for (int index = 0; index < 10; ++index) + testblock.push_back(new BlockInfo(testchunkcopy.GetBlock(index, 0, 0), "A block in a reset chunk containing lots of grass")); + + for (int index = 0; index < testblock.size(); ++index) + testblock[index]->Show(); + return 0; } diff --git a/SQCSim2021/main.h b/SQCSim2021/main.h index 6a0944c..a326241 100644 --- a/SQCSim2021/main.h +++ b/SQCSim2021/main.h @@ -1,7 +1,11 @@ #ifndef MAIN_H__ #define MAIN_H__ +#include +#include #include "define.h" +#include "chunk.h" +#include "blockinfo.h" #endif