Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge remote-tracking branch 'upstream/master'

  • Loading branch information...
commit 7c7cf10583d4e5cb80a4284c12e657b87352dacb 2 parents 9d11440 + c58685c
@rabbidrabbit authored
Showing with 82 additions and 58 deletions.
  1. +16 −1 color.js
  2. +10 −3 index.html
  3. +35 −32 player.js
  4. +20 −21 server.js
  5. +1 −1  snakes.html
View
17 color.js
@@ -40,6 +40,18 @@ Color.prototype.randomized = function(x) { return this.clone().randomize(x
Color.prototype.toString = function() {
return 'rgb(' + Math.round(this.r) + ', ' + Math.round(this.g) + ', ' + Math.round(this.b) + ')';
};
+Color.prototype.toHexString = function() {
+ return '#' + this.toInt().toString(16);
+};
+Color.prototype.toInt = function(rgb) {
+ return Math.round(this.r) << 16 | Math.round(this.g) << 8 | Math.round(this.b);
+}
+Color.fromInt = function(rgb) {
+ var b = rgb & 0xff;
+ var g = (rgb >>= 8) & 0xff;
+ var r = (rgb >>= 8) & 0xff;
+ return new Color(r, g, b);
+}
Color.prototype.clone = function() {
var c = new Color;
c.r = this.r, c.b = this.b, c.g = this.g
@@ -55,7 +67,10 @@ Color.clipComponent = function(x) {
return x > 255 ? 255 : x < 0 ? 0 : x;
}
Color.ify = function(data) {
- return new Color(data.r, data.g, data.b);
+ if(typeof data == "number")
+ return Color.fromInt(data);
+ else if(data instanceof Object)
+ return new Color(data.r, data.g, data.b);
}
Color.randomHue = function() {
var a = Math.random() * 3;
View
13 index.html
@@ -8,11 +8,18 @@
<script src="color.js"></script>
<script src="ball.js"></script>
<script src="world.js"></script>
- <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script><link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Terminal+Dosis+Light|Ubuntu:300|Ubuntu:300italic" />
+ <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script><link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Dosis:300,700" />
+ <meta property="og:title" content="Snakes - a game written in Javascript and HTML5 canvas"/>
+ <meta property="og:type" content="website"/>
+ <meta property="og:url" content="http://42nd.org:8090/"/>
+ <meta property="og:description" content="A game for two (or more) players. Control the snakes with the arrow keys and WASD. Eat or be eaten!"/>
+ <meta property="og:image" content="http://eric-wieser.tk/images/snaps/snakes.png" />
+ <meta property="fb:admins" content="eric.wieser"/>
<style>
html, body {
background: #202020; width: 100%; height: 100%; margin: 0; padding: 0;
- font-family: 'Terminal Dosis Light', Arial, sans-serif;
+ font-family: 'Dosis', Arial, sans-serif;
+ font-weight:300;
}
#message {
color: white;
@@ -44,7 +51,7 @@
width: 50%;
}
#scores li span.name {
- font-weight: bold;
+ font-weight: 700;
text-align: right;
padding-right: 0.5em;
}
View
67 player.js
@@ -16,40 +16,43 @@ Player = function Player(socket, name, snake) {
}
});
- socket.on('disconnect', function () {
- if($this.connected) {
- $this.onQuit();
- $this.connected = false;
- snake.destroy()
- name = null;
- snake = null;
- }
- });
+ socket.on('disconnect', this.disconnect.bind(this));
+}
+Player.prototype.disconnect = function() {
+ if(this.connected) {
+ this.onQuit();
+ this.connected = false;
+ this.snake.destroy()
+ this.name = null;
+ this.snake = null;
+ }
}
-Player.listenFor = function(socket, onJoined) {
- socket.on('join', function(name, callback) {
- name = name.replace(/^\s+|\s+$/, '');
- if(name.length < 3 || name.length > 64) {
- //Name is of a stupid length
- callback(false, true);
- } else if(!(name in players)) {
- snake = new Snake(
- 10,
- Color.randomHue(),
- universe.randomPosition(),
- universe
- );
- snake.name = name;
- snake.target = snake.head.position.clone();
- onJoined.call(new Player(socket, name, snake));
+Player.listener = function(onJoined) {
+ return function(socket) {
+ socket.on('join', function(name, callback) {
+ name = name.replace(/^\s+|\s+$/, '');
+ if(name.length < 3 || name.length > 64) {
+ //Name is of a stupid length
+ callback(false, true);
+ } else if(!(name in players)) {
+ snake = new Snake(
+ 10,
+ Color.randomHue(),
+ universe.randomPosition(),
+ universe
+ );
+ snake.name = name;
+ snake.target = snake.head.position.clone();
+ onJoined.call(new Player(socket, name, snake));
- callback(true);
- } else {
- //Name already taken
- callback(false);
- console.log("Name " + name + " invalid!");
- }
- });
+ callback(true);
+ } else {
+ //Name already taken
+ callback(false);
+ console.log("Name " + name + " invalid!");
+ }
+ });
+ }
}
Player.prototype.resendAllEntities = function() {
View
41 server.js
@@ -27,27 +27,25 @@ app.get('/local', function (req, res) {
io = socketio.listen(app);
io.set('log level', 2);
-io.sockets.on('connection', function (socket) {
- Player.listenFor(socket, function playerJoined() {
- console.log("Player "+this.name+" joined");
- if(Object.keys(players).length == 1) {
- generateBalls(50);
- console.log("Balls placed");
- }
+io.sockets.on('connection', Player.listener(function() {
+ console.log("Player "+this.name+" joined");
+ if(Object.keys(players).length == 1) {
+ generateBalls(50);
+ console.log("Balls placed");
+ }
- players[this.name] = this;
+ players[this.name] = this;
- this.onQuit.stuff = function() {
- delete players[this.name];
- console.log("Player "+this.name+" quit");
- //Clear the world if the player is last to leave
- if(Object.isEmpty(players)) {
- universe.clear();
- console.log("Universe cleared");
- }
+ this.onQuit.stuff = function() {
+ delete players[this.name];
+ console.log("Player "+this.name+" quit");
+ //Clear the world if the player is last to leave
+ if(Object.isEmpty(players)) {
+ universe.clear();
+ console.log("Universe cleared");
}
- });
-});
+ }
+}));
universe.onEntityRemoved.updateClients = function(e) {
@@ -57,7 +55,7 @@ universe.onEntityAdded.updateClients = function(e) {
io.sockets.emit('entityadded', {
p: e.position,
r: e.radius,
- c: e.color,
+ c: e.color.toInt(),
i: e._id
});
}
@@ -68,7 +66,7 @@ updateClients = function() {
universe.entities.forEach(function(e) {
var entityUpdate = {};
entityUpdate.pos = e.position;
- entityUpdate.color = e.color;
+ entityUpdate.color = e.color.toInt();
entityUpdate.radius = e.radius;
if(e.ownerSnake && e.ownerSnake.name) {
entityUpdate.playername = e.ownerSnake.name;
@@ -164,7 +162,8 @@ stdin.on('data', function(chunk) {
} else if(matches = /^\s*balls (\d+)/.exec(chunk)) {
generateBalls(+matches[1]);
} else if(matches = /^\s*kick (.+)/.exec(chunk)) {
- delete player[matches[1]];
+ var player = players[matches[1]]
+ player && player.disconnect();
} else {
console.log("sending message");
io.sockets.emit('servermessage', ""+chunk);
View
2  snakes.html
@@ -3,7 +3,7 @@
<head>
<title>Snakes</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
- <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Terminal+Dosis+Light|Ubuntu:300|Ubuntu:300italic" />
+ <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Dosis:300,800" />
<meta property="og:title" content="Snakes - a game written in Javascript and HTML5 canvas"/>
<meta property="og:type" content="website"/>
<meta property="og:url" content="http://42nd.org:8090/snakes.html"/>
Please sign in to comment.
Something went wrong with that request. Please try again.