-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.js
207 lines (197 loc) · 8.03 KB
/
game.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
var KEY_SPACE = 32;
var KEY_LEFT = 37;
var KEY_RIGHT = 39;
var KEY_UP = 38;
var KEY_DOWN = 40;
var KEY_NAMES = { 37: 'left', 39: 'right', 38: 'up', 40: 'down' };
$.ajaxSetup ({ cache: false });
$.fn.extend({
triggerAnimationClass: function(className, callback) {
return this
.addClass(className)
.on('animationend', function animationend(e) {
if (e.originalEvent.animationName !== className + '-animation') {
return;
}
$(this)
.off('animationend', animationend)
.removeClass(className);
(callback || $.noop)();
});
}
});
$(function() {
var current = { room: $(), tile: $() };
var rooms = {};
var tile_types = {
add: function(tile, level) {
tile.on('enter-tile', function() {
tile.data('to').forEach(function(id) {
var value_tile = level.find('#' + id);
value_tile.trigger('set-value', [value_tile.data('value') + tile.data('value')]);
});
});
tile.text('+' + (tile.data('value')));
},
equal: function(tile, level) {
var key = level.find('#' + tile.data('key'));
tile
.toggleClass('occupied', key.data('value') !== tile.data('value'))
.text(tile.data('value'));
key.on('set-value', function(e, value) {
tile.toggleClass('occupied', value !== tile.data('value'))
});
},
load: function(tile, level) {
tile.on('enter-tile', function() {
load_room(tile.data('room'), tile.data('start'));
});
},
subtract: function(tile, level) {
tile.on('enter-tile', function() {
tile.data('to').forEach(function(id) {
var value_tile = level.find('#' + id);
value_tile.trigger('set-value', [value_tile.data('value') - tile.data('value')]);
});
});
tile.text('-' + (tile.data('value')));
},
variable: function(tile, level) {
tile.on('set-value', function(e, value) {
tile.text(tile.data().value = value);
});
tile.trigger('set-value', [tile.data('value') || 0]);
level.on('enter-room', function() {
tile.trigger('set-value', [Number(tile.attr('data-value')) || 0]);
});
}
};
var block = block || $('<div class="block"><div class="block-inner"><div class="front"></div><div class="back"></div><div class="top"></div><div class="bottom"></div><div class="left"></div><div class="right"></div></div></div>');
var window_width = $(window).width();
var window_height = $(window).height();
$(window).on('resize', function() {
window_width = $(window).width();
window_height = $(window).height();
if (!current.room.length || !current.tile.length) {
return;
}
position_room(current.room, current.tile);
});
var counter = 0;
function toggleControls(on) {
counter += on ? -1 : 1;
$(document)[counter > 0 ? 'off' : 'on']('keydown', keycontrols);
}
function keycontrols(e) {
var next_tile;
switch (e.which) {
case KEY_SPACE:
next_tile = current.tile;
break;
case KEY_LEFT:
next_tile = current.tile.prev('.tile');
break;
case KEY_RIGHT:
next_tile = current.tile.next('.tile');
break;
case KEY_UP:
case KEY_DOWN:
next_tile = $(current.tile.data(KEY_NAMES[e.which]));
break;
default:
return;
}
if (!next_tile.length || (e.which !== KEY_SPACE && next_tile.hasClass('occupied'))) {
return;
}
current.tile
.removeClass('occupied')
.trigger('leave-tile');
current.tile = next_tile.addClass('occupied');
position_room(current.room, current.tile);
toggleControls(false);
block.triggerAnimationClass(e.which !== KEY_SPACE ? 'roll-' + KEY_NAMES[e.which] : 'jump', function() {
var tile_offset = current.tile.offset()
tile_offset.left += 5;
tile_offset.top += 5;
block.offset(tile_offset); // FIXME
current.tile.trigger('enter-tile');
toggleControls(true);
});
}
function load_room(room_name, start_id) {
toggleControls(false);
var last_room = current.room;
var last_tile = current.tile;
current = { room: $(), tile: $() };
if (rooms[room_name]) {
return on_load(rooms[room_name]);
}
rooms[room_name] = $('<div class="room"></div>').load('/rooms/' + room_name + '.html', function(contents, status) {
if (status !== 'success' && status !== 'notmodified') {
console.error('error', status);
return;
}
var level = rooms[room_name].find('.level');
var grid = level.find('.row').get().map(function(row) {
return $(row).find('.tile,.nothing').get();
});
for (var y = 0; y < grid.length; y++) {
for (var x = 0; x < grid[y].length; x++) {
var $this = $(grid[y][x]);
if (y > 0 && !$(grid[y - 1][x]).hasClass('nothing')) {
$this.data('up', grid[y - 1][x]);
}
if (y < grid.length - 1 && !$(grid[y + 1][x]).hasClass('nothing')) {
$this.data('down', grid[y + 1][x]);
}
}
}
Object.keys(tile_types).forEach(function(type) {
level.find('.' + type).each(function() {
var $this = $(this);
tile_types[type]($this, level);
});
});
on_load(rooms[room_name]);
});
function on_load(element) {
last_room
.trigger('leave-room')
.triggerAnimationClass('leave', function() {
last_room
.removeClass('current-room')
.detach();
});
last_tile
.removeClass('occupied')
.trigger('leave-tile');
current = { room: element, tile: element.find(start_id ? '#' + start_id : '#start').addClass('occupied') };
current.room.appendTo('body');
position_room(current.room, current.tile);
current.room.triggerAnimationClass(last_room.length ? 'enter' : 'enter-long', function() {
current.room.find('.level').trigger('enter-room');
current.room.addClass('current-room');
current.room.append(block);
block.offset(current.tile.offset());
current.tile.trigger('enter-tile');
toggleControls(true);
});
}
}
var tile_width, tile_height
function position_room(room, tile) {
var position;
var room_width = room.outerWidth(true);
var room_height = room.outerHeight(true);
if (room_width > window_width || room_height > window_height) {
position = tile.position();
tile_width = tile_width || tile.width();
tile_height = tile_height || tile.height();
}
var x = room_width <= window_width ? (window_width - room_width) / 2 : Math.min(0, Math.max(window_width - room_width, (window_width) / 2 - tile_width + 2 - position.left));
var y = room_height <= window_height ? (window_height - room_height) / 2 : Math.min(0, Math.max(window_height - room_height, (window_height) / 2 - tile_height + 2 - position.top));
room.css('transform', 'translate3d(' + x + 'px,' + y + 'px, 0)');
}
load_room('start');
});