entityjs 0.2.2 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (264) hide show
  1. data/.gitignore +7 -6
  2. data/.rspec +2 -0
  3. data/README.md +170 -49
  4. data/Rakefile +9 -0
  5. data/bin/entityjs +24 -14
  6. data/entityjs.gemspec +15 -9
  7. data/lib/entityjs/assets.rb +167 -0
  8. data/lib/entityjs/command.rb +52 -27
  9. data/lib/entityjs/commands/build.rb +128 -0
  10. data/lib/entityjs/commands/comp.rb +74 -0
  11. data/lib/entityjs/commands/font.rb +73 -0
  12. data/lib/entityjs/commands/new.rb +59 -0
  13. data/lib/entityjs/commands/server.rb +54 -0
  14. data/lib/entityjs/commands/templates.rb +28 -0
  15. data/lib/entityjs/commands/test.rb +69 -0
  16. data/lib/entityjs/config.rb +130 -0
  17. data/lib/entityjs/dirc.rb +184 -0
  18. data/lib/entityjs/parsers/parse_tmx.rb +41 -0
  19. data/lib/entityjs/parsers/parse_xml.rb +39 -0
  20. data/lib/entityjs/version.rb +1 -1
  21. data/lib/entityjs.rb +53 -0
  22. data/license.txt +1 -1
  23. data/public/play.html +122 -0
  24. data/public/qunit/qunit.css +226 -0
  25. data/public/qunit/qunit.entity.js +190 -0
  26. data/public/qunit/qunit.input.js +200 -0
  27. data/public/qunit/qunit.js +1598 -0
  28. data/public/qunit/qunit.mock.js +97 -0
  29. data/public/tests.html +45 -0
  30. data/spec/javascripts/helpers/accept.png +0 -0
  31. data/spec/javascripts/helpers/alligator.mp3 +0 -0
  32. data/spec/javascripts/helpers/alligator.ogg +0 -0
  33. data/spec/javascripts/helpers/canvas.js +15 -0
  34. data/spec/javascripts/helpers/entityunit.js +50 -0
  35. data/spec/javascripts/helpers/factories.js +11 -0
  36. data/spec/javascripts/helpers/jquery-1.7.1.min.js +4 -0
  37. data/spec/javascripts/src/core/comp_spec.js +145 -0
  38. data/spec/javascripts/src/core/entity_spec.js +324 -0
  39. data/spec/javascripts/src/core/load_spec.js +71 -0
  40. data/spec/javascripts/src/core/query_spec.js +157 -0
  41. data/spec/javascripts/src/core/re_spec.js +61 -0
  42. data/spec/javascripts/src/core/system_spec.js +41 -0
  43. data/spec/javascripts/src/cycle/draw_spec.js +106 -0
  44. data/spec/javascripts/src/cycle/tick_spec.js +20 -0
  45. data/spec/javascripts/src/cycle/update_spec.js +86 -0
  46. data/spec/javascripts/src/display/align_spec.js +43 -0
  47. data/spec/javascripts/src/display/circle_spec.js +26 -0
  48. data/spec/javascripts/src/display/group_spec.js +7 -0
  49. data/spec/javascripts/src/display/image_spec.js +32 -0
  50. data/spec/javascripts/src/display/imgtext_spec.js +32 -0
  51. data/spec/javascripts/src/display/rect_spec.js +13 -0
  52. data/spec/javascripts/src/display/screen_spec.js +47 -0
  53. data/spec/javascripts/src/display/sprite_spec.js +28 -0
  54. data/spec/javascripts/src/display/text_spec.js +30 -0
  55. data/spec/javascripts/src/input/keyboard_spec.js +67 -0
  56. data/spec/javascripts/src/input/mouse_spec.js +133 -0
  57. data/spec/javascripts/src/input/pressed_spec.js +31 -0
  58. data/spec/javascripts/src/math/bisect_spec.js +59 -0
  59. data/spec/javascripts/src/math/body_spec.js +30 -0
  60. data/spec/javascripts/src/math/drag_spec.js +38 -0
  61. data/spec/javascripts/src/math/force_spec.js +134 -0
  62. data/spec/javascripts/src/math/hit_spec.js +28 -0
  63. data/spec/javascripts/src/math/hitmap_spec.js +52 -0
  64. data/spec/javascripts/src/math/limit_spec.js +35 -0
  65. data/spec/javascripts/src/math/point_spec.js +57 -0
  66. data/spec/javascripts/src/math/tile_spec.js +78 -0
  67. data/spec/javascripts/src/media/sound_spec.js +40 -0
  68. data/spec/javascripts/src/pattern/automap_spec.js +93 -0
  69. data/spec/javascripts/src/pattern/flicker_spec.js +66 -0
  70. data/spec/javascripts/src/pattern/timestep_spec.js +23 -0
  71. data/spec/javascripts/src/save/storage_spec.js +37 -0
  72. data/spec/javascripts/src/util/log_spec.js +9 -0
  73. data/spec/javascripts/src/util/polyfill_spec.js +13 -0
  74. data/spec/javascripts/src/util/random_spec.js +33 -0
  75. data/spec/javascripts/src/util/scene_spec.js +52 -0
  76. data/spec/javascripts/src/util/sheet_spec.js +25 -0
  77. data/spec/javascripts/src/util/support_spec.js +17 -0
  78. data/spec/javascripts/support/jasmine.yml +76 -0
  79. data/spec/javascripts/support/jasmine_config.rb +32 -0
  80. data/spec/javascripts/support/jasmine_runner.rb +32 -0
  81. data/spec/lib/entityjs/assets_spec.rb +216 -0
  82. data/spec/lib/entityjs/command_spec.rb +53 -0
  83. data/spec/lib/entityjs/commands/build_spec.rb +69 -0
  84. data/spec/lib/entityjs/commands/comp_spec.rb +45 -0
  85. data/spec/lib/entityjs/commands/font_spec.rb +7 -0
  86. data/spec/lib/entityjs/commands/new_spec.rb +20 -0
  87. data/spec/lib/entityjs/commands/templates_spec.rb +9 -0
  88. data/spec/lib/entityjs/commands/test_spec.rb +31 -0
  89. data/spec/lib/entityjs/config_spec.rb +19 -0
  90. data/spec/lib/entityjs/dirc_spec.rb +83 -0
  91. data/spec/lib/entityjs/version_spec.rb +9 -0
  92. data/spec/spec_helper.rb +21 -3
  93. data/spec/support/factories.rb +19 -0
  94. data/spec/support/mygame.rb +11 -0
  95. data/src/core/comp.js +318 -0
  96. data/src/core/entity.js +549 -0
  97. data/src/core/load.js +242 -0
  98. data/src/core/query.js +354 -0
  99. data/src/core/re.js +74 -0
  100. data/src/core/system.js +121 -0
  101. data/src/cycle/draw.js +178 -0
  102. data/src/cycle/tick.js +25 -0
  103. data/src/{entityjs/cycle → cycle}/tween.js +60 -60
  104. data/src/{entityjs/cycle → cycle}/update.js +86 -85
  105. data/src/{entityjs/cycle → cycle}/wait.js +22 -21
  106. data/src/{entityjs/cycle → cycle}/worker.js +8 -8
  107. data/src/display/align.js +45 -0
  108. data/src/display/circle.js +33 -0
  109. data/src/{entityjs/display → display}/group.js +62 -62
  110. data/src/display/image.js +35 -0
  111. data/src/display/imgtext.js +102 -0
  112. data/src/{entityjs/display → display}/rect.js +18 -18
  113. data/src/display/screen.js +57 -0
  114. data/src/display/sprite.js +54 -0
  115. data/src/display/text.js +44 -0
  116. data/src/{entityjs/input → input}/keyboard.js +152 -150
  117. data/src/input/mouse.js +111 -0
  118. data/src/input/pressed.js +41 -0
  119. data/src/{entityjs/input → input}/touch.js +24 -28
  120. data/src/math/bisect.js +84 -0
  121. data/src/math/body.js +70 -0
  122. data/src/{entityjs/math → math}/drag.js +44 -38
  123. data/src/math/force.js +143 -0
  124. data/src/math/hit.js +32 -0
  125. data/src/math/hitmap.js +167 -0
  126. data/src/math/limit.js +37 -0
  127. data/src/math/point.js +40 -0
  128. data/src/math/tile.js +109 -0
  129. data/src/media/channel.js +93 -0
  130. data/src/{entityjs/media → media}/playlist.js +4 -4
  131. data/src/media/sound.js +114 -0
  132. data/src/{entityjs/net → net}/socket.js +51 -51
  133. data/src/pattern/automap.js +133 -0
  134. data/src/{entityjs/pattern → pattern}/flicker.js +206 -213
  135. data/src/pattern/timestep.js +31 -0
  136. data/src/{entityjs/save → save}/database.js +6 -6
  137. data/src/{entityjs/save → save}/storage.js +47 -56
  138. data/src/{entityjs/util → util}/log.js +17 -25
  139. data/src/{entityjs/util → util}/polyfill.js +24 -24
  140. data/src/util/random.js +20 -0
  141. data/src/util/scene.js +102 -0
  142. data/src/util/sheet.js +35 -0
  143. data/src/{entityjs/util → util}/support.js +109 -132
  144. data/{examples/keys → templates/arrow_keys/assets/images}/arrow.png +0 -0
  145. data/templates/arrow_keys/config.yml +22 -0
  146. data/templates/arrow_keys/readme.txt +9 -0
  147. data/templates/arrow_keys/scripts/display/arrow.js +69 -0
  148. data/templates/arrow_keys/scripts/init.js +10 -0
  149. data/templates/arrow_keys/scripts/input/controls.js +35 -0
  150. data/templates/arrow_keys/scripts/scenes/home.js +20 -0
  151. data/templates/arrow_keys/scripts/scenes/load.js +57 -0
  152. data/templates/arrow_keys/tests/display/arrow_test.js +29 -0
  153. data/templates/arrow_keys/tests/init_test.js +4 -0
  154. data/templates/arrow_keys/tests/input/controls_test.js +32 -0
  155. data/templates/arrow_keys/tests/scenes/home_test.js +0 -0
  156. data/templates/arrow_keys/tests/scenes/load_test.js +16 -0
  157. data/templates/blank/config.yml +22 -0
  158. data/templates/blank/readme.txt +79 -0
  159. data/templates/platform/assets/images/bit.png +0 -0
  160. data/templates/platform/assets/images/hero.png +0 -0
  161. data/templates/platform/assets/images/items.png +0 -0
  162. data/templates/platform/assets/images/tiles.png +0 -0
  163. data/templates/platform/assets/levels/level1.tmx +44 -0
  164. data/templates/platform/assets/sounds/coin.mp3 +0 -0
  165. data/templates/platform/assets/sounds/coin.ogg +0 -0
  166. data/templates/platform/config.yml +22 -0
  167. data/templates/platform/readme.txt +87 -0
  168. data/templates/platform/scripts/display/bit.js +12 -0
  169. data/templates/platform/scripts/display/hero.js +77 -0
  170. data/templates/platform/scripts/display/tile.js +2 -0
  171. data/templates/platform/scripts/display/tsprite.js +17 -0
  172. data/templates/platform/scripts/init.js +7 -0
  173. data/templates/platform/scripts/items/coin.js +27 -0
  174. data/templates/platform/scripts/items/item.js +41 -0
  175. data/templates/platform/scripts/items/spring.js +25 -0
  176. data/templates/platform/scripts/scenes/home.js +10 -0
  177. data/templates/platform/scripts/scenes/load.js +27 -0
  178. data/templates/platform/scripts/scenes/play.js +31 -0
  179. data/templates/platform/scripts/util/counter.js +34 -0
  180. data/templates/platform/scripts/util/level.js +84 -0
  181. data/templates/platform/tests/display/bit_test.js +7 -0
  182. data/templates/platform/tests/display/hero_test.js +73 -0
  183. data/templates/platform/tests/display/tile_test.js +7 -0
  184. data/templates/platform/tests/display/tsprite_test.js +8 -0
  185. data/templates/platform/tests/factories.js +30 -0
  186. data/templates/platform/tests/items/coin_test.js +28 -0
  187. data/templates/platform/tests/items/item_test.js +36 -0
  188. data/templates/platform/tests/items/spring_test.js +21 -0
  189. data/templates/platform/tests/scenes/home_test.js +9 -0
  190. data/templates/platform/tests/scenes/load_test.js +11 -0
  191. data/templates/platform/tests/scenes/play_test.js +15 -0
  192. data/templates/platform/tests/util/counter_test.js +9 -0
  193. data/templates/platform/tests/util/level_test.js +29 -0
  194. metadata +249 -97
  195. data/build/build_debug.bat +0 -1
  196. data/build/build_min.bat +0 -1
  197. data/build/config.php +0 -64
  198. data/build/debug.php +0 -48
  199. data/build/files.php +0 -74
  200. data/build/jsmin.php +0 -376
  201. data/build/min.php +0 -50
  202. data/changelog.txt +0 -53
  203. data/examples/keys/keys.html +0 -59
  204. data/examples/keys/keys.js +0 -148
  205. data/examples/mouse/mouse.html +0 -58
  206. data/examples/mouse/mouse.js +0 -60
  207. data/examples/scenes/home.png +0 -0
  208. data/examples/scenes/scenes.html +0 -55
  209. data/examples/scenes/scenes.js +0 -134
  210. data/examples/scenes/win.png +0 -0
  211. data/examples/sounds/sound1.mp3 +0 -0
  212. data/examples/sounds/sound1.ogg +0 -0
  213. data/examples/sounds/sounds.html +0 -56
  214. data/examples/sounds/sounds.js +0 -44
  215. data/examples/style/background.png +0 -0
  216. data/examples/style/sheet.css +0 -762
  217. data/examples/tiles/tiles.html +0 -56
  218. data/examples/tiles/tiles.js +0 -85
  219. data/examples/tiles/tiles.png +0 -0
  220. data/lib/blank/config.js +0 -4
  221. data/lib/blank/config.yml +0 -21
  222. data/lib/blank/home.js +0 -11
  223. data/lib/blank/init.js +0 -7
  224. data/lib/blank/load.js +0 -21
  225. data/lib/blank/play.html +0 -29
  226. data/lib/entity.debug.js +0 -52
  227. data/lib/entity.min.js +0 -184
  228. data/lib/entityjs/comp.rb +0 -11
  229. data/lib/entityjs/game.rb +0 -93
  230. data/lib/entityjs/min.rb +0 -11
  231. data/lib/entityjs/refresh.rb +0 -14
  232. data/spec/lib/entityjs/game_spec.rb +0 -11
  233. data/src/entityjs/core/component.js +0 -306
  234. data/src/entityjs/core/entity.js +0 -516
  235. data/src/entityjs/core/load.js +0 -224
  236. data/src/entityjs/core/query.js +0 -410
  237. data/src/entityjs/core/re.js +0 -70
  238. data/src/entityjs/core/system.js +0 -125
  239. data/src/entityjs/cycle/draw.js +0 -185
  240. data/src/entityjs/cycle/ticker.js +0 -27
  241. data/src/entityjs/display/anchor.js +0 -53
  242. data/src/entityjs/display/bitfont.js +0 -93
  243. data/src/entityjs/display/bitmap.js +0 -37
  244. data/src/entityjs/display/circle.js +0 -30
  245. data/src/entityjs/display/font.js +0 -41
  246. data/src/entityjs/display/screen.js +0 -46
  247. data/src/entityjs/display/sprite.js +0 -37
  248. data/src/entityjs/input/mouse.js +0 -123
  249. data/src/entityjs/input/pressed.js +0 -81
  250. data/src/entityjs/math/bind.js +0 -76
  251. data/src/entityjs/math/bisect.js +0 -69
  252. data/src/entityjs/math/body.js +0 -39
  253. data/src/entityjs/math/hitmap.js +0 -165
  254. data/src/entityjs/math/hittest.js +0 -26
  255. data/src/entityjs/math/physics.js +0 -142
  256. data/src/entityjs/math/point.js +0 -57
  257. data/src/entityjs/math/tile.js +0 -91
  258. data/src/entityjs/media/channel.js +0 -93
  259. data/src/entityjs/media/sound.js +0 -110
  260. data/src/entityjs/pattern/arraymap.js +0 -89
  261. data/src/entityjs/pattern/timestep.js +0 -34
  262. data/src/entityjs/util/random.js +0 -38
  263. data/src/entityjs/util/scene.js +0 -101
  264. data/src/entityjs/util/sheet.js +0 -51
