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) {
const isCollidingWith = this.entity.isCollidingWith;
for (let i = 0; i < isCollidingWith.length; ++i) {
const entity = isCollidingWith[i];
if (entity.is('vulnerable') && !entity.isInvulnerable) {
entity.takeDamageFrom(this.entity);
if (AVOCADO_SERVER) {
const isCollidingWith = this.entity.isCollidingWith;
for (let i = 0; i < isCollidingWith.length; ++i) {
const entity = isCollidingWith[i];
if (entity.is('vulnerable') && !entity.isInvulnerable) {
entity.takeDamageFrom(this.entity);
}
}
}
}

View File

@ -77,7 +77,7 @@ export class Vulnerable extends Trait {
else {
damage.from = undefined;
}
this.entity.emit('tookDamage', damage, 'client');
this.acceptDamage(damage);
}
break;
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() {
if (!this._isHydrating) {
return;
@ -129,27 +147,6 @@ export class Vulnerable extends Trait {
listeners() {
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: () => {
this._isInvulnerable = true;
},
@ -206,7 +203,7 @@ export class Vulnerable extends Trait {
from: entity.instanceUuid,
};
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({
THREE: 'three',
}));
config.plugins.push(new webpack.DefinePlugin({
AVOCADO_CLIENT: true,
AVOCADO_SERVER: false,
}));
module.exports = config;

View File

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