mirror of
https://github.com/logsol/chuck.js.git
synced 2026-05-11 10:37:34 +00:00
Implemented local positioning as master - included punkbuster instead of webrtc for now. #49
This commit is contained in:
parent
efe4c76cc2
commit
e7f4b6043d
7 changed files with 124 additions and 24 deletions
|
|
@ -1,10 +1,11 @@
|
|||
define([
|
||||
"Game/Core/Control/PlayerController",
|
||||
"Lib/Utilities/NotificationCenter",
|
||||
"Lib/Utilities/Protocol/Parser"
|
||||
"Lib/Utilities/Protocol/Parser",
|
||||
"Game/Config/Settings"
|
||||
],
|
||||
|
||||
function(Parent, Nc, Parser) {
|
||||
function(Parent, Nc, Parser, Settings) {
|
||||
|
||||
function PlayerController(player) {
|
||||
|
||||
|
|
@ -39,6 +40,27 @@ function(Parent, Nc, Parser) {
|
|||
this.player.suicide();
|
||||
};
|
||||
|
||||
PlayerController.prototype.meStateUpdate = function(update) {
|
||||
|
||||
if(!this.player.doll) {
|
||||
console.warn('me state update, even though doll does not exist');
|
||||
return;
|
||||
}
|
||||
|
||||
var difference = {
|
||||
x: Math.abs(update.p.x - this.player.doll.body.GetPosition().x),
|
||||
y: Math.abs(update.p.y - this.player.doll.body.GetPosition().y)
|
||||
}
|
||||
|
||||
if(difference.x < Settings.PUNKBUSTER_DIFFERENCE_METERS
|
||||
|| difference.y < Settings.PUNKBUSTER_DIFFERENCE_METERS) {
|
||||
this.player.doll.body.SetAwake(true);
|
||||
this.player.doll.body.SetPosition(update.p);
|
||||
} else {
|
||||
// HARD UPDATE FOR SELF
|
||||
}
|
||||
};
|
||||
|
||||
return PlayerController;
|
||||
|
||||
});
|
||||
|
|
@ -108,7 +108,7 @@ function (Parent, PhysicsEngine, Settings, PlayerController, requestAnimFrame, N
|
|||
Nc.trigger(Nc.ns.channel.to.client.gameCommand.broadcast, 'worldUpdate', update);
|
||||
}
|
||||
|
||||
this.worldUpdateTimeout = setTimeout(this.updateWorld.bind(this), Settings.WORLD_UPDATE_BROADCAST_INTERVAL);
|
||||
this.worldUpdateTimeout = setTimeout(this.updateWorld.bind(this), Settings.NETWORK_UPDATE_INTERVAL);
|
||||
}
|
||||
|
||||
GameController.prototype.getWorldUpdateObject = function(getSleeping) {
|
||||
|
|
|
|||
|
|
@ -10,10 +10,11 @@ define([
|
|||
"Game/Client/GameObjects/GameObject",
|
||||
"Game/Client/GameObjects/Doll",
|
||||
"Game/Client/View/DomController",
|
||||
"Lib/Utilities/Protocol/Helper"
|
||||
"Lib/Utilities/Protocol/Helper",
|
||||
"Game/Client/Me"
|
||||
],
|
||||
|
||||
function (Parent, Box2D, PhysicsEngine, ViewManager, PlayerController, Nc, requestAnimFrame, Settings, GameObject, Doll, DomController, ProtocolHelper) {
|
||||
function (Parent, Box2D, PhysicsEngine, ViewManager, PlayerController, Nc, requestAnimFrame, Settings, GameObject, Doll, DomController, ProtocolHelper, Me) {
|
||||
|
||||
if (!window.cancelAnimationFrame) {
|
||||
window.cancelAnimationFrame = function(id) {
|
||||
|
|
@ -53,6 +54,7 @@ function (Parent, Box2D, PhysicsEngine, ViewManager, PlayerController, Nc, reque
|
|||
|
||||
if(this.me) {
|
||||
this.me.update();
|
||||
this.localMePositionUpdate();
|
||||
}
|
||||
|
||||
for (var i = 0; i < this.gameObjects.animated.length; i++) {
|
||||
|
|
@ -64,6 +66,12 @@ function (Parent, Box2D, PhysicsEngine, ViewManager, PlayerController, Nc, reque
|
|||
DomController.statsEnd();
|
||||
}
|
||||
|
||||
GameController.prototype.localMePositionUpdate = function() {
|
||||
if(this.me.isStateUpdateNeeded()) {
|
||||
Nc.trigger(Nc.ns.client.to.server.gameCommand.send, "meStateUpdate", this.me.getStateUpdate());
|
||||
}
|
||||
};
|
||||
|
||||
GameController.prototype.onClientReadyResponse = function(options) {
|
||||
|
||||
if (options.worldUpdate) {
|
||||
|
|
@ -88,7 +96,7 @@ function (Parent, Box2D, PhysicsEngine, ViewManager, PlayerController, Nc, reque
|
|||
};
|
||||
}
|
||||
|
||||
this.createMe(options.userId);
|
||||
this.setMe();
|
||||
|
||||
this.clientIsReady = true; // needs to stay before onSpawnPlayer
|
||||
|
||||
|
|
@ -108,16 +116,21 @@ function (Parent, Box2D, PhysicsEngine, ViewManager, PlayerController, Nc, reque
|
|||
var gameObject = userData;
|
||||
if(updateData[gameObject.uid]) {
|
||||
var update = updateData[gameObject.uid];
|
||||
|
||||
if (gameObject instanceof Doll) {
|
||||
if(gameObject === this.me.doll) {
|
||||
this.me.setLastServerState(update);
|
||||
continue; // this is to ignore own doll updates from world update
|
||||
}
|
||||
gameObject.setActionState(update.as);
|
||||
gameObject.lookAt(update.laxy.x, update.laxy.y);
|
||||
}
|
||||
|
||||
body.SetAwake(true);
|
||||
body.SetPosition(update.p);
|
||||
body.SetAngle(update.a);
|
||||
body.SetLinearVelocity(update.lv);
|
||||
body.SetAngularVelocity(update.av);
|
||||
|
||||
if (gameObject instanceof Doll) {
|
||||
gameObject.setActionState(update.as);
|
||||
gameObject.lookAt(update.laxy.x, update.laxy.y);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -125,8 +138,12 @@ function (Parent, Box2D, PhysicsEngine, ViewManager, PlayerController, Nc, reque
|
|||
|
||||
}
|
||||
|
||||
GameController.prototype.createMe = function (playerId) {
|
||||
this.me = this.players[playerId];
|
||||
GameController.prototype.createMe = function(user) {
|
||||
this.me = new Me(user.id, this.physicsEngine, user);
|
||||
this.players[user.id] = this.me;
|
||||
};
|
||||
|
||||
GameController.prototype.setMe = function() {
|
||||
this.me.setPlayerController(new PlayerController(this.me));
|
||||
this.view.setMe(this.me);
|
||||
}
|
||||
|
|
|
|||
51
app/Game/Client/Me.js
Normal file
51
app/Game/Client/Me.js
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
define([
|
||||
"Game/Client/Player",
|
||||
"Game/Config/Settings"
|
||||
],
|
||||
|
||||
function (Parent, Settings) {
|
||||
|
||||
function Me(id, physicsEngine, user) {
|
||||
Parent.call(this, id, physicsEngine, user);
|
||||
|
||||
this.lastServerState = {
|
||||
p: {
|
||||
x: 0,
|
||||
y: 0
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
Me.prototype = Object.create(Parent.prototype);
|
||||
|
||||
Me.prototype.setLastServerState = function(update) {
|
||||
this.lastServerState = update;
|
||||
};
|
||||
|
||||
Me.prototype.isStateUpdateNeeded = function() {
|
||||
|
||||
if(!this.doll) return false;
|
||||
|
||||
var difference = {
|
||||
x: Math.abs(this.lastServerState.p.x - this.doll.body.GetPosition().x),
|
||||
y: Math.abs(this.lastServerState.p.y - this.doll.body.GetPosition().y)
|
||||
}
|
||||
|
||||
if(difference.x > Settings.ME_STATE_MAX_DIFFERENCE_METERS
|
||||
|| difference.y > Settings.ME_STATE_MAX_DIFFERENCE_METERS) {
|
||||
console.log('AAAAHHHH');
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
};
|
||||
|
||||
Me.prototype.getStateUpdate = function() {
|
||||
return {
|
||||
p: this.doll.body.GetPosition().Copy(),
|
||||
lv: this.doll.body.GetLinearVelocity().Copy()
|
||||
}
|
||||
};
|
||||
|
||||
return Me;
|
||||
|
||||
});
|
||||
|
|
@ -61,7 +61,8 @@ function (ProtocolHelper, GameController, User, Nc, Settings, DomController) {
|
|||
Networker.prototype.onJoinSuccess = function (options) {
|
||||
console.log("join success")
|
||||
|
||||
this.onUserJoined(options.user);
|
||||
this.onUserJoined(options.user, true);
|
||||
this.meUserId = options.user.id;
|
||||
|
||||
if (options.joinedUsers) {
|
||||
for(var i = 0; i < options.joinedUsers.length; i++) {
|
||||
|
|
@ -78,9 +79,13 @@ function (ProtocolHelper, GameController, User, Nc, Settings, DomController) {
|
|||
};
|
||||
|
||||
Networker.prototype.onLevelLoaded = function() {
|
||||
this.gameController.createMe(this.users[this.meUserId]);
|
||||
|
||||
for (var userId in this.users) {
|
||||
if(this.meUserId != userId) {
|
||||
this.gameController.createPlayer(this.users[userId]);
|
||||
}
|
||||
}
|
||||
|
||||
this.sendGameCommand("clientReady");
|
||||
};
|
||||
|
|
@ -132,17 +137,18 @@ function (ProtocolHelper, GameController, User, Nc, Settings, DomController) {
|
|||
|
||||
// Commands from server
|
||||
|
||||
Networker.prototype.onUserJoined = function (options) {
|
||||
Networker.prototype.onUserJoined = function (options, isMe) {
|
||||
var user = new User(options.id, options);
|
||||
console.log(options.nickname)
|
||||
this.users[user.id] = user;
|
||||
|
||||
if (this.gameController
|
||||
if (!isMe
|
||||
&& this.gameController
|
||||
&& this.gameController.level
|
||||
&& this.gameController.level.isLoaded) {
|
||||
|
||||
this.gameController.createPlayer(user);
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
Networker.prototype.onUserLeft = function (userId) {
|
||||
|
|
|
|||
|
|
@ -61,9 +61,9 @@ define(function() {
|
|||
USE_WEBGL: true,
|
||||
|
||||
// NETWORKING
|
||||
WORLD_UPDATE_BROADCAST_INTERVAL: 70,
|
||||
NETWORK_UPDATE_INTERVAL: 70,
|
||||
CHANNEL_DESTRUCTION_TIME: 30,
|
||||
NETWORK_LOG_INCOMING: true,
|
||||
NETWORK_LOG_INCOMING: false,
|
||||
NETWORK_LOG_OUTGOING: false,
|
||||
NETWORK_LOG_FILTER: ['ping', 'pong', 'worldUpdate', 'lookAt'],
|
||||
|
||||
|
|
@ -71,7 +71,11 @@ define(function() {
|
|||
CHANNEL_END_ROUND_TIME: 4, //10,
|
||||
CHANNEL_DEFAULT_MAX_USERS: 40,
|
||||
CHANNEL_DEFAULT_SCORE_LIMIT: 10,
|
||||
CHANNEL_DEFAULT_LEVELS: ['stones2', 'debug', 'stones2', 'debug']
|
||||
CHANNEL_DEFAULT_LEVELS: ['stones2', 'debug', 'stones2', 'debug'],
|
||||
|
||||
// ME STATE
|
||||
ME_STATE_MAX_DIFFERENCE_METERS: 1,
|
||||
PUNKBUSTER_DIFFERENCE_METERS: 1
|
||||
}
|
||||
|
||||
Settings.TILE_RATIO = Settings.ORIGINAL_TILE_SIZE / Settings.TILE_SIZE;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue