refactor: format code
This commit is contained in:
@@ -14,21 +14,21 @@
|
||||
namespace td {
|
||||
namespace game {
|
||||
|
||||
World::World(Game* game) : m_Game(game){
|
||||
#if WRITE_MAP
|
||||
World::World(Game* game) : m_Game(game) {
|
||||
#if WRITE_MAP
|
||||
loadMapFromFile("");
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
|
||||
TilePtr World::getTile(std::int32_t x, std::int32_t y){
|
||||
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())
|
||||
auto chunkIt = m_Chunks.find({ chunkX, chunkY });
|
||||
if (chunkIt == m_Chunks.end())
|
||||
return nullptr;
|
||||
|
||||
ChunkPtr chunk = chunkIt->second;
|
||||
@@ -36,7 +36,7 @@ TilePtr World::getTile(std::int32_t x, std::int32_t y){
|
||||
return getTilePtr(chunk->getTileIndex(subChunkY * Chunk::ChunkWidth + subChunkX));
|
||||
}
|
||||
|
||||
bool World::loadMap(const protocol::WorldBeginDataPacket* worldHeader){
|
||||
bool World::loadMap(const protocol::WorldBeginDataPacket* worldHeader) {
|
||||
m_TowerPlacePalette = worldHeader->getTowerTilePalette();
|
||||
m_WalkablePalette = worldHeader->getWalkableTileColor();
|
||||
m_DecorationPalette = worldHeader->getDecorationPalette();
|
||||
@@ -54,15 +54,15 @@ bool World::loadMap(const protocol::WorldBeginDataPacket* worldHeader){
|
||||
return true;
|
||||
}
|
||||
|
||||
bool World::loadMap(const protocol::WorldDataPacket* worldData){
|
||||
bool World::loadMap(const protocol::WorldDataPacket* worldData) {
|
||||
m_Chunks = worldData->getChunks();
|
||||
return true;
|
||||
}
|
||||
|
||||
bool World::loadMapFromFile(const std::string& fileName){
|
||||
bool World::loadMapFromFile(const std::string& fileName) {
|
||||
#if !WRITE_MAP
|
||||
DataBuffer buffer;
|
||||
if(!buffer.ReadFile(fileName)){
|
||||
if (!buffer.ReadFile(fileName)) {
|
||||
std::cerr << "Failed to load map from file " << fileName << " !\n";
|
||||
}
|
||||
|
||||
@@ -89,10 +89,10 @@ bool World::loadMapFromFile(const std::string& fileName){
|
||||
return true;
|
||||
|
||||
#else
|
||||
m_WalkablePalette = {102, 102, 153};
|
||||
m_WalkablePalette = { 102, 102, 153 };
|
||||
|
||||
m_TowerPlacePalette[0] = {204, 51, 0};
|
||||
m_TowerPlacePalette[1] = {255, 153, 0};
|
||||
m_TowerPlacePalette[0] = { 204, 51, 0 };
|
||||
m_TowerPlacePalette[1] = { 255, 153, 0 };
|
||||
|
||||
WalkableTile walkableTileDown; // 1
|
||||
walkableTileDown.direction = Direction::PositiveY;
|
||||
@@ -137,12 +137,12 @@ bool World::loadMapFromFile(const std::string& fileName){
|
||||
getRedTeam().getSpawn().y = 13;
|
||||
getRedTeam().getSpawn().direction = Direction::PositiveY;
|
||||
|
||||
m_SpawnColorPalette[(uint) TeamColor::Red] = {255, 0, 0};
|
||||
m_SpawnColorPalette[(uint)TeamColor::Red] = { 255, 0, 0 };
|
||||
|
||||
//Chunks
|
||||
|
||||
Chunk chunk0;
|
||||
for (int i = 0; i <= 6; i++){
|
||||
for (int i = 0; i <= 6; i++) {
|
||||
chunk0.palette.push_back(i);
|
||||
}
|
||||
chunk0.tiles = {
|
||||
@@ -182,7 +182,7 @@ bool World::loadMapFromFile(const std::string& fileName){
|
||||
|
||||
|
||||
Chunk chunk1;
|
||||
for (int i = 0; i <= 6; i++){
|
||||
for (int i = 0; i <= 6; i++) {
|
||||
chunk1.palette.push_back(i);
|
||||
}
|
||||
|
||||
@@ -223,7 +223,7 @@ bool World::loadMapFromFile(const std::string& fileName){
|
||||
|
||||
|
||||
Chunk chunk2;
|
||||
for (int i = 0; i <= 6; i++){
|
||||
for (int i = 0; i <= 6; i++) {
|
||||
chunk2.palette.push_back(i);
|
||||
}
|
||||
|
||||
@@ -262,9 +262,9 @@ bool World::loadMapFromFile(const std::string& fileName){
|
||||
0,0,0,0,0,0,0,0,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)});
|
||||
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 (in x coordinate)
|
||||
@@ -276,35 +276,35 @@ bool World::loadMapFromFile(const std::string& fileName){
|
||||
getBlueTeam().getSpawn().y = 13;
|
||||
getBlueTeam().getSpawn().direction = Direction::PositiveY;
|
||||
|
||||
m_SpawnColorPalette[(uint) TeamColor::Blue] = {0, 0, 255};
|
||||
m_SpawnColorPalette[(uint)TeamColor::Blue] = { 0, 0, 255 };
|
||||
|
||||
Chunk chunk01;
|
||||
chunk01.palette = {0, 1, 2, 3, 4, 7, 8};
|
||||
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.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.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)});
|
||||
|
||||
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{
|
||||
bool World::saveMap(const std::string& fileName) const {
|
||||
protocol::WorldBeginDataPacket headerPacket(this);
|
||||
protocol::WorldDataPacket dataPacket(this);
|
||||
|
||||
@@ -319,11 +319,11 @@ bool World::saveMap(const std::string& fileName) const{
|
||||
return buffer.WriteFile(fileName);
|
||||
}
|
||||
|
||||
void World::tick(std::uint64_t delta){
|
||||
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){
|
||||
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);
|
||||
@@ -331,82 +331,82 @@ void World::spawnMobAt(MobID id, MobType type, std::uint8_t level, PlayerID send
|
||||
m_Mobs.push_back(mob);
|
||||
}
|
||||
|
||||
void World::moveMobs(std::uint64_t delta){
|
||||
for(MobPtr mob : m_Mobs){
|
||||
void World::moveMobs(std::uint64_t delta) {
|
||||
for (MobPtr mob : m_Mobs) {
|
||||
TilePtr tile = getTile(mob->getX(), mob->getY());
|
||||
|
||||
if(tile != nullptr && tile->getType() == TileType::Walk){
|
||||
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;
|
||||
}
|
||||
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;
|
||||
}
|
||||
case TileType::None:{
|
||||
return nullptr;
|
||||
}
|
||||
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;
|
||||
}
|
||||
case TileType::None: {
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
Team& World::getRedTeam(){
|
||||
Team& World::getRedTeam() {
|
||||
return m_Game->getRedTeam();
|
||||
}
|
||||
|
||||
const Team& World::getRedTeam() const{
|
||||
const Team& World::getRedTeam() const {
|
||||
return m_Game->getRedTeam();
|
||||
}
|
||||
|
||||
Team& World::getBlueTeam(){
|
||||
Team& World::getBlueTeam() {
|
||||
return m_Game->getBlueTeam();
|
||||
}
|
||||
|
||||
const Team& World::getBlueTeam() const{
|
||||
const Team& World::getBlueTeam() const {
|
||||
return m_Game->getBlueTeam();
|
||||
}
|
||||
|
||||
Team& World::getTeam(TeamColor team){
|
||||
Team& World::getTeam(TeamColor team) {
|
||||
return m_Game->getTeam(team);
|
||||
}
|
||||
|
||||
const Team& World::getTeam(TeamColor team) const{
|
||||
const Team& World::getTeam(TeamColor team) const {
|
||||
return m_Game->getTeam(team);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user