entityjs 0.4.1 → 0.4.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (111) hide show
  1. data/.gitignore +1 -0
  2. data/bin/entityjs +1 -1
  3. data/entityjs.gemspec +3 -2
  4. data/lib/entityjs.rb +13 -1
  5. data/lib/entityjs/command.rb +22 -16
  6. data/lib/entityjs/commands/build.rb +96 -48
  7. data/lib/entityjs/commands/eunit.rb +1 -1
  8. data/lib/entityjs/commands/html.rb +31 -0
  9. data/lib/entityjs/commands/min.rb +37 -0
  10. data/lib/entityjs/commands/new.rb +3 -0
  11. data/lib/entityjs/commands/server.rb +9 -3
  12. data/lib/entityjs/config.rb +163 -59
  13. data/lib/entityjs/dirc.rb +28 -0
  14. data/lib/entityjs/page.rb +73 -34
  15. data/lib/entityjs/version.rb +1 -1
  16. data/public/favicon.ico +0 -0
  17. data/public/play.html +33 -28
  18. data/public/qunit/qunit.entity.js +56 -6
  19. data/public/qunit/qunit.input.js +5 -5
  20. data/public/test.html +49 -0
  21. data/spec/javascripts/src/core/comp_spec.js +17 -6
  22. data/spec/javascripts/src/core/entity_spec.js +4 -5
  23. data/spec/javascripts/src/core/query_spec.js +25 -0
  24. data/spec/javascripts/src/core/re_spec.js +3 -3
  25. data/spec/javascripts/src/cycle/update_spec.js +8 -8
  26. data/spec/javascripts/src/display/align_spec.js +2 -2
  27. data/spec/javascripts/src/display/el_spec.js +17 -0
  28. data/spec/javascripts/src/input/keyboard_spec.js +4 -4
  29. data/spec/javascripts/src/input/mouse_spec.js +8 -7
  30. data/spec/javascripts/src/math/random_spec.js +8 -0
  31. data/spec/javascripts/src/util/clone_spec.js +20 -0
  32. data/spec/lib/entityjs/commands/build_spec.rb +3 -8
  33. data/spec/lib/entityjs/commands/html_spec.rb +27 -0
  34. data/spec/lib/entityjs/commands/min_spec.rb +30 -0
  35. data/spec/lib/entityjs/config_spec.rb +6 -0
  36. data/spec/lib/entityjs/page_spec.rb +4 -4
  37. data/src/core/comp.js +87 -31
  38. data/src/core/entity.js +38 -43
  39. data/src/core/load.js +2 -8
  40. data/src/core/query.js +17 -5
  41. data/src/core/re.js +1 -1
  42. data/src/core/system.js +8 -11
  43. data/src/cycle/drawlist.js +11 -11
  44. data/src/cycle/update.js +5 -5
  45. data/src/display/align.js +10 -3
  46. data/src/display/animate.js +26 -12
  47. data/src/display/el.js +99 -0
  48. data/src/input/keyboard.js +5 -5
  49. data/src/input/mouse.js +16 -11
  50. data/src/math/bisect.js +1 -1
  51. data/src/math/force.js +4 -4
  52. data/src/math/hitmap.js +2 -1
  53. data/src/math/iso.js +1 -1
  54. data/src/math/random.js +17 -2
  55. data/src/math/tile.js +1 -1
  56. data/src/media/sound.js +1 -1
  57. data/src/pattern/pathfind.js +2 -4
  58. data/src/save/storage.js +1 -1
  59. data/src/util/clone.js +12 -0
  60. data/src/util/scene.js +21 -16
  61. data/templates/circle/game.json +16 -0
  62. data/templates/circle/scripts/displays/circle.js +40 -0
  63. data/templates/circle/scripts/scenes/home.js +8 -17
  64. data/templates/circle/tests/scenes/home_test.js +3 -11
  65. data/templates/dom/game.json +6 -0
  66. data/templates/dom/scripts/els/btn.js +5 -0
  67. data/templates/dom/scripts/els/contain.js +5 -0
  68. data/templates/dom/scripts/els/header.js +5 -0
  69. data/templates/{animation → dom}/scripts/init.js +0 -0
  70. data/templates/dom/scripts/scenes/home.js +22 -0
  71. data/templates/{animation → dom}/scripts/scenes/load.js +0 -0
  72. data/templates/dom/tests/scenes/home_test.js +9 -0
  73. data/templates/{animation → dom}/tests/scenes/load_test.js +0 -0
  74. data/templates/isometric/game.json +16 -0
  75. data/templates/platform/game.json +18 -0
  76. data/templates/platform/scripts/displays/hero.js +1 -1
  77. data/templates/platform/scripts/items/coin.js +1 -1
  78. data/templates/platform/scripts/items/spring.js +1 -1
  79. data/templates/platform/tests/displays/hero_test.js +4 -4
  80. data/templates/platform/tests/factories.js +1 -1
  81. data/templates/platform/tests/items/coin_test.js +1 -1
  82. data/templates/pong/game.json +16 -0
  83. data/templates/tiltmaze/game.json +16 -0
  84. data/templates/tiltmaze/scripts/structs/level.js +1 -1
  85. data/templates/tiltmaze/scripts/tiles/walltile.js +1 -1
  86. metadata +45 -32
  87. data/.rspec +0 -2
  88. data/public/tests.html +0 -31
  89. data/src/cycle/worker.js +0 -9
  90. data/templates/animation/assets/images/hero.png +0 -0
  91. data/templates/animation/scripts/scenes/home.js +0 -41
  92. data/templates/animation/tests/init_test.js +0 -4
  93. data/templates/animation/tests/scenes/home_test.js +0 -15
  94. data/templates/arrow_keys/assets/images/arrow.png +0 -0
  95. data/templates/arrow_keys/config.yml +0 -22
  96. data/templates/arrow_keys/readme.txt +0 -9
  97. data/templates/arrow_keys/scripts/displays/arrow.js +0 -69
  98. data/templates/arrow_keys/scripts/init.js +0 -10
  99. data/templates/arrow_keys/scripts/inputs/controls.js +0 -35
  100. data/templates/arrow_keys/scripts/scenes/home.js +0 -20
  101. data/templates/arrow_keys/scripts/scenes/load.js +0 -57
  102. data/templates/arrow_keys/tests/displays/arrow_test.js +0 -29
  103. data/templates/arrow_keys/tests/init_test.js +0 -4
  104. data/templates/arrow_keys/tests/inputs/controls_test.js +0 -32
  105. data/templates/arrow_keys/tests/scenes/home_test.js +0 -0
  106. data/templates/arrow_keys/tests/scenes/load_test.js +0 -18
  107. data/templates/circle/config.yml +0 -22
  108. data/templates/isometric/config.yml +0 -22
  109. data/templates/platform/config.yml +0 -23
  110. data/templates/pong/config.yml +0 -22
  111. data/templates/tiltmaze/config.yml +0 -25
