This commit is contained in:
Jeena 2014-02-10 22:22:14 +01:00
parent 89c5e4a5d8
commit ed23753c04
17 changed files with 286 additions and 224 deletions

View file

@ -37,6 +37,9 @@ function (Key) {
if(callback) this._playerController[callback]();
key.setActive(true);
}
// Prevent tab from changing focus
if(e.keyCode == 9) return false;
}
KeyboardInput.prototype._onKeyUp = function (e) {
@ -46,6 +49,9 @@ function (Key) {
if(callback) this._playerController[callback]();
key.setActive(false);
}
// Prevent tab from changing focus
if(e.keyCode == 9) return false;
}
return KeyboardInput;

View file

@ -28,7 +28,9 @@ function (Parent, KeyboardInput, MouseInput, NotificationCenter) {
down: 40,
right: 39,
space: 32
space: 32,
tab: 9
}
this.init(keys);
@ -47,6 +49,8 @@ function (Parent, KeyboardInput, MouseInput, NotificationCenter) {
this.keyboardInput.registerKey(keys.w, 'jump');
this.keyboardInput.registerKey(keys.up, 'jump');
this.keyboardInput.registerKey(keys.space, 'jump');
this.keyboardInput.registerKey(keys.tab, 'showInfo', 'hideInfo');
}
PlayerController.prototype.moveLeft = function () {
@ -78,7 +82,16 @@ function (Parent, KeyboardInput, MouseInput, NotificationCenter) {
PlayerController.prototype.handActionRequest = function(x, y) {
var options = {x:x, y:y};
NotificationCenter.trigger("sendGameCommand", "handActionRequest", options);
};
};
PlayerController.prototype.showInfo = function() {
NotificationCenter.trigger("game/toggleInfo", true);
};
PlayerController.prototype.hideInfo = function() {
NotificationCenter.trigger("game/toggleInfo", false);
};
return PlayerController;
});

View file

@ -18,6 +18,8 @@ function (Parent, Box2D, PhysicsEngine, ViewManager, PlayerController, Notificat
this.view = ViewManager.createView();
this.me = null;
NotificationCenter.on("game/toggleInfo", this.toggleInfo, this);
Parent.call(this);
}
@ -136,6 +138,11 @@ function (Parent, Box2D, PhysicsEngine, ViewManager, PlayerController, Notificat
}
};
GameController.prototype.onPlayerKill = function(playerId) {
var player = this.players[options.playerId];
player.kill();
};
GameController.prototype.loadLevel = function (path) {
Parent.prototype.loadLevel.call(this, path);
}
@ -148,5 +155,56 @@ function (Parent, Box2D, PhysicsEngine, ViewManager, PlayerController, Notificat
Parent.prototype.userLeft.call(this, user);
}
GameController.prototype.toggleInfo = function(show) {
var playersArray = [];
for (var key in this.players) {
playersArray.push(this.players[key]);
};
var sortedPlayers = playersArray.sort(function(a,b) {
if(a.score > b.score) return 1;
if(a.score < b.score) return -1;
if(a.deaths < b.deaths) return 1;
if(a.deaths > b.deaths) return -1;
return 0;
});
function pad(string, max, alignLeft) {
string = string.substring(0, max - 1);
var spaces = new Array( max - string.length + 1 ).join(" ");
if(alignLeft) {
return string + spaces;
} else {
return spaces + string;
}
}
var string = "" +
pad("#", 2, false) + " " +
pad("Name", 12, true) +
pad("Score", 6, false) +
pad("Deaths", 7, false) +
pad("Health", 7, false) +
"\n-----------------------------------\n";
var lines = [];
sortedPlayers.forEach(function(player, i) {
var name = player == this.me ? "You" : "Player " + (Object.keys(this.players).indexOf(player.id) + 1);
lines.push(
pad("" + (i + 1) + ".", 2, false) + " " +
pad(name, 12, true) +
pad("" + player.stats.score, 6, false) +
pad("" + player.stats.deaths, 7, false) +
pad("" + parseInt(player.stats.health, 10), 7, false)
);
}, this);
string += lines.join("\n");
this.view.toggleInfo(show, string);
};
return GameController;
});

View file

@ -71,7 +71,9 @@ function (Parent, Settings, NotificationCenter, Exception) {
NotificationCenter.trigger("view/createAnimatedMesh", texturePaths, callback, {
visible: false,
pivot: "mb"
pivot: "mb",
width: 35,
height: 40
});
}
@ -82,7 +84,11 @@ function (Parent, Settings, NotificationCenter, Exception) {
self.headMesh = mesh;
NotificationCenter.trigger("view/addMesh", mesh);
}
NotificationCenter.trigger("view/createMesh", texturePath, callback, { pivot: "mb" });
NotificationCenter.trigger("view/createMesh", texturePath, callback, {
pivot: "mb",
width: 10,
height: 12
});
}
@ -130,7 +136,8 @@ function (Parent, Settings, NotificationCenter, Exception) {
this.animatedMeshes[this.actionState],
{
x: this.body.GetPosition().x * Settings.RATIO,
y: this.body.GetPosition().y * Settings.RATIO
y: this.body.GetPosition().y * Settings.RATIO,
rotation: this.body.GetAngle()
}
);

