fixes the sending and rendering of game stats

This commit is contained in:
Jeena 2015-04-19 17:31:41 +02:00
parent cad112419d
commit a66a327386
8 changed files with 81 additions and 62 deletions

View file

@ -202,6 +202,7 @@ function (Parent, PhysicsEngine, Settings, PlayerController, requestAnimFrame, N
this.spawnPlayer(player, 0);
};
// FIXME: remove this method
GameController.prototype.onResetLevel = function(userId) {
console.log('OH NO!!! ON RESET LEVEL IS CALLED AND RESPAWNES PLAYERS');
@ -214,7 +215,6 @@ function (Parent, PhysicsEngine, Settings, PlayerController, requestAnimFrame, N
};
GameController.prototype.destroy = function() {
console.log("clearTimeout")
clearTimeout(this.animationTimeout);
clearTimeout(this.worldUpdateTimeout);

View file

@ -65,12 +65,12 @@ function (Parent, Nc) {
if(this.stats.health < 0) this.stats.health = 0;
this.broadcastStats();
if(this.stats.health <= 0) {
if(enemy != this) enemy.score();
this.kill(enemy, byItem);
}
this.broadcastStats();
};
Player.prototype.spawn = function(x, y) {

View file

@ -189,6 +189,23 @@ function (Parent, Box2D, PhysicsEngine, ViewManager, PlayerController, Nc, reque
throw new Exception("No player with id: " + options.playerId);
}
player.setStats(options.stats);
var playersArray = [];
for (var key in this.players) {
playersArray.push(this.players[key]);
}
var sortedPlayers = playersArray.sort(function(a,b) {
if(a.stats.score > b.stats.score) return -1;
if(a.stats.score < b.stats.score) return 1;
if(a.stats.deaths < b.stats.deaths) return -1;
if(a.stats.deaths > b.stats.deaths) return 1;
if(a.stats.health > b.stats.health) return -1;
if(a.stats.health < b.stats.health) return 1;
return 0;
});
Nc.trigger(Nc.ns.client.view.gameStats.update, sortedPlayers);
};
GameController.prototype.onPlayerKill = function(options) {
@ -238,23 +255,7 @@ function (Parent, Box2D, PhysicsEngine, ViewManager, PlayerController, Nc, reque
};
GameController.prototype.toggleGameStats = function(show) {
var playersArray = [];
for (var key in this.players) {
playersArray.push(this.players[key]);
}
var sortedPlayers = playersArray.sort(function(a,b) {
if(a.stats.score > b.stats.score) return -1;
if(a.stats.score < b.stats.score) return 1;
if(a.stats.deaths < b.stats.deaths) return -1;
if(a.stats.deaths > b.stats.deaths) return 1;
if(a.stats.health > b.stats.health) return -1;
if(a.stats.health < b.stats.health) return 1;
return 0;
});
Nc.trigger(Nc.ns.client.view.gameStats.toggle, show, sortedPlayers);
Nc.trigger(Nc.ns.client.view.gameStats.toggle, show);
};
GameController.prototype.destroy = function() {

View file

@ -1,8 +1,9 @@
define([
"Lib/Utilities/Abstract",
"Lib/Utilities/NotificationCenter"
],
function (Abstract) {
function (Abstract, Nc) {
"use strict";

View file

@ -46,7 +46,6 @@ function (PIXI, Nc, Settings, ColorConverter) {
this.container.addChild(this.dialog);
this.graphics = new PIXI.Graphics();
// this.dialog.addChild(this.graphics);
/*
gameContainer
@ -61,62 +60,72 @@ function (PIXI, Nc, Settings, ColorConverter) {
*/
this.container.visible = false;
this.sortedPlayers = [];
Nc.on(Nc.ns.client.view.gameStats.toggle, this.toggle, this);
this.ncTokens = [
Nc.on(Nc.ns.client.view.gameStats.toggle, this.toggle, this),
Nc.on(Nc.ns.client.view.gameStats.update, this.update, this)
];
}
GameStats.prototype.getInfoContainer = function() {
return this.container;
};
GameStats.prototype.toggle = function(show, sortedPlayers) {
GameStats.prototype.toggle = function(show) {
if(show) {
this.background.clear();
this.graphics.clear();
this.dialog.removeChildren();
// redraw background
this.background.beginFill(this.style.colors.background);
this.background.drawRect(0, 0, Settings.STAGE_WIDTH, Settings.STAGE_HEIGHT);
this.background.endFill();
// redraw text and graphics
var string = "" +
" #".pad(7, true) + " " +
"Name".pad(12, true) +
"Score".pad(6, false) +
"Deaths".pad(7, false) +
"Health".pad(9, false) + " ";
var line = new PIXI.Text(string, {
font: "normal " + this.style.fontSize + "px 'Joystix'",
fill: this.style.colors.headline
});
line.position = new PIXI.Point(0, 0);
this.dialog.addChild(line);
this.drawPlayers(sortedPlayers);
var x = Settings.STAGE_WIDTH / 2 - this.dialog.getBounds().width / 2,
y = Settings.STAGE_HEIGHT / 2 - (sortedPlayers.length + 1) * (this.style.line.height + this.style.line.spacing) / 2;
this.dialog.position = new PIXI.Point(x, y);
this.dialog.addChild(this.graphics);
this.redraw();
// show stats with filters
this.container.visible = true;
this.gameContainer.filters = this.filters;
this.filters.forEach(function(filter) { filter.dirty = true; });
} else {
this.container.visible = false;
this.gameContainer.filters = null;
}
}
GameStats.prototype.update = function(sortedPlayers) {
this.sortedPlayers = sortedPlayers;
this.redraw();
};
GameStats.prototype.redraw = function() {
this.background.clear();
this.graphics.clear();
this.dialog.removeChildren();
// redraw background
this.background.beginFill(this.style.colors.background);
this.background.drawRect(0, 0, Settings.STAGE_WIDTH, Settings.STAGE_HEIGHT);
this.background.endFill();
// redraw text and graphics
var string = "" +
" #".pad(7, true) + " " +
"Name".pad(12, true) +
"Score".pad(6, false) +
"Deaths".pad(7, false) +
"Health".pad(9, false) + " ";
var line = new PIXI.Text(string, {
font: "normal " + this.style.fontSize + "px 'Joystix'",
fill: this.style.colors.headline
});
line.position = new PIXI.Point(0, 0);
this.dialog.addChild(line);
this.drawPlayers(this.sortedPlayers);
var x = Settings.STAGE_WIDTH / 2 - this.dialog.getBounds().width / 2,
y = Settings.STAGE_HEIGHT / 2 - (this.sortedPlayers.length + 1) * (this.style.line.height + this.style.line.spacing) / 2;
this.dialog.position = new PIXI.Point(x, y);
this.dialog.addChild(this.graphics);
};
GameStats.prototype.drawPlayers = function(sortedPlayers) {
sortedPlayers.forEach(function(player, i) {
@ -185,6 +194,12 @@ function (PIXI, Nc, Settings, ColorConverter) {
}
};
GameStats.prototype.destroy = function() {
for (var i = 0; i < this.ncTokens.length; i++) {
Nc.off(this.ncTokens[i]);
};
};
return GameStats;
});

View file

@ -50,7 +50,7 @@ function (Parent, PIXI, Nc, Settings) {
var self = this;
setTimeout(function(){
self.mainText.visible = false;
}, 2000);
}, Settings.SCORE_MESSAGE_TIMEOUT);
}

View file

@ -55,6 +55,7 @@ function () {
RAGDOLL_DESTRUCTION_TIME: 10,
VIEWPORT_SPEED_FACTOR: 640,
VIEWPORT_LOOK_AHEAD: 0.1,
SCORE_MESSAGE_TIMEOUT: 3500,
// restitution: bouncyness, friction: rubbing, density: mass
TILE_FRICTION: 0.99,

View file

@ -66,7 +66,8 @@ function (Exception) {
},
gameStats: {
toggle: null,
kill: null
kill: null,
update: null
},
swiper: {
swipe: null,