Skip to content
Browse files

start

  • Loading branch information...
1 parent 3fb67ff commit dcdf541650b10e3b5b89f32a952358cfc566ff74 @drewlesueur committed
Showing with 131 additions and 25 deletions.
  1. +33 −8 app.coffee
  2. +53 −7 app.js
  3. +44 −10 bomber-server.coffee
  4. +1 −0 index.html
View
41 app.coffee
@@ -4,9 +4,7 @@ $(document).ready () ->
window.ws = new WebSocket "ws://bomber.the.tl:9998/"
ws.onopen = () ->
console.log "open"
- ws.send "hahaha"
ws.onmessage = (e) ->
- console.log "data is", e.data
data = JSON.parse e.data
console.log data
app[data[0]] data[1]
@@ -21,26 +19,53 @@ app.addField = () ->
$(app.field).css
height: "320px"
width: "320px"
+
$(app.field).attr "id", "field"
$('#wrapper').append app.field
+ if $.os.ios or $.os.android or $.os.webos
+ clicky = "touchstart"
+ else
+ clicky = "click"
+ $('#field').bind clicky, (e) ->
+ e.preventDefault()
+ if e.touches
+ e = e.touches[0]
+ ws.send JSON.stringify ["repos"
+ x: e.clientX
+ y: e.clientY
+ ]
+app.getByCid = {}
+app.renderUser = (user) ->
+ if not (user.__cid of app.getByCid)
+ app.users.push user
+ user.dom = document.createElement "div"
+
+ $(user.dom).html user.__cid
+ $(user.dom).css
+ "position": "absolute"
+ top: "0"
+ left: "0"
+ app.getByCid[user.__cid] = user
+ $('#field').append user.dom
-app.renderUser = user
- user.anim
+ else
+ oldUser = app.getByCid[user.__cid]
+ user = _.extend oldUser, user
+ $(user.dom).anim
translateX: user.x + "px"
translateY: user.y + "px"
-
-app.updateUsers data
+app.users = []
+app.updateUsers = (data) ->
for user in data
app.renderUser user
app.log = (str) ->
- console.log str
+ #console.log str
app.tileWidth = 32
app.tileHeight = 32
app.renderScreen = (tiles) ->
app.addField()
- console.log "rendering screen"
x = 0
y = 0
if _.isString tiles
View
60 app.js
@@ -2,12 +2,10 @@
$(document).ready(function() {
window.ws = new WebSocket("ws://bomber.the.tl:9998/");
ws.onopen = function() {
- console.log("open");
- return ws.send("hahaha");
+ return console.log("open");
};
ws.onmessage = function(e) {
var data;
- console.log("data is", e.data);
data = JSON.parse(e.data);
console.log(data);
return app[data[0]](data[1]);
@@ -21,23 +19,71 @@
});
window.app = {};
app.addField = function() {
+ var clicky;
app.field = document.createElement("div");
$(app.field).css({
height: "320px",
width: "320px"
});
$(app.field).attr("id", "field");
- return $('#wrapper').append(app.field);
+ $('#wrapper').append(app.field);
+ if ($.os.ios || $.os.android || $.os.webos) {
+ clicky = "touchstart";
+ } else {
+ clicky = "click";
+ }
+ return $('#field').bind(clicky, function(e) {
+ e.preventDefault();
+ if (e.touches) {
+ e = e.touches[0];
+ }
+ return ws.send(JSON.stringify([
+ "repos", {
+ x: e.clientX,
+ y: e.clientY
+ }
+ ]));
+ });
+ };
+ app.getByCid = {};
+ app.renderUser = function(user) {
+ var oldUser;
+ if (!(user.__cid in app.getByCid)) {
+ app.users.push(user);
+ user.dom = document.createElement("div");
+ $(user.dom).html(user.__cid);
+ $(user.dom).css({
+ "position": "absolute",
+ top: "0",
+ left: "0"
+ });
+ app.getByCid[user.__cid] = user;
+ $('#field').append(user.dom);
+ } else {
+ oldUser = app.getByCid[user.__cid];
+ user = _.extend(oldUser, user);
+ }
+ return $(user.dom).anim({
+ translateX: user.x + "px",
+ translateY: user.y + "px"
+ });
};
- app.log = function(str) {
- return console.log(str);
+ app.users = [];
+ app.updateUsers = function(data) {
+ var user, _i, _len, _results;
+ _results = [];
+ for (_i = 0, _len = data.length; _i < _len; _i++) {
+ user = data[_i];
+ _results.push(app.renderUser(user));
+ }
+ return _results;
};
+ app.log = function(str) {};
app.tileWidth = 32;
app.tileHeight = 32;
app.renderScreen = function(tiles) {
var cell, row, str, tile, x, y, _i, _j, _len, _len2, _results;
app.addField();
- console.log("rendering screen");
x = 0;
y = 0;
if (_.isString(tiles)) {
View
54 bomber-server.coffee
@@ -15,7 +15,8 @@ screen = """
users = []
removeByCid = (cid) ->
for stream, index in users
- if stream.__cid == cid
+
+ if (not stream) or (stream.__cid == cid)
users.splice index, 1
ws = require("simple-websocket")
@@ -25,19 +26,52 @@ server = ws.createServer
server.on "wsConnection", (stream) ->
users.push stream
stream.__cid = _.uniqueId()
-
+ stream.x = 0
+ stream.y = 0
ws.write stream, JSON.stringify ["renderScreen", screen]
stream.on "wsMessage", (message) ->
- 1
+ console.log "the message is " + message
+ data = JSON.parse message
+ console.log "the first part is" + data[0]
+ if data[0] of handle
+ handle[data[0]] stream, data[1]
#ws.write stream, message + "say what"
server.listen ws.options.port
-
+handle = {}
+handle.repos = (stream, info) ->
+ console.log "got here"
+ stream.oldx = stream.x
+ stream.oldy = stream.y
+ stream.hasChanged = true
+ stream.x = info.x
+ stream.y = info.y
setInterval () ->
- for stream in users
- if not stream.writable
- removeByCid stream.__cid
- else
- ws.write stream, JSON.stringify ["log", "chekup"]
-, 10000
+ changes = []
+
+
+ for stream, index in users
+ try
+ if stream.hasChanged
+ changes.push
+ __cid: stream.__cid
+ x: stream.x
+ y: stream.y
+ stream.hasChanged = false
+ catch e
+ users.splice index, 1
+ for stream,index in users
+ try
+ if not stream
+ users.splice index, 1
+ if not stream.writable
+ removeByCid stream.__cid
+ else
+ if changes.length > 0
+ ws.write stream, JSON.stringify ["updateUsers", changes]
+ #ws.write stream, JSON.stringify ["log", "chekup"]
+ 1
+ catch e
+ users.splice index, 1
+, 33
View
1 index.html
@@ -10,6 +10,7 @@
<script src="http://includes.the.tl/neckbrace.js"></script>
<script src="http://includes.the.tl/zepto/fx.js"></script>
<script src="http://includes.the.tl/zepto/touch.js"></script>
+<script src="http://includes.the.tl/zepto/detect.js"></script>
<script src="http://includes.the.tl/util.js"></script>
<script src="app.js"></script>
<style>

0 comments on commit dcdf541

Please sign in to comment.
Something went wrong with that request. Please try again.