fix: client/server damage

This commit is contained in:
cha0s 2019-04-22 21:03:16 -05:00
parent 809c4647e4
commit fd6cfd51c1
4 changed files with 35 additions and 29 deletions

View File

@ -35,11 +35,13 @@ export class Damaging extends Trait {
} }
tick(elapsed) { tick(elapsed) {
const isCollidingWith = this.entity.isCollidingWith; if (AVOCADO_SERVER) {
for (let i = 0; i < isCollidingWith.length; ++i) { const isCollidingWith = this.entity.isCollidingWith;
const entity = isCollidingWith[i]; for (let i = 0; i < isCollidingWith.length; ++i) {
if (entity.is('vulnerable') && !entity.isInvulnerable) { const entity = isCollidingWith[i];
entity.takeDamageFrom(this.entity); if (entity.is('vulnerable') && !entity.isInvulnerable) {
entity.takeDamageFrom(this.entity);
}
} }
} }
} }

View File

@ -77,7 +77,7 @@ export class Vulnerable extends Trait {
else { else {
damage.from = undefined; damage.from = undefined;
} }
this.entity.emit('tookDamage', damage, 'client'); this.acceptDamage(damage);
} }
break; break;
default: default:
@ -86,6 +86,24 @@ export class Vulnerable extends Trait {
} }
} }
acceptDamage(damage) {
const context = createContext();
context.add('entity', this.entity);
context.add('damage', damage);
const actions = behaviorItemFromJSON(
this._tookDamageActionsJSON
);
const tuple = {
context,
actions,
};
this.tookDamageActions.push(tuple);
actions.on('actionsFinished', () => {
const index = this.tookDamageActions.indexOf(tuple);
this.tookDamageActions.splice(tuple);
});
}
addEmitter() { addEmitter() {
if (!this._isHydrating) { if (!this._isHydrating) {
return; return;
@ -129,27 +147,6 @@ export class Vulnerable extends Trait {
listeners() { listeners() {
return { return {
tookDamage: (damage, source) => {
if ('server' === source) {
return;
}
const context = createContext();
context.add('entity', this.entity);
context.add('damage', damage);
const actions = behaviorItemFromJSON(
this._tookDamageActionsJSON
);
const tuple = {
context,
actions,
};
this.tookDamageActions.push(tuple);
actions.on('actionsFinished', () => {
const index = this.tookDamageActions.indexOf(tuple);
this.tookDamageActions.splice(tuple);
});
},
dying: () => { dying: () => {
this._isInvulnerable = true; this._isInvulnerable = true;
}, },
@ -206,7 +203,7 @@ export class Vulnerable extends Trait {
from: entity.instanceUuid, from: entity.instanceUuid,
}; };
this.damageList[entity.instanceUuid].push(damage); this.damageList[entity.instanceUuid].push(damage);
this.entity.emit('tookDamage', damage, 'server'); this.entity.emit('tookDamage', damage);
} }
}, },

View File

@ -52,5 +52,9 @@ config.plugins.push(new HtmlWebpackPlugin({
config.plugins.push(new webpack.ProvidePlugin({ config.plugins.push(new webpack.ProvidePlugin({
THREE: 'three', THREE: 'three',
})); }));
config.plugins.push(new webpack.DefinePlugin({
AVOCADO_CLIENT: true,
AVOCADO_SERVER: false,
}));
module.exports = config; module.exports = config;

View File

@ -37,7 +37,10 @@ config.plugins.push(new StartServerPlugin({
nodeArgs: nodeArgs, nodeArgs: nodeArgs,
signal: true, signal: true,
})); }));
config.plugins.push(new webpack.DefinePlugin({
AVOCADO_CLIENT: false,
AVOCADO_SERVER: true,
}));
config.target = 'node'; config.target = 'node';
module.exports = config; module.exports = config;