@@ -0,0 +1,99 @@
1
+ /*
2
+ The element comp creates and displays DOM elements. This can be used to display buttons,
3
+ images, icons etc. on top of the canvas element.
4
+
5
+ re.e('el:a').$el //jquery element refence
6
+
7
+ Jquery must be available to work!
8
+
9
+ */
10
+ re.c('el')
11
+ .requires('align')
12
+ .defines({
13
+
14
+ posX:function(x){
15
+ if(re.is(x)){
16
+ return this.$el.css('left', x);
17
+ }
18
+
19
+ return this.$el.position().left;
20
+ },
21
+
22
+ posY:function(y){
23
+ if(re.is(y)){
24
+ return this.$el.css('top', y);
25
+ }
26
+ return this.$el.position().top;
27
+ },
28
+
29
+ sizeX:function(){
30
+ return this.$el.outerWidth();
31
+ },
32
+
33
+ sizeY:function(){
34
+ return this.$el.outerHeight();
35
+ },
36
+
37
+ click:function(f){
38
+ var that = this;
39
+ this.$el.click(function(e){
40
+ f.call(that,e);
41
+ return false;
42
+ });
43
+ return this;
44
+ },
45
+
46
+ $:function(a,b){
47
+ return this.$el.find(a,b);
48
+ },
49
+
50
+ setEl:function(el){
51
+ this.remove();
52
+
53
+ this.el = el;
54
+ this.$el = $(el).addClass('el');
55
+
56
+ this.posX(0);
57
+ this.posY(0);
58
+ return this;
59
+ },
60
+
61
+ text:function(t){
62
+ if(re.is(t)){
63
+ this.$el.text(t);
64
+ return this;
65
+ }
66
+ return this.$el.text();
67
+ },
68
+
69
+ //places element to parent of canvas
70
+ place:function(){
71
+ $(re.sys.canvas).parent().append(this.el);
72
+ return this;
73
+ },
74
+
75
+ remove:function(){
76
+ if(this.$el){
77
+ this.$el.remove();
78
+ this.$el = this.el = null;
79
+ }
80
+ return this;
81
+ },
82
+
83
+ hide:function(){
84
+ this.$el.hide();
85
+ return this;
86
+ },
87
+
88
+ show:function(){
89
+ this.$el.show();
90
+ return this;
91
+ }
92
+
93
+ })
94
+ .init(function(e){
95
+ if(e) this.setEl(re.$new(e));
96
+ })
97
+ .dispose(function(){
98
+ this.remove();
99
+ });
@@ -111,7 +111,7 @@ re.c('keyboard')
111
111
  .trigger('ctrl+k')
112
112
  */
113
113
  event: function(e){
114
- var that = re._c.keyboard;
114
+ var that = re.keyboard;
115
115
 
116
116
  var tagName = (e.target || e.srcElement || {}).tagName;
117
117
 
@@ -151,11 +151,11 @@ re.c('keyboard')
151
151
  }
152
152
 
153
153
  })
154
- .init(function(c){
154
+ .init(function(){
155
155
  //add to statics key array
156
- c.l.push(this);
156
+ re.keyboard.l.push(this);
157
157
  })
158
- .dispose(function(c){
158
+ .dispose(function(){
159
159
  //remove from statics key array
160
- c.l.splice(c.l.indexOf(this), 1);
160
+ re.keyboard.l.splice(re.keyboard.l.indexOf(this), 1);
161
161
  });
@@ -47,7 +47,7 @@ re.c('mouse')
47
47
  re.pressed.d[c] = (e.type == 'mousedown');
48
48
  }
49
49
 
50
- re.c('mouse').event(e, c);
50
+ re.mouse.event(e, c);
51
51
 
52
52
  },
53
53
 
@@ -68,7 +68,7 @@ re.c('mouse')
68
68
  }
69
69
 
70
70
 
71
- var listeners = re.c('mouse').l;
71
+ var listeners = re.mouse.l;
72
72
 
73
73
  /*
74
74
  if(re.preventDefault && re.preventDefault.d[key]){
@@ -76,17 +76,20 @@ re.c('mouse')
76
76
  }
77
77
  */
78
78
 
79
- var c, t, obj;
79
+ var c, t, obj, tx, ty;
80
80
  for(var i=0; i<listeners.length; i++){
81
81
  t = listeners[i];
82
82
  if(t.screenable){
83
- x = re.screen.toScreenX(x);
84
- y = re.screen.toScreenY(y);
83
+ tx = re.screen.toScreenX(x);
84
+ ty = re.screen.toScreenY(y);
85
+ } else {
86
+ tx = x;
87
+ ty = y;
85
88
  }
86
89
 
87
90
  //offset mouse coordinates
88
- var tx = x + t.offX;
89
- var ty = y + t.offY;
91
+ tx += t.offX;
92
+ ty += t.offY;
90
93
 
91
94
  t.trigger(e.type, tx, ty, e);
92
95
 
@@ -102,6 +105,8 @@ re.c('mouse')
102
105
  re.listener('mousedown', this.press, c);
103
106
  re.listener('mouseup', this.press, c);
104
107
  re.listener('mousemove', this.event, c);
108
+ re.listener('mouseover', this.event, c);
109
+ re.listener('mouseout', this.event, c);
105
110
  re.listener('click', this.event, c);
106
111
  re.listener('dblclick', this.event, c);
107
112
  re.listener('contextmenu', this.event, c);
@@ -112,12 +117,12 @@ re.c('mouse')
112
117
  offX:0,
113
118
  offY:0
114
119
  })
