|
|
@@ -1,5 +1,6 @@
|
|
|
#include "Entity.h"
|
|
|
|
|
|
+#include <Mat4.h>
|
|
|
#include <Text.h>
|
|
|
|
|
|
#include "BlockType.h"
|
|
|
@@ -443,6 +444,8 @@ void Entity::calculateTarget(Framework::Vec3<float> basePos,
|
|
|
int pz = (int)floor(headPosition.z);
|
|
|
direction.normalize();
|
|
|
Direction dir = BOTTOM;
|
|
|
+ bool found = false;
|
|
|
+ bool changed = false;
|
|
|
while (true)
|
|
|
{
|
|
|
if (getDefaultBlock(
|
|
|
@@ -450,13 +453,10 @@ void Entity::calculateTarget(Framework::Vec3<float> basePos,
|
|
|
Framework::Vec3<int>{px, py, pz}, dimensionId, 0))
|
|
|
->isInteractable(zItem))
|
|
|
{
|
|
|
+ found = true;
|
|
|
if (!target || !target->isBlock({px, py, pz}, dir))
|
|
|
{
|
|
|
- cs.lock();
|
|
|
- delete target;
|
|
|
- target = new ActionTarget({px, py, pz}, dir);
|
|
|
- cs.unlock();
|
|
|
- onTargetChange();
|
|
|
+ changed = true;
|
|
|
}
|
|
|
break;
|
|
|
}
|
|
|
@@ -540,6 +540,29 @@ void Entity::calculateTarget(Framework::Vec3<float> basePos,
|
|
|
}
|
|
|
}
|
|
|
if (target)
|
|
|
+ {
|
|
|
+ changed = true;
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ float distSq = Framework::Vec3<float>((float)px, (float)py, (float)pz)
|
|
|
+ .abstandSq(headPosition);
|
|
|
+ Entity* zte = Game::INSTANCE->zDimension(dimensionId)
|
|
|
+ ->zTarget(headPosition, direction, distSq);
|
|
|
+ if (zte)
|
|
|
+ {
|
|
|
+ if (!target || !target->isEntity(zte->getId()))
|
|
|
+ {
|
|
|
+ cs.lock();
|
|
|
+ delete target;
|
|
|
+ target = new ActionTarget(zte->getId());
|
|
|
+ cs.unlock();
|
|
|
+ onTargetChange();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else if (changed)
|
|
|
+ {
|
|
|
+ if (target && !found)
|
|
|
{
|
|
|
cs.lock();
|
|
|
delete target;
|
|
|
@@ -547,7 +570,14 @@ void Entity::calculateTarget(Framework::Vec3<float> basePos,
|
|
|
cs.unlock();
|
|
|
onTargetChange();
|
|
|
}
|
|
|
- break;
|
|
|
+ else
|
|
|
+ {
|
|
|
+ cs.lock();
|
|
|
+ delete target;
|
|
|
+ target = new ActionTarget({px, py, pz}, dir);
|
|
|
+ cs.unlock();
|
|
|
+ onTargetChange();
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -922,3 +952,94 @@ void Entity::setRemoved()
|
|
|
{
|
|
|
removed = true;
|
|
|
}
|
|
|
+
|
|
|
+double Entity::getHitDistance(
|
|
|
+ Framework::Vec3<float> rayOrigin, Framework::Vec3<float> rayDirection) const
|
|
|
+{
|
|
|
+ Framework::Mat4<float> rotMat
|
|
|
+ = Framework::Mat4<float>::rotationX(-rotation.x)
|
|
|
+ * Framework::Mat4<float>::rotationY(-rotation.y)
|
|
|
+ * Framework::Mat4<float>::rotationZ(-rotation.z);
|
|
|
+ Framework::Vec3<float> rotatedRayOrigin = rotMat * rayOrigin;
|
|
|
+ Framework::Vec3<float> rotatedRayDirection = rotMat * rayDirection;
|
|
|
+ rotatedRayDirection.normalize();
|
|
|
+ if (rotatedRayDirection.x != 0)
|
|
|
+ {
|
|
|
+ float d;
|
|
|
+ if (rotatedRayDirection.x > 0)
|
|
|
+ {
|
|
|
+ float border = getPosition().x - boundingBox.x;
|
|
|
+ d = (border - rotatedRayOrigin.x) / rotatedRayDirection.x;
|
|
|
+ }
|
|
|
+ else if (rotatedRayDirection.x < 0)
|
|
|
+ {
|
|
|
+ float border = getPosition().x + boundingBox.x;
|
|
|
+ d = (border - rotatedRayOrigin.x) / rotatedRayDirection.x;
|
|
|
+ }
|
|
|
+ if (d > 0)
|
|
|
+ {
|
|
|
+ Framework::Vec3<float> hitPoint
|
|
|
+ = rotatedRayOrigin + rotatedRayDirection * d;
|
|
|
+ if (hitPoint.y >= getPosition().y - boundingBox.y
|
|
|
+ && hitPoint.y <= getPosition().y + boundingBox.y
|
|
|
+ && hitPoint.z >= getPosition().z - boundingBox.z
|
|
|
+ && hitPoint.z <= getPosition().z + boundingBox.z)
|
|
|
+ {
|
|
|
+ return d;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (rotatedRayDirection.y != 0)
|
|
|
+ {
|
|
|
+ float d;
|
|
|
+ if (rotatedRayDirection.y > 0)
|
|
|
+ {
|
|
|
+ float border = getPosition().y - boundingBox.y;
|
|
|
+ d = (border - rotatedRayOrigin.y) / rotatedRayDirection.y;
|
|
|
+ }
|
|
|
+ else if (rotatedRayDirection.y < 0)
|
|
|
+ {
|
|
|
+ float border = getPosition().y + boundingBox.y;
|
|
|
+ d = (border - rotatedRayOrigin.y) / rotatedRayDirection.y;
|
|
|
+ }
|
|
|
+ if (d > 0)
|
|
|
+ {
|
|
|
+ Framework::Vec3<float> hitPoint
|
|
|
+ = rotatedRayOrigin + rotatedRayDirection * d;
|
|
|
+ if (hitPoint.x >= getPosition().x - boundingBox.x
|
|
|
+ && hitPoint.x <= getPosition().x + boundingBox.x
|
|
|
+ && hitPoint.z >= getPosition().z - boundingBox.z
|
|
|
+ && hitPoint.z <= getPosition().z + boundingBox.z)
|
|
|
+ {
|
|
|
+ return d;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (rotatedRayDirection.z != 0)
|
|
|
+ {
|
|
|
+ float d;
|
|
|
+ if (rotatedRayDirection.z > 0)
|
|
|
+ {
|
|
|
+ float border = getPosition().z - boundingBox.z;
|
|
|
+ d = (border - rotatedRayOrigin.z) / rotatedRayDirection.z;
|
|
|
+ }
|
|
|
+ else if (rotatedRayDirection.z < 0)
|
|
|
+ {
|
|
|
+ float border = getPosition().z + boundingBox.z;
|
|
|
+ d = (border - rotatedRayOrigin.z) / rotatedRayDirection.z;
|
|
|
+ }
|
|
|
+ if (d > 0)
|
|
|
+ {
|
|
|
+ Framework::Vec3<float> hitPoint
|
|
|
+ = rotatedRayOrigin + rotatedRayDirection * d;
|
|
|
+ if (hitPoint.x >= getPosition().x - boundingBox.x
|
|
|
+ && hitPoint.x <= getPosition().x + boundingBox.x
|
|
|
+ && hitPoint.y >= getPosition().y - boundingBox.y
|
|
|
+ && hitPoint.y <= getPosition().y + boundingBox.y)
|
|
|
+ {
|
|
|
+ return d;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return NAN;
|
|
|
+}
|