mirror of
https://github.com/logsol/chuck.js.git
synced 2026-05-11 18:47:35 +00:00
fixed #57
This commit is contained in:
parent
810a74a28b
commit
39bdac0d7b
12 changed files with 118 additions and 100 deletions
|
|
@ -46,22 +46,26 @@
|
||||||
|
|
||||||
// Channel command callbacks
|
// Channel command callbacks
|
||||||
|
|
||||||
Channel.prototype.onAddUser = function (userId) {
|
Channel.prototype.onAddUser = function (options) {
|
||||||
var self = this;
|
var self = this;
|
||||||
|
|
||||||
if(!this.gameController.level || !this.gameController.level.isLoaded) {
|
if(!this.gameController.level || !this.gameController.level.isLoaded) {
|
||||||
var token = Nc.on("game/level/loaded", function() {
|
var token = Nc.on("game/level/loaded", function() {
|
||||||
self.sendJoinSuccess(userId);
|
self.sendJoinSuccess(options);
|
||||||
Nc.off(token);
|
Nc.off(token);
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
self.sendJoinSuccess(userId);
|
self.sendJoinSuccess(options);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Channel.prototype.sendJoinSuccess = function(userId) {
|
Channel.prototype.sendJoinSuccess = function(options) {
|
||||||
var user = new User(userId, this);
|
var user = new User(options.id, options);
|
||||||
var joinedUsers = Object.keys(this.users);
|
|
||||||
|
var joinedUsers = [];
|
||||||
|
for(var userId in this.users) {
|
||||||
|
joinedUsers.push(this.users[userId].options)
|
||||||
|
}
|
||||||
|
|
||||||
var levelUid = null;
|
var levelUid = null;
|
||||||
if(this.gameController.level) {
|
if(this.gameController.level) {
|
||||||
|
|
@ -71,14 +75,16 @@
|
||||||
this.users[user.id] = user;
|
this.users[user.id] = user;
|
||||||
|
|
||||||
var options = {
|
var options = {
|
||||||
userId: user.id,
|
user: user.options,
|
||||||
channelName: this.name,
|
|
||||||
joinedUsers: joinedUsers,
|
joinedUsers: joinedUsers,
|
||||||
levelUid: levelUid
|
levelUid: levelUid
|
||||||
};
|
};
|
||||||
|
|
||||||
Nc.trigger('user/' + user.id + "/joinSuccess", options);
|
//Nc.trigger('user/' + user.id + "/joinSuccess", options);
|
||||||
Nc.trigger('user/joined', user);
|
user.sendControlCommand("joinSuccess", options);
|
||||||
|
Nc.trigger('user/joined', user);
|
||||||
|
|
||||||
|
this.broadcastControlCommandExcept("userJoined", user.options, user);
|
||||||
};
|
};
|
||||||
|
|
||||||
Channel.prototype.onReleaseUser = function (userId) {
|
Channel.prototype.onReleaseUser = function (userId) {
|
||||||
|
|
|
||||||
|
|
@ -88,15 +88,6 @@ function (Parent, PhysicsEngine, Settings, PlayerController, requestAnimFrame, N
|
||||||
}, respawnTime * 1000);
|
}, respawnTime * 1000);
|
||||||
};
|
};
|
||||||
|
|
||||||
/*
|
|
||||||
GameController.prototype.createPlayer = function(user) {
|
|
||||||
var player = new Player(user.id, this.physicsEngine);
|
|
||||||
player.setPlayerController(new PlayerController(player))
|
|
||||||
|
|
||||||
return player;
|
|
||||||
};
|
|
||||||
*/
|
|
||||||
|
|
||||||
GameController.prototype.updateWorld = function () {
|
GameController.prototype.updateWorld = function () {
|
||||||
|
|
||||||
var update = this.getWorldUpdateObject(false);
|
var update = this.getWorldUpdateObject(false);
|
||||||
|
|
|
||||||
|
|
@ -5,8 +5,8 @@ define([
|
||||||
|
|
||||||
function (Parent, Nc) {
|
function (Parent, Nc) {
|
||||||
|
|
||||||
function Player(id, physicsEngine) {
|
function Player(id, physicsEngine, user) {
|
||||||
Parent.call(this, id, physicsEngine);
|
Parent.call(this, id, physicsEngine, user);
|
||||||
}
|
}
|
||||||
|
|
||||||
Player.prototype = Object.create(Parent.prototype);
|
Player.prototype = Object.create(Parent.prototype);
|
||||||
|
|
|
||||||
|
|
@ -7,20 +7,13 @@ define([
|
||||||
|
|
||||||
function(Parent, Nc, ProtocolHelper, ProtocolParser) {
|
function(Parent, Nc, ProtocolHelper, ProtocolParser) {
|
||||||
|
|
||||||
function User(id, channel) {
|
function User(id, options) {
|
||||||
Parent.call(this, id);
|
Parent.call(this, id, options);
|
||||||
|
|
||||||
this.channel = channel;
|
|
||||||
this.player = null;
|
this.player = null;
|
||||||
this.isReady = false;
|
this.isReady = false;
|
||||||
var self = this;
|
var self = this;
|
||||||
|
|
||||||
Nc.on('user/joined', function(user) { // FIXME: use sendToAllUsersExcept instead
|
|
||||||
if(user.id != self.id) {
|
|
||||||
self.sendControlCommand("userJoined", user.id);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
Nc.on('user/' + this.id + "/joinSuccess", function(options) {
|
Nc.on('user/' + this.id + "/joinSuccess", function(options) {
|
||||||
self.sendControlCommand("joinSuccess", options);
|
self.sendControlCommand("joinSuccess", options);
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -208,10 +208,12 @@ function (Parent, Box2D, PhysicsEngine, ViewManager, PlayerController, Nc, reque
|
||||||
};
|
};
|
||||||
|
|
||||||
var sortedPlayers = playersArray.sort(function(a,b) {
|
var sortedPlayers = playersArray.sort(function(a,b) {
|
||||||
if(a.score > b.score) return 1;
|
if(a.stats.score > b.stats.score) return -1;
|
||||||
if(a.score < b.score) return -1;
|
if(a.stats.score < b.stats.score) return 1;
|
||||||
if(a.deaths < b.deaths) return 1;
|
if(a.stats.deaths < b.stats.deaths) return -1;
|
||||||
if(a.deaths > b.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;
|
return 0;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
@ -236,7 +238,7 @@ function (Parent, Box2D, PhysicsEngine, ViewManager, PlayerController, Nc, reque
|
||||||
|
|
||||||
var lines = [];
|
var lines = [];
|
||||||
sortedPlayers.forEach(function(player, i) {
|
sortedPlayers.forEach(function(player, i) {
|
||||||
var name = player == this.me ? "You" : "Player " + (Object.keys(this.players).indexOf(player.id) + 1);
|
var name = player.getNickname();
|
||||||
lines.push(
|
lines.push(
|
||||||
pad("" + (i + 1) + ".", 2, false) + " " +
|
pad("" + (i + 1) + ".", 2, false) + " " +
|
||||||
pad(name, 12, true) +
|
pad(name, 12, true) +
|
||||||
|
|
|
||||||
|
|
@ -13,7 +13,6 @@ function (ProtocolHelper, GameController, User, Nc, Settings, DomController) {
|
||||||
this.socketLink = socketLink;
|
this.socketLink = socketLink;
|
||||||
this.gameController = null;
|
this.gameController = null;
|
||||||
this.users = {};
|
this.users = {};
|
||||||
this.userId = null;
|
|
||||||
|
|
||||||
this.socketLink.on('connect', this.onConnect.bind(this));
|
this.socketLink.on('connect', this.onConnect.bind(this));
|
||||||
this.socketLink.on('disconnect', this.onDisconnect.bind(this));
|
this.socketLink.on('disconnect', this.onDisconnect.bind(this));
|
||||||
|
|
@ -57,12 +56,11 @@ function (ProtocolHelper, GameController, User, Nc, Settings, DomController) {
|
||||||
|
|
||||||
Networker.prototype.onJoinSuccess = function (options) {
|
Networker.prototype.onJoinSuccess = function (options) {
|
||||||
console.log("join success")
|
console.log("join success")
|
||||||
this.userId = options.userId;
|
|
||||||
|
|
||||||
this.gameController = new GameController();
|
this.gameController = new GameController();
|
||||||
this.gameController.loadLevel(options.levelUid);
|
this.gameController.loadLevel(options.levelUid);
|
||||||
|
|
||||||
this.onUserJoined(options.userId);
|
this.onUserJoined(options.user);
|
||||||
|
|
||||||
if (options.joinedUsers) {
|
if (options.joinedUsers) {
|
||||||
for(var i = 0; i < options.joinedUsers.length; i++) {
|
for(var i = 0; i < options.joinedUsers.length; i++) {
|
||||||
|
|
@ -114,9 +112,10 @@ function (ProtocolHelper, GameController, User, Nc, Settings, DomController) {
|
||||||
|
|
||||||
// Commands from server
|
// Commands from server
|
||||||
|
|
||||||
Networker.prototype.onUserJoined = function (userId) {
|
Networker.prototype.onUserJoined = function (options) {
|
||||||
var user = new User(userId);
|
var user = new User(options.id, options);
|
||||||
this.users[userId] = user;
|
console.log(options.nickname)
|
||||||
|
this.users[user.id] = user;
|
||||||
|
|
||||||
if (this.gameController
|
if (this.gameController
|
||||||
&& this.gameController.level
|
&& this.gameController.level
|
||||||
|
|
|
||||||
|
|
@ -6,8 +6,8 @@ define([
|
||||||
|
|
||||||
function (Parent, Nc, Settings) {
|
function (Parent, Nc, Settings) {
|
||||||
|
|
||||||
function Player(id, physicsEngine) {
|
function Player(id, physicsEngine, user) {
|
||||||
Parent.call(this, id, physicsEngine);
|
Parent.call(this, id, physicsEngine, user);
|
||||||
|
|
||||||
this.playerInfoView = null;
|
this.playerInfoView = null;
|
||||||
this.playerInfoViewVisibleTimeout = null;
|
this.playerInfoViewVisibleTimeout = null;
|
||||||
|
|
@ -81,6 +81,10 @@ function (Parent, Nc, Settings) {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Player.prototype.getNickname = function() {
|
||||||
|
return this.user.options.nickname;
|
||||||
|
};
|
||||||
|
|
||||||
Player.prototype.render = function() {
|
Player.prototype.render = function() {
|
||||||
|
|
||||||
// dolls are self responsible
|
// dolls are self responsible
|
||||||
|
|
|
||||||
|
|
@ -94,7 +94,7 @@ function (PhysicsEngine, TiledLevel, Player, Nc) {
|
||||||
}
|
}
|
||||||
|
|
||||||
GameController.prototype.createPlayer = function(user) {
|
GameController.prototype.createPlayer = function(user) {
|
||||||
var player = new Player(user.id, this.physicsEngine);
|
var player = new Player(user.id, this.physicsEngine, user);
|
||||||
this.players[user.id] = player;
|
this.players[user.id] = player;
|
||||||
return player;
|
return player;
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -10,13 +10,14 @@ define([
|
||||||
|
|
||||||
function (Doll, Settings, Nc, Exception, SpectatorDoll, RagDoll) {
|
function (Doll, Settings, Nc, Exception, SpectatorDoll, RagDoll) {
|
||||||
|
|
||||||
function Player (id, physicsEngine) {
|
function Player (id, physicsEngine, user) {
|
||||||
this.stats = {
|
this.stats = {
|
||||||
health: 100,
|
health: 100,
|
||||||
deaths: 0,
|
deaths: 0,
|
||||||
score: 0
|
score: 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.user = user;
|
||||||
this.physicsEngine = physicsEngine;
|
this.physicsEngine = physicsEngine;
|
||||||
this.playerController = null;
|
this.playerController = null;
|
||||||
this.doll;
|
this.doll;
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,13 @@
|
||||||
define(function () {
|
define(function () {
|
||||||
|
|
||||||
function User (id) {
|
function User (id, options) {
|
||||||
this.id = id;
|
this.id = id;
|
||||||
|
this.options = options;
|
||||||
|
|
||||||
|
this.options.id = this.id;
|
||||||
|
if(!this.options.nickname) {
|
||||||
|
this.options.nickname = this.id;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return User;
|
return User;
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,7 @@ define([
|
||||||
function (Parent, ProtocolHelper, Nc) {
|
function (Parent, ProtocolHelper, Nc) {
|
||||||
|
|
||||||
function User (socketLink, coordinator) {
|
function User (socketLink, coordinator) {
|
||||||
Parent.call(this, socketLink.id);
|
Parent.call(this, socketLink.id, {});
|
||||||
|
|
||||||
this.coordinator = coordinator;
|
this.coordinator = coordinator;
|
||||||
this.socketLink = socketLink;
|
this.socketLink = socketLink;
|
||||||
|
|
@ -58,7 +58,11 @@ function (Parent, ProtocolHelper, Nc) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.channelPipe.send('channel', { addUser: this.id });
|
var userOptions = {
|
||||||
|
id: this.id,
|
||||||
|
nickname: options.nickname
|
||||||
|
}
|
||||||
|
this.channelPipe.send('channel', { addUser: userOptions });
|
||||||
};
|
};
|
||||||
|
|
||||||
User.prototype.onGameCommand = function(options) {
|
User.prototype.onGameCommand = function(options) {
|
||||||
|
|
|
||||||
|
|
@ -47,7 +47,7 @@
|
||||||
</div>
|
</div>
|
||||||
</form>
|
</form>
|
||||||
|
|
||||||
<form action="game.html" method="GET" id="customjoinform">
|
<form action="#" method="GET" id="customjoinform">
|
||||||
<p>
|
<p>
|
||||||
<label>
|
<label>
|
||||||
Link to share with your friends<br>
|
Link to share with your friends<br>
|
||||||
|
|
@ -60,7 +60,7 @@
|
||||||
</p>
|
</p>
|
||||||
</form>
|
</form>
|
||||||
|
|
||||||
<form action="game.html" method="GET" id="listform">
|
<form action="#" method="GET" id="listform">
|
||||||
<h2>Channel list</h2>
|
<h2>Channel list</h2>
|
||||||
<table>
|
<table>
|
||||||
<thead>
|
<thead>
|
||||||
|
|
@ -113,7 +113,7 @@
|
||||||
}
|
}
|
||||||
document.body.className = "";
|
document.body.className = "";
|
||||||
} else {
|
} else {
|
||||||
console.error("Ajax error: " + xhr.status + " " + xhr.statusText)
|
console.error("Ajax error: " + xhr.status + " " + xhr.responseText)
|
||||||
$("#list").innerHTML = "";
|
$("#list").innerHTML = "";
|
||||||
document.body.className = "offline";
|
document.body.className = "offline";
|
||||||
}
|
}
|
||||||
|
|
@ -172,65 +172,77 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
$("form#createform").onsubmit = function(e) {
|
$("form#createform").onsubmit = function(e) {
|
||||||
var maps = [];
|
try {
|
||||||
var checkboxes = document.querySelectorAll("form#createform input[name=maps]");
|
|
||||||
for (var i = 0; i < checkboxes.length; i++) {
|
var maps = [];
|
||||||
var checkbox = checkboxes[i];
|
var checkboxes = document.querySelectorAll("form#createform input[name=maps]");
|
||||||
if(checkbox.checked) {
|
for (var i = 0; i < checkboxes.length; i++) {
|
||||||
maps.push(checkbox.value);
|
var checkbox = checkboxes[i];
|
||||||
|
if(checkbox.checked) {
|
||||||
|
maps.push(checkbox.value);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
if(maps.length == 0) {
|
||||||
|
alert("Please choose at least one map.")
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
};
|
|
||||||
|
|
||||||
if(maps.length == 0) {
|
var name = $("#customname").value;
|
||||||
alert("Please choose at least one map.")
|
if(!name) {
|
||||||
return false;
|
alert("Please provide a channel name.")
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
localStorage["customname"] = name;
|
||||||
|
|
||||||
|
var options = {
|
||||||
|
channelName: name,
|
||||||
|
maps: maps,
|
||||||
|
maxUsers: 10,
|
||||||
|
minUsers: 2
|
||||||
|
}
|
||||||
|
|
||||||
|
var xhr = new XMLHttpRequest();
|
||||||
|
xhr.onreadystatechange = function() {
|
||||||
|
if(xhr.readyState == 4) {
|
||||||
|
if(xhr.status == 200) {
|
||||||
|
onCreateSuccess(JSON.parse(xhr.responseText).success);
|
||||||
|
} else {
|
||||||
|
console.log(xhr.responseText)
|
||||||
|
alert(JSON.parse(xhr.responseText).error)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
xhr.open("POST", "/api", true);
|
||||||
|
xhr.send(JSON.stringify({command:"createChannel", options: options}));
|
||||||
|
} catch(e) {
|
||||||
|
console.error(e)
|
||||||
}
|
}
|
||||||
|
|
||||||
var name = $("#customname").value;
|
|
||||||
if(!name) {
|
|
||||||
alert("Please provide a channel name.")
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
localStorage["customname"] = name;
|
|
||||||
|
|
||||||
var options = {
|
|
||||||
channelName: name,
|
|
||||||
maps: maps,
|
|
||||||
maxUsers: 10,
|
|
||||||
minUsers: 2
|
|
||||||
}
|
|
||||||
|
|
||||||
var xhr = new XMLHttpRequest();
|
|
||||||
xhr.onreadystatechange = function() {
|
|
||||||
if(xhr.readyState == 4) {
|
|
||||||
if(xhr.status == 200) {
|
|
||||||
onCreateSuccess(JSON.parse(xhr.responseText).success);
|
|
||||||
} else {
|
|
||||||
console.log(xhr.responseText)
|
|
||||||
alert(JSON.parse(xhr.responseText).error)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
xhr.open("POST", "/api", true);
|
|
||||||
xhr.send(JSON.stringify({command:"createChannel", options: options}));
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
$("form#customjoinform").onsubmit = function(e) {
|
$("form#customjoinform").onsubmit = function(e) {
|
||||||
var nickname = $("#nick").value;
|
try {
|
||||||
if(!nickname || nickname.length < 3) {
|
var nickname = $("#nick").value;
|
||||||
alert("nickname too short")
|
if(!nickname || nickname.length < 3) {
|
||||||
return false;
|
alert("nickname too short")
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
localStorage["player"] = JSON.stringify({nickname: nickname});
|
||||||
|
|
||||||
|
var name = $("#customname").value;
|
||||||
|
localStorage["channel"] = JSON.stringify({
|
||||||
|
name: name
|
||||||
|
});
|
||||||
|
|
||||||
|
window.location.href = "/game.html";
|
||||||
|
|
||||||
|
} catch(e) {
|
||||||
|
console.error(e)
|
||||||
}
|
}
|
||||||
localStorage["player"] = JSON.stringify({nickname: nickname});
|
|
||||||
|
|
||||||
var name = $("form#createform input[name=channel]").value;
|
|
||||||
localStorage["channel"] = JSON.stringify({
|
|
||||||
name: name
|
|
||||||
});
|
|
||||||
|
|
||||||
window.location.href = "/game.html";
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue