Ver código fonte

chore: refactored the portal component so that other scripts can use the same function to move objects to other zones

tags/v0.9.0^2
Shaun 3 anos atrás
pai
commit
f00b194a79
2 arquivos alterados com 57 adições e 43 exclusões
  1. +11
    -43
      src/server/components/portal.js
  2. +46
    -0
      src/server/components/portal/sendObjToZone.js

+ 11
- 43
src/server/components/portal.js Ver arquivo

@@ -1,4 +1,6 @@
let roles = require('../config/roles');
const roles = require('../config/roles');

const sendObjToZone = require('./portal/sendObjToZone');

module.exports = {
type: 'portal',
@@ -17,55 +19,21 @@ module.exports = {
},

collisionEnter: async function (obj) {
const { player, instance: { physics, syncer: globalSyncer } } = obj;

if (!player)
if (!obj.player)
return;
else if (this.patronLevel) {
if (!roles.isRoleLevel(obj, this.patronLevel, 'enter this area'))
return;
}

if (obj.zoneName === this.toZone) {
physics.removeObject(obj, obj.x, obj.y);

obj.x = this.toPos.x;
obj.y = this.toPos.y;

physics.addObject(obj, obj.x, obj.y);

globalSyncer.queue('onRespawn', {
x: obj.x,
y: obj.y
}, [obj.serverId]);

return;
}

obj.fireEvent('beforeRezone');

obj.destroyed = true;

await obj.auth.doSave();

const simpleObj = obj.getSimple(true, false, true);

const { toPos, toRelativePos } = this;
if (toPos) {
simpleObj.x = this.toPos.x;
simpleObj.y = this.toPos.y;
} else if (toRelativePos) {
simpleObj.x = this.obj.x + toRelativePos.x;
simpleObj.y = this.obj.y + toRelativePos.y;
}
const { toZone: zoneName, toPos, toRelativePos } = this;

process.send({
method: 'rezone',
id: obj.serverId,
args: {
obj: simpleObj,
newZone: this.toZone
}
await sendObjToZone({
obj,
invokingObj: this,
zoneName,
toPos,
toRelativePos
});
}
};

+ 46
- 0
src/server/components/portal/sendObjToZone.js Ver arquivo

@@ -0,0 +1,46 @@
const sendObjToZone = async ({ obj, invokingObj, zoneName, toPos, toRelativePos }) => {
const { instance: { physics, syncer: globalSyncer } } = obj;

if (obj.zoneName === zoneName) {
physics.removeObject(obj, obj.x, obj.y);

obj.x = toPos.x;
obj.y = toPos.y;

physics.addObject(obj, obj.x, obj.y);

globalSyncer.queue('onRespawn', {
x: obj.x,
y: obj.y
}, [obj.serverId]);

return;
}

obj.fireEvent('beforeRezone');

obj.destroyed = true;

await obj.auth.doSave();

const simpleObj = obj.getSimple(true, false, true);

if (toPos) {
simpleObj.x = toPos.x;
simpleObj.y = toPos.y;
} else if (toRelativePos) {
simpleObj.x = invokingObj.obj.x + toRelativePos.x;
simpleObj.y = invokingObj.obj.y + toRelativePos.y;
}

process.send({
method: 'rezone',
id: obj.serverId,
args: {
obj: simpleObj,
newZone: zoneName
}
});
};

module.exports = sendObjToZone;

Carregando…
Cancelar
Salvar