View file

@ -20,6 +20,8 @@ function (DomController, Settings, Exception, NotificationCenter) {
NotificationCenter.on("view/fullscreenChange", this.onFullscreenChange, this);
NotificationCenter.on("view/toggleDebugMode", this.onToggleDebugMode, this);
NotificationCenter.on("view/toggleInfo", this.onToggleInfo, this);
}
AbstractView.prototype.isWebGlEnabled = function () {
@ -37,35 +39,35 @@ function (DomController, Settings, Exception, NotificationCenter) {
}
AbstractView.prototype.loadPlayerMesh = function(player) {
throw new Exception('Abstract Function loadPlayerMesh not overwritten ');
throw new Exception('Abstract Function loadPlayerMesh not overwritten');
};
AbstractView.prototype.loadMeshes = function(objects) {
throw new Exception('Abstract Function loadMeshes not overwritten ');
throw new Exception('Abstract Function loadMeshes not overwritten');
};
AbstractView.prototype.render = function () {
throw new Exception('Abstract Function render not overwritten ');
throw new Exception('Abstract Function render not overwritten');
}
AbstractView.prototype.createMesh = function (texturePath, callback, options) {
throw new Exception('Abstract Function createMesh not overwritten ');
throw new Exception('Abstract Function createMesh not overwritten');
}
AbstractView.prototype.createAnimatedMesh = function (texturePaths, callback, options) {
throw new Exception('Abstract Function createAnimatedMesh not overwritten ');
throw new Exception('Abstract Function createAnimatedMesh not overwritten');
}
AbstractView.prototype.addMesh = function(mesh) {
throw new Exception('Abstract Function addMesh not overwritten ');
throw new Exception('Abstract Function addMesh not overwritten');
};
AbstractView.prototype.removeMesh = function(mesh) {
throw new Exception('Abstract Function removeMesh not overwritten ');
throw new Exception('Abstract Function removeMesh not overwritten');
};
AbstractView.prototype.updateMesh = function(mesh, options) {
throw new Exception('Abstract Function updateMesh not overwritten ');
throw new Exception('Abstract Function updateMesh not overwritten');
};
AbstractView.prototype.setMe = function(player) {
@ -73,15 +75,15 @@ function (DomController, Settings, Exception, NotificationCenter) {
};
AbstractView.prototype.addPlayer = function(player) {
throw new Exception('Abstract Function addPlayer not overwritten ');
throw new Exception('Abstract Function addPlayer not overwritten');
};
AbstractView.prototype.removPlayer = function(player) {
throw new Exception('Abstract Function removPlayer not overwritten ');
throw new Exception('Abstract Function removPlayer not overwritten');
};
AbstractView.prototype.setCameraPosition = function (x, y) {
throw new Exception('Abstract Function setCameraPosition not overwritten ');
throw new Exception('Abstract Function setCameraPosition not overwritten');
}
AbstractView.prototype.calculateCameraPosition = function() {
@ -101,7 +103,7 @@ function (DomController, Settings, Exception, NotificationCenter) {
};
AbstractView.prototype.setCameraZoom = function (z) {
throw new Exception('Abstract Function setCameraZoom not overwritten ');
throw new Exception('Abstract Function setCameraZoom not overwritten');
};
AbstractView.prototype.onFullscreenChange = function(isFullScreen) {
@ -125,5 +127,9 @@ function (DomController, Settings, Exception, NotificationCenter) {
this.debugMode = debugMode;
};
AbstractView.prototype.toggleInfo = function(show, string) {
throw new Exception('Abstract Function showInfo not overwritten');
};
return AbstractView;
});

View file

@ -16,6 +16,9 @@ function (Parent, DomController, PIXI, Settings, NotificationCenter) {
this.camera = null;
this.stage = null;
this.container = null;
this.infoContainer = null;
this.infoFilters = [];
this.infoBox = null;
this.init();
this.pixi = PIXI;
}
@ -35,21 +38,40 @@ function (Parent, DomController, PIXI, Settings, NotificationCenter) {
this.stage = new PIXI.Stage(0x333333);
this.initCamera();
/*
var blurFilter = new PIXI.BlurFilter();
blurFilter.blurX = 12;
blurFilter.blurY = 0;
var grayFilter = new PIXI.GrayFilter();
grayFilter.gray = .6;
this.stage.filters = [grayFilter];
*/
this.initInfo();
this.setCanvas(this.renderer.view);
}
PixiView.prototype.initCamera = function () {
this.container = new PIXI.DisplayObjectContainer();
this.stage.addChild(this.container);
}
PixiView.prototype.initInfo = function() {
this.infoContainer = new PIXI.DisplayObjectContainer();
this.stage.addChild(this.infoContainer);
var blurFilter = new PIXI.BlurFilter();
blurFilter.blurX = 12;
blurFilter.blurY = 12;
var grayFilter = new PIXI.GrayFilter();
grayFilter.gray = 0.85;
this.infoFilters = [blurFilter, grayFilter];
this.infoText = new PIXI.Text("", {font: "normal 20px monospace", fill: "red", align: "center"});
this.infoBox = new PIXI.Graphics();
this.infoBox.alpha = 0.7;
this.infoContainer.addChild(this.infoBox);
this.infoContainer.addChild(this.infoText);
this.infoContainer.visible = false;
};
PixiView.prototype.render = function () {
if(this.me) {
if(this.me && this.me.isSpawned) {
var pos = this.calculateCameraPosition();
this.setCameraPosition(pos.x, pos.y);
}
@ -79,11 +101,15 @@ function (Parent, DomController, PIXI, Settings, NotificationCenter) {
var textures = [];
for (var i = 0; i < texturePaths.length; i++) {
var texture = PIXI.Texture.fromImage(texturePaths[i]);
texture.width = options.width;
texture.height = options.height;
PIXI.texturesToUpdate.push(texture);
textures.push(texture);
}
var mesh = new PIXI.MovieClip(textures);
if(options) this.updateMesh(mesh, options);
mesh.animationSpeed = 0.5;
mesh.play();
@ -117,12 +143,6 @@ function (Parent, DomController, PIXI, Settings, NotificationCenter) {
};
}
PixiView.prototype.initCamera = function () {
this.container = new PIXI.DisplayObjectContainer();
this.stage.addChild(this.container);
}
PixiView.prototype.calculateCameraPosition = function() {
var zoom = this.container.scale.x;
@ -161,5 +181,35 @@ function (Parent, DomController, PIXI, Settings, NotificationCenter) {
}
};
PixiView.prototype.toggleInfo = function(show, string) {
if(show) {
this.infoText.setText(string);
this.infoText.updateText();
this.infoText.dirty = false;
var x = Settings.STAGE_WIDTH / 2 - this.infoText.width / 2,
y = Settings.STAGE_HEIGHT / 2 - this.infoText.height / 2;
this.infoText.position = new PIXI.Point(x, y);
var borderWidth = 3;
var padding = 20;
this.infoBox.clear();
this.infoBox.beginFill(0x000000);
this.infoBox.lineStyle(borderWidth, 0xAA0000);
this.infoBox.drawRect(0, 0, this.infoText.width - borderWidth + 2 * padding * 2, this.infoText.height - borderWidth + 2 * padding);
this.infoBox.endFill();
this.infoBox.position.x = this.infoText.position.x + borderWidth/2 - padding * 2;
this.infoBox.position.y = this.infoText.position.y + borderWidth/2 - padding;
this.infoContainer.visible = true;
this.container.filters = this.infoFilters;
this.infoFilters.forEach(function(filter) { filter.dirty = true; });
} else {
this.infoText.setText("...");
this.infoContainer.visible = false;
this.container.filters = null;
}
};
return PixiView;
});

View file

@ -19,7 +19,7 @@ define({
DEFAULT_LEVELS: ['stones2'],
RATIO: 21, //35
OBJECT_RATIO: 21 / 25,
OBJECT_RATIO: 20 / 25,
TILE_SIZE: 20, //15, 25 is original picture
CAMERA_IS_ORTHOGRAPHIC: true,
VIEW_CONTROLLER: 0 ? 'Three' : 'Pixi',

View file

@ -156,6 +156,10 @@ function (Parent, Box2D, Settings, CollisionDetector, Item) {
this.setActionState("fall");
}
Doll.prototype.kill = function() {
this.body.SetFixedRotation(false);
};
Doll.prototype.getPosition = function() {
var pos = this.body.GetPosition();
return {

View file

@ -19,8 +19,8 @@ function (Parent, Box2D, Settings, Exception) {
var bodyDef = new Box2D.Dynamics.b2BodyDef();
bodyDef.type = Box2D.Dynamics.b2Body.b2_staticBody;
bodyDef.position.x = this.options.x * Settings.TILE_SIZE / Settings.RATIO;
bodyDef.position.y = this.options.y * Settings.TILE_SIZE / Settings.RATIO;
bodyDef.position.x = (this.options.x * Settings.TILE_SIZE + Settings.TILE_SIZE / 2) / Settings.RATIO;
bodyDef.position.y = (this.options.y * Settings.TILE_SIZE + Settings.TILE_SIZE / 2) / Settings.RATIO;
bodyDef.angle = (this.options.r || 0) * 90 * Math.PI / 180;
return bodyDef;

View file

@ -36,45 +36,58 @@ function (Doll, Settings, NotificationCenter) {
}
Player.prototype.getPosition = function () {
if(!this.doll) return false;
return this.doll.getPosition();
}
Player.prototype.getHeadPosition = function () {
if(!this.doll) return false;
if(!this.isSpawned) return false;
return this.doll.getHeadPosition();
}
Player.prototype.move = function (direction) {
if(!this.isSpawned) return false;
this.doll.move(direction);
}
Player.prototype.stop = function () {
if(!this.isSpawned) return false;
this.doll.stop();
}
Player.prototype.jump = function () {
if(!this.isSpawned) return false;
this.doll.jump();
}
Player.prototype.lookAt = function (x, y) {
if(this.doll) this.doll.lookAt(x, y);
if(!this.isSpawned) return false;
this.doll.lookAt(x, y);
}
Player.prototype.grab = function(item) {
if(!this.isSpawned) return false;
item.beingGrabbed(this);
this.doll.grab(item);
this.holdingItem = item;
};
Player.prototype.throw = function(x, y, item) {
if(!this.isSpawned) return false;
item.beingReleased(this);
this.doll.throw(item, x, y);
this.holdingItem = null;
};
Player.prototype.kill = function(killedBy) {
if(!this.isSpawned) return false;
// FIXME: do something better then just respawn in GameController
if(this.holdingItem) {
this.throw(0, 0, this.holdingItem)
}
this.doll.kill();
this.isSpawned = false;
NotificationCenter.trigger("player/killed", this);
};
@ -90,6 +103,9 @@ function (Doll, Settings, NotificationCenter) {
}
Player.prototype.destroy = function () {
if(this.holdingItem) {
this.throw(0, 0, this.holdingItem);
}
this.doll.destroy();
}

View file

@ -55,20 +55,23 @@ function (Parent, PhysicsEngine, Settings, PlayerController, requestAnimFrame, N
}
GameController.prototype.spawnPlayer = function(player) {
var self = this;
var spawnPoint = this.level.getRandomSpawnPoint();
player.spawn(spawnPoint.x, spawnPoint.y);
this.gameObjects.animated.push(player.getDoll());
setTimeout(function() {
player.spawn(spawnPoint.x, spawnPoint.y);
self.gameObjects.animated.push(player.getDoll());
var message = {
spawnPlayer: {
id: player.id,
x: spawnPoint.x,
y: spawnPoint.y
}
};
NotificationCenter.trigger("broadcastControlCommand", "gameCommand", message);
var message = {
spawnPlayer: {
id: player.id,
x: spawnPoint.x,
y: spawnPoint.y
}
};
NotificationCenter.trigger("broadcastControlCommand", "gameCommand", message);
}, 5000);
};
GameController.prototype.createPlayer = function(user) {

View file

@ -57,8 +57,10 @@ function (Parent, Item, Box2D, NotificationCenter) {
damage.Abs();
damage.Multiply(itemMass);
var player = item.lastMoved.player;
var callback = function() {
self.player.addDamage(damage.Length() * 2, item.lastMoved.player);
self.player.addDamage(damage.Length() * 2, player);
}
NotificationCenter.trigger("engine/addToWorldQueue", callback)

View file

@ -12,6 +12,7 @@ function (Parent, NotificationCenter) {
Player.prototype = Object.create(Parent.prototype);
Player.prototype.handActionRequest = function(x, y) {
if(!this.doll) return false;
var item = null;
var isHolding = !!this.holdingItem;
@ -56,31 +57,27 @@ function (Parent, NotificationCenter) {
};
Player.prototype.addDamage = function(damage, enemy) {
this.updateHealth(this.stats.health - damage);
this.stats.health -= damage;
if(this.stats.health <= 0) {
enemy.score();
this.kill();
} else {
this.broadcastStats();
}
};
Player.prototype.spawn = function(x, y) {
Parent.prototype.spawn.call(this, x, y);
this.updateHealth(100);
};
Player.prototype.updateHealth = function(health) {
this.stats.health = health;
NotificationCenter.trigger("user/" + this.id + "/gameCommand", "updateStats", {
playerId: this.id,
stats: this.stats
});
this.stats.health = 100;
this.broadcastStats();
};
Player.prototype.kill = function() {
Parent.prototype.kill.call(this);
this.stats.deaths++;
this.broadcastStats();
NotificationCenter.trigger("broadcastGameCommand", "playerKill", this.id);
};
Player.prototype.score = function() {