115
- .init(function(c){
120
+ .init(function(){
116
121
  //add to listener array
117
- c.l.push(this);
122
+ re.mouse.l.push(this);
118
123
  })
119
- .dispose(function(c){
124
+ .dispose(function(){
120
125
  //remove from listener array
121
126
 
122
- c.l.splice(c.l.indexOf(this), 1);
127
+ re.mouse.l.splice(re.mouse.l.indexOf(this), 1);
123
128
  });
@@ -15,7 +15,7 @@ Note: width is called the bisect and the bi is the transformed x,y positions.
15
15
 
16
16
 
17
17
  */
18
- re.bisect = re.c('bisect')
18
+ re.c('bisect')
19
19
  .statics({
20
20
 
21
21
  toX:function(bi, width, size){
@@ -20,7 +20,7 @@ e.hitmap == re.hitmap //true
20
20
 
21
21
  Warning - this component is not delta time safe. It assumes a fixed timestep.
22
22
  */
23
- re.force = re.c('force')
23
+ re.c('force')
24
24
  .requires('update')
25
25
  .statics({
26
26
  graX:0,
@@ -125,13 +125,13 @@ re.force = re.c('force')
125
125
  }
126
126
 
127
127
  })
128
- .init(function(c){
128
+ .init(function(){
129
129
 
130
130
  //setup defaults
131
131
  this.def({
132
132
  hitmap:re.hitmap,
133
- graX:c.graX,
134
- graY:c.graY
133
+ graX:re.force.graX,
134
+ graY:re.force.graY
135
135
  });
136
136
 
137
137
  this.on('update', this.force_update);
@@ -164,4 +164,5 @@ re.c('hitmap')
164
164
 
165
165
  }
166
166
 
167
- });
167
+ });
168
+ re.hitmap = null; //remove default hitmap so force defaults to none
@@ -1,4 +1,4 @@
1
- re.iso = re.c('iso')
1
+ re.c('iso')
2
2
  .statics({
3
3
  sizeX:30,
4
4
  sizeY:30,
@@ -3,19 +3,34 @@ re.random() // 0 - 1 floats
3
3
  re.random(10) // 0 - 9 integer
4
4
  re.random(10, 30) // 10 - 30 integer
5
5
  re.random([1, 10, 40]) // 1 or 10 or 40
6
+ re.random({ok:10, b:10, c:1}) //ok or b or c
6
7
 
7
8
  */
8
9
  re.random = function(max, min){
9
10
  var r = Math.random();
10
11
  if(re.is(max, 'array')){
11
12
  return max[r * max.length | 0];
13
+ } else if(re.is(max, 'object')){
14
+
15
+ var result;
16
+ for (var prop in max){
17
+ if (Math.random() < 1/++r || !re.is(result)){
18
+ result = prop;
19
+ }
20
+ }
21
+
22
+ return result;
12
23
  }
13
24
  switch(arguments.length){
14
25
  case 0:
15
26
  return r;
16
27
  case 1:
17
- return r * max | 0;
28
+ return r * max;
18
29
  case 2:
19
- return r * (max - min + 1) + min | 0;
30
+ return r * (max - min + 1) + min;
20
31
  }
32
+ };
33
+
34
+ re.randomInt = function(){
35
+ return re.random.apply(this, arguments)|0;
21
36
  };
@@ -41,7 +41,7 @@ Its recommended you avoid negative tile values
41
41
 
42
42
  TODO: remove size vars from entity. Use global size instead
43
43
  */
44
- re.tile = re.c('tile')
44
+ re.c('tile')
45
45
  .statics({
46
46
  sizeX:40,
47
47
  sizeY:40,
@@ -42,7 +42,7 @@ re('sound').method('play');
42
42
  This will be fixed with the channel component.
43
43
 
44
44
  */
45
- re.sound = re.c('sound')
45
+ re.c('sound')
46
46
  .statics({
47
47
 
48
48
  enabled:true
@@ -53,14 +53,12 @@ while(ints.length){
53
53
  }
54
54
 
55
55
  */
56
- re.pathfind = re.c('pathfind')
57
- .statics({
58
- search:function(){
56
+ re.c('pathfind')
57
+ .method(function(){
59
58
  var p = re.e('pathfind');
60
59
  var path = p.pathfind.apply(p, arguments);
61
60
  p.dispose();
62
61
  return path;
63
- }
64
62
  })
65
63
  .defines({
66
64
 
@@ -10,7 +10,7 @@ re.e('storage:session');
10
10
 
11
11
  */
12
12
  re.c('storage')
13
- .init(function(c, type){
13
+ .init(function(type){
14
14
  this.storage = window[type+'Storage'];
15
15
  })
16
16
  .defines({
@@ -0,0 +1,12 @@
1
+ /*
2
+ Clones the given object. This returns a shallow copy.
3
+ */
4
+ re.clone = function(b){
5
+ if(!re.is('object')) return b;
6
+ if(re.is(b,'array')) return b.slice();
7
+ var o = {};
8
+ for(var i in b){
9
+ o[i] = b[i];
10
+ }
11
+ return o;
12
+ };
@@ -27,40 +27,45 @@ re.e('scene:game')
27
27
  })
28
28
 
29
29
  */
30
- re.scene = function(title){
31
- var s = re.c('scene');
30
+
31
+ re.c('scene')
32
+ .statics({
33
+
34
+ _scenes:{}
35
+
36
+ })
37
+ .method(function(title){
38
+ var d = re.scene;
32
39
 
33
40
  if(!re.is(title)){
34
- return s._scenes[re.scene.current];
41
+ return d._scenes[re.scene.current];
35
42
  }
36
43
 
37
- if(!s._scenes[title]){
44
+ if(!d._scenes[title]){
38
45
  //add scene
39
46
  re.e('scene:'+title);
40
47
  }
41
48
 
42
- return s._scenes[title];
43
- };
44
-
45
- re.c('scene')
46
- .statics({
47
-
48
- _scenes:{}
49
-
49
+ return d._scenes[title];
50
50
  })
51
- .init(function(c, title){
51
+ .init(function(title){
52
52
 
53
- c._scenes[title] = this;
53
+ re.scene._scenes[title] = this;
54
54
  this.sceneName = title;
55
55
 
56
56
  })
57
- .dispose(function(c){
57
+ .dispose(function(){
58
58
 
59
- delete c._scenes[this.sceneName];
59
+ delete re.scene._scenes[this.sceneName];
60
60
 
61
61
  })
62
62
  .defines({
63
63
 
64
+ clear:function(){
65
+ re('el').query('draw').dispose();
66
+ return this;
67
+ },
68
+
64
69
  enter:function(title){
65
70
  if(!re.is(title, 'function')){
66
71
 
@@ -0,0 +1,16 @@
1
+ {
2
+ //configure game settings
3
+ "width": 500,
4
+ "height": 400,
5
+ "canvas-id": "game-canvas",
6
+
7
+ //ignore entity scripts
8
+ "entity-ignore": "wait",
9
+
10
+ "tests-ignore": null,
11
+ "scripts-ignore": null,
12
+ "tests-scripts-ignore":"init.js",
13
+
14
+ //order scripts
15
+ "order": null
16
+ }
@@ -0,0 +1,40 @@
1
+ /*
2
+ A simple circle component. Keep in mind this is extending the default circle in the
3
+ game engine.
4
+
5
+ A circle can be created on of two ways:
6
+
7
+ re.e('circle');
8
+
9
+ or through a factory:
10
+
11
+ re.circle(10, "#00CCFF");
12
+
13
+ */
14
+ re.c('circle')
15
+ .factory(function(radius, color){
16
+ //this is called when using re.circle()
17
+ this.radius = radius;
18
+ this.color = color;
19
+ })
20
+ .requires('align update')
21
+ .defines({
22
+ //define properties
23
+ speed:15,
24
+ color:'#ff0000'
25
+ })
26
+ .events({
27
+
28
+ //adds a listener for updates
29
+ update:function(){
30
+
31
+ //move on keypressed
32
+ if(re.pressed('a', 'left')) this.posX -= this.speed;
33
+ if(re.pressed('d', 'right')) this.posX += this.speed;
34
+
35
+ if(re.pressed('w', 'up')) this.posY -= this.speed;
36
+ if(re.pressed('s', 'down')) this.posY += this.speed;
37
+
38
+ }
39
+
40
+ });