|
|
@@ -0,0 +1,187 @@
|
|
|
+#include "Orientation.h"
|
|
|
+
|
|
|
+#include "BlockFilter.h"
|
|
|
+#include "Constants.h"
|
|
|
+#include "Game.h"
|
|
|
+#include "Chunk.h"
|
|
|
+
|
|
|
+OrientationConfig::OrientationConfig()
|
|
|
+ : ReferenceCounter()
|
|
|
+{}
|
|
|
+
|
|
|
+AttachToNeighborOrientationConfig::AttachToNeighborOrientationConfig()
|
|
|
+ : OrientationConfig(),
|
|
|
+ validDirections(NO_DIRECTION),
|
|
|
+ filter(0)
|
|
|
+{}
|
|
|
+
|
|
|
+AttachToNeighborOrientationConfig::~AttachToNeighborOrientationConfig()
|
|
|
+{
|
|
|
+ if (filter)
|
|
|
+ {
|
|
|
+ filter->release();
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+Direction AttachToNeighborOrientationConfig::calculateOrientation(
|
|
|
+ Framework::Vec3<int> pos, int dimensionId, Chunk* zChunk) const
|
|
|
+{
|
|
|
+ for (int i = 0; i < 6; i++)
|
|
|
+ {
|
|
|
+ Direction direction = getDirectionFromIndex(i);
|
|
|
+ if ((validDirections & direction) != 0)
|
|
|
+ {
|
|
|
+ Framework::Vec3<int> neighborPos = pos + getDirection(direction);
|
|
|
+ if (neighborPos.z >= 0 && neighborPos.z < WORLD_HEIGHT
|
|
|
+ && Game::getChunkCenter(neighborPos.x, neighborPos.y) == zChunk->getCenter())
|
|
|
+ {
|
|
|
+ const Block* zNeighborBlock = zChunk->zBlockConstWC(
|
|
|
+ neighborPos.x, neighborPos.y, neighborPos.z);
|
|
|
+ if (zNeighborBlock && filter->test(zNeighborBlock))
|
|
|
+ {
|
|
|
+ return getOppositeDirection(direction);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return NORTH;
|
|
|
+}
|
|
|
+
|
|
|
+void AttachToNeighborOrientationConfig::setValidDirections(
|
|
|
+ Directions validDirections)
|
|
|
+{
|
|
|
+ this->validDirections = validDirections;
|
|
|
+}
|
|
|
+
|
|
|
+Directions AttachToNeighborOrientationConfig::getValidDirections() const
|
|
|
+{
|
|
|
+ return validDirections;
|
|
|
+}
|
|
|
+
|
|
|
+void AttachToNeighborOrientationConfig::setFilter(BlockFilter* filter)
|
|
|
+{
|
|
|
+ if (this->filter)
|
|
|
+ {
|
|
|
+ this->filter->release();
|
|
|
+ }
|
|
|
+ this->filter = filter;
|
|
|
+}
|
|
|
+
|
|
|
+BlockFilter* AttachToNeighborOrientationConfig::zFilter() const
|
|
|
+{
|
|
|
+ return filter;
|
|
|
+}
|
|
|
+
|
|
|
+AttachToNeighborOrientationConfigFactory::
|
|
|
+ AttachToNeighborOrientationConfigFactory()
|
|
|
+ : SubTypeFactory()
|
|
|
+{}
|
|
|
+
|
|
|
+AttachToNeighborOrientationConfig*
|
|
|
+AttachToNeighborOrientationConfigFactory::fromJson(
|
|
|
+ Framework::JSON::JSONObject* zJson) const
|
|
|
+{
|
|
|
+ AttachToNeighborOrientationConfig* result
|
|
|
+ = new AttachToNeighborOrientationConfig();
|
|
|
+ result->setFilter(Game::INSTANCE->zTypeRegistry()->fromJson<BlockFilter>(
|
|
|
+ zJson->zValue("condition")));
|
|
|
+ for (Framework::JSON::JSONValue* direction :
|
|
|
+ *zJson->zValue("directions")->asArray())
|
|
|
+ {
|
|
|
+ Framework::Text dirStr = direction->asString()->getString();
|
|
|
+ if (dirStr.isEqual("TOP"))
|
|
|
+ {
|
|
|
+ result->setValidDirections(
|
|
|
+ result->getValidDirections() | Direction::TOP);
|
|
|
+ }
|
|
|
+ else if (dirStr.isEqual("BOTTOM"))
|
|
|
+ {
|
|
|
+ result->setValidDirections(
|
|
|
+ result->getValidDirections() | Direction::BOTTOM);
|
|
|
+ }
|
|
|
+ else if (dirStr.isEqual("EAST"))
|
|
|
+ {
|
|
|
+ result->setValidDirections(
|
|
|
+ result->getValidDirections() | Direction::EAST);
|
|
|
+ }
|
|
|
+ else if (dirStr.isEqual("NORTH"))
|
|
|
+ {
|
|
|
+ result->setValidDirections(
|
|
|
+ result->getValidDirections() | Direction::NORTH);
|
|
|
+ }
|
|
|
+ else if (dirStr.isEqual("WEST"))
|
|
|
+ {
|
|
|
+ result->setValidDirections(
|
|
|
+ result->getValidDirections() | Direction::WEST);
|
|
|
+ }
|
|
|
+ else if (dirStr.isEqual("SOUTH"))
|
|
|
+ {
|
|
|
+ result->setValidDirections(
|
|
|
+ result->getValidDirections() | Direction::SOUTH);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+}
|
|
|
+
|
|
|
+Framework::JSON::JSONObject*
|
|
|
+AttachToNeighborOrientationConfigFactory::toJsonObject(
|
|
|
+ AttachToNeighborOrientationConfig* zObject) const
|
|
|
+{
|
|
|
+ Framework::JSON::JSONObject* result = new Framework::JSON::JSONObject();
|
|
|
+ result->addValue("condition",
|
|
|
+ Game::INSTANCE->zTypeRegistry()->toJson(zObject->zFilter()));
|
|
|
+ Framework::JSON::JSONArray* directionsArray
|
|
|
+ = new Framework::JSON::JSONArray();
|
|
|
+ if (zObject->getValidDirections() & Direction::TOP)
|
|
|
+ {
|
|
|
+ directionsArray->addValue(new Framework::JSON::JSONString("TOP"));
|
|
|
+ }
|
|
|
+ if (zObject->getValidDirections() & Direction::BOTTOM)
|
|
|
+ {
|
|
|
+ directionsArray->addValue(new Framework::JSON::JSONString("BOTTOM"));
|
|
|
+ }
|
|
|
+ if (zObject->getValidDirections() & Direction::EAST)
|
|
|
+ {
|
|
|
+ directionsArray->addValue(new Framework::JSON::JSONString("EAST"));
|
|
|
+ }
|
|
|
+ if (zObject->getValidDirections() & Direction::NORTH)
|
|
|
+ {
|
|
|
+ directionsArray->addValue(new Framework::JSON::JSONString("NORTH"));
|
|
|
+ }
|
|
|
+ if (zObject->getValidDirections() & Direction::WEST)
|
|
|
+ {
|
|
|
+ directionsArray->addValue(new Framework::JSON::JSONString("WEST"));
|
|
|
+ }
|
|
|
+ if (zObject->getValidDirections() & Direction::SOUTH)
|
|
|
+ {
|
|
|
+ directionsArray->addValue(new Framework::JSON::JSONString("SOUTH"));
|
|
|
+ }
|
|
|
+ result->addValue("directions", directionsArray);
|
|
|
+ return result;
|
|
|
+}
|
|
|
+
|
|
|
+JSONObjectValidationBuilder*
|
|
|
+AttachToNeighborOrientationConfigFactory::addToValidator(
|
|
|
+ JSONObjectValidationBuilder* builder) const
|
|
|
+{
|
|
|
+ Framework::JSON::JSONArray* defaultDirectionsArray
|
|
|
+ = new Framework::JSON::JSONArray();
|
|
|
+ defaultDirectionsArray->addValue(new Framework::JSON::JSONString("EAST"));
|
|
|
+ defaultDirectionsArray->addValue(new Framework::JSON::JSONString("NORTH"));
|
|
|
+ defaultDirectionsArray->addValue(new Framework::JSON::JSONString("WEST"));
|
|
|
+ defaultDirectionsArray->addValue(new Framework::JSON::JSONString("SOUTH"));
|
|
|
+ return builder
|
|
|
+ ->withRequiredAttribute("condition",
|
|
|
+ Game::INSTANCE->zTypeRegistry()->getValidator<BlockFilter>())
|
|
|
+ ->withRequiredArray("directions")
|
|
|
+ ->addAcceptedStringInArray()
|
|
|
+ ->whichIsOneOf({"TOP", "BOTTOM", "EAST", "NORTH", "WEST", "SOUTH"})
|
|
|
+ ->finishString()
|
|
|
+ ->withDefault(defaultDirectionsArray)
|
|
|
+ ->finishArray();
|
|
|
+}
|
|
|
+
|
|
|
+const char* AttachToNeighborOrientationConfigFactory::getTypeToken() const
|
|
|
+{
|
|
|
+ return "attachToNeighbor";
|
|
|
+}
|