Permalink
Browse files

Jons inital stuff (in progress)

  • Loading branch information...
1 parent 9f856c2 commit 396c2bf3274e0a0b3a83bc3ae87a71c7601b1a80 @Binarytales Binarytales committed Dec 2, 2010
Showing with 271 additions and 0 deletions.
  1. +123 −0 workingfiles/jon/events.js
  2. +36 −0 workingfiles/jon/snowball.html
  3. +112 −0 workingfiles/jon/snowball.js
View
@@ -0,0 +1,123 @@
+var ev = (function (window, document, undefined) {
+ function each(el, fn) {
+ if (el && el.nodeName || el === window) {
+ fn(el);
+ } else if (el && el.length) {
+ for (var i = 0, l = el.length; i < l; i++) {
+ fn(el[i]);
+ }
+ }
+ }
+
+ var handlers = {},
+ guid = 0,
+ expando = 'e' + new Date;
+
+ var addEvent = (function () {
+ if (document.addEventListener) {
+ return function (el, type, fn) {
+ each(el, function (el) {
+ var ret = el.addEventListener(type, function (event) {
+ if (fn.call(el, event) === false) {
+ event.preventDefault();
+ event.stopPropagation();
+ }
+ }, false);
+ });
+ };
+ } else {
+ return function (el, type, fn) {
+ each(el, function (el) {
+ if (el['on' + type] === null) {
+ el.attachEvent('on' + type, function () {
+ return fn.call(el, window.event);
+ });
+ return;
+ }
+
+ if (handlers[type] === undefined) {
+ handlers[type] = {};
+ }
+
+ var id = el[expando] = guid++;
+
+ if (handlers[type][id] !== undefined) {
+ var old = handlers[type][id];
+ handlers[type][id] = function (event) {
+ if (fn.call(el, event) !== false) {
+ return old();
+ }
+ return false;
+ };
+ } else {
+ handlers[type][id] = function (event) {
+ return fn.call(el, event);
+ };
+ }
+
+ el = null; // <3 IE
+ });
+ };
+ }
+ })();
+
+ var fireEvent = (function () {
+ if (document.createEvent) {
+ return function (el, type, data) {
+ each(el, function (el) {
+ var event = document.createEvent('HTMLEvents');
+ event.initEvent(type, true, true);
+ event.data = data || {};
+ event.eventName = type;
+
+ el.dispatchEvent(event);
+ });
+ };
+ } else {
+ return function (el, type, data) {
+ each(el, function (el) {
+ try {
+ el['on' + type].call(el, data);
+ } catch (e) {
+ var id;
+ do {
+ id = el[expando];
+ if (!el || el.nodeType === 3 || el.nodeType === 8) {
+ return; // we're done - this shouldn't happen
+ } else {
+ handlers[type][id] && handlers[type][id].call(el, { type: type, data : data || {} });
+ }
+ } while (el = el.parentNode)
+ }
+ });
+ };
+ }
+ })();
+
+ return function (el) {
+ if (this === window) {
+ return new ev(el);
+ }
+ this.el = el;
+ this.addEvent = addEvent;
+ this.fireEvent = fireEvent;
+ };
+})(this, document);
+
+ev.prototype = {
+ on: function (type, fn) {
+ if (fn === undefined) {
+ for (var t in type) {
+ this.addEvent(this.el, t, type[t]);
+ }
+ } else {
+ this.addEvent(this.el, type, fn);
+ }
+
+ return this;
+ },
+ fire: function (type) {
+ this.fireEvent(this.el, type);
+ return this;
+ }
+};
@@ -0,0 +1,36 @@
+<!DOCTYPE html>
+ <html lang="en">
+<head>
+ <meta charset="utf-8">
+ <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
+
+ <title>Snowball Sleigher</title>
+
+ <style>
+ #join {
+ margin: 0 auto;
+ margin-top: 3em;
+ background: #CCC;
+ width: 10em;
+ padding: 1em;
+ text-align: center;
+ }
+ </style>
+
+</head>
+
+<body>
+
+ <form id="join">
+ <label>Username: <input type="text" /></label>
+ <input type="submit" />
+ </form>
+
+ <ul id="out"></ul>
+
+ <script src="http://forbind.net/js/?apikey=9f33aad21380e8a3686649586c99c6b2"></script>
+ <script src="events.js"></script>
+ <script src="snowball.js"></script>
+</body>
+</html>
@@ -0,0 +1,112 @@
+forbind.debug = true;
+
+// Debug Stuffs
+var output = document.getElementById('out'),
+ join = document.getElementById('join');
+
+function log(s) {
+ var item = document.createElement('li');
+ item.innerHTML = s;
+ output.appendChild(item);
+}
+
+// Setup & Fluff Stuffs
+join.onsubmit = function () {
+ username = join.getElementsByTagName('input')[0].value;
+ if(username && username != '') {
+ forbind.user({username: username});
+ forbind.join();
+ join.style.display = 'none';
+ } else {
+ log('> enter a username dumbass');
+ }
+ return false;
+};
+
+
+// Forbind Magic Stuffs
+
+forbind.on({
+ join: function (event) {
+ if (event.isme) {
+ log('all connected users: ', event.users); // event.users: array
+ } else {
+ log('new user connected: ', event.user); // event.user: object
+ }
+ },
+ ready: function () {
+ log('> FIGHT! (session ready)');
+ fight();
+ },
+ waiting: function (event) {
+ log('> waiting for ' + event.waitingfor + ' others to join...');
+ },
+ leave: function (event) {
+ log('> ' + event.user + ' left. ' + event.total + ' users remaining');
+ },
+ message: function (event) {
+ log(event.user + ': ' + event.data);
+ switch (event.data.type) {
+ case 'snowball':
+ // Maybe figure out who the user is?
+ // Call Seb's snowball event here
+ log('> snowball recieved!!!!! SPLATTER');
+ break;
+ // case 'avatar':
+ //
+ // break;
+ // case 'join':
+ //
+ // break;
+ // case 'leave':
+ //
+ // break;
+ default:
+ //
+ }
+ }
+});
+
+ev(document).on('snowball', function (event, velocity) {
+ console.log(arguments);
+ console.log('do I get here ever?');
+ log('> snowball fired!!!!! OMFG' + velocity);
+ forbind.send({
+ type: 'snowball',
+ velocity: velocity
+ });
+});
+
+
+
+
+
+
+
+
+// Simulation Stuffs
+
+var fight = function () {
+ document.onkeypress = function (event) {
+ switch (event.charCode) {
+ case 115: // S
+ ev(document).fire('fireSnowball', {
+ x: 100,
+ y: 50,
+ z: 12
+ });
+ break;
+ // case ??: // J
+ // // User Joins
+ // break;
+ // case ??: // L
+ // // User Leaves
+ // break;
+ // case ??: // A
+ // // User adds an avatar
+ // break;
+ default:
+ //
+ }
+ };
+}

0 comments on commit 396c2bf

Please sign in to comment.