summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/AssetManager.cpp294
-rw-r--r--src/AssetManager.hpp17
-rw-r--r--src/Block.cpp4
-rw-r--r--src/Block.hpp16
-rw-r--r--src/Entity.cpp12
-rw-r--r--src/Event.cpp1
-rw-r--r--src/Event.hpp3
-rw-r--r--src/Frustum.cpp2
-rw-r--r--src/GameState.cpp106
-rw-r--r--src/GlobalState.cpp77
10 files changed, 293 insertions, 239 deletions
diff --git a/src/AssetManager.cpp b/src/AssetManager.cpp
index 0507bc1..0561992 100644
--- a/src/AssetManager.cpp
+++ b/src/AssetManager.cpp
@@ -19,184 +19,183 @@ const std::string pathToTextureIndex = "./textures.json";
const fs::path pathToModels = "./assets/minecraft/models/";
AssetManager::AssetManager() {
- LoadIds();
- LoadTextureResources();
+ LoadIds();
+ LoadTextureResources();
LoadBlockModels();
}
void AssetManager::LoadIds() {
- std::ifstream in(pathToAssetsList);
- nlohmann::json index;
- in >> index;
- for (auto &it:index) {
- unsigned short id = it["type"].get<int>();
- unsigned char state = it["meta"].get<int>();
- std::string blockName = it["text_type"].get<std::string>();
+ std::ifstream in(pathToAssetsList);
+ nlohmann::json index;
+ in >> index;
+ for (auto &it:index) {
+ unsigned short id = it["type"].get<int>();
+ unsigned char state = it["meta"].get<int>();
+ std::string blockName = it["text_type"].get<std::string>();
assetIds[blockName] = BlockId{ id, state };
- }
- LOG(INFO) << "Loaded " << assetIds.size() << " ids";
+ }
+ LOG(INFO) << "Loaded " << assetIds.size() << " ids";
}
AssetManager::~AssetManager() {
- delete textureAtlas;
+ delete textureAtlas;
}
//TODO: This function must be replaced with runtime texture atlas generating
void AssetManager::LoadTextureResources() {
- std::ifstream in(pathToTextureIndex);
- nlohmann::json index;
- in >> index;
- std::string filename = index["meta"]["image"].get<std::string>();
- float textureWidth = index["meta"]["size"]["w"].get<int>();
- float textureHeight = index["meta"]["size"]["h"].get<int>();
+ std::ifstream in(pathToTextureIndex);
+ nlohmann::json index;
+ in >> index;
+ std::string filename = index["meta"]["image"].get<std::string>();
+ float textureWidth = index["meta"]["size"]["w"].get<int>();
+ float textureHeight = index["meta"]["size"]["h"].get<int>();
size_t sizeName = 0,sizeTexture = 0;
- for (auto &it:index["frames"]) {
- auto frame = it["frame"];
- TextureCoordinates coord;
- coord.x = frame["x"].get<int>() / textureWidth;
- coord.y = frame["y"].get<int>() / textureHeight;
- coord.w = frame["w"].get<int>() / textureWidth;
- coord.h = frame["h"].get<int>() / textureHeight;
- std::string assetName = it["filename"].get<std::string>();
- assetName.insert(0, "minecraft/textures/");
- assetName.erase(assetName.length() - 4);
+ for (auto &it:index["frames"]) {
+ auto frame = it["frame"];
+ TextureCoordinates coord;
+ coord.x = frame["x"].get<int>() / textureWidth;
+ coord.y = frame["y"].get<int>() / textureHeight;
+ coord.w = frame["w"].get<int>() / textureWidth;
+ coord.h = frame["h"].get<int>() / textureHeight;
+ std::string assetName = it["filename"].get<std::string>();
+ assetName.insert(0, "minecraft/textures/");
+ assetName.erase(assetName.length() - 4);
assetName.shrink_to_fit();
sizeName += sizeof(assetName) + assetName.capacity();
sizeTexture += sizeof(coord);
- assetTextures[assetName] = coord;
- }
- textureAtlas = new Texture(filename);
- LOG(INFO) << "Texture atlas id is " << textureAtlas->texture;
+ assetTextures[assetName] = coord;
+ }
+ textureAtlas = new Texture(filename);
+ LOG(INFO) << "Texture atlas id is " << textureAtlas->texture;
}
TextureCoordinates AssetManager::GetTextureByAssetName(std::string AssetName) {
- if (assetTextures.find(AssetName) != assetTextures.end())
- return assetTextures[AssetName];
- else
- return TextureCoordinates{-1, -1, -1, -1};
+ if (assetTextures.find(AssetName) != assetTextures.end())
+ return assetTextures[AssetName];
+ else
+ return TextureCoordinates{-1, -1, -1, -1};
}
std::string AssetManager::GetTextureAssetNameByBlockId(BlockTextureId block) {
- //Block sides: 0 - bottom, 1 - top, 2 - north, 3 - south, 4 - west, 5 - east 6 - every side
- const std::map<BlockTextureId, std::string> lookupTable = {
- {BlockTextureId(0, 0), "minecraft/textures/blocks/air"},
- {BlockTextureId(1, 0), "minecraft/textures/blocks/stone"},
- {BlockTextureId(1, 1), "minecraft/textures/blocks/stone_granite"},
-
- {BlockTextureId(2, 0, 0), "minecraft/textures/blocks/dirt"},
- {BlockTextureId(2, 0, 1), "minecraft/textures/blocks/grass_top"},
- {BlockTextureId(2, 0, 2), "minecraft/textures/blocks/grass_side"},
- {BlockTextureId(2, 0, 3), "minecraft/textures/blocks/grass_side"},
- {BlockTextureId(2, 0, 4), "minecraft/textures/blocks/grass_side"},
- {BlockTextureId(2, 0, 5), "minecraft/textures/blocks/grass_side"},
-
- {BlockTextureId(3, 0), "minecraft/textures/blocks/dirt"},
- {BlockTextureId(4, 0), "minecraft/textures/blocks/cobblestone"},
- {BlockTextureId(5, 0), "minecraft/textures/blocks/planks"},
-
- {BlockTextureId(7, 0), "minecraft/textures/blocks/bedrock"},
-
- {BlockTextureId(17, 0, 0), "minecraft/textures/blocks/log_oak_top"},
- {BlockTextureId(17, 0, 1), "minecraft/textures/blocks/log_oak_top"},
- {BlockTextureId(17, 0, 2), "minecraft/textures/blocks/log_oak"},
- {BlockTextureId(17, 0, 3), "minecraft/textures/blocks/log_oak"},
- {BlockTextureId(17, 0, 4), "minecraft/textures/blocks/log_oak"},
- {BlockTextureId(17, 0, 5), "minecraft/textures/blocks/log_oak"},
-
- {BlockTextureId(17, 1, 0), "minecraft/textures/blocks/log_spruce_top"},
- {BlockTextureId(17, 1, 1), "minecraft/textures/blocks/log_spruce_top"},
- {BlockTextureId(17, 1, 2), "minecraft/textures/blocks/log_spruce"},
- {BlockTextureId(17, 1, 3), "minecraft/textures/blocks/log_spruce"},
- {BlockTextureId(17, 1, 4), "minecraft/textures/blocks/log_spruce"},
- {BlockTextureId(17, 1, 5), "minecraft/textures/blocks/log_spruce"},
-
- {BlockTextureId(17, 2, 0), "minecraft/textures/blocks/log_birch_top"},
- {BlockTextureId(17, 2, 1), "minecraft/textures/blocks/log_birch_top"},
- {BlockTextureId(17, 2, 2), "minecraft/textures/blocks/log_birch"},
- {BlockTextureId(17, 2, 3), "minecraft/textures/blocks/log_birch"},
- {BlockTextureId(17, 2, 4), "minecraft/textures/blocks/log_birch"},
- {BlockTextureId(17, 2, 5), "minecraft/textures/blocks/log_birch"},
-
- {BlockTextureId(17, 3, 0), "minecraft/textures/blocks/log_jungle_top"},
- {BlockTextureId(17, 3, 1), "minecraft/textures/blocks/log_jungle_top"},
- {BlockTextureId(17, 3, 2), "minecraft/textures/blocks/log_jungle"},
- {BlockTextureId(17, 3, 3), "minecraft/textures/blocks/log_jungle"},
- {BlockTextureId(17, 3, 4), "minecraft/textures/blocks/log_jungle"},
- {BlockTextureId(17, 3, 5), "minecraft/textures/blocks/log_jungle"},
-
- {BlockTextureId(18, 0), "minecraft/textures/blocks/leaves_oak"},
- {BlockTextureId(18, 1), "minecraft/textures/blocks/leaves_spruce"},
- {BlockTextureId(18, 2), "minecraft/textures/blocks/leaves_birch"},
- {BlockTextureId(18, 3), "minecraft/textures/blocks/leaves_jungle"},
-
- {BlockTextureId(61, 0, 0), "minecraft/textures/blocks/furnace_side"},
- {BlockTextureId(61, 0, 1), "minecraft/textures/blocks/furnace_top"},
- {BlockTextureId(61, 0, 2), "minecraft/textures/blocks/furnace_front_off"},
- {BlockTextureId(61, 0, 3), "minecraft/textures/blocks/furnace_side"},
- {BlockTextureId(61, 0, 4), "minecraft/textures/blocks/furnace_side"},
- {BlockTextureId(61, 0, 5), "minecraft/textures/blocks/furnace_side"},
-
- {BlockTextureId(62, 0, 0), "minecraft/textures/blocks/furnace_side"},
- {BlockTextureId(62, 0, 1), "minecraft/textures/blocks/furnace_top"},
- {BlockTextureId(62, 0, 2), "minecraft/textures/blocks/furnace_front_on"},
- {BlockTextureId(62, 0, 3), "minecraft/textures/blocks/furnace_side"},
- {BlockTextureId(62, 0, 4), "minecraft/textures/blocks/furnace_side"},
- {BlockTextureId(62, 0, 5), "minecraft/textures/blocks/furnace_side"},
-
-
- {BlockTextureId(31, 0), "minecraft/textures/blocks/deadbush"},
- {BlockTextureId(31, 1), "minecraft/textures/blocks/tallgrass"},
- {BlockTextureId(31, 2), "minecraft/textures/blocks/fern"},
- };
- auto ret = lookupTable.find(block);
- if (ret == lookupTable.end())
- return "";
- else
- return ret->second;
+ //Block sides: 0 - bottom, 1 - top, 2 - north, 3 - south, 4 - west, 5 - east 6 - every side
+ const std::map<BlockTextureId, std::string> lookupTable = {
+ {BlockTextureId(0, 0), "minecraft/textures/blocks/air"},
+ {BlockTextureId(1, 0), "minecraft/textures/blocks/stone"},
+ {BlockTextureId(1, 1), "minecraft/textures/blocks/stone_granite"},
+
+ {BlockTextureId(2, 0, 0), "minecraft/textures/blocks/dirt"},
+ {BlockTextureId(2, 0, 1), "minecraft/textures/blocks/grass_top"},
+ {BlockTextureId(2, 0, 2), "minecraft/textures/blocks/grass_side"},
+ {BlockTextureId(2, 0, 3), "minecraft/textures/blocks/grass_side"},
+ {BlockTextureId(2, 0, 4), "minecraft/textures/blocks/grass_side"},
+ {BlockTextureId(2, 0, 5), "minecraft/textures/blocks/grass_side"},
+
+ {BlockTextureId(3, 0), "minecraft/textures/blocks/dirt"},
+ {BlockTextureId(4, 0), "minecraft/textures/blocks/cobblestone"},
+ {BlockTextureId(5, 0), "minecraft/textures/blocks/planks"},
+
+ {BlockTextureId(7, 0), "minecraft/textures/blocks/bedrock"},
+
+ {BlockTextureId(17, 0, 0), "minecraft/textures/blocks/log_oak_top"},
+ {BlockTextureId(17, 0, 1), "minecraft/textures/blocks/log_oak_top"},
+ {BlockTextureId(17, 0, 2), "minecraft/textures/blocks/log_oak"},
+ {BlockTextureId(17, 0, 3), "minecraft/textures/blocks/log_oak"},
+ {BlockTextureId(17, 0, 4), "minecraft/textures/blocks/log_oak"},
+ {BlockTextureId(17, 0, 5), "minecraft/textures/blocks/log_oak"},
+
+ {BlockTextureId(17, 1, 0), "minecraft/textures/blocks/log_spruce_top"},
+ {BlockTextureId(17, 1, 1), "minecraft/textures/blocks/log_spruce_top"},
+ {BlockTextureId(17, 1, 2), "minecraft/textures/blocks/log_spruce"},
+ {BlockTextureId(17, 1, 3), "minecraft/textures/blocks/log_spruce"},
+ {BlockTextureId(17, 1, 4), "minecraft/textures/blocks/log_spruce"},
+ {BlockTextureId(17, 1, 5), "minecraft/textures/blocks/log_spruce"},
+
+ {BlockTextureId(17, 2, 0), "minecraft/textures/blocks/log_birch_top"},
+ {BlockTextureId(17, 2, 1), "minecraft/textures/blocks/log_birch_top"},
+ {BlockTextureId(17, 2, 2), "minecraft/textures/blocks/log_birch"},
+ {BlockTextureId(17, 2, 3), "minecraft/textures/blocks/log_birch"},
+ {BlockTextureId(17, 2, 4), "minecraft/textures/blocks/log_birch"},
+ {BlockTextureId(17, 2, 5), "minecraft/textures/blocks/log_birch"},
+
+ {BlockTextureId(17, 3, 0), "minecraft/textures/blocks/log_jungle_top"},
+ {BlockTextureId(17, 3, 1), "minecraft/textures/blocks/log_jungle_top"},
+ {BlockTextureId(17, 3, 2), "minecraft/textures/blocks/log_jungle"},
+ {BlockTextureId(17, 3, 3), "minecraft/textures/blocks/log_jungle"},
+ {BlockTextureId(17, 3, 4), "minecraft/textures/blocks/log_jungle"},
+ {BlockTextureId(17, 3, 5), "minecraft/textures/blocks/log_jungle"},
+
+ {BlockTextureId(18, 0), "minecraft/textures/blocks/leaves_oak"},
+ {BlockTextureId(18, 1), "minecraft/textures/blocks/leaves_spruce"},
+ {BlockTextureId(18, 2), "minecraft/textures/blocks/leaves_birch"},
+ {BlockTextureId(18, 3), "minecraft/textures/blocks/leaves_jungle"},
+
+ {BlockTextureId(61, 0, 0), "minecraft/textures/blocks/furnace_side"},
+ {BlockTextureId(61, 0, 1), "minecraft/textures/blocks/furnace_top"},
+ {BlockTextureId(61, 0, 2), "minecraft/textures/blocks/furnace_front_off"},
+ {BlockTextureId(61, 0, 3), "minecraft/textures/blocks/furnace_side"},
+ {BlockTextureId(61, 0, 4), "minecraft/textures/blocks/furnace_side"},
+ {BlockTextureId(61, 0, 5), "minecraft/textures/blocks/furnace_side"},
+
+ {BlockTextureId(62, 0, 0), "minecraft/textures/blocks/furnace_side"},
+ {BlockTextureId(62, 0, 1), "minecraft/textures/blocks/furnace_top"},
+ {BlockTextureId(62, 0, 2), "minecraft/textures/blocks/furnace_front_on"},
+ {BlockTextureId(62, 0, 3), "minecraft/textures/blocks/furnace_side"},
+ {BlockTextureId(62, 0, 4), "minecraft/textures/blocks/furnace_side"},
+ {BlockTextureId(62, 0, 5), "minecraft/textures/blocks/furnace_side"},
+
+
+ {BlockTextureId(31, 0), "minecraft/textures/blocks/deadbush"},
+ {BlockTextureId(31, 1), "minecraft/textures/blocks/tallgrass"},
+ {BlockTextureId(31, 2), "minecraft/textures/blocks/fern"},
+ };
+
+ auto ret = lookupTable.find(block);
+ return (ret == lookupTable.end()) ? "" : ret->second;
}
GLuint AssetManager::GetTextureAtlas() {
- return textureAtlas->texture;
+ return textureAtlas->texture;
}
TextureCoordinates AssetManager::GetTextureByBlock(BlockTextureId block) {
- std::string assetName = this->GetTextureAssetNameByBlockId(block);
- return this->GetTextureByAssetName(assetName);
+ std::string assetName = this->GetTextureAssetNameByBlockId(block);
+ return this->GetTextureByAssetName(assetName);
}
const std::map<BlockTextureId, glm::vec4> &AssetManager::GetTextureAtlasIndexes() {
- if (!textureAtlasIndexes.empty())
- return textureAtlasIndexes;
-
- LOG(INFO) << "Initializing texture atlas...";
- for (int id = 1; id < 128; id++) {
- for (int state = 0; state < 16; state++) {
- BlockTextureId blockTextureId(id, state, 6);
- if (!this->GetTextureByBlock(blockTextureId) &&
- !this->GetTextureByBlock(BlockTextureId(id, state, 0))) {
- continue;
- }
- if (this->GetTextureByBlock(blockTextureId)) {
- for (int i = 0; i < 6; i++) {
- TextureCoordinates tc = this->GetTextureByBlock(BlockTextureId(id, state, 6));
- textureAtlasIndexes[BlockTextureId(id, state, i)] = glm::vec4(tc.x, tc.y, tc.w, tc.h);
- }
- } else {
- for (int i = 0; i < 6; i++) {
- TextureCoordinates tc = this->GetTextureByBlock(BlockTextureId(id, state, i));
- textureAtlasIndexes[BlockTextureId(id, state, i)] = glm::vec4(tc.x, tc.y, tc.w, tc.h);
- }
- }
- }
- }
- LOG(INFO) << "Created " << textureAtlasIndexes.size() << " texture indexes";
-
- return textureAtlasIndexes;
+ if (!textureAtlasIndexes.empty())
+ return textureAtlasIndexes;
+
+ LOG(INFO) << "Initializing texture atlas...";
+ for (int id = 1; id < 128; id++) {
+ for (int state = 0; state < 16; state++) {
+ BlockTextureId blockTextureId(id, state, 6);
+ if (!this->GetTextureByBlock(blockTextureId) &&
+ !this->GetTextureByBlock(BlockTextureId(id, state, 0))) {
+ continue;
+ }
+
+ if (this->GetTextureByBlock(blockTextureId)) {
+ for (int i = 0; i < 6; i++) {
+ TextureCoordinates tc = this->GetTextureByBlock(BlockTextureId(id, state, 6));
+ textureAtlasIndexes[BlockTextureId(id, state, i)] = glm::vec4(tc.x, tc.y, tc.w, tc.h);
+ }
+ } else {
+ for (int i = 0; i < 6; i++) {
+ TextureCoordinates tc = this->GetTextureByBlock(BlockTextureId(id, state, i));
+ textureAtlasIndexes[BlockTextureId(id, state, i)] = glm::vec4(tc.x, tc.y, tc.w, tc.h);
+ }
+ }
+ }
+ }
+ LOG(INFO) << "Created " << textureAtlasIndexes.size() << " texture indexes";
+
+ return textureAtlasIndexes;
}
AssetManager &AssetManager::Instance() {
- static AssetManager assetManager;
- return assetManager;
+ static AssetManager assetManager;
+ return assetManager;
}
const BlockModel *AssetManager::GetBlockModelByBlockId(BlockId block) {
@@ -233,14 +232,10 @@ const BlockModel *AssetManager::GetBlockModelByBlockId(BlockId block) {
std::string blockName = blockIdToBlockName[block];
auto modelIt = models.find(blockName);
- if (modelIt == models.end())
- return nullptr;
-
- return &modelIt->second;
+ return (modelIt == models.end()) ? nullptr : &modelIt->second;
}
void AssetManager::LoadBlockModels() {
-
std::function<void(std::string)> parseModel = [&](std::string ModelName) {
if (models.find(ModelName) != models.end())
return;
@@ -377,7 +372,6 @@ void AssetManager::LoadBlockModels() {
continue;
std::string modelName = dirEntry.path().stem().generic_string();
-
parseModel("block/" + modelName);
}
}
diff --git a/src/AssetManager.hpp b/src/AssetManager.hpp
index 1169b7d..845c2b2 100644
--- a/src/AssetManager.hpp
+++ b/src/AssetManager.hpp
@@ -13,7 +13,8 @@
class Texture;
struct TextureCoordinates {
- TextureCoordinates(float x = -1, float y = -1, float w = -1, float h = -1) : x(x), y(y), w(w), h(h) {}
+ TextureCoordinates(float x = -1, float y = -1, float w = -1, float h = -1)
+ : x(x), y(y), w(w), h(h) {}
bool operator==(const TextureCoordinates &rhs) const {
return x == rhs.x &&
@@ -35,11 +36,12 @@ struct TextureCoordinates {
struct BlockTextureId {
//Block sides: 0 - bottom, 1 - top, 2 - north, 3 - south, 4 - west, 5 - east 6 - every side
- BlockTextureId(int id = 0, int state = 0, int side = 6) : id(id), state(state), side(side) {}
+ BlockTextureId(int id = 0, int state = 0, int side = 6)
+ : id(id), state(state), side(side) {}
- int id:9;
- int state:4;
- int side:3;
+ int id : 9;
+ int state : 4;
+ int side : 3;
bool operator<(const BlockTextureId &rhs) const {
@@ -72,6 +74,7 @@ struct BlockModel {
fixed,
DisplayVariantsCount,
};
+
struct DisplayData {
Vector rotation;
Vector translation;
@@ -105,6 +108,7 @@ struct BlockModel {
east,
none,
};
+
struct FaceData {
struct Uv {
int x1, y1, x2, y2;
@@ -122,7 +126,8 @@ struct BlockModel {
std::vector<ElementData> Elements;
};
-inline bool operator==(const BlockModel::ElementData::FaceData::Uv &lhs, const BlockModel::ElementData::FaceData::Uv &rhs) {
+inline bool operator==(const BlockModel::ElementData::FaceData::Uv &lhs,
+ const BlockModel::ElementData::FaceData::Uv &rhs) {
return lhs.x1 == rhs.x1 && lhs.y1 == rhs.y1 && lhs.x2 == rhs.x2 && lhs.y2 == rhs.y2;
}
diff --git a/src/Block.cpp b/src/Block.cpp
index 667670d..303909b 100644
--- a/src/Block.cpp
+++ b/src/Block.cpp
@@ -2,7 +2,9 @@
Block::~Block() {}
-Block::Block(unsigned short id, unsigned char state, unsigned char light, unsigned char sky) : id(id), state(state), light(light), sky (sky) {}
+Block::Block(unsigned short id, unsigned char state,
+ unsigned char light, unsigned char sky)
+ : id(id), state(state), light(light), sky (sky) {}
Block::Block() : id(0), state(0), light(0), sky(0) {}
diff --git a/src/Block.hpp b/src/Block.hpp
index 12d2907..dfd6762 100644
--- a/src/Block.hpp
+++ b/src/Block.hpp
@@ -3,14 +3,13 @@
#include <functional>
struct Block {
- Block();
+ Block();
+ Block(unsigned short id, unsigned char state,
+ unsigned char light, unsigned char sky);
+ ~Block();
- Block(unsigned short id, unsigned char state, unsigned char light, unsigned char sky);
-
- ~Block();
-
- unsigned short id : 13;
- unsigned char state : 4;
+ unsigned short id : 13;
+ unsigned char state : 4;
unsigned char light : 4;
unsigned char sky : 4;
};
@@ -27,8 +26,7 @@ bool operator<(const BlockId& lhs, const BlockId &rhs);
namespace std {
template <>
struct hash<BlockId> {
- std::size_t operator()(const BlockId& k) const
- {
+ std::size_t operator()(const BlockId& k) const {
size_t id = std::hash<unsigned short>()(k.id);
size_t state = std::hash<unsigned char>()(k.state);
diff --git a/src/Entity.cpp b/src/Entity.cpp
index d91e78d..821d556 100644
--- a/src/Entity.cpp
+++ b/src/Entity.cpp
@@ -1,14 +1,12 @@
#include "Entity.hpp"
-VectorF Entity::DecodeVelocity(short x, short y, short z)
-{
+VectorF Entity::DecodeVelocity(short x, short y, short z) {
const float ticksPerSecond = 20;
const double velMod = 1 / 8000.0;
return VectorF(x * velMod * ticksPerSecond, y*velMod*ticksPerSecond, z*velMod*ticksPerSecond);
}
-VectorF Entity::DecodeDeltaPos(short deltaX, short deltaY, short deltaZ)
-{
+VectorF Entity::DecodeDeltaPos(short deltaX, short deltaY, short deltaZ) {
const double posMod = 4096.0;
return VectorF(deltaX / posMod, deltaY / posMod, deltaZ / posMod);
}
@@ -29,8 +27,7 @@ double Entity::EncodePitch(double pitch) {
return -pitch;
}
-Entity CreateObject(ObjectType type)
-{
+Entity CreateObject(ObjectType type) {
Entity entity;
entity.type = EntityType::Object;
switch (type) {
@@ -96,8 +93,7 @@ Entity CreateObject(ObjectType type)
return entity;
}
-Entity CreateMob(MobType type)
-{
+Entity CreateMob(MobType type) {
Entity entity;
entity.type = EntityType::Mob;
return entity;
diff --git a/src/Event.cpp b/src/Event.cpp
index 08c9ee7..5c126bb 100644
--- a/src/Event.cpp
+++ b/src/Event.cpp
@@ -57,6 +57,7 @@ void EventListener::PollEvents() {
std::lock_guard<std::recursive_mutex> rawLock (rawEventsMutex);
if (rawEvents.empty())
return;
+
std::lock_guard<std::recursive_mutex> eventsLock (eventsMutex);
std::lock_guard<std::recursive_mutex> handlersLock (handlersMutex);
while (!rawEvents.empty()) {
diff --git a/src/Event.hpp b/src/Event.hpp
index 4cdcdc3..4e04a5a 100644
--- a/src/Event.hpp
+++ b/src/Event.hpp
@@ -55,7 +55,7 @@ public:
template<typename T>
const T& get() const {
if (typeid(T) != data->Type())
- throw std::runtime_error(std::string("Type ") + typeid(T).name() + " encountered but " + data->Type().name() + " expected");
+ throw std::runtime_error(std::string("Type ") + typeid(T).name() +" encountered but " + data->Type().name() + " expected");
return static_cast<EventData<T>*>(data.get())->data;
}
};
@@ -69,6 +69,7 @@ class EventListener {
std::recursive_mutex eventsMutex;
std::queue<Event> rawEvents;
std::recursive_mutex rawEventsMutex;
+
public:
EventListener();
diff --git a/src/Frustum.cpp b/src/Frustum.cpp
index dbad920..e928e6b 100644
--- a/src/Frustum.cpp
+++ b/src/Frustum.cpp
@@ -3,7 +3,7 @@
#include <glm/gtc/type_ptr.hpp>
void Frustum::NormalizePlane(FrustumSide side) {
- float magnitude = (float)sqrt(frustum[side][A] * frustum[side][A] + frustum[side][B] * frustum[side][B] + frustum[side][C] * frustum[side][C]);
+ float magnitude = (float) sqrt(frustum[side][A] * frustum[side][A] + frustum[side][B] * frustum[side][B] + frustum[side][C] * frustum[side][C]);
frustum[side][A] /= magnitude;
frustum[side][B] /= magnitude;
diff --git a/src/GameState.cpp b/src/GameState.cpp
index 28e8007..6182d20 100644
--- a/src/GameState.cpp
+++ b/src/GameState.cpp
@@ -13,7 +13,10 @@ void GameState::Update(float deltaTime) {
auto delta = clock.now() - timeOfPreviousSendedPacket;
using namespace std::chrono_literals;
if (delta >= 50ms) {
- auto packetToSend = std::make_shared<PacketPlayerPositionAndLookSB>(player->pos.x, player->pos.y, player->pos.z, player->yaw, player->pitch, player->onGround);
+ auto packetToSend = std::make_shared<PacketPlayerPositionAndLookSB>(
+ player->pos.x, player->pos.y, player->pos.z,
+ player->yaw, player->pitch, player->onGround);
+
auto packet = std::static_pointer_cast<Packet>(packetToSend);
PUSH_EVENT("SendPacket",packet);
timeOfPreviousSendedPacket = clock.now();
@@ -22,7 +25,10 @@ void GameState::Update(float deltaTime) {
bool prevOnGround = player->onGround;
world.UpdatePhysics(deltaTime);
if (player->onGround != prevOnGround) {
- auto updatePacket = std::make_shared<PacketPlayerPosition>(player->pos.x, player->pos.y, player->pos.z, player->onGround);
+ auto updatePacket = std::make_shared<PacketPlayerPosition>(
+ player->pos.x, player->pos.y,
+ player->pos.z, player->onGround);
+
auto packet = std::static_pointer_cast<Packet>(updatePacket);
PUSH_EVENT("SendPacket",packet);
}
@@ -60,10 +66,13 @@ void GameState::UpdatePacket(std::shared_ptr<Packet> ptr)
PUSH_EVENT("EntityChanged", entity.entityId);
break;
}
+
case SpawnExperienceOrb:
break;
+
case SpawnGlobalEntity:
break;
+
case SpawnMob: {
auto packet = std::static_pointer_cast<PacketSpawnMob>(ptr);
Entity entity;
@@ -78,8 +87,10 @@ void GameState::UpdatePacket(std::shared_ptr<Packet> ptr)
PUSH_EVENT("EntityChanged", entity.entityId);
break;
}
+
case SpawnPainting:
break;
+
case SpawnPlayer: {
auto packet = std::static_pointer_cast<PacketSpawnPlayer>(ptr);
Entity entity;
@@ -105,28 +116,33 @@ void GameState::UpdatePacket(std::shared_ptr<Packet> ptr)
break;
case BlockAction:
break;
+
case BlockChange: {
auto packet = std::static_pointer_cast<PacketBlockChange>(ptr);
world.ParseChunkData(packet);
break;
}
+
case BossBar:
break;
case ServerDifficulty:
break;
case TabCompleteCB:
break;
+
case ChatMessageCB: {
auto packet = std::static_pointer_cast<PacketChatMessageCB>(ptr);
LOG(INFO) << "Message (" << int(packet->Position) << "): " << packet->JsonData.text;
PUSH_EVENT("ChatMessageReceived", std::make_tuple(packet->JsonData, packet->Position));
break;
}
+
case MultiBlockChange: {
auto packet = std::static_pointer_cast<PacketMultiBlockChange>(ptr);
world.ParseChunkData(packet);
break;
}
+
case ConfirmTransactionCB: {
auto packet = std::static_pointer_cast<PacketConfirmTransactionCB>(ptr);
if (packet->WindowId == 0) {
@@ -138,14 +154,17 @@ void GameState::UpdatePacket(std::shared_ptr<Packet> ptr)
}
break;
}
+
case CloseWindowCB:
break;
+
case OpenWindow: {
auto packet = std::static_pointer_cast<PacketOpenWindow>(ptr);
LOG(INFO) << "Open new window " << packet->WindowTitle << ": " << packet->WindowId;
break;
}
+
case WindowItems: {
auto packet = std::static_pointer_cast<PacketWindowItems>(ptr);
if (packet->WindowId == 0) {
@@ -154,8 +173,10 @@ void GameState::UpdatePacket(std::shared_ptr<Packet> ptr)
}
break;
}
+
case WindowProperty:
break;
+
case SetSlot: {
auto packet = std::static_pointer_cast<PacketSetSlot>(ptr);
if (packet->WindowId == 0) {
@@ -163,41 +184,51 @@ void GameState::UpdatePacket(std::shared_ptr<Packet> ptr)
}
break;
}
+
case SetCooldown:
break;
case PluginMessageCB:
break;
case NamedSoundEffect:
break;
+
case DisconnectPlay: {
auto packet = std::static_pointer_cast<PacketDisconnectPlay>(ptr);
LOG(INFO) << "Disconnect reason: " << packet->Reason;
PUSH_EVENT("Disconnected", packet->Reason);
break;
}
+
case EntityStatus:
break;
case Explosion:
break;
+
case UnloadChunk: {
auto packet = std::static_pointer_cast<PacketUnloadChunk>(ptr);
world.ParseChunkData(packet);
break;
}
+
case ChangeGameState:
break;
- case KeepAliveCB:
+
+ case KeepAliveCB: {
LOG(WARNING) << "Receive KeepAlive packet in GameState handler";
break;
+ }
+
case ChunkData: {
auto packet = std::static_pointer_cast<PacketChunkData>(ptr);
world.ParseChunkData(packet);
break;
}
+
case Effect:
break;
case Particle:
break;
+
case JoinGame: {
auto packet = std::static_pointer_cast<PacketJoinGame>(ptr);
Entity entity;
@@ -219,8 +250,10 @@ void GameState::UpdatePacket(std::shared_ptr<Packet> ptr)
PUSH_EVENT("PlayerConnected", 0);
break;
}
+
case Map:
break;
+
case EntityRelativeMove: {
auto packet = std::static_pointer_cast<PacketEntityRelativeMove>(ptr);
Entity &entity = world.GetEntity(packet->EntityId);
@@ -229,6 +262,7 @@ void GameState::UpdatePacket(std::shared_ptr<Packet> ptr)
LOG(INFO) << "M: " << packet->EntityId;
break;
}
+
case EntityLookAndRelativeMove: {
auto packet = std::static_pointer_cast<PacketEntityLookAndRelativeMove>(ptr);
Entity &entity = world.GetEntity(packet->EntityId);
@@ -237,6 +271,7 @@ void GameState::UpdatePacket(std::shared_ptr<Packet> ptr)
entity.yaw = packet->Yaw / 256.0;
break;
}
+
case EntityLook: {
auto packet = std::static_pointer_cast<PacketEntityLook>(ptr);
Entity &entity = world.GetEntity(packet->EntityId);
@@ -245,6 +280,7 @@ void GameState::UpdatePacket(std::shared_ptr<Packet> ptr)
//LOG(INFO) << "L: " << packet->EntityId;
break;
}
+
case EntityCB:
break;
case VehicleMove:
@@ -257,6 +293,7 @@ void GameState::UpdatePacket(std::shared_ptr<Packet> ptr)
break;
case PlayerListItem:
break;
+
case PlayerPositionAndLookCB: {
auto packet = std::static_pointer_cast<PacketPlayerPositionAndLookCB>(ptr);
if ((packet->Flags & 0x10) != 0) {
@@ -306,10 +343,12 @@ void GameState::UpdatePacket(std::shared_ptr<Packet> ptr)
PUSH_EVENT("SendPacket",std::static_pointer_cast<Packet>(packetPerformRespawn));
break;
}
+
case UseBed:
break;
case UnlockRecipes:
break;
+
case DestroyEntities: {
auto packet = std::static_pointer_cast<PacketDestroyEntities>(ptr);
for (unsigned int entityId : packet->EntityIds) {
@@ -317,6 +356,7 @@ void GameState::UpdatePacket(std::shared_ptr<Packet> ptr)
}
break;
}
+
case RemoveEntityEffect:
break;
case ResourcePackSend:
@@ -339,16 +379,19 @@ void GameState::UpdatePacket(std::shared_ptr<Packet> ptr)
break;
case AttachEntity:
break;
+
case EntityVelocity: {
auto packet = std::static_pointer_cast<PacketEntityVelocity>(ptr);
Entity &entity = world.GetEntity(packet->EntityId);
entity.vel = Entity::DecodeVelocity(packet->VelocityX, packet->VelocityY, packet->VelocityZ);
break;
}
+
case EntityEquipment:
break;
case SetExperience:
break;
+
case UpdateHealth: {
auto packet = std::static_pointer_cast<PacketUpdateHealth>(ptr);
g_PlayerHealth = packet->Health;
@@ -359,6 +402,7 @@ void GameState::UpdatePacket(std::shared_ptr<Packet> ptr)
}
break;
}
+
case ScoreboardObjective:
break;
case SetPassengers:
@@ -367,6 +411,7 @@ void GameState::UpdatePacket(std::shared_ptr<Packet> ptr)
break;
case UpdateScore:
break;
+
case SpawnPosition: {
auto packet = std::static_pointer_cast<PacketSpawnPosition>(ptr);
g_SpawnPosition = packet->Location;
@@ -374,12 +419,14 @@ void GameState::UpdatePacket(std::shared_ptr<Packet> ptr)
<< g_SpawnPosition.z;
break;
}
+
case TimeUpdate: {
auto packet = std::static_pointer_cast<PacketTimeUpdate>(ptr);
WorldAge = packet->WorldAge;
TimeOfDay = packet->TimeOfDay;
break;
}
+
case Title:
break;
case SoundEffect:
@@ -388,6 +435,7 @@ void GameState::UpdatePacket(std::shared_ptr<Packet> ptr)
break;
case CollectItem:
break;
+
case EntityTeleport: {
auto packet = std::static_pointer_cast<PacketEntityTeleport>(ptr);
Entity &entity = world.GetEntity(packet->EntityId);
@@ -396,6 +444,7 @@ void GameState::UpdatePacket(std::shared_ptr<Packet> ptr)
entity.yaw = packet->Yaw / 256.0;
break;
}
+
case Advancements:
break;
case EntityProperties:
@@ -403,6 +452,7 @@ void GameState::UpdatePacket(std::shared_ptr<Packet> ptr)
case EntityEffect:
break;
}
+
while (!playerInventory.pendingTransactions.empty()) {
auto packet = std::make_shared<PacketClickWindow>(playerInventory.pendingTransactions.front());
playerInventory.pendingTransactions.pop();
@@ -431,25 +481,33 @@ void GameState::HandleMovement(GameState::Direction direction, float deltaTime)
glm::vec3 vel = player->vel.glm();
switch (direction) {
- case FORWARD:
- vel += front * velocity;
- break;
- case BACKWARD:
- vel -= front * velocity;
- break;
- case RIGHT:
- vel += right * velocity;
- break;
- case LEFT:
- vel -= right * velocity;
- break;
- case JUMP:
- if (player->onGround) {
- vel.y += 10;
- player->onGround = false;
- }
- break;
- }
+ case FORWARD: {
+ vel += front * velocity;
+ break;
+ }
+
+ case BACKWARD: {
+ vel -= front * velocity;
+ break;
+ }
+
+ case RIGHT: {
+ vel += right * velocity;
+ break;
+ }
+
+ case LEFT: {
+ vel -= right * velocity;
+ break;
+ }
+
+ case JUMP:
+ if (player->onGround) {
+ vel.y += 10;
+ player->onGround = false;
+ }
+ break;
+ }
player->vel = VectorF(vel.x, vel.y, vel.z);
}
@@ -494,6 +552,4 @@ void GameState::StartDigging() {
PUSH_EVENT("SendPacket",packet);
}
-void GameState::StopDigging() {
-
-}
+void GameState::StopDigging() {}
diff --git a/src/GlobalState.cpp b/src/GlobalState.cpp
index ce8cb05..4490862 100644
--- a/src/GlobalState.cpp
+++ b/src/GlobalState.cpp
@@ -42,9 +42,10 @@ void InitEvents() {
isPhysRunning = true;
threadPhys = std::thread(&PhysExec);
try {
- nc = std::make_unique<NetworkClient>(std::get<0>(data), std::get<1>(data), std::get<2>(data));
- }
- catch (std::exception &e) {
+ nc = std::make_unique<NetworkClient>(std::get<0>(data),
+ std::get<1>(data),
+ std::get<2>(data));
+ } catch (std::exception &e) {
LOG(WARNING) << "Connection failed";
PUSH_EVENT("ConnectionFailed", std::string(e.what()));
isPhysRunning = false;
@@ -98,47 +99,47 @@ void PhysExec() {
if (!gs)
return;
switch (eventData.get<SDL_Scancode>()) {
- case SDL_SCANCODE_W:
- isMoving[GameState::FORWARD] = true;
- break;
- case SDL_SCANCODE_A:
- isMoving[GameState::LEFT] = true;
- break;
- case SDL_SCANCODE_S:
- isMoving[GameState::BACKWARD] = true;
- break;
- case SDL_SCANCODE_D:
- isMoving[GameState::RIGHT] = true;
- break;
- case SDL_SCANCODE_SPACE:
- isMoving[GameState::JUMP] = true;
- break;
- default:
- break;
- }
+ case SDL_SCANCODE_W:
+ isMoving[GameState::FORWARD] = true;
+ break;
+ case SDL_SCANCODE_A:
+ isMoving[GameState::LEFT] = true;
+ break;
+ case SDL_SCANCODE_S:
+ isMoving[GameState::BACKWARD] = true;
+ break;
+ case SDL_SCANCODE_D:
+ isMoving[GameState::RIGHT] = true;
+ break;
+ case SDL_SCANCODE_SPACE:
+ isMoving[GameState::JUMP] = true;
+ break;
+ default:
+ break;
+ }
});
listener.RegisterHandler("KeyReleased", [](const Event& eventData) {
if (!gs)
return;
switch (eventData.get<SDL_Scancode>()) {
- case SDL_SCANCODE_W:
- isMoving[GameState::FORWARD] = false;
- break;
- case SDL_SCANCODE_A:
- isMoving[GameState::LEFT] = false;
- break;
- case SDL_SCANCODE_S:
- isMoving[GameState::BACKWARD] = false;
- break;
- case SDL_SCANCODE_D:
- isMoving[GameState::RIGHT] = false;
- break;
- case SDL_SCANCODE_SPACE:
- isMoving[GameState::JUMP] = false;
- break;
- default:
- break;
+ case SDL_SCANCODE_W:
+ isMoving[GameState::FORWARD] = false;
+ break;
+ case SDL_SCANCODE_A:
+ isMoving[GameState::LEFT] = false;
+ break;
+ case SDL_SCANCODE_S:
+ isMoving[GameState::BACKWARD] = false;
+ break;
+ case SDL_SCANCODE_D:
+ isMoving[GameState::RIGHT] = false;
+ break;
+ case SDL_SCANCODE_SPACE:
+ isMoving[GameState::JUMP] = false;
+ break;
+ default:
+ break;
}
});