ULTIMA8: Update box struct better match rect methods

This commit is contained in:
Matthew Jimenez 2023-01-18 21:54:07 -06:00
parent 0d5d55b973
commit 2e11704ed9
3 changed files with 42 additions and 53 deletions

View file

@ -35,65 +35,54 @@ struct Box {
Box(int nx, int ny, int nz, int nxd, int nyd, int nzd)
: _x(nx), _y(ny), _z(nz), _xd(nxd), _yd(nyd), _zd(nzd) {}
void Set(int nx, int ny, int nz, int nxd, int nyd, int nzd) {
_x = nx;
_y = ny;
_z = nz;
_xd = nxd;
_yd = nyd;
_zd = nzd;
}
void Set(Box &o) {
*this = o;
}
// Check to see if a Box is 'valid'
bool IsValid() const {
// Check if the Box is empty (its width, height, or depth is 0) or invalid (its width, height, or depth are negative).
bool isEmpty() const {
return _xd > 0 && _yd > 0 && _zd > 0;
}
// Check to see if a Box is 'valid'
bool isValid() const {
return _xd >= 0 && _yd >= 0 && _zd >= 0;
}
// Check to see if a point is within the Box
bool InBox(int px, int py, int pz) const {
bool contains(int px, int py, int pz) const {
return (px > (_x - _xd) && py > (_y - _yd) && pz >= _z &&
px <= _x && py <= _y && pz < (_z + _zd));
}
// Move the Box (Relative)
void MoveRel(int32 dx, int32 dy, int32 dz) {
void translate(int32 dx, int32 dy, int32 dz) {
_x += dx;
_y += dy;
_z += dz;
}
// Move the Box (Absolute)
void MoveAbs(int32 nx, int32 ny, int32 nz) {
void moveTo(int32 nx, int32 ny, int32 nz) {
_x = nx;
_y = ny;
_z = nz;
}
// Resize the Box (Relative)
void ResizeRel(int32 dxd, int32 dyd, int32 dzd) {
_xd += dxd;
_yd += dyd;
_zd += dzd;
}
// Resize the Box (Absolute)
void ResizeAbs(int32 nxd, int32 nyd, int32 nzd) {
void resize(int32 nxd, int32 nyd, int32 nzd) {
_xd = nxd;
_yd = nyd;
_zd = nzd;
}
bool Overlaps(const Box &o) const {
bool overlaps(const Box &o) const {
if (_x <= o._x - o._xd || o._x <= _x - _xd) return false;
if (_y <= o._y - o._yd || o._y <= _y - _yd) return false;
if (_z + _zd <= o._z || o._z + o._zd <= _z) return false;
return true;
}
bool operator == (const Box &o) const {
bool operator==(const Box &rhs) const { return equals(rhs); }
bool operator!=(const Box &rhs) const { return !equals(rhs); }
bool equals(const Box &o) const {
return (_x == o._x && _y == o._y && _z == o._z &&
_xd == o._xd && _yd == o._yd && _zd == o._zd);
}

View file

@ -462,8 +462,8 @@ void AnimationTracker::checkWeaponHit() {
Box abox = a->getWorldBox();
abox.MoveAbs(_x, _y, _z);
abox.MoveRel(Direction_XFactor(_dir) * 32 * range, Direction_YFactor(_dir) * 32 * range, 0);
abox.moveTo(_x, _y, _z);
abox.translate(Direction_XFactor(_dir) * 32 * range, Direction_YFactor(_dir) * 32 * range, 0);
#ifdef WATCHACTOR
if (a->getObjId() == watchactor) {
@ -489,7 +489,7 @@ void AnimationTracker::checkWeaponHit() {
Box ibox = item->getWorldBox();
if (abox.Overlaps(ibox)) {
if (abox.overlaps(ibox)) {
hit = itemid;
#ifdef WATCHACTOR
if (a->getObjId() == watchactor) {

View file

@ -11,39 +11,39 @@ class U8BoxTestSuite : public CxxTest::TestSuite {
void test_simple_box() {
Ultima::Ultima8::Box box;
TS_ASSERT(!box.InBox(0,0,1));
TS_ASSERT(!box.InBox(0,1,0));
TS_ASSERT(!box.InBox(1,0,0));
box.ResizeRel(1,1,1);
TS_ASSERT(!box.InBox(-1,0,0));
TS_ASSERT(!box.InBox(0,-1,0));
TS_ASSERT(!box.InBox(0,0,-1));
TS_ASSERT(box.Overlaps(box));
TS_ASSERT(!box.contains(0,0,1));
TS_ASSERT(!box.contains(0, 1, 0));
TS_ASSERT(!box.contains(1, 0, 0));
box.resize(1, 1, 1);
TS_ASSERT(!box.contains(-1, 0, 0));
TS_ASSERT(!box.contains(0, -1, 0));
TS_ASSERT(!box.contains(0, 0, -1));
TS_ASSERT(box.overlaps(box));
TS_ASSERT(box == box);
box.ResizeRel(1,1,1);
TS_ASSERT(box.IsValid());
TS_ASSERT(box.Overlaps(box));
box.resize(2, 2, 2);
TS_ASSERT(box.isValid());
TS_ASSERT(box.overlaps(box));
TS_ASSERT(box == box);
// Note: These tests expect Box has reversed coordinates in x and y.
TS_ASSERT(box.InBox(-1,-1,1));
TS_ASSERT(box.InBox(-1,-1,0));
box.MoveRel(0, 0, 1);
TS_ASSERT(!box.InBox(-1,-1,0));
TS_ASSERT(box.InBox(-1,-1,2));
TS_ASSERT(box.contains(-1, -1, 1));
TS_ASSERT(box.contains(-1, -1, 0));
box.moveTo(0, 0, 1);
TS_ASSERT(!box.contains(-1, -1, 0));
TS_ASSERT(box.contains(-1, -1, 2));
Ultima::Ultima8::Box box2(box);
TS_ASSERT(box == box2);
TS_ASSERT(box.Overlaps(box2));
TS_ASSERT(box.overlaps(box2));
TS_ASSERT(box2 == box);
TS_ASSERT(box2.Overlaps(box));
TS_ASSERT(box2.overlaps(box));
Ultima::Ultima8::Box box3(0, 0, 0, 2, 2, 3);
TS_ASSERT(!(box2 == box3));
TS_ASSERT(box2.Overlaps(box3));
TS_ASSERT(box3.Overlaps(box2));
box3.ResizeAbs(1,1,1);
TS_ASSERT(!box3.Overlaps(box2));
TS_ASSERT(box2 != box3);
TS_ASSERT(box2.overlaps(box3));
TS_ASSERT(box3.overlaps(box2));
box3.resize(1, 1, 1);
TS_ASSERT(!box3.overlaps(box2));
}
};