1er commit
This commit is contained in:
386
src/game/World.cpp
Normal file
386
src/game/World.cpp
Normal file
@@ -0,0 +1,386 @@
|
||||
#include "game/World.h"
|
||||
#include "protocol/PacketDispatcher.h"
|
||||
#include "protocol/Protocol.h"
|
||||
#include "game/BaseGame.h"
|
||||
#include "misc/Random.h"
|
||||
|
||||
#include <cmath>
|
||||
#include "misc/Compression.h"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
#define WRITE_MAP 0
|
||||
|
||||
namespace td {
|
||||
namespace game {
|
||||
|
||||
World::World(Game* game) : m_Game(game){
|
||||
#if WRITE_MAP
|
||||
loadMapFromFile("");
|
||||
#endif
|
||||
}
|
||||
|
||||
TilePtr World::getTile(std::int32_t x, std::int32_t y){
|
||||
std::int16_t chunkX = x / Chunk::ChunkWidth;
|
||||
std::int16_t chunkY = y / Chunk::ChunkHeight;
|
||||
|
||||
std::uint16_t subChunkX = x % Chunk::ChunkWidth;
|
||||
std::uint16_t subChunkY = y % Chunk::ChunkHeight;
|
||||
|
||||
auto chunkIt = m_Chunks.find({chunkX, chunkY});
|
||||
if(chunkIt == m_Chunks.end())
|
||||
return nullptr;
|
||||
|
||||
ChunkPtr chunk = chunkIt->second;
|
||||
|
||||
return getTilePtr(chunk->getTileIndex(subChunkY * Chunk::ChunkWidth + subChunkX));
|
||||
}
|
||||
|
||||
bool World::loadMap(const protocol::WorldBeginDataPacket* worldHeader){
|
||||
m_TowerPlacePalette = worldHeader->getTowerTilePalette();
|
||||
m_WalkablePalette = worldHeader->getWalkableTileColor();
|
||||
m_DecorationPalette = worldHeader->getDecorationPalette();
|
||||
|
||||
m_Spawns[(std::size_t) TeamColor::Red] = worldHeader->getRedSpawn();
|
||||
m_Spawns[(std::size_t) TeamColor::Blue] = worldHeader->getBlueSpawn();
|
||||
|
||||
m_SpawnColorPalette = worldHeader->getSpawnPalette();
|
||||
|
||||
m_Castles[(std::size_t) TeamColor::Red] = worldHeader->getRedCastle();
|
||||
m_Castles[(std::size_t) TeamColor::Blue] = worldHeader->getBlueCastle();
|
||||
|
||||
m_TilePalette = worldHeader->getTilePalette();
|
||||
}
|
||||
|
||||
bool World::loadMap(const protocol::WorldDataPacket* worldData){
|
||||
m_Chunks = worldData->getChunks();
|
||||
}
|
||||
|
||||
bool World::loadMapFromFile(const std::string& fileName){
|
||||
#if !WRITE_MAP
|
||||
DataBuffer buffer;
|
||||
if(!buffer.ReadFile(fileName)){
|
||||
std::cerr << "Failed to load map from file " << fileName << " !\n";
|
||||
}
|
||||
|
||||
std::cout << "File size : " << buffer.GetSize() << std::endl;
|
||||
|
||||
DataBuffer mapHeaderPacketBuffer = utils::Decompress(buffer);
|
||||
DataBuffer mapDataPacketBuffer = utils::Decompress(buffer);
|
||||
|
||||
protocol::PacketType packetType;
|
||||
|
||||
mapHeaderPacketBuffer >> packetType;
|
||||
|
||||
protocol::WorldBeginDataPacket headerPacket;
|
||||
headerPacket.Deserialize(mapHeaderPacketBuffer);
|
||||
|
||||
mapDataPacketBuffer >> packetType;
|
||||
|
||||
protocol::WorldDataPacket dataPacket;
|
||||
dataPacket.Deserialize(mapDataPacketBuffer);
|
||||
|
||||
loadMap(&headerPacket);
|
||||
loadMap(&dataPacket);
|
||||
|
||||
#else
|
||||
m_WalkablePalette = {102, 102, 153};
|
||||
|
||||
m_TowerPlacePalette[0] = {204, 51, 0};
|
||||
m_TowerPlacePalette[1] = {255, 153, 0};
|
||||
|
||||
WalkableTile walkableTileDown; // 1
|
||||
walkableTileDown.direction = Direction::PositiveY;
|
||||
m_TilePalette.push_back(std::make_shared<WalkableTile>(walkableTileDown));
|
||||
|
||||
WalkableTile walkableTileUp; // 2
|
||||
walkableTileUp.direction = Direction::NegativeY;
|
||||
m_TilePalette.push_back(std::make_shared<WalkableTile>(walkableTileUp));
|
||||
|
||||
WalkableTile walkableTileLeft; // 3
|
||||
walkableTileLeft.direction = Direction::NegativeX;
|
||||
m_TilePalette.push_back(std::make_shared<WalkableTile>(walkableTileLeft));
|
||||
|
||||
WalkableTile walkableTileRight; // 4
|
||||
walkableTileRight.direction = Direction::PositiveX;
|
||||
m_TilePalette.push_back(std::make_shared<WalkableTile>(walkableTileRight));
|
||||
|
||||
TowerTile redTile0; // 5
|
||||
redTile0.color_palette_ref = 0;
|
||||
redTile0.team_owner = TeamColor::Red;
|
||||
m_TilePalette.push_back(std::make_shared<TowerTile>(redTile0));
|
||||
|
||||
TowerTile redTile1; // 6
|
||||
redTile1.color_palette_ref = 1;
|
||||
redTile1.team_owner = TeamColor::Red;
|
||||
m_TilePalette.push_back(std::make_shared<TowerTile>(redTile1));
|
||||
|
||||
TowerTile blueTile0; // 7
|
||||
blueTile0.color_palette_ref = 0;
|
||||
blueTile0.team_owner = TeamColor::Blue;
|
||||
m_TilePalette.push_back(std::make_shared<TowerTile>(blueTile0));
|
||||
|
||||
TowerTile blueTile1; // 8
|
||||
blueTile1.color_palette_ref = 1;
|
||||
blueTile1.team_owner = TeamColor::Blue;
|
||||
m_TilePalette.push_back(std::make_shared<TowerTile>(blueTile1));
|
||||
|
||||
m_Castles[(uint) TeamColor::Red].x = 58;
|
||||
m_Castles[(uint) TeamColor::Red].y = 43;
|
||||
|
||||
m_Spawns[(uint) TeamColor::Red].direction = Direction::PositiveY;
|
||||
m_Spawns[(uint) TeamColor::Red].x = 13;
|
||||
m_Spawns[(uint) TeamColor::Red].y = 13;
|
||||
|
||||
m_SpawnColorPalette[(uint) TeamColor::Red] = {255, 0, 0};
|
||||
|
||||
//Chunks
|
||||
|
||||
Chunk chunk0;
|
||||
for (int i = 0; i <= 6; i++){
|
||||
chunk0.palette.push_back(i);
|
||||
}
|
||||
chunk0.tiles = {
|
||||
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
||||
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
||||
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0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,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
||||
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
||||
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
||||
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
||||
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
||||
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
||||
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
||||
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
||||
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
||||
};
|
||||
|
||||
m_Chunks.insert({{0, 0}, std::make_shared<Chunk>(chunk0)});
|
||||
m_Chunks.insert({{1, 0}, std::make_shared<Chunk>(chunk1)});
|
||||
m_Chunks.insert({{1, 1}, std::make_shared<Chunk>(chunk2)});
|
||||
|
||||
|
||||
// blue map = same but with offset of 64
|
||||
|
||||
|
||||
m_Castles[(uint) TeamColor::Blue].x = 58 + 64;
|
||||
m_Castles[(uint) TeamColor::Blue].y = 43;
|
||||
|
||||
m_Spawns[(uint) TeamColor::Blue].direction = Direction::PositiveY;
|
||||
m_Spawns[(uint) TeamColor::Blue].x = 13 + 64;
|
||||
m_Spawns[(uint) TeamColor::Blue].y = 13;
|
||||
|
||||
m_SpawnColorPalette[(uint) TeamColor::Blue] = {0, 0, 255};
|
||||
|
||||
Chunk chunk01;
|
||||
chunk01.palette = {0, 1, 2, 3, 4, 7, 8};
|
||||
|
||||
chunk01.tiles = chunk0.tiles; // the tiles indicies are the same, only the palette has changed
|
||||
|
||||
|
||||
Chunk chunk11;
|
||||
chunk11.palette = {0, 1, 2, 3, 4, 7, 8};
|
||||
|
||||
chunk11.tiles = chunk1.tiles; // the tiles indicies are the same, only the palette has changed
|
||||
|
||||
|
||||
Chunk chunk21;
|
||||
chunk21.palette = {0, 1, 2, 3, 4, 7, 8};
|
||||
|
||||
chunk21.tiles = chunk2.tiles; // the tiles indicies are the same, only the palette has changed
|
||||
|
||||
m_Chunks.insert({{2, 0}, std::make_shared<Chunk>(chunk01)});
|
||||
m_Chunks.insert({{3, 0}, std::make_shared<Chunk>(chunk11)});
|
||||
m_Chunks.insert({{3, 1}, std::make_shared<Chunk>(chunk21)});
|
||||
|
||||
saveMap("tdmap.tdmap");
|
||||
#endif
|
||||
|
||||
}
|
||||
|
||||
bool World::saveMap(const std::string& fileName) const{
|
||||
protocol::WorldBeginDataPacket headerPacket(this);
|
||||
protocol::WorldDataPacket dataPacket(this);
|
||||
|
||||
DataBuffer mapHeaderCompressed = utils::Compress(headerPacket.Serialize());
|
||||
DataBuffer mapDataCompressed = utils::Compress(dataPacket.Serialize());
|
||||
|
||||
std::cout << "Header Packet Size : " << mapHeaderCompressed.GetSize() << std::endl;
|
||||
std::cout << "World Data Packet Size : " << mapDataCompressed.GetSize() << std::endl;
|
||||
|
||||
DataBuffer buffer = mapHeaderCompressed << mapDataCompressed;
|
||||
std::cout << "Total Size : " << buffer.GetSize() << std::endl;
|
||||
buffer.WriteFile(fileName);
|
||||
std::cout << "----- Map Saved ! -----\n";
|
||||
}
|
||||
|
||||
void World::tick(std::uint64_t delta){
|
||||
moveMobs(delta);
|
||||
}
|
||||
|
||||
void World::spawnMobAt(MobID id, MobType type, std::uint8_t level, PlayerID sender, float x, float y, Direction dir){
|
||||
MobPtr mob = MobFactory::createMob(id, type, level, sender);
|
||||
mob->setX(x);
|
||||
mob->setY(y);
|
||||
mob->setDirection(dir);
|
||||
m_Mobs.push_back(mob);
|
||||
}
|
||||
|
||||
void World::moveMobs(std::uint64_t delta){
|
||||
for(MobPtr mob : m_Mobs){
|
||||
TilePtr tile = getTile(mob->getX(), mob->getY());
|
||||
|
||||
Direction tileDir;
|
||||
|
||||
if(tile != nullptr && tile->getType() == TileType::Walk){
|
||||
WalkableTile* walkTile = dynamic_cast<WalkableTile*>(tile.get());
|
||||
mob->setDirection(walkTile->direction);
|
||||
}
|
||||
|
||||
float mobWalkSpeed = mob->getStats()->getMovementSpeed();
|
||||
|
||||
float walkAmount = mobWalkSpeed * ((float) delta / 1000.0f);
|
||||
|
||||
switch(mob->getDirection()){
|
||||
case Direction::NegativeX:{
|
||||
mob->setX(mob->getX() - walkAmount);
|
||||
break;
|
||||
}
|
||||
case Direction::PositiveX:{
|
||||
mob->setX(mob->getX() + walkAmount);
|
||||
break;
|
||||
}
|
||||
case Direction::NegativeY:{
|
||||
mob->setY(mob->getY() - walkAmount);
|
||||
break;
|
||||
}
|
||||
case Direction::PositiveY:{
|
||||
mob->setY(mob->getY() + walkAmount);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const Color& World::getTileColor(TilePtr tile) const{
|
||||
switch(tile->getType()){
|
||||
case TileType::Tower:{
|
||||
TowerTile* towerTile = (TowerTile*) tile.get();
|
||||
return m_TowerPlacePalette[towerTile->color_palette_ref];
|
||||
}
|
||||
case TileType::Walk:{
|
||||
return m_WalkablePalette;
|
||||
}
|
||||
case TileType::Decoration:{
|
||||
DecorationTile* towerTile = (DecorationTile*) tile.get();
|
||||
return m_DecorationPalette[towerTile->color_palette_ref];
|
||||
break;
|
||||
}
|
||||
}
|
||||
return m_DecorationPalette[0];
|
||||
}
|
||||
|
||||
} // namespace game
|
||||
} // namespace td
|
||||
Reference in New Issue
Block a user