Commit 92437a73 authored by DoNaTio s.r.o.'s avatar DoNaTio s.r.o.
Browse files

WhoIsTurn

parent 253fa6fb
......@@ -3,22 +3,22 @@
* @returns {{setName: Function, getName: Function}}
*/
module.exports = function() {
module.exports = function () {
var _name = '',
_racket = '',
_id = '';
_id = '',
_team = 0;
return {
setName: function(name){
setName: function (name) {
_name = name;
},
getName: function(){
getName: function () {
return _name;
},
setId: function(id){
setId: function (id) {
_id = id;
},
getId: function(){
getId: function () {
return _id;
},
setRacket: function (Racket) {
......@@ -27,20 +27,11 @@ module.exports = function() {
getRacket: function () {
return _racket;
},
setTeam: function(team){
setTeam: function (team) {
_team = team;
},
getTeam: function(){
getTeam: function () {
return _team;
},
stringify: function(){
var that = this;
return JSON.stringify({
name:that.getName(),
id: that.getId(),
racket: that.getRacket().stringify()
});
}
};
}
\ No newline at end of file
......@@ -2,53 +2,49 @@
* PlayersCollection
* @returns {{getCount: Function, addPlayer: Function}}
*/
module.exports = function() {
module.exports = function () {
var _players = {};
var _countPlayers = 0;
var _internalIndex = 0;
var _whoIsTurn = 0;
return {
getCount: function(){
getCount: function () {
return _countPlayers;
},
increaseCount: function(){
increaseCount: function () {
_countPlayers++;
},
decreaseCount: function(){
decreaseCount: function () {
_countPlayers--;
},
addPlayer: function(player){
addPlayer: function (player) {
this.increaseCount();
player.setId( _internalIndex );
player.setId(_internalIndex);
player.setTeam(_internalIndex % 2);
_players[player.getId()] = player;
_internalIndex++;
console.log(player.getTeam());
},
getIds: function() {
getIds: function () {
var id = [];
Object.keys(_players).forEach(function(key) {
Object.keys(_players).forEach(function (key) {
id.push(_players[key].getId());
});
return id;
},
getPlayerById: function(id){
getPlayerById: function (id) {
return _players[id];
},
removeById: function(id){
if(this.getPlayerById(id)){
removeById: function (id) {
if (this.getPlayerById(id)) {
delete _players[id];
this.decreaseCount();
}
},
getWhoIsTurn: function(userId){
var nextId = 1;
if(this.getPlayerById(userId + 1)){
nextId++;
}
return nextId;
getAllPlayers: function(){
return _players;
}
};
}
\ No newline at end of file
/**
* WhoIsTurn
* @returns {{getCount: Function, addPlayer: Function}}
*/
module.exports = function (PlayersColliction, Score) {
var _playersColliction = PlayersColliction;
var _score = Score;
var _whichTeam = 0;
var _team0 = [];
var _team1 = [];
return {
resetTeamStack: function () {
var allPlayers = _playersColliction.getAllPlayers();
for (var key in allPlayers) {
if (allPlayers[key].getTeam() == 0) {
_team0.push(key);
} else {
_team1.push(key);
}
}
},
getNext: function () {
var next;
if (_whichTeam) {
if (_team1[0] == undefined) {
this.resetTeamStack(1);
}
next = _team1[0];
delete _team1[0];
} else {
if (_team1[0] == undefined) {
this.resetTeamStack(0);
}
next = _team0[0];
delete _team0[0];
}
return next;
},
/**
*
* @returns id
*/
getWhoIsTurn: function () {
return _whoIsTurn;
},
/**
* return user.Id
* @param type
*/
changeWhoIsTurn: function () {
if (_whichTeam) {
_whichTeam = 0;
} else {
_whichTeam = 1;
}
return this.getNext();
}
};
}
\ No newline at end of file
......@@ -40,15 +40,16 @@ function ClientCollision(Ball, Racket, User) {
var racketVector = attributes.racket.getVector();
var racketAxis = attributes.racket.getAxis();
var racketDimension = attributes.racket.getDimension();
console.log(attributes.user.getIsTurn());
/**
* Racket collision
*/
if (
//attributes.user.getIsTurn()
//&&
(ballAxis.x > racketAxis.x && ballAxis.x < racketAxis.x + racketDimension.width) //x coordinate
attributes.user.getIsTurn()
&&
(ballAxis.y < racketAxis.y && ballAxis.y > racketAxis.y - racketDimension.height) //y coordinate
(ballAxis.x > racketAxis.x && ballAxis.x < (racketAxis.x + racketDimension.width) ) //x coordinate
&&
(ballAxis.y < racketAxis.y && ballAxis.y > (racketAxis.y - racketDimension.height) ) //y coordinate
) {
console.log('collision ball with racket');
var BD = racketAxis, // bod dotyku (poz. lopticky)
......
......@@ -65,7 +65,7 @@ $(function () {
d.x = playgroundHalf;
}
racket.setAxis(event.pageX - $playground.offset().left,event.pageY - $playground.offset().top);
racket.setAxis(event.pageX - $playground.offset().left, event.pageY - $playground.offset().top);
drawRacket(d);
socket.emit('move racket', d);
......
......@@ -15,6 +15,7 @@ var playgroundDimmesion = {h: 776, w: 968};
var score = require('./Score.js')(ball, playgroundDimmesion);
var collision = require('./Collision.js')(playgroundDimmesion, ball);
var whoIsTurn = require('./WhoIsTurn.js')(PlayersCollection, score);
server.listen(port, function () {
console.log('Server listening at port %d', port);
......@@ -42,7 +43,7 @@ setInterval(function () {
'vy': newVector.vy
});
}
}, 10);
}, 1);
// Routing
app.use(express.static(__dirname + '/public'));
......@@ -99,7 +100,7 @@ io.on('connection', function (socket) {
console.log('who is turn', data);
socket.broadcast.emit('who is turn', {
'userId': PlayersCollection.getWhoIsTurn(data.userId)
'userId': PlayersCollection.getWhoIsTurn()
});
});
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment