mirror of
https://github.com/logsol/chuck.js.git
synced 2026-05-11 10:37:34 +00:00
implemented clientReady and changed loading of assets, fixed unique ragdoll id
This commit is contained in:
parent
f578b92734
commit
695008afd8
278 changed files with 306 additions and 287 deletions
|
|
@ -56,6 +56,21 @@ function (Parent, Box2D, PhysicsEngine, ViewManager, PlayerController, Notificat
|
|||
DomController.statsEnd();
|
||||
}
|
||||
|
||||
GameController.prototype.onClientReadyResponse = function(options) {
|
||||
|
||||
if (options.spawnedPlayers) {
|
||||
for(var i = 0; i < options.spawnedPlayers.length; i++) {
|
||||
this.onSpawnPlayer(options.spawnedPlayers[i]);
|
||||
}
|
||||
}
|
||||
|
||||
if (options.worldUpdate) {
|
||||
this.onWorldUpdate(options.worldUpdate);
|
||||
}
|
||||
|
||||
this.createMe(options.userId);
|
||||
};
|
||||
|
||||
GameController.prototype.onWorldUpdate = function (updateData) {
|
||||
|
||||
var body = this.physicsEngine.world.GetBodyList();
|
||||
|
|
@ -66,7 +81,7 @@ function (Parent, Box2D, PhysicsEngine, ViewManager, PlayerController, Notificat
|
|||
if(updateData[gameObject.uid]) {
|
||||
var update = updateData[gameObject.uid];
|
||||
body.SetAwake(true);
|
||||
body.SetPosition(this.centerBetween(update.p, body.GetPosition()));
|
||||
body.SetPosition(update.p);
|
||||
body.SetAngle(update.a);
|
||||
body.SetLinearVelocity(update.lv);
|
||||
body.SetAngularVelocity(update.av);
|
||||
|
|
@ -82,25 +97,7 @@ function (Parent, Box2D, PhysicsEngine, ViewManager, PlayerController, Notificat
|
|||
|
||||
}
|
||||
|
||||
GameController.prototype.centerBetween = function(n, o) {
|
||||
var x, y;
|
||||
|
||||
if(n.x > o.x) {
|
||||
x = o.x + (n.x - o.x) / 2;
|
||||
} else {
|
||||
x = o.x - (o.x - n.x) / 2;
|
||||
}
|
||||
|
||||
if(n.y > o.y) {
|
||||
y = o.y + (n.y - o.y) / 2;
|
||||
} else {
|
||||
y = o.y - (o.y - n.y) / 2;
|
||||
}
|
||||
|
||||
return {x:x, y:y};
|
||||
};
|
||||
|
||||
GameController.prototype.onJoinMe = function (playerId) {
|
||||
GameController.prototype.createMe = function (playerId) {
|
||||
this.me = this.players[playerId];
|
||||
this.me.setPlayerController(new PlayerController(this.me));
|
||||
this.view.setMe(this.me);
|
||||
|
|
@ -161,7 +158,7 @@ function (Parent, Box2D, PhysicsEngine, ViewManager, PlayerController, Notificat
|
|||
GameController.prototype.onPlayerKill = function(options) {
|
||||
var player = this.players[options.playerId];
|
||||
var killedByPlayer = this.players[options.killedByPlayerId];
|
||||
player.kill(killedByPlayer);
|
||||
player.kill(killedByPlayer, options.ragDollId);
|
||||
};
|
||||
|
||||
GameController.prototype.onRemoveGameObject = function(options) {
|
||||
|
|
|
|||
|
|
@ -61,7 +61,14 @@ function (Parent, Settings, NotificationCenter, Exception) {
|
|||
|
||||
var texturePaths = [];
|
||||
for (var i = start; i <= end; i++) {
|
||||
texturePaths.push(Settings.GRAPHICS_PATH + "Animation/WithArms/ChuckAnimations0" + padF(i) + ".png");
|
||||
texturePaths.push(
|
||||
Settings.GRAPHICS_PATH
|
||||
+ Settings.GRAPHICS_SUBPATH_CHARACTERS
|
||||
+ this.characterName
|
||||
+ "/Animation/WithArms/ChuckAnimations0"
|
||||
+ padF(i)
|
||||
+ ".png"
|
||||
);
|
||||
}
|
||||
|
||||
var callback = function(mesh) {
|
||||
|
|
|
|||
|
|
@ -1,9 +1,11 @@
|
|||
define([
|
||||
"Game/Core/Loader/Level",
|
||||
"Game/Config/Settings"
|
||||
"Game/Config/Settings",
|
||||
"Lib/Utilities/NotificationCenter",
|
||||
"Lib/Vendor/Pixi"
|
||||
],
|
||||
|
||||
function (Parent, Settings) {
|
||||
function (Parent, Settings, NotificationCenter, PIXI) {
|
||||
|
||||
function Level (uid, engine, gameObjects) {
|
||||
Parent.call(this, uid, engine, gameObjects);
|
||||
|
|
@ -12,12 +14,12 @@ function (Parent, Settings) {
|
|||
Level.prototype = Object.create(Parent.prototype);
|
||||
|
||||
Level.prototype.loadLevelDataFromPath = function (path, callback) {
|
||||
|
||||
var self = this;
|
||||
var xhr = new XMLHttpRequest();
|
||||
xhr.onreadystatechange = function() {
|
||||
if(xhr.readyState == 4) {
|
||||
if(xhr.status == 200) {
|
||||
callback(JSON.parse(xhr.responseText))
|
||||
self.loadAssets(JSON.parse(xhr.responseText), callback);
|
||||
} else {
|
||||
console.error("Ajax error: " + xhr.status + " " + xhr.statusText)
|
||||
}
|
||||
|
|
@ -27,5 +29,62 @@ function (Parent, Settings) {
|
|||
xhr.send(null);
|
||||
}
|
||||
|
||||
Level.prototype.loadAssets = function(levelData, callback) {
|
||||
|
||||
var paths = this.getAssetPaths(levelData);
|
||||
|
||||
var count = 0;
|
||||
var numPaths = paths.length;
|
||||
var loader = new PIXI.AssetLoader(paths);
|
||||
loader.onComplete = function() { callback(levelData); };
|
||||
loader.onProgress = function() {
|
||||
var progress = parseInt(100 / numPaths * ++count, 10) + 1;
|
||||
NotificationCenter.trigger("view/updateLoader", progress);
|
||||
}
|
||||
loader.load();
|
||||
};
|
||||
|
||||
Level.prototype.getAssetPaths = function(levelData) {
|
||||
|
||||
var paths = [];
|
||||
|
||||
// Get chuck
|
||||
var padF = function(n) {
|
||||
if(n<10) return "00" + n;
|
||||
if(n<100) return "0" + n;
|
||||
return n;
|
||||
}
|
||||
|
||||
var characterNames = ["Chuck"];
|
||||
var animationSets = ["WithArms"]; // FIXME add WithoutArms
|
||||
for (var i = 0; i < characterNames.length; i++) {
|
||||
var characterName = characterNames[i];
|
||||
for (var j = 1; j <= 126; j++) {
|
||||
for (var k = 0; k < animationSets.length; k++) {
|
||||
var animationSet = animationSets[k];
|
||||
paths.push(
|
||||
Settings.GRAPHICS_PATH
|
||||
+ Settings.GRAPHICS_SUBPATH_CHARACTERS
|
||||
+ characterName
|
||||
+ "/Animation/"
|
||||
+ animationSet
|
||||
+ "/ChuckAnimations0"
|
||||
+ padF(j)
|
||||
+ ".png"
|
||||
);
|
||||
};
|
||||
};
|
||||
};
|
||||
|
||||
paths.push(
|
||||
Settings.GRAPHICS_PATH
|
||||
+ Settings.GRAPHICS_SUBPATH_CHARACTERS
|
||||
+ characterName
|
||||
+ "/head.png"
|
||||
);
|
||||
|
||||
return paths;
|
||||
};
|
||||
|
||||
return Level;
|
||||
});
|
||||
|
|
@ -1,9 +1,50 @@
|
|||
define([
|
||||
"Game/Core/Loader/TiledLevel"
|
||||
"Game/Core/Loader/TiledLevel",
|
||||
"Game/Config/Settings"
|
||||
],
|
||||
|
||||
function (Parent) {
|
||||
function (Parent, Settings) {
|
||||
|
||||
function TiledLevel(uid, engine, gameObjects) {
|
||||
Parent.call(this, uid, engine, gameObjects);
|
||||
}
|
||||
|
||||
return Parent;
|
||||
TiledLevel.prototype = Object.create(Parent.prototype);
|
||||
|
||||
TiledLevel.prototype.getAssetPaths = function(levelData) {
|
||||
var paths = Parent.prototype.getAssetPaths.call(this, levelData);
|
||||
|
||||
// Get tiles images
|
||||
for (var i = 0; i < levelData.tilesets.length; i++) {
|
||||
var tileset = levelData.tilesets[i];
|
||||
for (var key in tileset.tiles) {
|
||||
var imagePpath = tileset.tiles[key].image;
|
||||
if(imagePpath) {
|
||||
paths.push(Settings.MAPS_PATH + imagePpath);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// Get items images
|
||||
var objects = this.getLayer(levelData, "items").objects;
|
||||
for (var i = 0; i < objects.length; i++) {
|
||||
var object = objects[i];
|
||||
var options = object.properties;
|
||||
|
||||
var texturePath = Settings.GRAPHICS_PATH
|
||||
+ Settings.GRAPHICS_SUBPATH_ITEMS
|
||||
+ options.category + '/'
|
||||
+ options.image;
|
||||
|
||||
paths.push(texturePath);
|
||||
};
|
||||
|
||||
// FIXME: Get background image
|
||||
|
||||
return paths;
|
||||
|
||||
}
|
||||
|
||||
return TiledLevel;
|
||||
|
||||
});
|
||||
|
|
@ -13,6 +13,7 @@ function (ProtocolHelper, GameController, User, NotificationCenter, Settings, Do
|
|||
this.socketLink = socketLink;
|
||||
this.gameController = null;
|
||||
this.users = {};
|
||||
this.userId = null;
|
||||
|
||||
this.socketLink.on('connect', this.onConnect.bind(this));
|
||||
this.socketLink.on('disconnect', this.onDisconnect.bind(this));
|
||||
|
|
@ -26,6 +27,7 @@ function (ProtocolHelper, GameController, User, NotificationCenter, Settings, Do
|
|||
});
|
||||
|
||||
NotificationCenter.on("sendGameCommand", this.sendGameCommand, this);
|
||||
NotificationCenter.on("game/level/loaded", this.onLevelLoaded, this);
|
||||
}
|
||||
|
||||
// Socket callbacks
|
||||
|
|
@ -43,15 +45,12 @@ function (ProtocolHelper, GameController, User, NotificationCenter, Settings, Do
|
|||
}
|
||||
|
||||
Networker.prototype.onJoinSuccess = function (options) {
|
||||
NotificationCenter.on("game/level/loaded", function() {
|
||||
this.onLevelLoaded(options);
|
||||
}, this);
|
||||
this.userId = options.userId;
|
||||
|
||||
this.gameController = new GameController();
|
||||
this.gameController.loadLevel(options.levelUid);
|
||||
|
||||
this.onUserJoined(options.userId);
|
||||
this.gameController.onJoinMe(options.userId);
|
||||
|
||||
if (options.joinedUsers) {
|
||||
for(var i = 0; i < options.joinedUsers.length; i++) {
|
||||
|
|
@ -62,20 +61,15 @@ function (ProtocolHelper, GameController, User, NotificationCenter, Settings, Do
|
|||
this.initPing();
|
||||
}
|
||||
|
||||
Networker.prototype.onLevelLoaded = function(options) {
|
||||
if (options.spawnedPlayers) {
|
||||
for(var i = 0; i < options.spawnedPlayers.length; i++) {
|
||||
this.gameController.onSpawnPlayer(options.spawnedPlayers[i]);
|
||||
}
|
||||
Networker.prototype.onLevelLoaded = function() {
|
||||
for (var userId in this.users) {
|
||||
this.gameController.createPlayer(this.users[userId]);
|
||||
}
|
||||
|
||||
if (options.worldUpdate) {
|
||||
this.gameController.onWorldUpdate(options.worldUpdate);
|
||||
}
|
||||
this.sendGameCommand("clientReady");
|
||||
};
|
||||
|
||||
Networker.prototype.initPing = function() {
|
||||
|
||||
this.ping();
|
||||
};
|
||||
|
||||
|
|
@ -85,7 +79,8 @@ function (ProtocolHelper, GameController, User, NotificationCenter, Settings, Do
|
|||
|
||||
|
||||
// Sending commands
|
||||
|
||||
|
||||
// Remember: control commands are coordinator relevant commands
|
||||
Networker.prototype.sendCommand = function (command, options) {
|
||||
var message = ProtocolHelper.encodeCommand(command, options);
|
||||
this.socketLink.send(message);
|
||||
|
|
@ -105,12 +100,18 @@ function (ProtocolHelper, GameController, User, NotificationCenter, Settings, Do
|
|||
Networker.prototype.onUserJoined = function (userId) {
|
||||
var user = new User(userId);
|
||||
this.users[userId] = user;
|
||||
this.gameController.userJoined(user);
|
||||
|
||||
if (this.gameController
|
||||
&& this.gameController.level
|
||||
&& this.gameController.level.isLoaded) {
|
||||
|
||||
this.gameController.createPlayer(user);
|
||||
};
|
||||
}
|
||||
|
||||
Networker.prototype.onUserLeft = function (userId) {
|
||||
var user = this.users[userId];
|
||||
this.gameController.userLeft(user);
|
||||
this.gameController.onUserLeft(user);
|
||||
delete this.users[userId];
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -50,11 +50,6 @@ function (Parent, NotificationCenter, Settings) {
|
|||
}
|
||||
};
|
||||
|
||||
|
||||
Player.prototype.kill = function(killedByPlayer) {
|
||||
Parent.prototype.kill.call(this, killedByPlayer);
|
||||
};
|
||||
|
||||
Player.prototype.spawn = function(x, y) {
|
||||
Parent.prototype.spawn.call(this, x, y);
|
||||
this.setPlayerInfoVisible(false);
|
||||
|
|
|
|||
|
|
@ -26,6 +26,8 @@ function (DomController, Settings, Exception, NotificationCenter) {
|
|||
NotificationCenter.on("view/createAndAddPlayerInfo", this.onCreateAndAddPlayerInfo, this);
|
||||
NotificationCenter.on("view/updatePlayerInfo", this.onUpdatePlayerInfo, this);
|
||||
NotificationCenter.on("view/removePlayerInfo", this.onRemovePlayerInfo, this);
|
||||
|
||||
NotificationCenter.on("view/updateLoader", this.onUpdateLoader, this);
|
||||
}
|
||||
|
||||
AbstractView.prototype.isWebGlEnabled = function () {
|
||||
|
|
@ -147,5 +149,9 @@ function (DomController, Settings, Exception, NotificationCenter) {
|
|||
throw new Exception('Abstract Function onRemovePlayerInfo not overwritten');
|
||||
};
|
||||
|
||||
AbstractView.prototype.onUpdateLoader = function(progress) {
|
||||
throw new Exception('Abstract Function onUpdateLoader not overwritten');
|
||||
};
|
||||
|
||||
return AbstractView;
|
||||
});
|
||||
|
|
@ -19,6 +19,7 @@ function (Parent, DomController, PIXI, Settings, NotificationCenter) {
|
|||
this.infoContainer = null;
|
||||
this.infoFilters = [];
|
||||
this.infoBox = null;
|
||||
this.loader = null;
|
||||
this.init();
|
||||
this.pixi = PIXI;
|
||||
}
|
||||
|
|
@ -39,6 +40,7 @@ function (Parent, DomController, PIXI, Settings, NotificationCenter) {
|
|||
|
||||
this.initCamera();
|
||||
this.initInfo();
|
||||
this.initLoader();
|
||||
|
||||
this.setCanvas(this.renderer.view);
|
||||
}
|
||||
|
|
@ -247,5 +249,41 @@ function (Parent, DomController, PIXI, Settings, NotificationCenter) {
|
|||
this.container.removeChild(playerInfo);
|
||||
};
|
||||
|
||||
PixiView.prototype.initLoader = function() {
|
||||
this.loader = new PIXI.Graphics();
|
||||
this.stage.addChild(this.loader);
|
||||
this.onUpdateLoader(0);
|
||||
};
|
||||
|
||||
PixiView.prototype.onUpdateLoader = function(progress) {
|
||||
var width = 200,
|
||||
height = 5,
|
||||
borderWidth = 1;
|
||||
|
||||
if(progress < 100) {
|
||||
this.loader.clear();
|
||||
|
||||
this.loader.beginFill(0x000000);
|
||||
this.loader.lineStyle(borderWidth, 0x000000);
|
||||
this.loader.drawRect(0, 0, width, height);
|
||||
this.loader.endFill();
|
||||
|
||||
if(progress > 0) {
|
||||
var color = 0xFF0FA3;
|
||||
this.loader.beginFill(color);
|
||||
this.loader.lineStyle(0, 0x000000);
|
||||
this.loader.drawRect(borderWidth, borderWidth, width * progress / 100, height);
|
||||
this.loader.endFill();
|
||||
}
|
||||
|
||||
this.loader.position = new PIXI.Point(
|
||||
Settings.STAGE_WIDTH / 2 - width / 2 - borderWidth,
|
||||
Settings.STAGE_HEIGHT / 2 - height / 2 - borderWidth
|
||||
);
|
||||
}
|
||||
|
||||
this.loader.visible = progress < 100;
|
||||
};
|
||||
|
||||
return PixiView;
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue