Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

beta is near. final fix

  • Loading branch information...
commit 259f06bb791bc4bd7e63adea74c024ca98dbf22c 1 parent e8f7c4f
@stravid authored
View
23 README.md
@@ -1,17 +1,18 @@
-Achtung die Kurve
+Achtung die Kurve ([Online Demo](http://stravid.com/projects/achtung-die-kurve/))
===
-Authors
----
-[David Strauß](http://www.stravid.com)
-[Mathias Paumgarten](http://www.mathias-paumgarten.com)
-
-Description
----
-This is a browser-game enterily driven by Javascript. The concept is based on a game
+### Description ###
+This is a browser game entirely driven by JavaScript. The concept is based on a game
called "Achtung die Kurve" what is a a very old Flash based game.
In this game you play a line which can be controlled by any device, usually the keyboard.
-By controlling you can decide to curve the line in any direction you desire. As long as you
-don't hit any other player of the frame your line is constantly growing which will eventually
+The line is constantly moving forward so the player has only control over the direction of the line.
+As long as you don't hit any other player or the border your line is constantly growing which will eventually
lead to a situation where you can't go anywhere without crashing someone or something.
+
+### Documentation ###
+The [documentation](https://github.com/stravid/achtung-die-kurve/blob/master/documentation.js) describes how to implement Achtung die Kurve on your very own website. There is also a [online demo](http://stravid.com/projects/achtung-die-kurve/)!
+
+### Authors ###
+David Strauß ([stravid.com](http://www.stravid.com))
+Mathias Paumgarten ([mathias-paumgarten.com](http://www.mathias-paumgarten.com))
View
26 documentation.js
@@ -23,29 +23,29 @@ var game = new Game("myCanvas", 500, 700, false);
var myPlayerID = game.addPlayer("Mathias");
/*
-* Removes a Player from the current game. The player will be left out, when the next round starts.
+* Removes a Player from the current game. The player will be left out, when the next round starts.
*
-* Parameter: playerID : int
+* Parameter: playerID : int
*/
game.removePlayer(myPlayerID);
/*
* Sets a callback to be triggered everytime a player dies.
*
-* Parameter: callback : funtion
+* Parameter: callback : function
*/
-game.setCollisionCallback(function (deadPlayer) {
- console.log ("Player with ID %i just died.");
+game.setCollisionCallback(function (playerID) {
+ console.log ("Player with ID %i just died.", playerID);
});
/*
-* Starts the game. With this action it imediatly starts drawing the lines.
+* Starts the game. With this action it immediatly starts drawing the lines.
*/
game.start();
/*
* Restarts the game at any point. This method doesn't drop the score that has been
-* set till now
+* set till now.
*/
game.restart();
@@ -63,13 +63,13 @@ game.stop();
*/
/*
-* Starts a mechanism that tracks the scroe of each player over multiple games
+* Starts a mechanism that tracks the score of each player over multiple games
* until you force the mechanism to stop.
*/
game.startSession();
/*
-* Stops the counting of scroes for each player. Doesn't reset scores.
+* Stops the counting of scores for each player. Doesn't reset scores.
*/
game.stopSession();
@@ -79,14 +79,16 @@ game.stopSession();
*/
game.setRoundCallback(function(stats) {
console.log("the winnerID is %i ", stats.winnerID);
- console.log("Ranks: %o", stats.rank); // prints an array including the IDs of the player
+
+ // prints an array including the IDs of the player
+ console.log("Ranks: %o", stats.rank);
});
/*
* ========================================
* There are a few accessors for info on each player. Playerinfo is always accessed over the
-* playerID. All players are managed and controlled over the playermanager.
+* playerID. All players are managed and controlled over the PlayerManager.
* ========================================
*/
@@ -96,7 +98,7 @@ game.setRoundCallback(function(stats) {
*/
/*
-* Returns the color of a player.
+* Returns the hex color of a player.
*
* Parameter: playerID : int
*
View
24 examples/html/index.html
@@ -30,11 +30,11 @@
<div id="rightColumn">
<h1>Achtung die Kurve</h1>
<p>
- Achtung die Kurve is a fast paced multiplayer single screen game. You can fork the project over at <a href="http://github.com/stravid/achtung-die-kurve/">GitHub</a>!
+ Play with your friends on a single keyboard and try to survive as long as possible!
+ You can fork the project over at <a href="http://github.com/stravid/achtung-die-kurve/">GitHub</a>!
</p>
<p>
- Created by<br />
<a href="http://mathias-paumgarten.com">Mathias Paumgarten</a><br />
<a href="http://stravid.com">David Strauß</a>
</p>
@@ -53,13 +53,21 @@
<div id="addPlayerContainer">
<h2>Add Player</h2>
<fieldset>
- <input type="text" id="playerNameInput" placeholder="Player Name"/>
- <select id="playerControlsSelect"></select>
+ <table cellspacing="0" cellpadding="0">
+ <tr>
+ <td class="left">
+ <input type="text" id="playerNameInput" placeholder="Player Name"/>
+ </td>
+ <td class="right">
+ <select id="playerControlsSelect"></select>
+ </td>
+ </tr>
+ </table>
<br />
<input type="button" id="addPlayerButton" class="button" value="Add Player"/>
</fieldset>
</div>
- </form>
+ </form>
</div>
<div id="leftColumn">
@@ -69,7 +77,11 @@
<h2 class="huge">Help</h2>
<h3 class="yellow">Introduction</h3>
<p>
- In <strong>Achtung die Kurve</strong> you are a colored line constantly moving forward, you can only change the direction you are heading. The goal of the game is to be the last moving line on the screen. To achieve this you have to dodge the other players, the borders and even yourself. So watch out and move around savely!
+ The goal of the game is to survive as long as possible. You and your friends share the keyboard to control the line.
+ The lines are constantly moving forward. The players can only adjust the direction of the line.
+ </p>
+ <p>
+ A player loses if his line touches a line or the border of the game. He then has to wait for the start of the next round to play again. For every player you survive you get a point.
</p>
<p>
<strong>Achtung die Kurve is played on a single screen, so make sure you have some friends around!</strong>
View
2  examples/html/javascripts/adk.minified.js
@@ -1 +1 @@
-var ColorManager=function(a,b){this.hue=Math.random();this.saturation=a;this.value=b};ColorManager.prototype.getColor=function(){this.hue+=0.618033988749895;this.hue%=1;return this.convertHSVToRGB(Math.floor(this.hue*360),this.saturation,this.value)};ColorManager.prototype.reset=function(){this.hue=Math.random()};ColorManager.prototype.convertRGBToHex=function(a){var b,c;b=a[0].toString(16);c=a[1].toString(16);a=a[2].toString(16);if(b.length<2)b="0"+b;if(c.length<2)c="0"+c;if(a.length<2)a="0"+a;return"#"+b+c+a};ColorManager.prototype.convertHSVToRGB=function(a,b,c){var e,g,f,d=[];if(b===0){d[0]=c;d[1]=c;d[2]=c}e=a/60;a=Math.floor(e);g=e-a;e=c*(1-b);f=c*(1-b*g);b=c*(1-b*(1-g));if(a===0)d=[c,b,e];else if(a==1)d=[f,c,e];else if(a==2)d=[e,c,b];else if(a==3)d=[e,f,c];else if(a==4)d=[b,e,c];else if(a==5)d=[c,e,f];return[Math.floor(d[0]*255),Math.floor(d[1]*255),Math.floor(d[2]*255)]};var Config={canvasWidth:0,canvasHeight:0,lineWidth:3,frameRate:100,pixelsPerSecond:100,holeSize:10,threshold:100,colorSaturation:0.99,colorValue:0.99},Engine=function(a,b){this.intervalID=0;this.drawingContext=a;this.players=b;this.lastHit=this.onRoundOver=this.onCollision=null;this.countWins=false};Engine.prototype.start=function(){var a=this;this.playerRank=[];if(this.intervalID===0)this.intervalID=setInterval(function(){a.draw()},1E3/Config.frameRate)};Engine.prototype.stop=function(){clearInterval(this.intervalID);this.intervalID=0};Engine.prototype.draw=function(){for(var a,b,c,e=false,g=0;g<this.players.length;g++){a=this.players[g];if(!(!a.isPlaying||!a.isAlive||a.canceled)){c=Config.pixelsPerSecond*(1E3/Config.frameRate/1E3);b=Math.cos(a.angle*Math.PI/180)*c;c=Math.sin(a.angle*Math.PI/180)*c;if(a.hole===0){if(this.hitTest({x:a.x+b,y:a.y+c})){this.playerRank.unshift(a.ID);a.isAlive=false;e=true;for(var f=0,d=0;d<this.players.length;d++)if(this.players[d].isAlive){f++;this.countWins&&this.players[d].wins++}f<2&&this.stop();this.checkForCallback(a.ID);if(f<2){this.onRoundOver&&this.onRoundOver();return}}this.drawingContext.strokeStyle=a.color;this.drawingContext.fillStyle=a.color;this.drawingContext.beginPath();this.drawingContext.lineWidth=Config.lineWidth;this.drawingContext.moveTo(a.x,a.y);this.drawingContext.lineTo(a.x+b,a.y+c);this.drawingContext.stroke()}else{a.hole--;a.hole===0&&a.calculateNextHole()}a.x+=b;a.y+=c;a.distance+=Math.sqrt(Math.pow(b,2)+Math.pow(c,2))}}if(!e)this.lastHit=null};Engine.prototype.hitTest=function(a){if(a.x>Config.canvasWidth||a.y>Config.canvasHeight||a.x<0||a.y<0)return true;if(this.drawingContext.getImageData(a.x,a.y,1,1).data[3]>Config.threshold)return true;return false};Engine.prototype.checkForCallback=function(a){if(this.onCollision){if(this.lastHit===null||this.lastHit!=a)this.onCollision(a);this.lastHit=a}};Engine.prototype.setCollisionCallback=function(a){this.onCollision=a};Engine.prototype.setRoundCallback=function(a){this.onRoundOver=a};var Game=function(a,b,c,e){if(e)this.useFullscreen=e;Config.canvasWidth=b;Config.canvasHeight=c;this.canvasElement=document.getElementById(a);if(this.useFullscreen){Config.canvasWidth=window.innerWidth;Config.canvasHeight=window.innerHeight}this.canvasElement.width=Config.canvasWidth;this.canvasElement.height=Config.canvasHeight;if(this.canvasElement.getContext)this.drawingContext=this.canvasElement.getContext("2d");else throw"No canvas support";this.playerManager=new PlayerManager;this.engine=new Engine(this.drawingContext,this.playerManager.players);this.engineOnHalt=false};Game.prototype.getDrawingContext=function(){return this.drawingContext};Game.prototype.start=function(){if(this.playerManager.numberOfPlayers()===0){this.engineOnHalt=true;this.drawFrame()}else{this.drawFrame();this.playerManager.initializePlayers();this.engine.start();this.engineOnHalt=false}};Game.prototype.restart=function(){this.engine.stop();this.drawingContext.clearRect(0,0,Config.canvasWidth,Config.canvasHeight);this.start()};Game.prototype.stop=function(){this.engine.stop()};Game.prototype.addPlayer=function(a){a=this.playerManager.addPlayer(a);this.engineOnHalt&&this.start();return a};Game.prototype.removePlayer=function(a){this.playerManager.removePlayer(a);game.playerManager.numberOfPlayersAlive()<1&&game.stop()};Game.prototype.handleControl=function(a,b){this.playerManager.navigatePlayer(a,b)};Game.prototype.setCollisionCallback=function(a){this.engine.setCollisionCallback(a)};Game.prototype.setRoundCallback=function(a){that=this;this.engine.setRoundCallback(function(){that.engine.playerRank.unshift(that.playerManager.getAlivePlayers()[0]);var b={winnerID:that.playerManager.getAlivePlayers()[0],rank:that.engine.playerRank};a(b)})};Game.prototype.startSession=function(){this.playerManager.resetScores();this.engine.countWins=true};Game.prototype.stopSession=function(){this.engine.countWins=false};Game.prototype.drawFrame=function(){this.drawingContext.lineWidth=10;this.drawingContext.strokeStyle="#E3D42E";this.drawingContext.strokeRect(0,0,Config.canvasWidth-0,Config.canvasHeight-0)};var Player=function(){this.y=this.x=200;this.speed=1;this.angle=0;this.color=this.name="";this.ID=null;this.distance=0;this.canceled=this.isAlive=this.isPlaying=false;this.wins=this.hole=0;this.calculateNextHole()};Player.prototype.navigate=function(a){a=Math.min(Math.max(a,-1),1);this.angle+=2*a;this.angle%=360;if(this.angle<0)this.angle+=360};Player.prototype.resetTimeout=function(){clearTimeout(this.holeTimeoutID)};Player.prototype.calculateNextHole=function(){var a=this;this.holeTimoutID=setTimeout(function(){a.hole=parseInt(Config.holeSize/(1E3/Config.frameRate/1E3*Config.pixelsPerSecond))},(5+Math.random()*5)*1E3)};var PlayerManager=function(){this.players=[];this.colorManager=new ColorManager(Config.colorSaturation,Config.colorValue)};PlayerManager.prototype.addPlayer=function(a){var b=new Player;b.name=a;b.color=this.getColor();return b.ID=this.playerPush(b)};PlayerManager.prototype.playerPush=function(a){for(var b=0;b<this.players.length;b++)if(this.players[b].canceled){this.players[b]=a;return b}this.players.push(a);return this.players.length-1};PlayerManager.prototype.removePlayer=function(a){this.getPlayerByID(a).canceled=true};PlayerManager.prototype.initializePlayers=function(){for(var a=0;a<this.players.length;a++){var b=this.players[a];b.x=Utilities.random(Config.canvasWidth/4,3*Config.canvasWidth/4);b.y=Utilities.random(Config.canvasHeight/4,3*Config.canvasHeight/4);b.angle=Math.random()*360;b.isPlaying=true;b.isAlive=true;b.resetTimeout()}};PlayerManager.prototype.getColor=function(){return this.colorManager.convertRGBToHex(this.colorManager.getColor())};PlayerManager.prototype.navigatePlayer=function(a,b){this.getPlayerByID(a).navigate(b)};PlayerManager.prototype.numberOfPlayersAlive=function(){for(var a=0,b=0;b<this.players.length;b++)this.players[b].isAlive&&!this.players[b].canceled&&a++;return a};PlayerManager.prototype.numberOfPlayers=function(){for(var a=0,b=0;b<this.players.length;b++)this.players[b].canceled||a++;return a};PlayerManager.prototype.resetScores=function(){for(var a=0;a<this.players.length;a++){this.players[a].wins=0;this.players[a].distane=0}};PlayerManager.prototype.getPlayerByID=function(a){return this.players[a]};PlayerManager.prototype.getPlayerName=function(a){return this.players[a].name};PlayerManager.prototype.getPlayerDistance=function(a){return this.players[a].distance};PlayerManager.prototype.getPlayerColor=function(a){return this.players[a].color};PlayerManager.prototype.getPlayerWins=function(a){return this.players[a].wins};PlayerManager.prototype.getAlivePlayers=function(){for(var a=[],b=0;b<this.players.length;b++)this.players[b].isAlive&&!this.players[b].canceled&&a.push(this.players[b].ID);return a};var Utilities={random:function(a,b){return Math.floor(Math.random()*(b-a+1))+a}};
+var ColorManager=function(a,b){this.hue=Math.random();this.saturation=a;this.value=b};ColorManager.prototype.getColor=function(){this.hue+=0.618033988749895;this.hue%=1;return this.convertHSVToRGB(Math.floor(this.hue*360),this.saturation,this.value)};ColorManager.prototype.reset=function(){this.hue=Math.random()};ColorManager.prototype.convertRGBToHex=function(a){var b,c;b=a[0].toString(16);c=a[1].toString(16);a=a[2].toString(16);if(b.length<2)b="0"+b;if(c.length<2)c="0"+c;if(a.length<2)a="0"+a;return"#"+b+c+a};ColorManager.prototype.convertHSVToRGB=function(a,b,c){var e,g,f,d=[];if(b===0){d[0]=c;d[1]=c;d[2]=c}e=a/60;a=Math.floor(e);g=e-a;e=c*(1-b);f=c*(1-b*g);b=c*(1-b*(1-g));if(a===0)d=[c,b,e];else if(a==1)d=[f,c,e];else if(a==2)d=[e,c,b];else if(a==3)d=[e,f,c];else if(a==4)d=[b,e,c];else if(a==5)d=[c,e,f];return[Math.floor(d[0]*255),Math.floor(d[1]*255),Math.floor(d[2]*255)]};var Config={canvasWidth:0,canvasHeight:0,lineWidth:3,frameRate:100,pixelsPerSecond:100,holeSize:10,threshold:100,colorSaturation:0.99,colorValue:0.99},Engine=function(a,b){this.intervalID=0;this.drawingContext=a;this.players=b;this.lastHit=this.onRoundOver=this.onCollision=null;this.countWins=false};Engine.prototype.start=function(){var a=this;this.playerRank=[];if(this.intervalID===0)this.intervalID=setInterval(function(){a.draw()},1E3/Config.frameRate)};Engine.prototype.stop=function(){clearInterval(this.intervalID);this.intervalID=0};Engine.prototype.draw=function(){for(var a,b,c,e=false,g=0;g<this.players.length;g++){a=this.players[g];if(!(!a.isPlaying||!a.isAlive||a.canceled)){c=Config.pixelsPerSecond*(1E3/Config.frameRate/1E3);b=Math.cos(a.angle*Math.PI/180)*c;c=Math.sin(a.angle*Math.PI/180)*c;if(a.hole===0){if(this.hitTest({x:a.x+b,y:a.y+c})){this.playerRank.unshift(a.ID);a.isAlive=false;e=true;for(var f=0,d=0;d<this.players.length;d++)if(this.players[d].isAlive&&this.players[d].isPlaying&&!this.players[d].canceled){f++;this.countWins&&this.players[d].wins++}f<2&&this.stop();this.checkForCallback(a.ID);if(f<2){this.onRoundOver&&this.onRoundOver();return}}this.drawingContext.strokeStyle=a.color;this.drawingContext.fillStyle=a.color;this.drawingContext.beginPath();this.drawingContext.lineWidth=Config.lineWidth;this.drawingContext.moveTo(a.x,a.y);this.drawingContext.lineTo(a.x+b,a.y+c);this.drawingContext.stroke()}else{a.hole--;a.hole===0&&a.calculateNextHole()}a.x+=b;a.y+=c;a.distance+=Math.sqrt(Math.pow(b,2)+Math.pow(c,2))}}if(!e)this.lastHit=null};Engine.prototype.hitTest=function(a){if(a.x>Config.canvasWidth||a.y>Config.canvasHeight||a.x<0||a.y<0)return true;if(this.drawingContext.getImageData(a.x,a.y,1,1).data[3]>Config.threshold)return true;return false};Engine.prototype.checkForCallback=function(a){if(this.onCollision){if(this.lastHit===null||this.lastHit!=a)this.onCollision(a);this.lastHit=a}};Engine.prototype.setCollisionCallback=function(a){this.onCollision=a};Engine.prototype.setRoundCallback=function(a){this.onRoundOver=a};var Game=function(a,b,c,e){if(e)this.useFullscreen=e;Config.canvasWidth=b;Config.canvasHeight=c;this.canvasElement=document.getElementById(a);if(this.useFullscreen){Config.canvasWidth=window.innerWidth;Config.canvasHeight=window.innerHeight}this.canvasElement.width=Config.canvasWidth;this.canvasElement.height=Config.canvasHeight;if(this.canvasElement.getContext)this.drawingContext=this.canvasElement.getContext("2d");else throw"No canvas support";this.playerManager=new PlayerManager;this.engine=new Engine(this.drawingContext,this.playerManager.players);this.engineOnHalt=false};Game.prototype.getDrawingContext=function(){return this.drawingContext};Game.prototype.start=function(){if(this.playerManager.numberOfPlayers()<2){this.engineOnHalt=true;this.drawFrame()}else{this.drawFrame();this.playerManager.initializePlayers();this.engine.start();this.engineOnHalt=false}};Game.prototype.restart=function(){this.engine.stop();this.drawingContext.clearRect(0,0,Config.canvasWidth,Config.canvasHeight);this.start()};Game.prototype.stop=function(){this.engine.stop()};Game.prototype.addPlayer=function(a){a=this.playerManager.addPlayer(a);this.engineOnHalt&&this.start();return a};Game.prototype.removePlayer=function(a){this.playerManager.removePlayer(a);if(this.playerManager.numberOfPlayersAlive()<2){this.stop();this.engine.onRoundOver&&this.engine.onRoundOver()}};Game.prototype.handleControl=function(a,b){this.playerManager.navigatePlayer(a,b)};Game.prototype.setCollisionCallback=function(a){this.engine.setCollisionCallback(a)};Game.prototype.setRoundCallback=function(a){that=this;this.engine.setRoundCallback(function(){that.engine.playerRank.unshift(that.playerManager.getAlivePlayers()[0]);var b={winnerID:that.playerManager.getAlivePlayers()[0],rank:that.engine.playerRank};a(b)})};Game.prototype.startSession=function(){this.playerManager.resetScores();this.engine.countWins=true};Game.prototype.stopSession=function(){this.engine.countWins=false};Game.prototype.drawFrame=function(){this.drawingContext.lineWidth=10;this.drawingContext.strokeStyle="#E3D42E";this.drawingContext.strokeRect(0,0,Config.canvasWidth-0,Config.canvasHeight-0)};var Player=function(){this.y=this.x=200;this.speed=1;this.angle=0;this.color=this.name="";this.ID=null;this.distance=0;this.canceled=this.isAlive=this.isPlaying=false;this.wins=this.hole=0;this.calculateNextHole()};Player.prototype.navigate=function(a){a=Math.min(Math.max(a,-1),1);this.angle+=2*a;this.angle%=360;if(this.angle<0)this.angle+=360};Player.prototype.resetTimeout=function(){clearTimeout(this.holeTimeoutID)};Player.prototype.calculateNextHole=function(){var a=this;this.holeTimoutID=setTimeout(function(){a.hole=parseInt(Config.holeSize/(1E3/Config.frameRate/1E3*Config.pixelsPerSecond))},(5+Math.random()*5)*1E3)};var PlayerManager=function(){this.players=[];this.colorManager=new ColorManager(Config.colorSaturation,Config.colorValue)};PlayerManager.prototype.addPlayer=function(a){var b=new Player;b.name=a;b.color=this.getColor();b.ID=this.players.length;return this.playerPush(b)};PlayerManager.prototype.playerPush=function(a){for(var b=0;b<this.players.length;b++)if(this.players[b].canceled){this.players[b]=a;return b}this.players.push(a);return this.players.length-1};PlayerManager.prototype.removePlayer=function(a){this.getPlayerByID(a).canceled=true};PlayerManager.prototype.initializePlayers=function(){for(var a=0;a<this.players.length;a++){var b=this.players[a];b.x=Utilities.random(Config.canvasWidth/4,3*Config.canvasWidth/4);b.y=Utilities.random(Config.canvasHeight/4,3*Config.canvasHeight/4);b.angle=Math.random()*360;b.isPlaying=true;b.isAlive=true;b.resetTimeout()}};PlayerManager.prototype.getColor=function(){return this.colorManager.convertRGBToHex(this.colorManager.getColor())};PlayerManager.prototype.navigatePlayer=function(a,b){this.getPlayerByID(a).navigate(b)};PlayerManager.prototype.numberOfPlayersAlive=function(){for(var a=0,b=0;b<this.players.length;b++)this.players[b].isAlive&&!this.players[b].canceled&&a++;return a};PlayerManager.prototype.numberOfPlayers=function(){for(var a=0,b=0;b<this.players.length;b++)this.players[b].canceled||a++;return a};PlayerManager.prototype.resetScores=function(){for(var a=0;a<this.players.length;a++){this.players[a].wins=0;this.players[a].distane=0}};PlayerManager.prototype.getPlayerByID=function(a){return this.players[a]};PlayerManager.prototype.getPlayerName=function(a){return this.players[a].name};PlayerManager.prototype.getPlayerDistance=function(a){return this.players[a].distance};PlayerManager.prototype.getPlayerColor=function(a){return this.players[a].color};PlayerManager.prototype.getPlayerWins=function(a){return this.players[a].wins};PlayerManager.prototype.getAlivePlayers=function(){for(var a=[],b=0;b<this.players.length;b++)this.players[b].isAlive&&!this.players[b].canceled&&a.push(this.players[b].ID);return a};var Utilities={random:function(a,b){return Math.floor(Math.random()*(b-a+1))+a}};
View
235 examples/html/javascripts/script.js
@@ -1,7 +1,10 @@
-(function() {
+/*global window: false */
+
+var Game = Game || {};
+
+(function(Game) {
var canvasID = 'canvas',
domLeftColumn = document.getElementById('leftColumn'),
- domRightColumn = document.getElementById('rightColumn'),
domHelpContainer = document.getElementById('helpContainer'),
domCanvas = document.getElementById(canvasID),
domAddPlayerButton = document.getElementById('addPlayerButton'),
@@ -20,9 +23,8 @@
temporarySortItem,
numberOfDirectionProcessesPerSecond = 100,
processCurrentDirectionsIntervalID,
- endscreenPlayers = [],
- killList = [],
scoreList = [],
+ roundResult = [],
i,
j,
player,
@@ -68,44 +70,48 @@
setCurrentDirection(keysInUse[event.keyCode].playerID, keysInUse[event.keyCode].direction);
}
},
- handleAddPlayerClick = function() {
- if (domPlayerNameInput.value.length > minimalPlayerNameLength) {
- addPlayer(domPlayerNameInput.value, domPlayerControlsSelect.value);
-
- domPlayerNameInput.value = '';
+ processCurrentDirections = function() {
+ for (var playerID in currentDirections) {
+ if (currentDirections.hasOwnProperty(playerID)) {
+ game.handleControl(playerID, currentDirections[playerID]);
+ }
}
},
+ startCurrentDirectionsProcess = function() {
+ processCurrentDirectionsIntervalID = setInterval(processCurrentDirections, 1000 / numberOfDirectionProcessesPerSecond);
+ },
handleStartGameClick = function() {
domCanvas.className = 'show';
domStartGameContainer.className = 'hide';
domHelpContainer.className = 'hide';
-
game.start();
- //game.restart();
game.startSession();
startCurrentDirectionsProcess();
},
- addPlayer = function(name, controlID) {
- player = {};
-
- player.ID = game.addPlayer(name);
- player.name = name;
- player.color = game.playerManager.getPlayerColor(player.ID);
- player.controlID = controlID;
- player.points = 0;
-
- players.push(player);
+ activateControls = function(playerID, controlID) {
+ keysInUse[listOfControls[controlID].leftKeyCode] = {
+ playerID: playerID,
+ direction: -1
+ };
- activateControls(player.ID, controlID);
- writePlayerControls();
+ keysInUse[listOfControls[controlID].rightKeyCode] = {
+ playerID: playerID,
+ direction: 1
+ };
- updatePlayerList();
- checkPlayerLimit();
+ listOfControls[controlID].inUse = true;
+ },
+ writePlayerControls = function() {
+ temporaryString = '';
- if (players.length > 1) {
- domStartGameContainer.className = 'show';
+ for (i = 0; i < listOfControls.length; i++) {
+ if (!listOfControls[i].inUse) {
+ temporaryString += '<option id="control-' + i + '" value="' + i + '">' + listOfControls[i].label + '</option>';
+ }
}
+
+ domPlayerControlsSelect.innerHTML = temporaryString;
},
sort = function(array, key) {
for (i = 0; i < array.length; i++) {
@@ -118,67 +124,24 @@
}
}
},
- handleCollision = function(playerID) {
- killList.unshift(playerID);
-
- if (game.playerManager.numberOfPlayersAlive() < 2) {
- game.stop();
-
- setTimeout(function() {
- game.restart();
- }, 3000);
-
- for (var i = 0; i < players.length; i++) {
- players[i].points = game.playerManager.getPlayerWins(players[i].ID);
- }
-
- updatePlayerList();
-
- for (var i = 0; i < players.length; i++) {
- var isIncluded = false;
-
- for (var j = 0; j < killList.length; j++) {
- if (players[i].ID == killList[j]) {
- isIncluded = true;
- break;
- }
- }
-
- if (!isIncluded) {
- killList.unshift(players[i].ID);
- break;
- }
- }
-
- drawEndScreen();
-
- killList = [];
- }
- },
- drawEndScreen = function() {
- drawingContext.font = "50px Georgia serif";
- drawingContext.textAlign = 'center';
- var start = (domLeftColumn.clientHeight / 2) - (50 * players.length) / 2
-
- for (var i = 0; i < killList.length; i++) {
- drawingContext.fillStyle = players[killList[i]].color;
- drawingContext.fillText(i + 1 + '. ' + players[killList[i]].name, domLeftColumn.clientWidth / 2, start + i * 50);
- }
- },
updatePlayerList = function() {
if (players.length) {
domPlayerListContainer.className = 'show';
- for (var i = 0; i < players.length; i++) {
- scoreList[i] = players[i];
+ scoreList = [];
+
+ for (i = 0; i < players.length; i++) {
+ if (players[i].isPlaying) {
+ scoreList.push(players[i]);
+ }
}
sort(scoreList, 'points');
temporaryString = '';
- for (var i = 0; i < scoreList.length; i++) {
- temporaryString += '<li style="color:' + scoreList[i].color + ';"> ' + scoreList[i].name + ' - ' + scoreList[i].points + ' points</li>';
+ for (i = 0; i < scoreList.length; i++) {
+ temporaryString += '<li id="' + scoreList[i].ID + '" style="color:' + scoreList[i].color + ';"> ' + scoreList[i].name + ' - ' + scoreList[i].points + ' points <span>Remove</span></li>';
}
domPlayerList.innerHTML = temporaryString;
@@ -186,13 +149,6 @@
domPlayerListContainer.className = 'hide';
}
},
- checkPlayerLimit = function() {
- if (getNumberOfUnusedControls()) {
- domAddPlayerContainer.className = 'show';
- } else {
- domAddPlayerContainer.className = 'hide';
- }
- },
getNumberOfUnusedControls = function() {
numberOfUnusedControls = 0;
@@ -204,37 +160,98 @@
return numberOfUnusedControls;
},
- activateControls = function(playerID, controlID) {
- keysInUse[listOfControls[controlID].leftKeyCode] = {
- playerID: playerID,
- direction: -1
- };
+ checkPlayerLimit = function() {
+ if (getNumberOfUnusedControls()) {
+ domAddPlayerContainer.className = 'show';
+ } else {
+ domAddPlayerContainer.className = 'hide';
+ }
+ },
+ addPlayer = function(name, controlID) {
+ player = {};
- keysInUse[listOfControls[controlID].rightKeyCode] = {
- playerID: playerID,
- direction: 1
- };
+ player.ID = game.addPlayer(name);
+ player.name = name;
+ player.color = game.playerManager.getPlayerColor(player.ID);
+ player.controlID = controlID;
+ player.points = 0;
+ player.isPlaying = true;
- listOfControls[controlID].inUse = true;
+ players.push(player);
+
+ activateControls(player.ID, controlID);
+ writePlayerControls();
+
+ updatePlayerList();
+ checkPlayerLimit();
+
+ if (players.length > 1 && domStartGameContainer.className == 'hide') {
+ domStartGameContainer.className = 'show';
+ }
},
- writePlayerControls = function() {
- temporaryString = '';
+ handleAddPlayerClick = function() {
+ if (domPlayerNameInput.value.length > minimalPlayerNameLength) {
+ addPlayer(domPlayerNameInput.value, domPlayerControlsSelect.value);
- for (i = 0; i < listOfControls.length; i++) {
- if (!listOfControls[i].inUse) {
- temporaryString += '<option id="control-' + i + '" value="' + i + '">' + listOfControls[i].label + '</option>';
+ domPlayerNameInput.value = '';
+ }
+ },
+ drawEndScreen = function() {
+ drawingContext.font = "50px Georgia serif";
+ drawingContext.textAlign = 'center';
+ var start = (domLeftColumn.clientHeight / 2) - (50 * players.length) / 2;
+
+ for (i = 0; i < roundResult.length; i++) {
+ drawingContext.fillStyle = players[roundResult[i]].color;
+ drawingContext.fillText(i + 1 + '. ' + players[roundResult[i]].name, domLeftColumn.clientWidth / 2, start + i * 50);
+ }
+ },
+ handleRoundEnd = function(statistics) {
+ game.stop();
+
+ roundResult = statistics.rank;
+
+ for (i = 0; i < players.length; i++) {
+ if (players[i].isPlaying) {
+ players[i].points = game.playerManager.getPlayerWins(players[i].ID);
}
}
- domPlayerControlsSelect.innerHTML = temporaryString;
+ updatePlayerList();
+ drawEndScreen();
+
+ setTimeout(function() {
+ game.restart();
+ }, 2500);
},
- processCurrentDirections = function() {
- for (i in currentDirections) {
- game.handleControl(i, currentDirections[i]);
+ removePlayer = function(playerID) {
+ playerID = parseInt(playerID, 10);
+
+ game.removePlayer(playerID);
+ var index = -1;
+
+ for (i = 0; i < players.length; i++) {
+ if (players[i].ID == playerID) {
+ index = i;
+ break;
+ }
}
+
+ delete keysInUse[listOfControls[players[i].controlID].leftKeyCode];
+ delete keysInUse[listOfControls[players[i].controlID].rightKeyCode];
+ delete currentDirections[playerID];
+ listOfControls[players[i].controlID].inUse = false;
+
+ players[index].isPlaying = false;
+
+ writePlayerControls();
+
+ updatePlayerList();
},
- startCurrentDirectionsProcess = function() {
- processCurrentDirectionsIntervalID = setInterval(processCurrentDirections, 1000 / numberOfDirectionProcessesPerSecond);
+ handleRemovePlayerClick = function(event) {
+ if (event.target.nodeName.toUpperCase() == 'SPAN') {
+ removePlayer(event.target.parentNode.id);
+ }
};
window.onkeydown = handleKeyDown;
@@ -243,7 +260,9 @@
domAddPlayerButton.onclick = handleAddPlayerClick;
domStartGameButton.onclick = handleStartGameClick;
- game.setCollisionCallback(handleCollision);
+ domPlayerList.onclick = handleRemovePlayerClick;
+
+ game.setRoundCallback(handleRoundEnd);
writePlayerControls();
-})();
+})(Game);
View
37 examples/html/stylesheets/style.css
@@ -76,7 +76,6 @@ html, body {
}
#rightColumn {
- width: 380px;
width: 25%;
display: block;
float: right;
@@ -113,6 +112,28 @@ input[type=text] {
background-color: transparent;
padding: 7px;
margin-bottom: 8px;
+ width: 100%;
+ display: inline-block;
+ margin-right: 5px;
+}
+
+fieldset {
+ width: 100%;
+}
+
+/* DIRTY HACK */
+
+table {
+ width: 100%
+}
+
+.left {
+ width: 100%;
+}
+
+.right {
+ width: auto;
+ padding-left: 20px;
}
select {
@@ -120,8 +141,10 @@ select {
background-color: #1b1b1b;
padding: 9px;
margin-bottom: 8px;
- width: 207px;
text-align: right;
+ display: inline-block;
+ margin-left: 5px;
+ width: auto;
}
ul {
@@ -135,4 +158,14 @@ ul {
#helpContainer p {
width: 75%;
+}
+
+li span {
+ display: none;
+ cursor: pointer;
+ color: red;
+}
+
+li:hover span {
+ display: inline-block;
}
View
2  javascripts/adk.minified.js
@@ -1 +1 @@
-var ColorManager=function(a,b){this.hue=Math.random();this.saturation=a;this.value=b};ColorManager.prototype.getColor=function(){this.hue+=0.618033988749895;this.hue%=1;return this.convertHSVToRGB(Math.floor(this.hue*360),this.saturation,this.value)};ColorManager.prototype.reset=function(){this.hue=Math.random()};ColorManager.prototype.convertRGBToHex=function(a){var b,c;b=a[0].toString(16);c=a[1].toString(16);a=a[2].toString(16);if(b.length<2)b="0"+b;if(c.length<2)c="0"+c;if(a.length<2)a="0"+a;return"#"+b+c+a};ColorManager.prototype.convertHSVToRGB=function(a,b,c){var e,g,f,d=[];if(b===0){d[0]=c;d[1]=c;d[2]=c}e=a/60;a=Math.floor(e);g=e-a;e=c*(1-b);f=c*(1-b*g);b=c*(1-b*(1-g));if(a===0)d=[c,b,e];else if(a==1)d=[f,c,e];else if(a==2)d=[e,c,b];else if(a==3)d=[e,f,c];else if(a==4)d=[b,e,c];else if(a==5)d=[c,e,f];return[Math.floor(d[0]*255),Math.floor(d[1]*255),Math.floor(d[2]*255)]};var Config={canvasWidth:0,canvasHeight:0,lineWidth:3,frameRate:100,pixelsPerSecond:100,holeSize:10,threshold:100,colorSaturation:0.99,colorValue:0.99},Engine=function(a,b){this.intervalID=0;this.drawingContext=a;this.players=b;this.lastHit=this.onRoundOver=this.onCollision=null;this.countWins=false};Engine.prototype.start=function(){var a=this;this.playerRank=[];if(this.intervalID===0)this.intervalID=setInterval(function(){a.draw()},1E3/Config.frameRate)};Engine.prototype.stop=function(){clearInterval(this.intervalID);this.intervalID=0};Engine.prototype.draw=function(){for(var a,b,c,e=false,g=0;g<this.players.length;g++){a=this.players[g];if(!(!a.isPlaying||!a.isAlive||a.canceled)){c=Config.pixelsPerSecond*(1E3/Config.frameRate/1E3);b=Math.cos(a.angle*Math.PI/180)*c;c=Math.sin(a.angle*Math.PI/180)*c;if(a.hole===0){if(this.hitTest({x:a.x+b,y:a.y+c})){this.playerRank.unshift(a.ID);a.isAlive=false;e=true;for(var f=0,d=0;d<this.players.length;d++)if(this.players[d].isAlive){f++;this.countWins&&this.players[d].wins++}f<2&&this.stop();this.checkForCallback(a.ID);if(f<2){this.onRoundOver&&this.onRoundOver();return}}this.drawingContext.strokeStyle=a.color;this.drawingContext.fillStyle=a.color;this.drawingContext.beginPath();this.drawingContext.lineWidth=Config.lineWidth;this.drawingContext.moveTo(a.x,a.y);this.drawingContext.lineTo(a.x+b,a.y+c);this.drawingContext.stroke()}else{a.hole--;a.hole===0&&a.calculateNextHole()}a.x+=b;a.y+=c;a.distance+=Math.sqrt(Math.pow(b,2)+Math.pow(c,2))}}if(!e)this.lastHit=null};Engine.prototype.hitTest=function(a){if(a.x>Config.canvasWidth||a.y>Config.canvasHeight||a.x<0||a.y<0)return true;if(a.x>Config.canvasWidth||a.y>Config.canvasHeight||a.x<0||a.y<0)return true;if(this.drawingContext.getImageData(a.x,a.y,1,1).data[3]>Config.threshold)return true;return false};Engine.prototype.checkForCallback=function(a){if(this.onCollision){if(this.lastHit===null||this.lastHit!=a)this.onCollision(a);this.lastHit=a}};Engine.prototype.setCollisionCallback=function(a){this.onCollision=a};Engine.prototype.setRoundCallback=function(a){this.onRoundOver=a};var Game=function(a,b,c,e){if(e)this.useFullscreen=e;Config.canvasWidth=b;Config.canvasHeight=c;this.canvasElement=document.getElementById(a);if(this.useFullscreen){Config.canvasWidth=window.innerWidth;Config.canvasHeight=window.innerHeight}this.canvasElement.width=Config.canvasWidth;this.canvasElement.height=Config.canvasHeight;if(this.canvasElement.getContext)this.drawingContext=this.canvasElement.getContext("2d");else throw"No canvas support";this.playerManager=new PlayerManager;this.engine=new Engine(this.drawingContext,this.playerManager.players);this.engineOnHalt=false};Game.prototype.getDrawingContext=function(){return this.drawingContext};Game.prototype.start=function(){if(this.playerManager.numberOfPlayers()===0){this.engineOnHalt=true;this.drawFrame()}else{this.drawFrame();this.playerManager.initializePlayers();this.engine.start();this.engineOnHalt=false}};Game.prototype.restart=function(){this.engine.stop();this.drawingContext.clearRect(0,0,Config.canvasWidth,Config.canvasHeight);this.start()};Game.prototype.stop=function(){this.engine.stop()};Game.prototype.addPlayer=function(a){a=this.playerManager.addPlayer(a);this.engineOnHalt&&this.start();return a};Game.prototype.removePlayer=function(a){this.playerManager.removePlayer(a);game.playerManager.numberOfPlayersAlive()<1&&game.stop()};Game.prototype.handleControl=function(a,b){this.playerManager.navigatePlayer(a,b)};Game.prototype.setCollisionCallback=function(a){this.engine.setCollisionCallback(a)};Game.prototype.setRoundCallback=function(a){that=this;this.engine.setRoundCallback(function(){that.engine.playerRank.unshift(that.playerManager.getAlivePlayers()[0]);var b={winnerID:that.playerManager.getAlivePlayers()[0],rank:that.engine.playerRank};a(b)})};Game.prototype.startSession=function(){this.playerManager.resetScores();this.engine.countWins=true};Game.prototype.stopSession=function(){this.engine.countWins=false};Game.prototype.drawFrame=function(){this.drawingContext.lineWidth=10;this.drawingContext.strokeStyle="#E3D42E";this.drawingContext.strokeRect(0,0,Config.canvasWidth-0,Config.canvasHeight-0)};var Player=function(){this.y=this.x=200;this.speed=1;this.angle=0;this.color=this.name="";this.ID=null;this.distance=0;this.canceled=this.isAlive=this.isPlaying=false;this.wins=this.hole=0;this.calculateNextHole()};Player.prototype.navigate=function(a){a=Math.min(Math.max(a,-1),1);this.angle+=2*a;this.angle%=360;if(this.angle<0)this.angle+=360};Player.prototype.resetTimeout=function(){clearTimeout(this.holeTimeoutID)};Player.prototype.calculateNextHole=function(){var a=this;this.holeTimoutID=setTimeout(function(){a.hole=parseInt(Config.holeSize/(1E3/Config.frameRate/1E3*Config.pixelsPerSecond))},(5+Math.random()*5)*1E3)};var PlayerManager=function(){this.players=[];this.colorManager=new ColorManager(Config.colorSaturation,Config.colorValue)};PlayerManager.prototype.addPlayer=function(a){var b=new Player;b.name=a;b.color=this.getColor();return b.ID=this.playerPush(b)};PlayerManager.prototype.playerPush=function(a){for(var b=0;b<this.players.length;b++)if(this.players[b].canceled){this.players[b]=a;return b}this.players.push(a);return this.players.length-1};PlayerManager.prototype.removePlayer=function(a){this.getPlayerByID(a).canceled=true};PlayerManager.prototype.initializePlayers=function(){for(var a=0;a<this.players.length;a++){var b=this.players[a];b.x=Utilities.random(Config.canvasWidth/4,3*Config.canvasWidth/4);b.y=Utilities.random(Config.canvasHeight/4,3*Config.canvasHeight/4);b.angle=Math.random()*360;b.isPlaying=true;b.isAlive=true;b.resetTimeout()}};PlayerManager.prototype.getColor=function(){return this.colorManager.convertRGBToHex(this.colorManager.getColor())};PlayerManager.prototype.navigatePlayer=function(a,b){this.getPlayerByID(a).navigate(b)};PlayerManager.prototype.numberOfPlayersAlive=function(){for(var a=0,b=0;b<this.players.length;b++)this.players[b].isAlive&&!this.players[b].canceled&&a++;return a};PlayerManager.prototype.numberOfPlayers=function(){for(var a=0,b=0;b<this.players.length;b++)this.players[b].canceled||a++;return a};PlayerManager.prototype.resetScores=function(){for(var a=0;a<this.players.length;a++){this.players[a].wins=0;this.players[a].distane=0}};PlayerManager.prototype.getPlayerByID=function(a){return this.players[a]};PlayerManager.prototype.getPlayerName=function(a){return this.players[a].name};PlayerManager.prototype.getPlayerDistance=function(a){return this.players[a].distance};PlayerManager.prototype.getPlayerColor=function(a){return this.players[a].color};PlayerManager.prototype.getPlayerWins=function(a){return this.players[a].wins};PlayerManager.prototype.getAlivePlayers=function(){for(var a=[],b=0;b<this.players.length;b++)this.players[b].isAlive&&!this.players[b].canceled&&a.push(this.players[b].ID);return a};var Utilities={random:function(a,b){return Math.floor(Math.random()*(b-a+1))+a}};
+var ColorManager=function(a,b){this.hue=Math.random();this.saturation=a;this.value=b};ColorManager.prototype.getColor=function(){this.hue+=0.618033988749895;this.hue%=1;return this.convertHSVToRGB(Math.floor(this.hue*360),this.saturation,this.value)};ColorManager.prototype.reset=function(){this.hue=Math.random()};ColorManager.prototype.convertRGBToHex=function(a){var b,c;b=a[0].toString(16);c=a[1].toString(16);a=a[2].toString(16);if(b.length<2)b="0"+b;if(c.length<2)c="0"+c;if(a.length<2)a="0"+a;return"#"+b+c+a};ColorManager.prototype.convertHSVToRGB=function(a,b,c){var e,g,f,d=[];if(b===0){d[0]=c;d[1]=c;d[2]=c}e=a/60;a=Math.floor(e);g=e-a;e=c*(1-b);f=c*(1-b*g);b=c*(1-b*(1-g));if(a===0)d=[c,b,e];else if(a==1)d=[f,c,e];else if(a==2)d=[e,c,b];else if(a==3)d=[e,f,c];else if(a==4)d=[b,e,c];else if(a==5)d=[c,e,f];return[Math.floor(d[0]*255),Math.floor(d[1]*255),Math.floor(d[2]*255)]};var Config={canvasWidth:0,canvasHeight:0,lineWidth:3,frameRate:100,pixelsPerSecond:100,holeSize:10,threshold:100,colorSaturation:0.99,colorValue:0.99},Engine=function(a,b){this.intervalID=0;this.drawingContext=a;this.players=b;this.lastHit=this.onRoundOver=this.onCollision=null;this.countWins=false};Engine.prototype.start=function(){var a=this;this.playerRank=[];if(this.intervalID===0)this.intervalID=setInterval(function(){a.draw()},1E3/Config.frameRate)};Engine.prototype.stop=function(){clearInterval(this.intervalID);this.intervalID=0};Engine.prototype.draw=function(){for(var a,b,c,e=false,g=0;g<this.players.length;g++){a=this.players[g];if(!(!a.isPlaying||!a.isAlive||a.canceled)){c=Config.pixelsPerSecond*(1E3/Config.frameRate/1E3);b=Math.cos(a.angle*Math.PI/180)*c;c=Math.sin(a.angle*Math.PI/180)*c;if(a.hole===0){if(this.hitTest({x:a.x+b,y:a.y+c})){this.playerRank.unshift(a.ID);a.isAlive=false;e=true;for(var f=0,d=0;d<this.players.length;d++)if(this.players[d].isAlive&&this.players[d].isPlaying&&!this.players[d].canceled){f++;this.countWins&&this.players[d].wins++}f<2&&this.stop();this.checkForCallback(a.ID);if(f<2){this.onRoundOver&&this.onRoundOver();return}}this.drawingContext.strokeStyle=a.color;this.drawingContext.fillStyle=a.color;this.drawingContext.beginPath();this.drawingContext.lineWidth=Config.lineWidth;this.drawingContext.moveTo(a.x,a.y);this.drawingContext.lineTo(a.x+b,a.y+c);this.drawingContext.stroke()}else{a.hole--;a.hole===0&&a.calculateNextHole()}a.x+=b;a.y+=c;a.distance+=Math.sqrt(Math.pow(b,2)+Math.pow(c,2))}}if(!e)this.lastHit=null};Engine.prototype.hitTest=function(a){if(a.x>Config.canvasWidth||a.y>Config.canvasHeight||a.x<0||a.y<0)return true;if(this.drawingContext.getImageData(a.x,a.y,1,1).data[3]>Config.threshold)return true;return false};Engine.prototype.checkForCallback=function(a){if(this.onCollision){if(this.lastHit===null||this.lastHit!=a)this.onCollision(a);this.lastHit=a}};Engine.prototype.setCollisionCallback=function(a){this.onCollision=a};Engine.prototype.setRoundCallback=function(a){this.onRoundOver=a};var Game=function(a,b,c,e){if(e)this.useFullscreen=e;Config.canvasWidth=b;Config.canvasHeight=c;this.canvasElement=document.getElementById(a);if(this.useFullscreen){Config.canvasWidth=window.innerWidth;Config.canvasHeight=window.innerHeight}this.canvasElement.width=Config.canvasWidth;this.canvasElement.height=Config.canvasHeight;if(this.canvasElement.getContext)this.drawingContext=this.canvasElement.getContext("2d");else throw"No canvas support";this.playerManager=new PlayerManager;this.engine=new Engine(this.drawingContext,this.playerManager.players);this.engineOnHalt=false};Game.prototype.getDrawingContext=function(){return this.drawingContext};Game.prototype.start=function(){if(this.playerManager.numberOfPlayers()<2){this.engineOnHalt=true;this.drawFrame()}else{this.drawFrame();this.playerManager.initializePlayers();this.engine.start();this.engineOnHalt=false}};Game.prototype.restart=function(){this.engine.stop();this.drawingContext.clearRect(0,0,Config.canvasWidth,Config.canvasHeight);this.start()};Game.prototype.stop=function(){this.engine.stop()};Game.prototype.addPlayer=function(a){a=this.playerManager.addPlayer(a);this.engineOnHalt&&this.start();return a};Game.prototype.removePlayer=function(a){this.playerManager.removePlayer(a);if(this.playerManager.numberOfPlayersAlive()<2){this.stop();this.engine.onRoundOver&&this.engine.onRoundOver()}};Game.prototype.handleControl=function(a,b){this.playerManager.navigatePlayer(a,b)};Game.prototype.setCollisionCallback=function(a){this.engine.setCollisionCallback(a)};Game.prototype.setRoundCallback=function(a){that=this;this.engine.setRoundCallback(function(){that.engine.playerRank.unshift(that.playerManager.getAlivePlayers()[0]);var b={winnerID:that.playerManager.getAlivePlayers()[0],rank:that.engine.playerRank};a(b)})};Game.prototype.startSession=function(){this.playerManager.resetScores();this.engine.countWins=true};Game.prototype.stopSession=function(){this.engine.countWins=false};Game.prototype.drawFrame=function(){this.drawingContext.lineWidth=10;this.drawingContext.strokeStyle="#E3D42E";this.drawingContext.strokeRect(0,0,Config.canvasWidth-0,Config.canvasHeight-0)};var Player=function(){this.y=this.x=200;this.speed=1;this.angle=0;this.color=this.name="";this.ID=null;this.distance=0;this.canceled=this.isAlive=this.isPlaying=false;this.wins=this.hole=0;this.calculateNextHole()};Player.prototype.navigate=function(a){a=Math.min(Math.max(a,-1),1);this.angle+=2*a;this.angle%=360;if(this.angle<0)this.angle+=360};Player.prototype.resetTimeout=function(){clearTimeout(this.holeTimeoutID)};Player.prototype.calculateNextHole=function(){var a=this;this.holeTimoutID=setTimeout(function(){a.hole=parseInt(Config.holeSize/(1E3/Config.frameRate/1E3*Config.pixelsPerSecond))},(5+Math.random()*5)*1E3)};var PlayerManager=function(){this.players=[];this.colorManager=new ColorManager(Config.colorSaturation,Config.colorValue)};PlayerManager.prototype.addPlayer=function(a){var b=new Player;b.name=a;b.color=this.getColor();b.ID=this.players.length;return this.playerPush(b)};PlayerManager.prototype.playerPush=function(a){for(var b=0;b<this.players.length;b++)if(this.players[b].canceled){this.players[b]=a;return b}this.players.push(a);return this.players.length-1};PlayerManager.prototype.removePlayer=function(a){this.getPlayerByID(a).canceled=true};PlayerManager.prototype.initializePlayers=function(){for(var a=0;a<this.players.length;a++){var b=this.players[a];b.x=Utilities.random(Config.canvasWidth/4,3*Config.canvasWidth/4);b.y=Utilities.random(Config.canvasHeight/4,3*Config.canvasHeight/4);b.angle=Math.random()*360;b.isPlaying=true;b.isAlive=true;b.resetTimeout()}};PlayerManager.prototype.getColor=function(){return this.colorManager.convertRGBToHex(this.colorManager.getColor())};PlayerManager.prototype.navigatePlayer=function(a,b){this.getPlayerByID(a).navigate(b)};PlayerManager.prototype.numberOfPlayersAlive=function(){for(var a=0,b=0;b<this.players.length;b++)this.players[b].isAlive&&!this.players[b].canceled&&a++;return a};PlayerManager.prototype.numberOfPlayers=function(){for(var a=0,b=0;b<this.players.length;b++)this.players[b].canceled||a++;return a};PlayerManager.prototype.resetScores=function(){for(var a=0;a<this.players.length;a++){this.players[a].wins=0;this.players[a].distane=0}};PlayerManager.prototype.getPlayerByID=function(a){return this.players[a]};PlayerManager.prototype.getPlayerName=function(a){return this.players[a].name};PlayerManager.prototype.getPlayerDistance=function(a){return this.players[a].distance};PlayerManager.prototype.getPlayerColor=function(a){return this.players[a].color};PlayerManager.prototype.getPlayerWins=function(a){return this.players[a].wins};PlayerManager.prototype.getAlivePlayers=function(){for(var a=[],b=0;b<this.players.length;b++)this.players[b].isAlive&&!this.players[b].canceled&&a.push(this.players[b].ID);return a};var Utilities={random:function(a,b){return Math.floor(Math.random()*(b-a+1))+a}};
View
2  javascripts/config.js
@@ -11,4 +11,4 @@ var Config = {
colorSaturation: 0.99,
colorValue: 0.99
-};
+};
View
10 javascripts/engine.js
@@ -56,7 +56,7 @@ Engine.prototype.draw = function() {
var count = 0;
for (var j = 0; j < this.players.length; j++) {
- if (this.players[j].isAlive) {
+ if (this.players[j].isAlive && this.players[j].isPlaying && !this.players[j].canceled) {
count++;
@@ -74,7 +74,7 @@ Engine.prototype.draw = function() {
if (this.onRoundOver) {
this.onRoundOver();
}
-
+
return;
}
}
@@ -109,10 +109,6 @@ Engine.prototype.draw = function() {
Engine.prototype.hitTest = function(point) {
// FIXME: Sometimes unexpected hits.
-
- if (point.x > Config.canvasWidth || point.y > Config.canvasHeight || point.x < 0 || point.y < 0) {
- return true;
- }
if (point.x > Config.canvasWidth || point.y > Config.canvasHeight || point.x < 0 || point.y < 0) {
return true;
@@ -147,4 +143,4 @@ Engine.prototype.setCollisionCallback = function(callback) {
Engine.prototype.setRoundCallback = function(callback) {
this.onRoundOver = callback;
-};
+};
View
15 javascripts/game.js
@@ -33,7 +33,7 @@ Game.prototype.getDrawingContext = function() {
Game.prototype.start = function() {
- if (this.playerManager.numberOfPlayers() === 0) {
+ if (this.playerManager.numberOfPlayers() < 2) {
this.engineOnHalt = true;
this.drawFrame();
return;
@@ -66,10 +66,14 @@ Game.prototype.addPlayer = function(name) {
};
Game.prototype.removePlayer = function (playerID) {
- this.playerManager.removePlayer(playerID);
+ this.playerManager.removePlayer(playerID);
- if (game.playerManager.numberOfPlayersAlive() < 1) {
- game.stop();
+ if (this.playerManager.numberOfPlayersAlive() < 2) {
+ this.stop();
+
+ if (this.engine.onRoundOver) {
+ this.engine.onRoundOver();
+ }
}
};
@@ -109,5 +113,4 @@ Game.prototype.drawFrame = function () {
this.drawingContext.lineWidth = 10;
this.drawingContext.strokeStyle = "#E3D42E";
this.drawingContext.strokeRect(0, 0, Config.canvasWidth - 0, Config.canvasHeight - 0);
-};
-
+};
View
8 javascripts/playermanager.js
@@ -9,7 +9,9 @@ PlayerManager.prototype.addPlayer = function(name) {
newPlayer.name = name;
newPlayer.color = this.getColor();
- return newPlayer.ID = this.playerPush(newPlayer);
+ newPlayer.ID = this.players.length;
+
+ return this.playerPush(newPlayer);
};
PlayerManager.prototype.playerPush = function (newPlayer) {
@@ -40,7 +42,7 @@ PlayerManager.prototype.initializePlayers = function() {
player.angle = Math.random() * 360;
player.isPlaying = true;
player.isAlive = true;
- player.resetTimeout();
+ player.resetTimeout();
}
};
@@ -116,4 +118,4 @@ PlayerManager.prototype.getAlivePlayers = function() {
}
return alivePlayers;
-};
+};
Please sign in to comment.
Something went wrong with that request. Please try again.