summaryrefslogtreecommitdiff
path: root/app.js
diff options
context:
space:
mode:
Diffstat (limited to 'app.js')
-rw-r--r--app.js90
1 files changed, 73 insertions, 17 deletions
diff --git a/app.js b/app.js
index c6ed8ac..aea700a 100644
--- a/app.js
+++ b/app.js
@@ -1,9 +1,9 @@
var mongojs = require("mongojs");
var db = mongojs("localhost:27017/ThePixelWorld", ["account","progress"]);
-
var express = require("express");
var app = express();
+
var serv = require("http").Server(app);
app.get('/', function(req,res) {
@@ -80,7 +80,24 @@ var Player = function(id){
else
self.speedY = 0;
}
+
+ self.getInitPack = function() {
+ return {
+ id:self.id,
+ x:self.x,
+ y:self.y,
+ number:self.number
+ };
+ }
+ self.getUpdatePack = function() {
+ return {
+ id:self.id,
+ x:self.x,
+ y:self.y
+ };
+ }
Player.list[id] = self;
+ initPack.player.push(self.getInitPack());
return self;
}
@@ -102,10 +119,23 @@ Player.onConnect = function(socket){
else if (data.inputId === 'mouseAngle')
player.mouseAngle = data.state;
});
+
+ socket.emit('init',{
+ player:Player.getAllInitPack(),
+ bullet:Bullet.getAllInitPack(),
+ });
+}
+
+Player.getAllInitPack = function(){
+ var players = [];
+ for(var i in Player.list)
+ players.push(Player.list[i].getInitPack());
+ return players;
}
Player.onDisconnect = function(socket){
delete Player.list[socket.id];
+ removePack.player.push(socket.id);
}
Player.update = function(){
@@ -113,11 +143,7 @@ Player.update = function(){
for(var i in Player.list){
var player = Player.list[i];
player.update();
- pack.push({
- x:player.x,
- y:player.y,
- number:player.number
- });
+ pack.push(player.getUpdatePack());
}
return pack;
}
@@ -143,7 +169,25 @@ var Bullet = function(parent, angle) {
}
}
}
+
+ self.getInitPack = function() {
+ return {
+ id:self.id,
+ x:self.x,
+ y:self.y,
+ };
+ }
+
+ self.getUpdatePack = function() {
+ return {
+ id:self.id,
+ x:self.x,
+ y:self.y,
+ };
+ }
+
Bullet.list[self.id] = self;
+ initPack.bullet.push(self.getInitPack());
return self;
}
@@ -154,20 +198,21 @@ Bullet.update = function(){
for(var i in Bullet.list){
var bullet = Bullet.list[i];
bullet.update();
- if(bullet.toRemove)
+ if(bullet.toRemove){
delete Bullet.list[i];
+ removePack.bullet.push(bullet.id);
+ }
else
- pack.push({
- x:bullet.x,
- y:bullet.y,
- });
+ pack.push(bullet.getUpdatePack());
}
return pack;
}
-var USERS = {
- "bob":"bub",
- "asd": "fgh"
+Bullet.getAllInitPack = function(){
+ var bullets = [];
+ for(var i in Bullet.list)
+ bullets.push(Bullet.list[i].getInitPack());
+ return bullets;
}
var isValidPassword = function(data, cb){
@@ -203,6 +248,7 @@ io.sockets.on('connection', function(socket){
isValidPassword(data, function(res){
if (res){
Player.onConnect(socket);
+ socket.playerName = data.username;
socket.emit('signInResponse', {success:true});
} else {
socket.emit('signInResponse', {success:false});
@@ -228,7 +274,7 @@ io.sockets.on('connection', function(socket){
socket.on('sendMsgToServer', function(data){
var playerName = ("" + socket.id).slice(2,7);
for (var i in SOCKET_LIST){
- SOCKET_LIST[i].emit('addToChat', playerName + ': ' + data);
+ SOCKET_LIST[i].emit('addToChat', socket.playerName + ': ' + data);
}
});
@@ -239,6 +285,9 @@ io.sockets.on('connection', function(socket){
});
});
+var initPack = {player:[], bullet:[]};
+var removePack = {player:[], bullet:[]};
+
setInterval(function(){
var pack = {
player:Player.update(),
@@ -247,6 +296,13 @@ setInterval(function(){
for(var i in SOCKET_LIST){
var socket = SOCKET_LIST[i];
- socket.emit('newPositions', pack);
+ socket.emit('init', initPack);
+ socket.emit('update', pack);
+ socket.emit('remove', removePack);
}
-},1000/25); \ No newline at end of file
+ initPack.player = [];
+ initPack.bullet = [];
+ removePack.player = [];
+ removePack.bullet = [];
+
+}, 1000/25); \ No newline at end of file