Browse Source

bug #1998: Fixed regions firing collisionExit and refiring collisionEnter when not needed

1993-leagues
Shaun 8 months ago
parent
commit
a6bfc8ecaa
3 changed files with 66 additions and 36 deletions
  1. +0
    -10
      src/server/components/notice.js
  2. +38
    -20
      src/server/objects/objBase.js
  3. +28
    -6
      src/server/world/physics.js

+ 0
- 10
src/server/components/notice.js View File

@@ -8,8 +8,6 @@ module.exports = {

maxLevel: 0,

contents: [],

init: function (blueprint) {
this.msg = blueprint.msg;
this.msgFunction = blueprint.msgFunction;
@@ -20,10 +18,6 @@ module.exports = {
this.syncer = this.obj.instance.syncer;
},

destroy: function () {
this.contents.forEach(c => this.collisionExit(c));
},

callAction: function (obj, actionName) {
let action = this.actions[actionName];
if (!action)
@@ -61,8 +55,6 @@ module.exports = {
else if ((this.maxLevel) && (obj.stats.values.level > this.maxLevel))
return;

this.contents.push(obj);

this.callAction(obj, 'enter');

if (!this.msg && !this.msgFunction)
@@ -100,8 +92,6 @@ module.exports = {
return;
}

this.contents.spliceWhere(c => (c === obj));

this.callAction(obj, 'exit');

if (!this.msg)


+ 38
- 20
src/server/objects/objBase.js View File

@@ -7,6 +7,8 @@ module.exports = {

eventListeners: [],

collisionContents: undefined,

addComponent: function (type, blueprint, isTransfer) {
let cpn = this[type];
if (!cpn) {
@@ -326,32 +328,49 @@ module.exports = {
},

collisionEnter: function (obj) {
let cpns = this.components;
let cLen = cpns.length;
if (this.collisionContents === undefined)
this.collisionContents = [];

const { collisionContents, components: cpns } = this;

if (collisionContents.includes(obj.id))
return;

collisionContents.push(obj.id);

const cLen = cpns.length;
for (let i = 0; i < cLen; i++) {
let c = cpns[i];
if (c.collisionEnter) {
if (c.collisionEnter(obj))
return true;
}
const c = cpns[i];
if (c.collisionEnter && c.collisionEnter(obj))
return true;
}
},

collisionStay: function (obj) {
let cpns = this.components;
let cLen = cpns.length;
const { collisionContents, components: cpns } = this;

if (!collisionContents.includes(obj.id))
return;

const cLen = cpns.length;
for (let i = 0; i < cLen; i++) {
let c = cpns[i];
const c = cpns[i];
if (c.collisionStay)
c.collisionStay(obj);
}
},

collisionExit: function (obj) {
let cpns = this.components;
let cLen = cpns.length;
const { collisionContents, components: cpns } = this;

if (!collisionContents.includes(obj.id))
return;

collisionContents.spliceWhere(c => c === obj.id);

const cLen = cpns.length;
for (let i = 0; i < cLen; i++) {
let c = cpns[i];
const c = cpns[i];
if (c.collisionExit)
c.collisionExit(obj);
}
@@ -404,13 +423,12 @@ module.exports = {
},

destroy: function () {
let cpns = this.components;
let len = cpns.length;
for (let i = 0; i < len; i++) {
let c = cpns[i];
if (c.destroy)
c.destroy();
}
const { components: cpns, collisionContents } = this;

if (collisionContents)
collisionContents.forEach(c => this.collisionExit(c));

cpns.forEach(c => c.destroy && c.destroy());
},

toString: function () {


+ 28
- 6
src/server/world/physics.js View File

@@ -25,7 +25,7 @@ module.exports = {
});
},

addRegion: function (obj) {
addRegion: function (obj, oldPosition) {
let lowX = obj.x;
let lowY = obj.y;
let highX = lowX + obj.width;
@@ -44,8 +44,19 @@ module.exports = {
for (let k = 0; k < cLen; k++) {
let c = cell[k];

c.collisionEnter(obj);
obj.collisionEnter(c);
//Only remove if the old position didn't contain the object
if (
!oldPosition ||
(
oldPosition.x + oldPosition.width <= c.x ||
oldPosition.x > c.x ||
oldPosition.y + oldPosition.height <= c.y ||
oldPosition.y > c.y
)
) {
c.collisionEnter(obj);
obj.collisionEnter(c);
}
}

cell.push(obj);
@@ -53,7 +64,7 @@ module.exports = {
}
},

removeRegion: function (obj) {
removeRegion: function (obj, newPosition) {
let oId = obj.id;

let lowX = obj.x;
@@ -75,8 +86,19 @@ module.exports = {
let c = cell[k];

if (c.id !== oId) {
c.collisionExit(obj);
obj.collisionExit(c);
//Only remove if the new position won't still contain the object
if (
!newPosition ||
(
newPosition.x + newPosition.width <= c.x ||
newPosition.x > c.x ||
newPosition.y + newPosition.height <= c.y ||
newPosition.y > c.y
)
) {
c.collisionExit(obj);
obj.collisionExit(c);
}
} else {
cell.splice(k, 1);
k--;


Loading…
Cancel
Save