entityjs 0.3.1 → 0.3.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 (148) hide show
  1. data/README.md +18 -14
  2. data/entityjs.gemspec +1 -0
  3. data/lib/entityjs/assets.rb +13 -163
  4. data/lib/entityjs/command.rb +5 -2
  5. data/lib/entityjs/commands/build.rb +63 -23
  6. data/lib/entityjs/commands/eunit.rb +44 -0
  7. data/lib/entityjs/commands/release.rb +1 -1
  8. data/lib/entityjs/commands/server.rb +19 -4
  9. data/lib/entityjs/compile.rb +152 -0
  10. data/lib/entityjs/config.rb +8 -0
  11. data/lib/entityjs/dirc.rb +70 -17
  12. data/lib/entityjs/page.rb +106 -0
  13. data/lib/entityjs/parsers/parse_coffee.rb +12 -0
  14. data/lib/entityjs/version.rb +1 -1
  15. data/lib/entityjs.rb +18 -4
  16. data/public/qunit/qunit.css +9 -3
  17. data/public/qunit/qunit.entity.js +113 -31
  18. data/public/qunit/qunit.js +296 -235
  19. data/public/qunit/qunit.mock.js +54 -2
  20. data/public/tests.html +0 -15
  21. data/spec/javascripts/src/cycle/tween_spec.js +59 -0
  22. data/spec/javascripts/src/display/screen_spec.js +0 -15
  23. data/spec/javascripts/src/display/text_spec.js +4 -0
  24. data/spec/javascripts/src/input/mouse_spec.js +18 -18
  25. data/spec/javascripts/src/input/preventdefault_spec.js +14 -0
  26. data/spec/javascripts/src/math/drag_spec.js +2 -2
  27. data/spec/javascripts/src/math/hit_spec.js +24 -0
  28. data/spec/javascripts/src/math/point_spec.js +3 -3
  29. data/spec/javascripts/src/math/tile_spec.js +1 -1
  30. data/spec/javascripts/src/media/sound_spec.js +1 -1
  31. data/spec/javascripts/src/pattern/automap_spec.js +3 -3
  32. data/spec/javascripts/src/pattern/flicker_spec.js +2 -1
  33. data/spec/javascripts/src/util/random_spec.js +17 -0
  34. data/spec/javascripts/src/util/scene_spec.js +15 -0
  35. data/spec/lib/entityjs/assets_spec.rb +2 -201
  36. data/spec/lib/entityjs/commands/build_spec.rb +18 -5
  37. data/spec/lib/entityjs/commands/eunit_spec.rb +18 -0
  38. data/spec/lib/entityjs/commands/release_spec.rb +2 -0
  39. data/spec/lib/entityjs/compile_spec.rb +204 -0
  40. data/spec/lib/entityjs/dirc_spec.rb +8 -4
  41. data/spec/lib/entityjs/page_spec.rb +59 -0
  42. data/spec/support/factories.rb +0 -2
  43. data/src/core/entity.js +4 -18
  44. data/src/core/query.js +1 -1
  45. data/src/core/re.js +6 -6
  46. data/src/core/system.js +17 -12
  47. data/src/cycle/tween.js +93 -31
  48. data/src/display/align.js +11 -6
  49. data/src/display/circle.js +5 -4
  50. data/src/display/imgtext.js +33 -22
  51. data/src/display/screen.js +0 -8
  52. data/src/display/text.js +37 -4
  53. data/src/input/keyboard.js +15 -5
  54. data/src/input/mouse.js +15 -25
  55. data/src/input/preventdefault.js +11 -0
  56. data/src/math/body.js +35 -16
  57. data/src/math/drag.js +2 -2
  58. data/src/math/force.js +2 -2
  59. data/src/math/hit.js +15 -6
  60. data/src/math/hitmap.js +6 -6
  61. data/src/math/point.js +1 -1
  62. data/src/math/tile.js +22 -17
  63. data/src/media/sound.js +1 -1
  64. data/src/pattern/automap.js +16 -27
  65. data/src/pattern/flicker.js +9 -5
  66. data/src/util/random.js +4 -0
  67. data/src/util/scene.js +2 -6
  68. data/templates/arrow_keys/scripts/{display → displays}/arrow.js +0 -0
  69. data/templates/arrow_keys/scripts/{input → inputs}/controls.js +0 -0
  70. data/templates/arrow_keys/tests/{display → displays}/arrow_test.js +1 -1
  71. data/templates/arrow_keys/tests/{input → inputs}/controls_test.js +0 -0
  72. data/templates/arrow_keys/tests/scenes/load_test.js +2 -0
  73. data/templates/{blank → circle}/config.yml +0 -0
  74. data/templates/{blank → circle}/readme.txt +0 -0
  75. data/templates/circle/scripts/init.js +7 -0
  76. data/templates/circle/scripts/scenes/home.js +62 -0
  77. data/templates/circle/scripts/scenes/load.js +11 -0
  78. data/templates/circle/tests/scenes/home_test.js +29 -0
  79. data/templates/circle/tests/scenes/load_test.js +15 -0
  80. data/templates/platform/config.yml +2 -1
  81. data/templates/platform/readme.txt +15 -16
  82. data/templates/platform/scripts/{display → displays}/bit.js +0 -0
  83. data/templates/platform/scripts/{display → displays}/hero.js +0 -0
  84. data/templates/platform/scripts/{display → displays}/tile.js +0 -0
  85. data/templates/platform/scripts/{display → displays}/tsprite.js +0 -0
  86. data/templates/platform/{assets → scripts}/levels/level1.tmx +0 -0
  87. data/templates/platform/scripts/{util → utils}/counter.js +0 -0
  88. data/templates/platform/scripts/{util → utils}/level.js +0 -0
  89. data/templates/platform/tests/{display → displays}/bit_test.js +1 -1
  90. data/templates/platform/tests/displays/hero_test.js +73 -0
  91. data/templates/platform/tests/{display → displays}/tile_test.js +1 -1
  92. data/templates/platform/tests/displays/tsprite_test.js +8 -0
  93. data/templates/platform/tests/items/coin_test.js +10 -10
  94. data/templates/platform/tests/items/item_test.js +9 -9
  95. data/templates/platform/tests/items/spring_test.js +3 -3
  96. data/templates/platform/tests/scenes/load_test.js +2 -1
  97. data/templates/platform/tests/{util → utils}/counter_test.js +2 -2
  98. data/templates/platform/tests/{util → utils}/level_test.js +3 -0
  99. data/templates/pong/config.yml +22 -0
  100. data/templates/pong/readme.txt +3 -0
  101. data/templates/pong/scripts/controls/ai.js +30 -0
  102. data/templates/pong/scripts/controls/arena.js +33 -0
  103. data/templates/pong/scripts/controls/hitmap.js +54 -0
  104. data/templates/pong/scripts/controls/player.js +24 -0
  105. data/templates/pong/scripts/controls/twoarena.js +14 -0
  106. data/templates/pong/scripts/displays/ball.js +29 -0
  107. data/templates/pong/scripts/displays/counter.js +22 -0
  108. data/templates/pong/scripts/displays/paddle.js +22 -0
  109. data/templates/pong/scripts/init.js +9 -0
  110. data/templates/pong/scripts/scenes/game.js +61 -0
  111. data/templates/pong/scripts/scenes/home.js +55 -0
  112. data/templates/pong/scripts/scenes/over.js +29 -0
  113. data/templates/pong/tests/controls/ai_test.js +34 -0
  114. data/templates/pong/tests/controls/arena_test.js +20 -0
  115. data/templates/pong/tests/controls/hitmap_test.js +89 -0
  116. data/templates/pong/tests/controls/player_test.js +25 -0
  117. data/templates/pong/tests/controls/twoarena_test.js +7 -0
  118. data/templates/pong/tests/displays/ball_test.js +10 -0
  119. data/templates/pong/tests/displays/counter_test.js +13 -0
  120. data/templates/pong/tests/displays/paddle_test.js +17 -0
  121. data/templates/pong/tests/scenes/game_test.js +16 -0
  122. data/templates/pong/tests/scenes/home_test.js +30 -0
  123. data/templates/pong/tests/scenes/over_test.js +16 -0
  124. data/templates/tiltmaze/config.yml +25 -0
  125. data/templates/tiltmaze/readme.txt +79 -0
  126. data/templates/tiltmaze/scripts/displays/ball.js +78 -0
  127. data/templates/tiltmaze/scripts/displays/target.js +13 -0
  128. data/templates/tiltmaze/scripts/displays/tile.js +2 -0
  129. data/templates/tiltmaze/scripts/init.js +7 -0
  130. data/templates/tiltmaze/scripts/levels/level1.json +14 -0
  131. data/templates/tiltmaze/scripts/levels/level2.json +14 -0
  132. data/templates/tiltmaze/scripts/levels/level3.json +14 -0
  133. data/templates/tiltmaze/scripts/levels/level4.json +15 -0
  134. data/templates/tiltmaze/scripts/levels/level5.json +18 -0
  135. data/templates/tiltmaze/scripts/scenes/game.js +56 -0
  136. data/templates/tiltmaze/scripts/scenes/home.js +24 -0
  137. data/templates/tiltmaze/scripts/structs/level.js +82 -0
  138. data/templates/tiltmaze/scripts/tiles/walltile.js +147 -0
  139. data/templates/tiltmaze/tests/displays/ball_test.js +67 -0
  140. data/templates/tiltmaze/tests/displays/target_test.js +8 -0
  141. data/templates/tiltmaze/tests/factories.js +38 -0
  142. data/templates/tiltmaze/tests/scenes/game_test.js +59 -0
  143. data/templates/tiltmaze/tests/scenes/home_test.js +7 -0
  144. data/templates/tiltmaze/tests/structs/level_test.js +44 -0
  145. data/templates/tiltmaze/tests/tiles/walltile_test.js +36 -0
  146. metadata +106 -33
  147. data/templates/platform/tests/display/hero_test.js +0 -73
  148. data/templates/platform/tests/display/tsprite_test.js +0 -8
