Skip to content

Instantly share code, notes, and snippets.

@Rseding91
Created May 24, 2018 04:02
Show Gist options
  • Save Rseding91/8a28c5196ca9aabaeaba5e72fcb54d88 to your computer and use it in GitHub Desktop.
Save Rseding91/8a28c5196ca9aabaeaba5e72fcb54d88 to your computer and use it in GitHub Desktop.
Vector TransportBelt::calculateEntityMovement(MapPosition oldPosition) const
{
RotationDefinition rotationDefinition = this->rotationPoint;
if (rotationDefinition.empty())
return Vector(this->getPrototype()->speed, this->direction);
Vector originalVector(rotationDefinition.center, oldPosition);
double distance = originalVector.distance();
double orientation = originalVector.getOrientation().getFraction();
if (rotationDefinition.orientation == RotationDefinition::Orientation::ClockWise)
{
orientation += 0.35 * this->getPrototype()->speed * (2 - distance);
if (orientation > rotationDefinition.maxOrientation)
{
orientation = rotationDefinition.maxOrientation;
Vector finalVector(distance, RealOrientation(orientation));
finalVector += Vector(0.02, this->direction); // move to another belt
finalVector -= originalVector;
return finalVector;
}
}
else
{
orientation -= 0.35 * this->getPrototype()->speed * (2 - distance);
if (orientation < rotationDefinition.maxOrientation)
{
orientation = rotationDefinition.maxOrientation;
Vector finalVector(distance, RealOrientation(orientation));
finalVector += Vector(0.02, this->direction); // move to another belt
finalVector -= originalVector;
return finalVector;
}
}
Vector finalVector(distance, RealOrientation(orientation));
finalVector -= originalVector;
return finalVector;
}
TransportBelt::RotationDefinition TransportBelt::getRotationPoint() const
{
switch (this->direction)
{
case Direction::North:
{
if (this->connections.south && this->connections.south->direction == Direction::North)
return RotationDefinition();
bool leftActive = this->connections.west && this->connections.west->direction == Direction::East;
bool rightActive = this->connections.east && this->connections.east->direction == Direction::West;
if (leftActive == rightActive)
return RotationDefinition();
if (leftActive)
/*
* ^
* /|\
* |
* ----+
*/
return RotationDefinition(this->position.x - 0.5, this->position.y - 0.5, RotationDefinition::Orientation::CounterClockWise, 0.25);
else
/*
* ^
* /|\
* |
* +----
*/
return RotationDefinition(this->position.x + 0.5, this->position.y - 0.5, RotationDefinition::Orientation::ClockWise, 0.75);
}
case Direction::South:
{
if (this->connections.north && this->connections.north->direction == Direction::South)
return RotationDefinition();
bool leftActive = this->connections.west && this->connections.west->direction == Direction::East;
bool rightActive = this->connections.east && this->connections.east->direction == Direction::West;
if (leftActive == rightActive)
return RotationDefinition();
if (leftActive)
/*
* ----+
* |
* \|/
* V
*/
return RotationDefinition(this->position.x - 0.5, this->position.y + 0.5, RotationDefinition::Orientation::ClockWise, 0.25);
else
/*
* +----
* |
* \|/
* V
*/
return RotationDefinition(this->position.x + 0.5, this->position.y + 0.5, RotationDefinition::Orientation::CounterClockWise, 0.75);
break;
}
case Direction::East:
{
if (this->connections.west && this->connections.west->direction == Direction::East)
return RotationDefinition();
bool topActive = this->connections.north && this->connections.north->direction == Direction::South;
bool bottomActive = this->connections.south && this->connections.south->direction == Direction::North;
if (topActive == bottomActive)
return RotationDefinition();
if (topActive)
/*
* |
* | \
* +----
* /
*/
return RotationDefinition(this->position.x + 0.5, this->position.y - 0.5, RotationDefinition::Orientation::CounterClockWise, 0.5);
else
/*
* \
* +-----
* | /
* |
*/
return RotationDefinition(this->position.x + 0.5, this->position.y + 0.5, RotationDefinition::Orientation::ClockWise, 1);
break;
}
case Direction::West:
{
if (this->connections.east && this->connections.east->direction == Direction::West)
return RotationDefinition();
bool topActive = this->connections.north && this->connections.north->direction == Direction::South;
bool bottomActive = this->connections.south && this->connections.south->direction == Direction::North;
if (topActive == bottomActive)
return RotationDefinition();
if (topActive)
/*
* |
* / |
* ----+
* \
*/
return RotationDefinition(this->position.x - 0.5, this->position.y - 0.5, RotationDefinition::Orientation::ClockWise, 0.5);
else // if (bottomActive)
/*
* /
* ----+
* \ |
* |
*/
return RotationDefinition(this->position.x - 0.5, this->position.y + 0.5, RotationDefinition::Orientation::CounterClockWise, 0);
break;
}
default: LOG_AND_ABORT("wrong transport belt direction: %u.", this->direction.getIndex());
}
return RotationDefinition();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment