forked from nko2/website
-
Notifications
You must be signed in to change notification settings - Fork 13
/
application.js
329 lines (275 loc) · 8.44 KB
/
application.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
// safe console usage
(function(b){function c(){}for(var d="assert,count,debug,dir,dirxml,error,exception,group,groupCollapsed,groupEnd,info, log,markTimeline,profile,profileEnd,time,timeEnd,trace,warn".split(","),a;a=d.pop();)b[a]=b[a]||c})(window.console=window.console||{});
//// a lil place for ourselves
var nko = { };
//// Vector
nko.Vector = function(x, y) {
if (typeof(x) === 'undefined') return
if (typeof(x) === 'number') {
this.x = x || 0;
this.y = y || 0;
} else {
this.x = x.x;
this.y = x.y;
}
};
nko.Vector.prototype = {
constructor: nko.Vector,
plus: function(other) {
return new this.constructor(this.x + other.x, this.y + other.y);
},
minus: function(other) {
return new this.constructor(this.x - other.x, this.y - other.y);
},
times: function(s) {
return new this.constructor(this.x * s, this.y * s);
},
length: function() {
return Math.sqrt(Math.pow(this.x, 2) + Math.pow(this.y, 2));
},
toString: function() {
return this.x + 'px, ' + this.y + 'px';
},
cardinalDirection: function() {
if (Math.abs(this.x) > Math.abs(this.y))
return this.x < 0 ? 'w' : 'e';
else
return this.y < 0 ? 'n' : 's';
}
};
//// Thing
nko.Thing = function(options) {
if (!options) return;
var self = this
, options = options || {};
this.name = options.name;
this.pos = new nko.Vector(options.pos);
this.size = new nko.Vector(options.size);
this.div = $('<div class="thing">');
this.img = $('<img>', { src: '/images/734m/' + this.name + '.png' })
.load(function() {
self.size = new nko.Vector(this.width, this.height);
self.draw();
});
};
nko.Thing.prototype.toJSON = function() {
return {
name: this.name,
pos: this.pos,
size: this.size
};
};
nko.Thing.prototype.draw = function draw() {
var offset = new nko.Vector(this.size.x * -0.5, -this.size.y + 20);
this.div
.css({
left: this.pos.x,
top: this.pos.y,
width: this.size.x,
height: this.size.y,
'z-index': this.pos.y,
transform: 'translate(' + offset.toString() + ')',
background: 'url(' + this.img.attr('src') + ')'
})
.appendTo(document.body);
this.animate();
return this;
};
nko.Thing.prototype.animate = function() { };
nko.Thing.prototype.remove = function() {
this.div.fadeOut(function() { $(this).remove(); });
};
//// Dude
nko.Dude = function(options) {
nko.Thing.call(this, options);
this.state = 'idle';
this.frame = 0;
};
nko.Dude.prototype = new nko.Thing();
nko.Dude.prototype.constructor = nko.Dude;
nko.Dude.prototype.draw = function draw() {
this.idleFrames = (this.size.x - 640) / 80;
this.size.x = 80;
this.bubble = $('<div class="bubble">')
.css('bottom', this.size.y + 10)
.appendTo(this.div);
return nko.Thing.prototype.draw.call(this);
};
nko.Dude.prototype.frameOffset = { w: 0, e: 2, s: 4, n: 6, idle: 8 };
nko.Dude.prototype.animate = function animate(state) {
var self = this;
clearTimeout(this.animateTimeout);
if (state) this.state = state;
var frames = this.state === 'idle' ? this.idleFrames : 2;
this.frame = ((this.frame + 1) % frames) + this.frameOffset[this.state];
this.div.css('background-position', (-this.frame * this.size.x) + 'px 0px');
this.animateTimeout = setTimeout(function() { self.animate() }, 400);
};
nko.Dude.prototype.goTo = function(pos) {
pos = new nko.Vector(pos);
var self = this
, delta = pos.minus(this.pos)
, duration = delta.length() / 200 * 1000;
this.animate(delta.cardinalDirection());
this.div
.stop()
.animate({
left: pos.x,
top: pos.y
}, {
duration: duration,
easing: 'linear',
step: function(now, fx) {
switch (fx.prop) {
case 'left':
self.pos.x = now;
break;
case 'top':
self.pos.y = now;
self.div.css('z-index', Math.floor(now));
break;
}
},
complete: function() {
self.pos = pos;
self.animate('idle');
}
});
};
nko.Dude.prototype.speak = function(text) {
if (!text)
this.bubble.fadeOut();
else {
this.bubble
.text(text)
.scrollTop(this.bubble.attr("scrollHeight"))
.fadeIn();
var self = this;
clearTimeout(this.speakTimeout);
this.speakTimeout = setTimeout(function() {
self.bubble.fadeOut();
}, 5000);
}
};
$(function() {
// countdown
var parts, start;
parts = $('time.start').attr('datetime').split(/[-:TZ]/);
parts[1]--; // js dates :( js dates are hot dates.
start = Date.UTC.apply(null, parts);
$('#countdown').each(function() {
var $this = $(this);
(function tick() {
var names = ['day', 'hour', 'minute', 'second']
, secs = (start - (new Date)) / 1000
, left = $.map([secs / 86400, secs % 86400 / 3600, secs % 3600 / 60, secs % 60], function(num, i) {
return [Math.floor(num), pluralize(names[i], num)];
}).join(' ');
$this.html(left + ' from now');
return setTimeout(tick, 800);
})();
function pluralize(str, count) {
return str + (parseInt(count) !== 1 ? 's' : '');
}
});
// a dude
var types = [ 'suit', 'littleguy', 'beast', 'gifter' ];
var me = new nko.Dude({
name: types[Math.floor(types.length * Math.random())],
pos: new nko.Vector(4000 + Math.random() * 800, 4200 + Math.random() * 200)
});
// some flare
new nko.Thing({ name: 'streetlamp', pos: new nko.Vector(4080, 4160) });
new nko.Thing({ name: 'livetree', pos: new nko.Vector(3920, 4000) });
new nko.Thing({ name: 'livetree', pos: new nko.Vector(4080, 3920) });
new nko.Thing({ name: 'livetree', pos: new nko.Vector(3840, 4960) });
new nko.Thing({ name: 'deadtree', pos: new nko.Vector(4000, 4960) });
new nko.Thing({ name: 'portopotty', pos: new nko.Vector(4080, 4960) });
// mark the ends of the universe
//new nko.Thing({ name: 'streetlamp', pos: new nko.Vector(0, 0) });
new nko.Thing({ name: 'streetlamp', pos: new nko.Vector(8000, 8000) });
$(window)
.load(function() { // center it
var page = $('.page#index')
, pos = page.position()
, left = pos.left - ($(this).width() - page.width()) / 2
, top = pos.top - ($(this).height() - page.height()) / 2;
$(this).scrollLeft(left).scrollTop(top)
})
.click(function(e) { // move on click
var pos = { x: e.pageX, y: e.pageY };
me.goTo(pos);
ws.send(JSON.stringify({
obj: me,
method: 'goTo',
arguments: [ pos ]
}));
// TODO move into nko.Viewport
var $win = $(this)
, left = $win.scrollLeft()
, top = $win.scrollTop()
, right = left + $win.width()
, bottom = top + $win.height()
, buffer = 160
, newLeft = left, newTop = top;
if (pos.x < left + buffer)
newLeft = left - $win.width()/2;
else if (pos.x > right - buffer)
newLeft = left + $win.width()/2;
if (pos.y < top + buffer)
newTop = top - $win.height()/2;
else if (pos.y > bottom - buffer)
newTop = top + $win.height()/2;
$('body')
.stop()
.animate({ scrollLeft: newLeft, scrollTop: newTop }, 1000, 'linear');
});
$('body')
.bind('touchstart', function(e) { // move on touch
var t = e.originalEvent.touches.item(0);
me.goTo(new nko.Vector(t.pageX, t.pageY));
});
// keyboard
var $text = $('<textarea>');
$text
.appendTo($('<div class="textarea-container">')
.appendTo(me.div))
.bind('keylisten keyup', function(e) {
var text = $text.val();
switch (e.keyName) {
case "up":
case "down":
case "left":
case "right":
// TODO move
return false;
default:
me.speak(text);
ws.send(JSON.stringify({
obj: me,
method: 'speak',
arguments: [ text ]
}));
}
}).focus();
$(document).keylisten(function() { $text.focus() });
// socket
var dudes = {};
var ws = new io.Socket();
ws.on('connect', function() {
ws.send(JSON.stringify({ obj: me }));
});
ws.on('message', function(data) {
var data = JSON.parse(data)
, dude = dudes[data.sessionId];
if (data.disconnect && dude) {
dude.remove();
delete dudes[data.sessionId];
}
if (data.obj && !dude)
dude = dudes[data.sessionId] = new nko.Dude(data.obj).draw();
if (data.method)
nko.Dude.prototype[data.method].apply(dude, data.arguments);
});
ws.connect();
});