board: automatic create a bridge if two straight tile are placed on top of each other

Dieser Commit ist enthalten in:
Reinder Feenstra 2021-09-05 16:53:31 +02:00
Ursprung 8132adb251
Commit e1d4d2df16
2 geänderte Dateien mit 128 neuen und 1 gelöschten Zeilen

Datei anzeigen

@ -45,8 +45,31 @@ Board::Board(const std::weak_ptr<World>& world, std::string_view _id) :
return false;
if(auto it = m_tiles.find(l); it != m_tiles.end())
if(!replace || !deleteTile(x, y))
{
if(!replace && classId == StraightRailTile::classId && std::dynamic_pointer_cast<StraightRailTile>(it->second)) // merge to bridge
{
const TileRotate tileRotate = it->second->rotate;
if((tileRotate == rotate + TileRotate::Deg90 || tileRotate == rotate - TileRotate::Deg90) && deleteTile(x, y))
{
classId = Bridge90RailTile::classId;
rotate = tileRotate;
}
else if((tileRotate == rotate + TileRotate::Deg45 || tileRotate == rotate + TileRotate::Deg225) && deleteTile(x, y))
{
classId = Bridge45LeftRailTile::classId;
rotate = tileRotate;
}
else if((tileRotate == rotate - TileRotate::Deg45 || tileRotate == rotate - TileRotate::Deg225) && deleteTile(x, y))
{
classId = Bridge45RightRailTile::classId;
rotate = tileRotate;
}
else
return false;
}
else if(!replace || !deleteTile(x, y))
return false;
}
auto tile = Tiles::create(w, classId);
if(!tile)

Datei anzeigen

@ -0,0 +1,104 @@
/**
* server/test/board/addtile.cpp
*
* This file is part of the traintastic test suite.
*
* Copyright (C) 2021 Reinder Feenstra
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <catch2/catch.hpp>
#include "../src/world/world.hpp"
#include "../src/board/board.hpp"
#include "../src/board/tile/rail/straightrailtile.hpp"
#include "../src/board/tile/rail/bridge90railtile.hpp"
#include "../src/board/tile/rail/bridge45leftrailtile.hpp"
#include "../src/board/tile/rail/bridge45rightrailtile.hpp"
TEST_CASE("Board: Merge tile, brigde 90", "[board][board-merge]")
{
auto world = World::create();
auto board = world->boards->add();
int16_t x = 0;
const int16_t y = 0;
for(auto r : {TileRotate::Deg0, TileRotate::Deg45, TileRotate::Deg90, TileRotate::Deg135})
{
REQUIRE(board->addTile(x, y, r, StraightRailTile::classId, false));
std::weak_ptr<Tile> straight = board->getTile({x, y});
REQUIRE_FALSE(straight.expired());
REQUIRE(straight.lock()->rotate == r);
REQUIRE(board->addTile(x, y, r + TileRotate::Deg90, StraightRailTile::classId, false));
REQUIRE(straight.expired());
std::weak_ptr<Tile> bridge = board->getTile({x, y});
REQUIRE_FALSE(bridge.expired());
REQUIRE(bridge.lock()->getClassId() == Bridge90RailTile::classId);
REQUIRE(bridge.lock()->rotate == r);
x++;
}
}
TEST_CASE("Board: Merge tile, brigde 45 left", "[board][board-merge]")
{
auto world = World::create();
auto board = world->boards->add();
int16_t x = 0;
const int16_t y = 0;
for(auto r : {TileRotate::Deg0, TileRotate::Deg45, TileRotate::Deg90, TileRotate::Deg135})
{
REQUIRE(board->addTile(x, y, r, StraightRailTile::classId, false));
std::weak_ptr<Tile> straight = board->getTile({x, y});
REQUIRE_FALSE(straight.expired());
REQUIRE(straight.lock()->rotate == r);
REQUIRE(board->addTile(x, y, r - TileRotate::Deg45, StraightRailTile::classId, false));
REQUIRE(straight.expired());
std::weak_ptr<Tile> bridge = board->getTile({x, y});
REQUIRE_FALSE(bridge.expired());
REQUIRE(bridge.lock()->getClassId() == Bridge45LeftRailTile::classId);
REQUIRE(bridge.lock()->rotate == r);
x++;
}
}
TEST_CASE("Board: Merge tile, brigde 45 right", "[board][board-merge]")
{
auto world = World::create();
auto board = world->boards->add();
int16_t x = 0;
const int16_t y = 0;
for(auto r : {TileRotate::Deg0, TileRotate::Deg45, TileRotate::Deg90, TileRotate::Deg135})
{
REQUIRE(board->addTile(x, y, r, StraightRailTile::classId, false));
std::weak_ptr<Tile> straight = board->getTile({x, y});
REQUIRE_FALSE(straight.expired());
REQUIRE(straight.lock()->rotate == r);
REQUIRE(board->addTile(x, y, r + TileRotate::Deg45, StraightRailTile::classId, false));
REQUIRE(straight.expired());
std::weak_ptr<Tile> bridge = board->getTile({x, y});
REQUIRE_FALSE(bridge.expired());
REQUIRE(bridge.lock()->getClassId() == Bridge45RightRailTile::classId);
REQUIRE(bridge.lock()->rotate == r);
x++;
}
}