data/README.md CHANGED
@@ -3,13 +3,11 @@ An HTML5 javascript game engine utlizing the entity-component design. Write high
3
3
 
4
4
  [EntityJS Website](http://entityjs.com) | [Demos](http://entityjs.com/demos) | [Tutorials](http://entityjs.com/tutorials) | [API](http://entityjs.com/api)
5
5
 
6
- ## Version 0.3
6
+ ## Version 0.3.2
7
7
 
8
- Version 0.3 is a major release with many revisions and a brand new ruby gem to work with. You can now type `entityjs new game-name` and a new game will be created. Of course there are more commands, check below.
9
-
10
- There is a new directory structure, testing framework, better minifier, better config file and more.
11
-
12
- **Warning:** there are many name changes which will break older entity games!
8
+ * Coffeescript is now supported.
9
+ * Assets directory is now used for external loading only.
10
+ * Scripts directory now accepts, tmx, xml, json and coffee files.
13
11
 
14
12
  ## API
15
13
  Currently the [API](http://entityjs.com/api) is out of date. It will slowly be updated everyday.
@@ -58,17 +56,17 @@ See all available templates:
58
56
 
59
57
  `entityjs comp rock`
60
58
 
61
- This will create a new component called *rock* at scripts/rock.js and will create a test file inside test/rock.js
59
+ This will create a new component called *rock* at scripts/rock.js and will create a test file.
62
60
 
63
61
  `entityjs comp display/hero`
64
62
 
65
- This will create a new component in the directory scripts/display.
63
+ This will create a hero component in the directory scripts/display.
66
64
 
67
65
  ### Creating a Test
68
66
 
69
67
  `entityjs test name test1 test1`
70
68
 
71
- Create a test in tests/name_test.js with two test methods.
69
+ Create a test in /tests/name_test.js with two test methods.
72
70
 
73
71
  ### Build Game
74
72
 
@@ -90,18 +88,18 @@ To run tests visit:
90
88
  Assets are located here:
91
89
  `localhost:2345/assets/*name`
92
90
 
91
+ View [all commands](/bendangelo/EntityJS/wiki/commands)
92
+
93
93
  ## Directory Structure
94
94
 
95
95
  * Assets - Contains all external assets
96
96
  * Images - Add any images here and retrieve them with `re.assets.images`
97
97
  * Sounds - Add any sounds and retrieve them with `re.assets.sounds`
98
- * [Custom] - Create any folders or files and they will be available in code.
99
- * json, tmx, xml, sounds and images are accepted files.
100
98
 
101
99
  * Builds - Contains all finished builds
102
100
 
103
- * Scripts - Contains all javascript sources.
104
- * Plugins - Contains external scripts from other authors.
101
+ * Scripts - Contains all js, coffee sources. Xml, tmx and json files will be converted into js.
102
+ * Plugins - Contains minified scripts for plugin play.
105
103
 
106
104
  * Tests - Contains test files to run in [QUnit](http://docs.jquery.com/QUnit)
107
105
 
@@ -193,7 +191,7 @@ Simply create a new directory in /assets named levels or anything you like to sa
193
191
  re('level1.tmx')[0]; //assuming the file name is level1.tmx
194
192
  re('level'); //find all levels that are in the /assets/levels directory
195
193
 
196
- If you are still confused create a new platform game and view how the levels are used.
194
+ If you are still confused create a new platform game and view levels are used.
197
195
 
198
196
  ## Quick Start Guide
199
197
  First you should install [ruby](http://rubyinstaller.org/) and the [entityjs gem](http://rubygems.org/gems/entityjs).
@@ -205,3 +203,9 @@ Move into the `mygame` directory and play the game:
205
203
  `entityjs server`
206
204
 
207
205
  Open your browser and navigate to `localhost:2345`
206
+
207
+ ## Contributing
208
+
209
+ Before sending a pull request it would be a good idea to run `rspec` and `rake jasmine` to make sure all tests pass in both ruby and javascript.
210
+
211
+ Running rspec will generate a /mygame/ folder with test files. It will be ignored by git so don't worry about it.
data/entityjs.gemspec CHANGED
@@ -25,6 +25,7 @@ Gem::Specification.new do |s|
25
25
  s.add_development_dependency "jasmine"
26
26
 
27
27
  s.add_dependency "sinatra"
28
+ s.add_dependency "coffee-script"
28
29
  s.add_dependency 'uglifier'
29
30
  s.add_dependency 'json'
30
31
  s.add_dependency 'cobravsmongoose'
@@ -3,180 +3,29 @@ module Entityjs
3
3
 
4
4
  class Assets
5
5
 
6
- def self.valid_datas
7
- ['xml', 'json', 'tmx']
6
+ def self.valid_images
7
+ ['gif', 'png', 'jpg', 'jpeg']
8
8
  end
9
9
 
10
- def self.datas_regex
11
- return /^.*\.#{self.valid_datas.join('|')}$/i
12
- end
13
-
14
- def self.set_vars(contents, tests=false)
15
- #read file for changes
16
- Config.instance.reload
17
-
18
- contents = contents.gsub("$WIDTH", Config.instance.width.to_s)
19
- contents = contents.gsub("$HEIGHT", Config.instance.height.to_s)
20
- contents = contents.gsub("$CANVAS_ID", Config.instance.canvas_id)
21
-
22
- contents.gsub("$JS", Assets.to_html(tests))
23
- end
24
-
25
- #combine all sources into valid html scripts
26
- def self.to_html(tests=false)
27
- js = %Q(
28
- <script type='text/javascript'>
29
- window.addEventListener\('load', function(){
30
- #{Assets.to_js}
31
- re.version = '#{VERSION}';
32
-
33
- }\);
34
- </script>
35
- )
36
- ent = Dirc.find_entity_src_url(Config.instance.entity_ignore)
37
- srcs = Dirc.find_scripts_url(Config.instance.scripts_ignore, Config.instance.scripts_order)
38
-
39
- if tests
40
- tests_src = Dirc.find_tests_url(Config.instance.tests_ignore)
41
- else
42
- tests_src = []
43
- end
44
-
45
- merg = ent | srcs | tests_src
46
-
47
- merg.each do |s|
48
- js += "\t<script src='#{s}' type='text/javascript'></script>\n"
49
- end
50
- js
51
- end
52
-
53
- #converts all assets / info into a js game header
54
- def self.to_js(path = nil, images = nil, sounds = nil, canvas = nil, datas = nil)
55
- path ||= 'assets/'
56
- images ||= self.images_to_js
57
- sounds ||= self.sounds_to_js
58
- canvas ||= Config.instance.canvas_id
59
- datas ||= self.datas_to_js
60
-
61
- return %Q(
62
- re.load.path = '#{path}';
63
- re.assets = {
64
- images:#{images},
65
- sounds:#{sounds}
66
- };
67
- re.canvas = '##{canvas}';
68
- #{datas}
69
- )
70
- end
71
-
72
- def self.images_to_js(images = nil)
73
- images ||= self.search('images')
74
-
75
- s = images.collect{|i| "'#{i}'"}.join(', ')
76
-
77
- "[#{s}]"
78
- end
79
-
80
- def self.sounds_to_js(sounds = nil)
81
- sounds ||= self.search('sounds')
82
-
83
- s = sounds.collect{|i| "'#{i}'"}.join(', ')
84
-
85
- "[#{s}]"
86
- end
87
-
88
- def self.datas_to_js
89
- s = self.search
90
-
91
- out = ''
92
-
93
- s.each do |i|
94
-
95
- file = i
96
- data = IO.read(Dirc.game_root+'/'+Config.assets_folder+'/'+i)
97
-
98
- out += self.data_to_js(i, data)
99
-
100
- end
101
-
102
-
103
- out
104
- end
105
-
106
- def self.data_to_js(file, data)
107
- contents = self.data_to_json(file, data)
108
-
109
- basename = File.basename(file)
110
- dirname = File.dirname(file)
111
-
112
- comps = []
113
-
114
- dirname.split('/').each do |i|
115
-
116
- #make singular
117
- if i[-1] == 's'
118
- i = i[0..-2]
119
- end
120
-
121
- comps.push i
122
-
123
- end
124
-
125
- #remove dot because no base dir was given
126
- comps.delete_if{|i| i=='.'}
127
-
128
- %Q(
129
- re.e('#{basename} #{comps.join(' ')}')
130
- .attr(#{contents});
131
- )
132
- end
133
-
134
- def self.data_to_json(file, data)
135
- ext = file.downcase
136
-
137
- case ext
138
- when /json$/
139
- return data
140
-
141
- when /tmx$/
142
-
143
- return ParseTMX.parse(data)
144
-
145
- when /xml$/
146
- return ParseXML.parse(data)
147
-
148
- when /csv$/
149
- raise 'CSV files are not supported at the moment'
150
-
151
- when /yml$/
152
- raise 'YML files are not supported at the moment'
153
-
154
- else
155
- return data
156
-
157
- end
158
-
159
- end
160
-
161
- def self.file_to_json(file)
162
- contents = IO.read(file)
163
-
164
- return self.data_to_json(file, contents)
10
+ def self.valid_sounds
11
+ ['mp3', 'ogg', 'aac', 'wav']
165
12
  end
166
13
 
167
14
  def self.search(type='*')
168
- images_folder = Config.images_folder
169
- sounds_folder = Config.sounds_folder
170
- assets_folder = Config.assets_folder
171
15
 
172
16
  case type
173
17
  when 'images'
174
- return self.find_files(images_folder+'/*').select{|i| i.match(/^*\.(gif|png|jpg|jpeg)$/i)}
18
+ images_folder = Config.images_folder
19
+ valid_images = self.valid_images.join(',')
20
+ return self.find_files(images_folder+"/**/*.{#{valid_images}}")
175
21
 
176
22
  when 'sounds'
177
- return self.find_files(sounds_folder+'/*').select{|i| i.match(/^*\.(mp3|ogg|aac|wav)$/i)}
23
+ sounds_folder = Config.sounds_folder
24
+ valid_sounds = self.valid_sounds.join(',')
25
+ return self.find_files(sounds_folder+"/**/*.{#{valid_sounds}}")
178
26
 
179
27
  else
28
+ #TODO: fix up
180
29
  return self.search_datas
181
30
 
182
31
  end
@@ -184,7 +33,8 @@ module Entityjs
184
33
  end
185
34
 
186
35
  def self.search_datas
187
- return self.find_files("#{Config.assets_folder}/*/*").select{|i| !i.match(/\/(images|sounds)\//i) && i.match(self.datas_regex)}
36
+ #TODO: find all other folders and generate a key in re.assets
37
+ return self.find_files("#{Config.assets_folder}/*/*").select{|i| i.match(/(images|sounds)\//i) == nil }
188
38
  end
189
39
 
190
40
  def self.find_files(search)
@@ -25,6 +25,8 @@ module Entityjs
25
25
 
26
26
  when 'release'
27
27
  return Entityjs::Release.generate(args)
28
+ when 'eunit'
29
+ return Entityjs::Eunit.generate(args)
28
30
 
29
31
  when /^(comp|c)$/
30
32
  return Entityjs::Comp.generate(args)
@@ -35,11 +37,11 @@ module Entityjs
35
37
  when /^(font|f)$/
36
38
  return Entityjs::Font.generate(args)
37
39
 
38
- when /^(version|v)$/
40
+ when /^(version|v|-v)$/
39
41
  puts 'EntityJS V'+Entityjs::VERSION
40
42
  return 0
41
43
 
42
- when /^(help|h)$/
44
+ when /^(help|h|-h)$/
43
45
  puts ""
44
46
  puts '---- Commands ----'
45
47
  puts 'entityjs new [name]'
@@ -49,6 +51,7 @@ module Entityjs
49
51
  puts 'entityjs test [name]'
50
52
  puts 'entityjs test [name] [tests]+'
51
53
  puts 'entityjs font [name] (soon...)'
54
+ puts 'entityjs release'
52
55
  puts 'entityjs build'
53
56
  puts 'entityjs build [name]'
54
57
  puts 'entityjs server'
@@ -34,30 +34,18 @@ module Entityjs
34
34
  Dirc.create_dir('builds', true)
35
35
 
36
36
  #create new directory
37
- if Dir.exists?(name)
37
+ if File.directory?(name)
38
38
  return 3
39
39
  end
40
40
 
41
- assets_root = Dirc.game_root+'/'
41
+ assets_root = Dirc.game_root+'/'+assets_folder
42
42
 
43
43
  Dir.mkdir(name)
44
44
  Dir.chdir(name)
45
-
46
- #copy images and sounds into assets
47
- Dirc.create_dir(assets_folder)
48
- Dir.chdir(assets_folder) do
49
-
50
- if Dir.exists? assets_root+images_folder
51
- puts "Copying images"
52
- FileUtils.cp_r assets_root+images_folder, 'images'
53
- end
54
-
55
- if Dir.exists? assets_root+sounds_folder
56
- puts "Copying sounds"
57
- FileUtils.cp_r assets_root+sounds_folder, 'sounds'
58
- end
59
-
60
- end
45
+
46
+ #copy everything inside the assets folder
47
+ puts "Copying assets folder"
48
+ FileUtils.cp_r assets_root, assets_folder
61
49
 
62
50
  #append all files into one big file
63
51
  puts "Compiling code"
@@ -121,20 +109,36 @@ module Entityjs
121
109
  return out
122
110
  end
123
111
 
112
+ #finds all js inside public/qunit and compiles into one string
113
+ def self.compile_eunit()
114
+ out = ''
115
+
116
+ units = Dirc.find_eunit_src
117
+
118
+ units.each do |i|
119
+ out += "\n"
120
+ out += IO.read(i)
121
+ out += "\n"
122
+ end
123
+
124
+ return out
125
+ end
126
+
124
127
  #compiles all game source and returns it
125
128
  def self.compile_scripts(ignore = nil, order=nil)
126
- scripts = Dirc.find_scripts(ignore, order)
129
+ #find with short urls for proper data processing
130
+ scripts = Dirc.find_scripts_short(ignore, order)
127
131
 
128
132
  out = ''
129
133
 
130
134
  scripts.each do |i|
131
135
  out += "\n"
132
- out += IO.read(i)
136
+ out += Compile.script_to_js(i)
133
137
  out += "\n"
134
138
  end
135
139
 
136
- #add levels, animations etc data
137
- out += Assets.to_js
140
+ #add js config
141
+ out += self.js_config
138
142
 
139
143
  return out
140
144
  end
@@ -145,13 +149,49 @@ module Entityjs
145
149
  code = Uglifier.compile(code, :copyright=>false)
146
150
 
147
151
  #add entity license statement
148
- if license
152
+ if license.is_a? String
153
+ code = license + code
154
+ elsif license
149
155
  code = Config.instance.license + code
150
156
  end
151
157
 
152
158
  return code
153
159
  end
154
160
 
161
+ def self.js_config(path = nil, images = nil, sounds = nil, canvas = nil)
162
+ path ||= 'assets/'
163
+ images ||= self.images_to_js
164
+ sounds ||= self.sounds_to_js
165
+ canvas ||= Config.instance.canvas_id
166
+
167
+ return %Q(
168
+ re.load.path = \"#{path}\";
169
+ re.assets = {
170
+ images:#{images},
171
+ sounds:#{sounds}
172
+ };
173
+ re.canvas = \"##{canvas}\";
174
+ )
175
+ end
176
+
177
+ #returns all images in a js array
178
+ def self.images_to_js(images = nil)
179
+ images ||= Assets.search('images')
180
+
181
+ s = images.collect{|i| "'#{i}'"}.join(', ')
182
+
183
+ "[#{s}]"
184
+ end
185
+
186
+ #returns all sounds in a js array
187
+ def self.sounds_to_js(sounds = nil)
188
+ sounds ||= Assets.search('sounds')
189
+
190
+ s = sounds.collect{|i| "'#{i}'"}.join(', ')
191
+
192
+ "[#{s}]"
193
+ end
194
+
155
195
  end
156
196
 
157
197
  end
@@ -0,0 +1,44 @@
1
+ module Entityjs
2
+
3
+ #outputs eunit compiled code
4
+ class Eunit
5
+
6
+ def self.generate(name=nil)
7
+
8
+ if name.is_a? Array
9
+ name = name.first
10
+ end
11
+
12
+ name ||= self.eunit_name
13
+
14
+ puts "Collecting test src files"
15
+
16
+ min = Entityjs::Build.compile_eunit
17
+
18
+ puts "Minifying"
19
+
20
+ license = "/* QUnit V1.5.0pre with EntityJS Entensions | http://docs.jquery.com/QUnit */\n"
21
+
22
+ min = Entityjs::Build.minify(min, license)
23
+
24
+ File.open(name, 'w') do |f|
25
+
26
+ f.write(min)
27
+
28
+ end
29
+
30
+ puts "Done!"
31
+ puts "File is at"
32
+ puts " ./#{name}"
33
+
34
+
35
+ return 0
36
+ end
37
+
38
+ def self.eunit_name
39
+ return "eunit-#{Entityjs::VERSION}.min.js"
40
+ end
41
+
42
+ end
43
+
44
+ end
@@ -1,6 +1,6 @@
1
1
  module Entityjs
2
2
 
3
- #compiles entity source code
3
+ #compiles entity source code and outputs it
4
4
  class Release
5
5
 
6
6
  def self.generate(name=nil)
@@ -22,21 +22,36 @@ module Entityjs
22
22
 
23
23
  get '/' do
24
24
 
25
- Assets.set_vars(IO.read("#{Entityjs::root}/public/play.html"))
25
+ Page.render_play()
26
26
 
27
27
  end
28
28
 
29
29
  get '/tests' do
30
30
 
31
- Assets.set_vars(IO.read("#{Entityjs::root}/public/tests.html"), true)
31
+ Page.render_tests()
32
32
 
33
33
  end
34
34
 
35
+ #entity source code
35
36
  get '/entityjs/*' do
36
37
  content_type 'text/javascript'
37
- IO.read(Entityjs::root+'/src/'+params[:splat].first)
38
+
39
+ Page.render_entityjs_src(params[:splat].first)
38
40
  end
39
41
 
42
+ get '/scripts/*' do
43
+ content_type 'text/javascript'
44
+
45
+ Page.render_script(params[:splat].first)
46
+ end
47
+
48
+ get '/tests/*' do
49
+ content_type 'text/javascript'
50
+
51
+ Page.render_test(params[:splat].first)
52
+ end
53
+
54
+ #qunit testing stuff
40
55
  get '/qunit/*' do
41
56
  file = params[:splat].first
42
57
  if file.match /\.js$/
@@ -45,7 +60,7 @@ module Entityjs
45
60
  content_type 'text/css'
46
61
  end
47
62
 
48
- IO.read(Entityjs::root+"/public/qunit/#{file}")
63
+ Page.render_eunit(file)
49
64
  end
50
65
 
51
66
 
@@ -0,0 +1,152 @@
1
+ #The Compile class transforms text data into js
2
+ #This includes, xml, tmx, json, coffeescript, etc
3
+ module Entityjs
4
+
5
+ class Compile
6
+
7
+ def self.valid_datas
8
+ ['xml', 'json', 'tmx', 'csv', 'yml']
9
+ end
10
+
11
+ def self.valid_scripts
12
+ ['js', 'coffee'] + self.valid_datas
13
+ end
14
+
15
+ def self.datas_regex
16
+ return /^.*\.#{self.valid_datas.join('|')}$/i
17
+ end
18
+
19
+ def self.valid_data?(file)
20
+ return file.match(datas_regex) != nil
21
+ end
22
+
23
+ def self.path
24
+ if @path.nil?
25
+ @path = Dirc.game_root
26
+ end
27
+
28
+ @path
29
+ end
30
+
31
+ def self.path=(v)
32
+ @path = v
33
+ end
34
+
35
+ def self.test_to_js(path, data=nil)
36
+ if data.nil?
37
+ #load
38
+ data = self.read_contents(Config.tests_folder+'/'+path)
39
+ end
40
+
41
+ return self.data_to_js(path, data)
42
+ end
43
+
44
+ def self.script_to_js(path, data=nil)
45
+ if data.nil?
46
+ #load
47
+ data = self.read_contents(Config.scripts_folder+'/'+path)
48
+ end
49
+
50
+ return self.data_to_js(path, data)
51
+ end
52
+
53
+ def self.data_to_js(path, data)
54
+ js = ''
55
+
56
+ if self.valid_data?(path)
57
+ comps = path.split('/')
58
+ js = self.data_to_entity(comps, data)
59
+ elsif path.match /\.coffee$/
60
+ js = self.coffee_to_js(data)
61
+ else
62
+ js = data
63
+ end
64
+
65
+ return js
66
+ end
67
+
68
+ #compiles data files into entities
69
+ def self.data_to_entity(comps, data)
70
+ if comps.is_a? String
71
+ comps = [comps]
72
+ end
73
+
74
+ #find extension for compiling, or else treat it as json
75
+ ext = comps.select{|i| !File.extname(i).empty? }.first
76
+
77
+ if ext.nil?
78
+ ext = '.json'
79
+ else
80
+ ext = File.extname(ext)
81
+ end
82
+
83
+ json = self.to_json(ext, data)
84
+
85
+ return self.to_entity(comps, json)
86
+ end
87
+
88
+ #converts given comps and json into an entityjs entity
89
+ # returns an entityjs component
90
+ def self.to_entity(comps, json)
91
+
92
+ #remove plurals
93
+ comps.collect! do |i|
94
+
95
+ #115 for ruby 1.8.7
96
+ if i[-1] == 's' || i[-1] == 115
97
+ i[0..-2]
98
+ else
99
+ i
100
+ end
101
+
102
+ end
103
+
104
+ #turn into one long strng
105
+ comps = comps.join(' ')
106
+
107
+ #output entity
108
+ return "re.e('#{comps}')\n.attr(#{json})"
109
+ end
110
+
111
+ #converts the given data to json
112
+ #the type defines the data type
113
+ #example:
114
+ # to_json('xml', "<map><bob>10</bob></map>")
115
+ # => "{"bob":{$:10}}"
116
+ def self.to_json(type, data)
117
+
118
+ type.downcase!
119
+
120
+ case type
121
+ when '.json'
122
+ return data
123
+ when '.tmx'
124
+ return self.tmx_to_json(data)
125
+ when '.xml'
126
+ return self.xml_to_json(data)
127
+ else
128
+ return data
129
+ end
130
+
131
+ end
132
+
133
+ def self.coffee_to_js(data)
134
+ return ParseCoffee.parse(data)
135
+ end
136
+
137
+ def self.tmx_to_json(data)
138
+ return ParseTMX.parse(data)
139
+ end
140
+
141
+ def self.xml_to_json(data)
142
+ return ParseXML.parse(data)
143
+ end
144
+
145
+ #used for stubbing
146
+ def self.read_contents(path)
147
+ IO.read(self.path+'/'+path)
148
+ end
149
+
150
+ end
151
+
152
+ end