Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: jessehattabaugh/RasterCrafter
base: 6769667614
...
head fork: jessehattabaugh/RasterCrafter
compare: beecab9bb5
  • 6 commits
  • 2 files changed
  • 0 commit comments
  • 1 contributor
Showing with 111 additions and 63 deletions.
  1. +89 −43 public/client.js
  2. +22 −20 server.js
View
132 public/client.js
@@ -9,7 +9,7 @@
// initialize socket
this.socket = io.connect();
-
+
this.socket.on('who are you', function (data) {
console.log("prompting for player id");
do{
@@ -27,32 +27,105 @@
socket.emit('here is my password', {password:password});
});
- this.socket.on('welcome', function (start) {
+ this.x = 0;
+ this.y = 0;
+ // todo: the player needs a concept of which direction it's facing
+
+ this.socket.on('welcome', function (pos) {
console.log("login successfull");
- self.x = start[0];
- self.y = start[1];
- // todo: show stuff
+ //console.dir(pos);
+ self.x = pos[0];
+ self.y = pos[1];
+ self.tuneIn();
+ // todo: show canvas
});
+ this.moving = false; // whether or not we are in the process of moving
+
+ this.move = function(x,y){
+ if (!this.moving){
+ this.moving = true;
+ // todo: if server never replies to this movement, no more
+ // movements will be possible, fix that, maybe timeout?
+ console.log('moving to '+x+'/'+y+);
+ this.socket.emit('move', [x,y]);
+ }
+ };
+
+ // handle movement keys
+ $('body').on('keydown', this, function(e){
+ // todo: handle diagonals
+ // todo: don't capture EVERYTHING it's annoying
+ var from_x = parseInt(e.data.x);
+ var from_y = parseInt(e.data.y);
+
+ if(e.which == 37 || e.which == 65){ // west
+ e.data.move(from_x-1, from_y);
+ } else if(e.which == 38 || e.which == 87){ //north
+ e.data.move(from_x, from_y-1);
+ } else if(e.which == 39 || e.which == 68){ // east
+ e.data.move(from_x+1, from_y);
+ } else if(e.which == 40 || e.which == 83){ // south
+ e.data.move(from_x, from_y+1);
+ } else {
+ console.log('key:'+e.which);
+ }
+ return false;
+ });
+
+ this.listening = []; // list of chunk ids
+
+ this.tuneIn = function(){
+
+ //if()
+
+ // if player crossed a chunk boundary
+ // leave chunk channels that are now too far away
+ // join chunk channels that are now in range
+
+ // todo:
+ // todo: call this function when screen is resized
+ }
+
this.socket.on('you moved', function(to){
- console.log("you moved");
- console.dir(to);
- self.x = to[0];
- self.y = to[1];
+ console.log("you moved");
+ console.dir(to);
+ self.x = to[0];
+ self.y = to[1];
+ self.moving = false;
+ self.tuneIn();
});
this.socket.on('invalid move', function(data){
console.log("can not move there");
console.dir(data);
+ self.moving = false;
+ });
+
+ this.socket.on('joined chunk', function(data){
+ console.log("you joined chunk x/y");
+ console.dir(data);
+ });
+
+ // todo: this.socket.on()
+
+ this.socket.on('someone logged in', function(data){
+ console.log("someone logged in");
+ console.dir(data);
+ });
+
+ this.socket.on('someone logged out', function(data){
+ console.log("someone logged outw");
+ console.dir(data);
});
- this.socket.on('someone joined', function(data){
- console.log("player joined");
+ this.socket.on('someone joined a chunk', function(data){
+ console.log("someone joined a chunk");
console.dir(data);
});
- this.socket.on('someone left', function(data){
- console.log("player left");
+ this.socket.on('someone left a chunk', function(data){
+ console.log("someone left a chunk");
console.dir(data);
});
@@ -60,44 +133,17 @@
console.log("player moved");
console.dir(data);
- // todo: add the player to a list of visible players
+
// todo: if user moves off chunk remove them from list
});
this.socket.on('disconnect', function(){
console.log("You are disconnected");
- // todo: hide stuff
+ // todo: hide controls
});
this.say = function(msg){
// todo: send a message over the socket
};
- // todo: seems like this could be wrapped up in a "state" object for easier handling
- this.x = 0;
- this.y = 0;
- // todo: the player needs a concept of which direction it's facing
-
- // handle arrow keys
- $('body').on('keydown', this, function(e){
- // todo: prevent repetitive presses
- // todo: handle diagonals
- // todo: don't capture EVERYTHING it's annoying
- if(e.which == 37 || e.which == 65){ // west
- e.data.move(e.data.x-1, e.data.y);
- } else if(e.which == 38 || e.which == 87){ //north
- e.data.move(e.data.x, e.data.y-1);
- } else if(e.which == 39 || e.which == 68){ // east
- e.data.move(e.data.x+1, e.data.y);
- } else if(e.which == 40 || e.which == 83){ // south
- e.data.move(e.data.x, e.data.y+1);
- } else {
- console.log('key:'+e.which);
- }
- return false;
- });
-
- this.move = function(x,y){
- console.log('moving to ('+x+','+y+')');
- this.socket.emit('move', [x,y]);
- };
}());
+//todo: I'd prefer to use the new operator
View
42 server.js
@@ -2,11 +2,12 @@ var express = require('express'),
app = express.createServer(express.static(__dirname + '/public')),
io = require('socket.io').listen(app),
redis = require("redis"),
- // currently the Redis connection is the default localhost:port
+ // Redis must be running on the default localhost port
db = redis.createClient().on("error", function(err) {
console.error("Redis Error: " + err);
});
+// serve the app's only html file
app.get('/', function(req, res) {
res.sendfile('index.html');
});
@@ -23,14 +24,12 @@ function redisDebug(err, res) {
}
};
-
-
io.sockets.on('connection', function(socket) {
console.log("New connection started");
socket.emit('who are you');
// todo: send a unique username
//socket.broadcast.emit('player joined');
- // send the player the chunks they are currently standing on
+
socket.on('my name is', function handleLogin(data) {
console.log("They say their name is " + data.plid);
socket.set('plid', data.plid, function() {
@@ -50,10 +49,10 @@ io.sockets.on('connection', function(socket) {
socket.on('here is my password', function handlePassword(data) {
console.log("Recieved a password");
socket.get('plid', function(err, plid) {
- db.hget('players:' + plid, 'password', function getPassword(err, password) {
+ db.hget('players:' + plid, 'password', function(err, password) {
if (password) {
if (data.password == password) {
- db.hgetall('players:' + plid, function getPlayerHash(err, coords) {
+ db.hmget('players:' + plid, 'x', 'y', function(err, coords) {
socket.emit('welcome', coords || [0, 0]);
});
} else {
@@ -68,8 +67,6 @@ io.sockets.on('connection', function(socket) {
}
});
});
-
-
});
function chuidFor(x, y) {
@@ -80,28 +77,32 @@ io.sockets.on('connection', function(socket) {
}
socket.on('move', function handleMove(to) {
- // if player crossed a chunk boundary
- // unsubscribe from chunk channels that are now too far away
- // subscribe to chunk channels that are now in range
+
console.log("Player movement");
console.dir(to);
socket.get('plid', function(err, plid) {
db.hmget('players:' + plid, 'x', 'y', function(err, from) {
-
- from_x = from[0] || 0;
- from_y = from[1] || 0;
+
+ from_x = parseInt(from[0]) || 0;
+ from_y = parseInt(from[1]) || 0;
// these might be empty if player has never moved
//todo: more random initial spawn point
- to_x = to[0] || from_x;
- to_y = to[1] || from_y;
- // not sure why these would be empty but just in case
+
+ to_x = parseInt(to[0]) || 0;
+ to_y = parseInt(to[1]) || 0;
+
from_chuid = chuidFor(from_x, from_y);
to_chuid = chuidFor(to_x, to_y);
-
- //todo: check for other players, objects etc
- if (Math.abs(from_x - to_x) <= 1 && Math.abs(from_y - to_y) <= 1) {
+
+ function taxiDistance(x1, y1, x2, y2){
+ return Math.abs(x1-x2) + Math.abs(y1-y2)
+ }
+
+
+ if (taxiDistance(from_x, from_y, to_x, to_y) <= 1) {
// can only move one tile at a time
+ //todo: check for other players, objects etc
if (to_x !== from_x) {
// moving horizontal
if (to_x < from_x) {
@@ -137,6 +138,7 @@ io.sockets.on('connection', function(socket) {
socket.emit('you moved', to);
} else {
socket.emit('invalid move', to);
+ console.log(from_x, from_y, to_x, to_y);
}
});
});

No commit comments for this range

Something went wrong with that request. Please try again.