Skip to content

Commit

Permalink
simulation moving to es6 in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
nikitaosyak committed Aug 2, 2016
1 parent 8a0779e commit d71052a
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 84 deletions.
4 changes: 2 additions & 2 deletions client/src/js/network/ClientRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export class ClientRouter extends RouterBase {
Facade.networkState.removePlayerById(deadId);
}
}
Facade.simulation.deleteClient(deadId);
Facade.simulation.deletePlayer(deadId);
}
}

Expand All @@ -93,7 +93,7 @@ export class ClientRouter extends RouterBase {
// Facade.networkState.removePlayerById(deadId);
if (leftClientId in Facade.networkState.players) {
Facade.networkState.removePlayerById(leftClientId);
Facade.simulation.deleteClient(leftClientId);
Facade.simulation.deletePlayer(leftClientId);
} else {
// do nothing, i guess
}
Expand Down
2 changes: 1 addition & 1 deletion client/src/js/utils/Input.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ Input.prototype = {
ts = now - this._lastVelStarted;
}
Facade.connection.rpc.velocity(this._velocity, ts);
Facade.simulation.addStreamAction(Date.now(), 0, Facade.myId, this._velocity.x, this._velocity.y, ts);
Facade.simulation.stream.addAction(Facade.myId, Date.now(), 0, this._velocity.x, this._velocity.y, ts);
this._lastX = this._velocity.x;
this._lastY = this._velocity.y;

Expand Down
12 changes: 4 additions & 8 deletions server/src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,15 @@ require('ws').createServer({host: '0.0.0.0', port:3000}, function(socket) {
client.send(JSON.stringify({id:"p_ack", time:elapsed}));
break;
case 'vd': // for 'VELOCITY DIFF'
simulation.addStreamAction(
elapsed, client.lag, client.id, m.x, m.y, m.dt
);
simulation.stream.addAction(client.id, elapsed, client.lag, m.x, m.y, m.dt);
break;
case 'pointer':
if (!player.alive) return;
player.pointer.x = m.x;
player.pointer.y = m.y;
break;
case 'requestShot':
simulation.addInstantAction(
elapsed, client.id, client.lag, m.lerp, m.time, m.to
);
simulation.instant.addAction(client.id, elapsed, client.lag, m.lerp, m.to);
break;
case 'requestSpawn':
player = spawnPlayer(client, false);
Expand Down Expand Up @@ -80,7 +76,7 @@ require('ws').createServer({host: '0.0.0.0', port:3000}, function(socket) {
console.log('main: client', client.toString(), 'leaving: removing from clients');

var removingId = client.id;
simulation.deleteClient(removingId);
simulation.deletePlayer(removingId);
delete clients[removingId];
client.purge();

Expand Down Expand Up @@ -133,7 +129,7 @@ timerUtil.addTimer(GameParams.serverUpdateTime, function(dt){
for (var j = shotData.hits.length - 1; j >= 0; j--) {
var cid = shotData.hits[j];
console.log('client', cid, ' is dead, will remove from simulation');
simulation.deleteClient(cid);
simulation.deletePlayer(cid);
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions shared/src/simulation/PlayerRegistry.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ export class PlayerRegistry {
delete this._players[id];
}

checkPlayerExist(id) {
if (!this.hasPlayer(id)) throw 'Registry: does not contains player ' + id;
}

// getPlayer(id) {
// if (!this.hasPlayer(id)) throw "registry: does not contains player " + id;
// return this._players[id];
Expand Down
111 changes: 43 additions & 68 deletions shared/src/simulation/Simulation.js
Original file line number Diff line number Diff line change
@@ -1,64 +1,9 @@
function Simulation(physics) {
this._instantTimeline = new InstantTimeline();
this._streamTimeline = new StreamTimeline();
this._registry = new PlayerRegistry();

this._physics = physics;
console.log('Simulation: created');
}
Simulation.prototype.constructor = Simulation;
import {InstantTimeline} from "./action/timeline/InstantTimeline";
import {StreamTimeline} from "./action/timeline/StreamTimeline";
import {PlayerRegistry} from "./PlayerRegistry";

Simulation.prototype = {

addPlayer: function(clientId, x, y, currentTime) {
// console.log('sim: adding new client %i at %i:%i on time %i', clientId, x, y, currentTime);
var player = this._registry.addPlayer(clientId, x, y);
this._physics.addActorBody(player.id, player.pos.x, player.pos.y);
this._streamTimeline.addClient(player.id, player.pos.x, player.pos.y, currentTime);
return player;
},

deleteClient: function(clientId) {
if (this._registry.hasPlayer(clientId)) {
// console.log('sim: removing client body and history', clientId);
this._physics.deleteActorBody(clientId);
this._streamTimeline.delete(clientId);
this._registry.removePlayer(clientId);
} else {
console.error("sim: attempt to delete non existing player %s", clientId);
}
},

addStreamAction: function(currentTime, clientLag, clientId, velX, velY, dt) {
if (!this._registry.hasPlayer(clientId)) throw "cannot add action on non existing player " + clientId;

this._streamTimeline.addAction(clientId, currentTime, clientLag, velX, velY, dt);
},

/**
* all numbers are integer
* @param {Number} currentTime - server elapsed time
* @param {Number} clientId -
* @param {Number} clientLag - half of rtt (last calculated)
* @param {Number} lerp - current interpolation time on the client in the moment of action
* @param {Number} timeDiff - time since last stream action on client
* @param {Point} to - crosshair point
*/
addInstantAction: function(currentTime, clientId, clientLag, lerp, timeDiff, to) {
if (!this._registry.hasPlayer(clientId)) throw "cannot add instant action on non existing player " + clientId;

var lastAction = this._streamTimeline.getLastAction(clientId);
var elapsedActionTime = currentTime - clientLag - lerp;
// var elapsedActionTime = lastAction.startTime + timeDiff - clientLag - lerp;
if (lastAction.type == MoveAction.TYPE) {
console.log('%d is moving while shooting. td %d lag %d, lerp %d, ct %d', clientId, timeDiff, clientLag, lerp, currentTime);
} else {
console.log('%d is standing still while shooting. td %d lag %d, lerp %d, ct %d', clientId, timeDiff, clientLag, lerp, currentTime);
}

this._instantTimeline.add(new InstantAction(clientId, elapsedActionTime, to));
},

simulateInstantActions: function(currentTime) {
var instantData = [];
var hitClients = [];
Expand Down Expand Up @@ -236,18 +181,48 @@ Simulation.prototype = {
}
};

Object.defineProperty(Simulation.prototype, "physics", {
get: function() {
return this._physics;
export class Simulation {
/**
* @param physicsInjection {Physics}
*/
constructor(physicsInjection) {
this._instantTimeline = new InstantTimeline();
this._streamTimeline = new StreamTimeline();
this._registry = new PlayerRegistry();
this._physics = physicsInjection;

console.log('Simulation: created');
}
});

Object.defineProperty(Simulation.prototype, "registry", {
get: function() {
return this._registry;
/** @returns {Physics} */
get physics() { return this._physics; }
/** @returns {PlayerRegistry} */
get registry() { return this._registry; }
/** @returns {StreamTimeline} */
get stream() { return this._streamTimeline; }
/** @returns {InstantTimeline} */
get instant() { return this._instantTimeline; }

addPlayer(clientId, x, y, currentTime) {
let p = this._registry.addPlayer(clientId, x, y);
this._physics.addActorBody(p.id, p.pos.x, p.pos.y);
this._streamTimeline.addClient(p.id, p.pos.x, p.pos.y, currentTime);
return p;
}

deletePlayer(clientId) {
this._registry.checkPlayerExist(clientId);
this._physics.deleteActorBody(clientId);
this._streamTimeline.removePlayer(clientId);
this._registry.removePlayer(clientId);
}
});

if (typeof module !== 'undefined') {
module.exports.Simulation = Simulation;
simulateInstant(currentTime) {
let instantData = [];
let hitClients = [];

while(!this._instantTimeline.isEmpty) {

}
}
}
21 changes: 17 additions & 4 deletions shared/src/simulation/action/timeline/InstantTimeline.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@

import {InstantAction} from "../InstantAction";
export class InstantTimeline {
constructor() {
/**
Expand All @@ -12,10 +12,23 @@ export class InstantTimeline {
get maximumEET() { return this._timeline.length > 0 ? this._timeline[0].elapsedExecuteTime : 0; }
get isEmpty() { return this._timeline.length === 0; }

/** @param value {InstantAction} */
add(value) {
// /** @param value {InstantAction} */
// add(value) {
// this._sorted = false;
// this._timeline.push(value);
// }

/**
* @param clientId
* @param currentTime - current server time
* @param clientLag - last client's lag
* @param lerp - client's interpolation time in the moment of action
* @param data - action data
*/
addAction(clientId, currentTime, clientLag, lerp, data) {
let elapsedActionTime = currentTime - clientLag - lerp;
this._timeline.push(new InstantAction(clientId, elapsedActionTime, data));
this._sorted = false;
this._timeline.push(value);
}

/** @returns {InstantAction} */
Expand Down
2 changes: 1 addition & 1 deletion shared/src/simulation/action/timeline/StreamTimeline.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export class StreamTimeline {
)];
}

removeClient(clientId) { delete this._current[clientId]; }
removePlayer(clientId) { delete this._current[clientId]; }

addAction(clientId, currentTime, lag, vx, vy, dt) {
if (!(clientId in this._current)) throw 'StreamTimeline: ' + clientId + ' is not in current';
Expand Down

0 comments on commit d71052a

Please sign in to comment.