data/src/core/load.js ADDED
@@ -0,0 +1,242 @@
1
+ (function(re){
2
+
3
+ var b = function(assets){
4
+ return new re.load.init(assets);
5
+ }
6
+
7
+ b.path = "";
8
+
9
+ b.imageExt = 'img';
10
+ b.soundExt = 'sfx';
11
+ b.images = ['gif', 'jpg', 'jpeg', 'png'];
12
+ b.sounds = ['wav', 'mp3', 'aac', 'ogg'];
13
+
14
+ /*
15
+ Loads images, sounds and other files into components.
16
+
17
+ All loaded assets will be put into a component with a ref to the asset.
18
+
19
+ //example of loading assets
20
+ re.load('tiles.png add.js attack.mp3')
21
+ .complete(function(arrayOfAssets){
22
+ //create new bitmap of tiles.png
23
+ re.e('bitmap tiles.png');
24
+
25
+ //new sound
26
+ re.e('sound attack.mp3');
27
+
28
+ //access image staticaly or localy
29
+ re.comp('tiles.png').image;
30
+ re.entity('tiles.png').image;
31
+
32
+ })
33
+ .error(function(assetThatCausedError){
34
+ //error
35
+ })
36
+ .progress(function(current, total, assetLoaded){
37
+ //called on loads
38
+ });
39
+
40
+ @warning only supports loading images and sounds
41
+
42
+ //load sound
43
+
44
+ //re.support will return the supported codec
45
+ re.load('run.'+re.support('ogg', 'aac'));
46
+
47
+ FUTURE remove directories from calls
48
+
49
+ */
50
+ var l = function(assets){
51
+
52
+ if(re.is(assets,'string')){
53
+ this.assets = assets.split(' ');
54
+
55
+ } else if(re.is(assets,'object')){
56
+ this.assets = [];
57
+ for(var i in assets){
58
+
59
+ if(assets.hasOwnProperty(i) && re.is(assets[i], 'array')){
60
+ this.assets = this.assets.concat(assets[i]);
61
+ }
62
+
63
+ }
64
+ } else {
65
+
66
+ this.assets = assets;
67
+ }
68
+
69
+ var a;
70
+ for(var i=0; i<this.assets.length; i++){
71
+
72
+ this.total++;
73
+
74
+ a = this.assets[i];
75
+
76
+ //copy full source path
77
+ var s = a;
78
+
79
+ //remove directories
80
+ var d = a.lastIndexOf('/');
81
+ if(d != -1){
82
+ a = a.substr(d+1, a.length);
83
+ }
84
+
85
+ //find file extension
86
+ var j = a.lastIndexOf('.')+1;
87
+ var ext = a.substr(j).toLowerCase();
88
+
89
+ //find name
90
+ var n = a.substr(0, j);
91
+
92
+ if(re.load.images.indexOf(ext) != -1){
93
+
94
+ this._loadImg(s, a, n);
95
+
96
+ } else if(re.load.sounds.indexOf(ext) != -1){
97
+
98
+ //check if support component exists first
99
+ if(!re.support || re.support(ext)){
100
+ //don't load the same sound twice
101
+ if(re._c[n+re.load.soundExt]){
102
+ //remove from array
103
+ this.total--;
104
+ this.assets.splice(i, 1);
105
+ continue;
106
+ }
107
+
108
+ this._loadSound(s, a, n);
109
+ }
110
+
111
+ }
112
+
113
+
114
+ }
115
+
116
+ return this;
117
+ }
118
+
119
+ var p = l.prototype;
120
+
121
+ p.current = 0;
122
+ p.total = 0;
123
+
124
+ p._loadImg = function(src, a, n){
125
+ var that = this;
126
+ var img = new Image();
127
+
128
+ //create new image component
129
+ re.c(a)
130
+ .alias(n+re.load.imageExt)
131
+ .statics({
132
+ image:img
133
+ })
134
+ .defines({
135
+ //save image for other components to copy or use
136
+ _image:img
137
+ });
138
+
139
+ img.onload = function(){
140
+ re.c(a).defaults({
141
+ sizeX:img.width,
142
+ sizeY:img.height
143
+ })
144
+ .defines('bisect', img.width);
145
+
146
+ that._loaded();
147
+ };
148
+
149
+ img.onerror = function(){
150
+
151
+ if(that._e){
152
+ that._e.call(that, a);
153
+ }
154
+
155
+ };
156
+
157
+ img.src = re.load.path+src;
158
+
159
+ return this;
160
+ };
161
+
162
+ p._loaded = function(){
163
+ this.current++;
164
+
165
+ if(this.current <= this.total){
166
+ if(this._p){
167
+ this._p(this.current, this.total, this.assets[this.current-1]);
168
+ }
169
+ }
170
+ if(this.current == this.total){
171
+ if(this._s){
172
+ this._s(this.assets);
173
+ }
174
+ }
175
+ };
176
+
177
+ p._loadSound = function(src, a, n){
178
+ var that = this;
179
+
180
+ var s = new Audio(re.load.path+src);
181
+ s.src = re.load.path+src;
182
+ s.preload = "auto";
183
+ s.load();
184
+
185
+ re.c(a)
186
+ //create statics codec for easy use
187
+ .alias(n+re.load.soundExt)
188
+ .statics({
189
+ sound:s
190
+ })
191
+ .defines({
192
+ _sound:s
193
+ });
194
+
195
+ //s.addEventListener('load',function(){that._loaded()}, false);
196
+ //called multiple times in firefox
197
+ var f = function(){
198
+ that._loaded();
199
+ //remove after first call
200
+ s.removeEventListener('canplaythrough', f);
201
+ };
202
+
203
+ s.addEventListener('canplaythrough',f,false);
204
+
205
+ s.addEventListener('error',function(){
206
+
207
+ if(that._e){
208
+ that._e.call(that, a);
209
+ }
210
+ },false);
211
+
212
+ }
213
+
214
+ p.progress = function(m){
215
+
216
+ this._p = m;
217
+
218
+ return this;
219
+ }
220
+
221
+ p.complete = function(m){
222
+
223
+ this._s = m;
224
+
225
+ if(this.assets.length == 0){
226
+ m([]);
227
+ }
228
+
229
+ return this;
230
+ }
231
+
232
+ p.error = function(m){
233
+
234
+ this._e = m;
235
+
236
+ return this;
237
+ }
238
+
239
+ re.load = b;
240
+ re.load.init = l;
241
+
242
+ }(re));
data/src/core/query.js ADDED
@@ -0,0 +1,354 @@
1
+ (function(re){
2
+
3
+ /*
4
+ Searches through all entities using the selector conditions
5
+
6
+ //identifiers
7
+ * identifies all
8
+ ^ identifies a bind
9
+ # identifies an id
10
+ ! identifies not
11
+
12
+ //select all entities
13
+ re('*');
14
+
15
+ //select all entities with 2d and ai components
16
+ re('2d ai');
17
+
18
+ //select all 2d entities without the monster component
19
+ re('2d -monster');
20
+
21
+ //select all draw entities and entities with the id equal to asset
22
+ re('draw #asset')
23
+ //returns a random entity
24
+ .random();
25
+
26
+ //select all entities with id of bob and player
27
+ //warning you cannot query for two ids
28
+ re('#bob player')
29
+ //takes values from all objects and returns an array
30
+ .pluck('width height');
31
+
32
+ //find all bitmap entities with update bind.
33
+ re('bitmap ^update');
34
+
35
+ //add color comp to all text components
36
+ re('text').comp('color');
37
+
38
+ //custom entity search
39
+ re(function(index){
40
+
41
+ if(entity.has('health') && entity.health > 0){
42
+ //add to query
43
+ return true;
44
+ }
45
+
46
+ //skip entity
47
+ return false;
48
+ })
49
+ //calls reset method on all entities
50
+ .method('reset');
51
+
52
+ //returns direct reference to first entity with id of player
53
+ re('#player').health = 100;
54
+
55
+ */
56
+ var q = function(selector){
57
+
58
+ if(selector){
59
+ this.query(selector);
60
+ }
61
+ }
62
+
63
+ /*
64
+ Sort a query string into objects
65
+ */
66
+ q._toObj = function(query){
67
+ if(q.c[query]){
68
+ return q.c[query];
69
+ }
70
+
71
+ //convert to object
72
+ var temp = query.split(' ');
73
+ var comps = [];
74
+ var binds = [];
75
+ var minus = [];
76
+ var id = '';
77
+
78
+ //split string
79
+ var fl;
80
+ var val;
81
+ for(var j = temp.length; j--;){
82
+ fl = temp[j].charAt(0);
83
+ val = temp[j].substr(1);
84
+
85
+ if(fl == '^'){
86
+ binds.push(val);
87
+ } else if(fl == '!'){
88
+ minus.push(val);
89
+ } else if(fl == '#'){
90
+ id = val;
91
+ } else {
92
+ comps.push(temp[j]);
93
+ }
94
+ }
95
+
96
+ var comp = {
97
+ comp:comps,
98
+ on:binds,
99
+ not:minus,
100
+ id:id
101
+ };
102
+
103
+ q.c[query] = comp;
104
+
105
+ return comp;
106
+ }
107
+
108
+ q.c = {};
109
+
110
+ var p = q.prototype = new Array;
111
+
112
+ p.query = function(select){
113
+ select = select || '';
114
+ var l = re._e.length, i = -1, e;
115
+
116
+ if(re.is(select, 'string')){
117
+
118
+ if(select == '*'){
119
+
120
+ this.push.apply(this, re._e.slice());
121
+
122
+ return this;
123
+ }
124
+
125
+ //optimize search and cache
126
+ var obj = q._toObj(select);
127
+
128
+ while(++i < l && (e = re._e[i])){
129
+ if(e.has(obj)) this.push(e);
130
+ }
131
+
132
+ } else if(re.is(select, 'function')){
133
+
134
+ while(++i < l && (e = re._e[i])){
135
+ if(select.call(e, i, l)) this.push(e);
136
+ }
137
+
138
+ }
139
+
140
+ return this;
141
+ }
142
+
143
+ /*
144
+ Calls the given method name on all entities.
145
+
146
+ re('enemies').method('rest');
147
+ */
148
+ p.method = function(m){
149
+ var b = Array.prototype.slice.call(arguments, 1);
150
+ return this.each(function(){
151
+
152
+ this[m].apply(this, b);
153
+ });
154
+
155
+ }
156
+
157
+ /*
158
+ example
159
+ re('draw').each(function(index, length){
160
+
161
+ });
162
+
163
+ returning false will break the loop
164
+ */
165
+ p.each = function(m){
166
+ var l = this.length, i = -1, e;
167
+
168
+ while(++i < l && (e = this[i]) && m.call(e, i, l) !== false);
169
+
170
+ return this;
171
+ }
172
+
173
+ /*
174
+ The map method allows multidimensional loops.
175
+
176
+ //map through and increase y every 3 entities.
177
+
178
+ re('draw').tilemap(3, function(x, y){
179
+ this.x(x * width);
180
+ this.y(Y * height);
181
+ });
182
+
183
+ //so instead of getting this back
184
+ [e,e,e,e,e...]
185
+
186
+ //you will get this
187
+ [
188
+ [e,e,e],
189
+ [e,e,e],
190
+ [e,e,e]
191
+ ...
192
+ ]
193
+
194
+ returning false will break the loop
195
+
196
+ */
197
+ p.tilemap = function(w, method){
198
+ var x = 0;
199
+ var y = 0;
200
+
201
+ return this.each(function(i, l){
202
+
203
+ if(method.call(this[i], x, y, i, l) == false) return false;
204
+
205
+ x++;
206
+
207
+ if(x == w){
208
+ x = 0;
209
+ y++;
210
+ }
211
+ });
212
+ }
213
+
214
+ /*
215
+ Returns an array of all components in the query.
216
+ */
217
+ p.comps = function(){
218
+ var l = [];
219
+
220
+ this.each(function(){
221
+ var k = this.comps();
222
+ for(var i=0; i<k.length; i++){
223
+ if(l.indexOf(k[i]) == -1){
224
+ l.push(k[i])
225
+ }
226
+ }
227
+ });
228
+
229
+ return l;
230
+ }
231
+
232
+ /*
233
+ Returns a random entity.
234
+ */
235
+ p.random = function(){
236
+ return this[~~(Math.random()*this.length)];
237
+ }
238
+
239
+ /*
240
+ The pluck method returns all values from all entities in an array.
241
+
242
+ //will return all pos objs from all entities.
243
+ re('point').pluck('pos visible');
244
+
245
+ //if we print...
246
+
247
+ [
248
+ {pos:0, visible:0}
249
+ ...
250
+ ]
251
+
252
+ */
253
+ p.pluck = function(value){
254
+ var t = [];
255
+
256
+ var k = value.split(' ');
257
+
258
+ this.each(function(){
259
+ var o = {};
260
+
261
+ for(var p in k){
262
+ if(k.hasOwnProperty(p)){
263
+ o[k[p]] = this[k[p]];
264
+ }
265
+ }
266
+
267
+ t.push(o);
268
+
269
+ });
270
+
271
+ return t;
272
+ }
273
+
274
+ p.defines = function(){
275
+ throw 'Deprecated use attr'
276
+ }
277
+
278
+ p.attr = function(obj, value){
279
+ return this.each(function(){
280
+ this.attr(obj,value);
281
+ });
282
+
283
+ }
284
+
285
+ p.defaults = function(){
286
+ throw 'Deprecated use def'
287
+ }
288
+
289
+ p.def = function(obj, value){
290
+ return this.each(function(){
291
+ this.def(obj, value);
292
+ });
293
+
294
+ }
295
+
296
+ p.comp = function(c){
297
+
298
+ return this.each(function(ref){
299
+ this.comp(c);
300
+ });
301
+
302
+ }
303
+
304
+ p.removeComp = function(c){
305
+ return this.each(function(ref){
306
+ this.removeComp(c);
307
+ });
308
+ }
309
+
310
+ p.on = function(type, method){
311
+
312
+ return this.each(function(){
313
+ this.on(type,method);
314
+ });
315
+
316
+ }
317
+
318
+ p.off = function(type, method){
319
+ return this.each(function(){
320
+ this.off(type, method);
321
+ });
322
+ }
323
+
324
+ p.trigger = function(){
325
+ var p = arguments;
326
+ return this.each(function(){
327
+ this.trigger.apply(this, p);
328
+ });
329
+ }
330
+
331
+ p.has = function(comp){
332
+
333
+ for(var i=0; i<this.length; i++){
334
+ if(!this[i].has(comp)){
335
+ return false;
336
+ }
337
+ }
338
+
339
+ return this.length != 0;
340
+ }
341
+
342
+ p.dispose = function(){
343
+
344
+ return this.each(function(){
345
+
346
+ this.dispose();
347
+
348
+ });
349
+
350
+ }
351
+
352
+ re.query = q;
353
+
354
+ }(re));
data/src/core/re.js ADDED
@@ -0,0 +1,74 @@
1
+ /*
2
+ EntityJS Javascript Game Engine
3
+ Copyright 2011, Ben D'Angelo
4
+ http://entityjs.com
5
+
6
+ Licensed under MIT http://entityjs.com/license
7
+
8
+ */
9
+ re = function(selector){
10
+ return new re.query(selector);
11
+ };
12
+
13
+ //automatically filled when compiled
14
+ re.version = "$VERSION";
15
+
16
+ //contains all entities
17
+ re._e = [];
18
+ //contains all components
19
+ re._c = {};
20
+
21
+ re.ready = function(r){
22
+ re.ready.c.push(r);
23
+
24
+ window.onload = function(){
25
+ for(var k in re.ready.c){
26
+ re.ready.c[k].call(re);
27
+ }
28
+ };
29
+ };
30
+ re.ready.c = [];
31
+
32
+ /*
33
+ The $ method is used for selecting ids and tags.
34
+ */
35
+ re.$ = function(s){
36
+ return re.$.c[s] = re.$.c[s] || ((s.charAt(0) == '#') ? document.getElementById(s.substr(1)) : document.getElementsByTagName(s)[0]);
37
+ };
38
+ //caches dom queries
39
+ re.$.c = {};
40
+
41
+ re.$new = function(n){
42
+ return document.createElement(n);
43
+ };
44
+
45
+ /*
46
+ Special polyfills and object additions
47
+ */
48
+ re.listener = function(t, c){
49
+ if(window.addEventListener){
50
+ window.addEventListener(t, c, true);
51
+ } else {
52
+ document.attachEvent('on'+t, c);
53
+ }
54
+ };
55
+
56
+ /*
57
+ Checks the existance of a variable or
58
+ */
59
+ re.is = function(obj, type){
60
+ if(arguments.length==1) return obj != null;
61
+
62
+ return obj != null && Object.prototype.toString.call(obj).slice(8, -1).toLowerCase() == type.toLowerCase();
63
+ };
64
+
65
+ if(!Array.prototype.indexOf){
66
+ Array.prototype.indexOf = function(o){
67
+ for(var i=0; i<this.length; i++){
68
+ if(this[i]==o){
69
+ return i;
70
+ }
71
+ }
72
+ return -1;
73
+ }
74
+ }