gamebox 0.3.4 → 0.4.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (184) hide show
  1. data/README.rdoc +38 -0
  2. data/Rakefile +1 -10
  3. data/TODO.txt +6 -6
  4. data/app_generators/gamebox_generator.rb +95 -0
  5. data/{lib/gamebox/templates/template_app → app_generators/templates}/.gitignore +0 -0
  6. data/app_generators/templates/Gemfile +7 -0
  7. data/app_generators/templates/NEXT_STEPS.txt +1 -0
  8. data/{lib/gamebox/templates/template_app/README → app_generators/templates/README.rdoc} +0 -0
  9. data/{lib/gamebox/templates/template_app → app_generators/templates}/Rakefile +0 -0
  10. data/{lib/gamebox/templates/template_app → app_generators/templates}/config/boot.rb +0 -0
  11. data/app_generators/templates/config/environment.rb +30 -0
  12. data/{lib/gamebox/templates/template_app → app_generators/templates}/config/game.yml +0 -0
  13. data/{lib/gamebox/templates/template_app → app_generators/templates}/data/fonts/FONTS_GO_HERE +0 -0
  14. data/{lib/gamebox/templates/template_app → app_generators/templates}/data/graphics/GRAPHICS_GO_HERE +0 -0
  15. data/{lib/gamebox/templates/template_app → app_generators/templates}/data/music/MUSIC_GOES_HERE +0 -0
  16. data/{lib/gamebox/templates/template_app → app_generators/templates}/data/sounds/SOUND_FX_GO_HERE +0 -0
  17. data/{lib/gamebox/templates → app_generators/templates/script}/actor.erb +0 -0
  18. data/{lib/gamebox/templates → app_generators/templates/script}/actor_spec.erb +0 -0
  19. data/{lib/gamebox/templates → app_generators/templates/script}/actor_view.erb +0 -0
  20. data/{lib/gamebox/templates → app_generators/templates/script}/actor_view_spec.erb +0 -0
  21. data/app_generators/templates/script/generate +12 -0
  22. data/{lib/gamebox/templates/template_app → app_generators/templates}/spec/helper.rb +0 -0
  23. data/app_generators/templates/src/actors/player.rb +8 -0
  24. data/{lib/gamebox/templates/template_app → app_generators/templates}/src/app.rb +0 -0
  25. data/app_generators/templates/src/demo_stage.rb +7 -0
  26. data/bin/gamebox +8 -70
  27. data/component_generators/actor_generator.rb +17 -0
  28. data/docs/CODE_REVIEW +1 -1
  29. data/docs/REFACTOR_NOTES.txt +25 -0
  30. data/docs/getting_started.rdoc +1 -1
  31. data/gamebox.gemspec +7 -4
  32. data/lib/gamebox.rb +6 -3
  33. data/lib/gamebox/actors/collidable_debugger.rb +13 -15
  34. data/lib/gamebox/actors/curtain.rb +44 -43
  35. data/lib/gamebox/actors/emitter.rb +3 -42
  36. data/lib/gamebox/actors/fps.rb +13 -6
  37. data/lib/gamebox/actors/label.rb +42 -34
  38. data/lib/gamebox/actors/logo.rb +2 -4
  39. data/lib/gamebox/actors/score.rb +37 -27
  40. data/lib/gamebox/actors/svg_actor.rb +45 -32
  41. data/lib/gamebox/behaviors/animated.rb +39 -59
  42. data/lib/gamebox/behaviors/audible.rb +14 -14
  43. data/lib/gamebox/behaviors/collidable.rb +65 -36
  44. data/lib/gamebox/behaviors/collidable/aabb_collidable.rb +2 -3
  45. data/lib/gamebox/behaviors/collidable/collidable_shape.rb +6 -4
  46. data/lib/gamebox/behaviors/collidable/polygon_collidable.rb +1 -1
  47. data/lib/gamebox/behaviors/emitting.rb +48 -0
  48. data/lib/gamebox/behaviors/graphical.rb +22 -56
  49. data/lib/gamebox/behaviors/layered.rb +8 -21
  50. data/lib/gamebox/behaviors/physical.rb +202 -213
  51. data/lib/gamebox/behaviors/positioned.rb +16 -0
  52. data/lib/gamebox/behaviors/projectile.rb +15 -0
  53. data/lib/gamebox/behaviors/visible.rb +16 -0
  54. data/lib/gamebox/core/aabb_helpers.rb +61 -0
  55. data/lib/gamebox/core/aabb_node.rb +118 -0
  56. data/lib/gamebox/core/aabb_tree.rb +137 -0
  57. data/lib/gamebox/core/actor.rb +102 -0
  58. data/lib/gamebox/core/actor_factory.rb +56 -0
  59. data/lib/gamebox/core/actor_view.rb +63 -0
  60. data/lib/gamebox/core/actor_view_factory.rb +40 -0
  61. data/lib/gamebox/{arbiter.rb → core/arbiter.rb} +31 -34
  62. data/lib/gamebox/{backstage.rb → core/backstage.rb} +0 -0
  63. data/lib/gamebox/core/behavior.rb +64 -0
  64. data/lib/gamebox/core/behavior_factory.rb +56 -0
  65. data/lib/gamebox/{class_finder.rb → core/class_finder.rb} +0 -0
  66. data/lib/gamebox/{config_manager.rb → core/config_manager.rb} +1 -1
  67. data/lib/gamebox/core/configuration.rb +39 -0
  68. data/lib/gamebox/core/core.rb +30 -0
  69. data/lib/gamebox/core/deprecated.rb +15 -0
  70. data/lib/gamebox/{director.rb → core/director.rb} +6 -11
  71. data/lib/gamebox/core/font_style.rb +26 -0
  72. data/lib/gamebox/core/font_style_factory.rb +11 -0
  73. data/lib/gamebox/core/game.rb +19 -0
  74. data/lib/gamebox/{hooked_gosu_window.rb → core/hooked_gosu_window.rb} +12 -6
  75. data/lib/gamebox/{input_manager.rb → core/input_manager.rb} +106 -99
  76. data/lib/gamebox/core/physics.rb +22 -0
  77. data/lib/gamebox/{physical_stage.rb → core/physics_manager.rb} +36 -30
  78. data/lib/gamebox/{resource_manager.rb → core/resource_manager.rb} +19 -18
  79. data/lib/gamebox/{sound_manager.rb → core/sound_manager.rb} +9 -7
  80. data/lib/gamebox/{stage.rb → core/stage.rb} +42 -80
  81. data/lib/gamebox/{stage_manager.rb → core/stage_manager.rb} +46 -53
  82. data/lib/gamebox/{stagehand.rb → core/stagehand.rb} +0 -0
  83. data/lib/gamebox/{svg_document.rb → core/svg_document.rb} +0 -0
  84. data/lib/gamebox/core/timer_manager.rb +50 -0
  85. data/lib/gamebox/{viewport.rb → core/viewport.rb} +2 -3
  86. data/lib/gamebox/{wrapped_screen.rb → core/wrapped_screen.rb} +12 -19
  87. data/lib/gamebox/gamebox_application.rb +7 -15
  88. data/lib/gamebox/lib/evented_attributes.rb +51 -0
  89. data/lib/gamebox/{ftor.rb → lib/ftor.rb} +0 -0
  90. data/lib/gamebox/lib/min_max_helpers.rb +10 -0
  91. data/lib/gamebox/lib/rect.rb +112 -54
  92. data/lib/gamebox/lib/yoda.rb +46 -0
  93. data/lib/gamebox/spec/helper.rb +317 -12
  94. data/lib/gamebox/stagehands/spatial_tree_stagehand.rb +61 -0
  95. data/lib/gamebox/version.rb +8 -3
  96. data/lib/gamebox/views/graphical_actor_view.rb +22 -29
  97. data/script/perf_aabb.rb +56 -0
  98. data/script/perf_array_access.rb +16 -0
  99. data/script/perf_collisions.rb +37 -18
  100. data/script/perf_struct_vs_array.rb +7 -7
  101. data/spec/acceptance/animation_spec.rb +65 -0
  102. data/spec/acceptance/basic_actor_lifecycle_spec.rb +92 -0
  103. data/spec/acceptance/built_in_collision_handling_spec.rb +55 -0
  104. data/spec/acceptance/chipmunk_collision_handling_spec.rb +83 -0
  105. data/spec/acceptance/fps_actor_spec.rb +40 -0
  106. data/spec/acceptance/pausing_spec.rb +61 -0
  107. data/spec/acceptance/timer_usage_spec.rb +53 -0
  108. data/spec/actors/emitter_spec.rb +5 -0
  109. data/spec/{label_spec.rb → actors/label_spec.rb} +1 -1
  110. data/spec/behaviors/animated_spec.rb +85 -0
  111. data/spec/behaviors/collidable_spec.rb +134 -0
  112. data/spec/{physical_spec.rb → behaviors/physical_spec.rb} +2 -1
  113. data/spec/behaviors/positioned_spec.rb +6 -0
  114. data/spec/behaviors/projectile_spec.rb +6 -0
  115. data/spec/core/aabb_tree_spec.rb +109 -0
  116. data/spec/core/actor_factory_spec.rb +44 -0
  117. data/spec/core/actor_spec.rb +78 -0
  118. data/spec/core/actor_view_spec.rb +53 -0
  119. data/spec/{arbiter_spec.rb → core/arbiter_spec.rb} +29 -30
  120. data/spec/core/backstage_spec.rb +37 -0
  121. data/spec/core/behavior_factory_spec.rb +50 -0
  122. data/spec/core/behavior_spec.rb +8 -0
  123. data/spec/core/configuration_spec.rb +8 -0
  124. data/spec/core/core_spec.rb +13 -0
  125. data/spec/core/font_style_factory_spec.rb +17 -0
  126. data/spec/core/font_style_spec.rb +41 -0
  127. data/spec/core/hooked_gosu_window_spec.rb +75 -0
  128. data/spec/core/input_manager_spec.rb +285 -0
  129. data/spec/core/physics_manager_spec.rb +11 -0
  130. data/spec/core/resource_manager_spec.rb +12 -0
  131. data/spec/core/stage_manager_spec.rb +140 -0
  132. data/spec/core/stage_spec.rb +73 -0
  133. data/spec/core/timer_manager_spec.rb +89 -0
  134. data/spec/{viewport_spec.rb → core/viewport_spec.rb} +6 -3
  135. data/spec/core/wrapped_screen_spec.rb +26 -0
  136. data/spec/fixtures/game.yml +7 -0
  137. data/spec/fixtures/snelpling/idle/1.png +0 -0
  138. data/spec/fixtures/snelpling/jump/1.png +0 -0
  139. data/spec/fixtures/snelpling/jump/2.png +0 -0
  140. data/spec/fixtures/snelpling/jump/3.png +0 -0
  141. data/spec/helper.rb +8 -0
  142. data/spec/{class_finder_spec.rb → lib/class_finder_spec.rb} +2 -1
  143. data/spec/stagehands/spatial_tree_stagehand_spec.rb +19 -0
  144. data/spec/views/graphical_actor_view_spec.rb +116 -0
  145. metadata +343 -144
  146. data/README.txt +0 -34
  147. data/lib/gamebox/actor.rb +0 -179
  148. data/lib/gamebox/actor_factory.rb +0 -57
  149. data/lib/gamebox/actor_view.rb +0 -44
  150. data/lib/gamebox/actors/spatial_debugger.rb +0 -62
  151. data/lib/gamebox/behavior.rb +0 -70
  152. data/lib/gamebox/behaviors/timed.rb +0 -33
  153. data/lib/gamebox/behaviors/updatable.rb +0 -12
  154. data/lib/gamebox/console_app.rb +0 -41
  155. data/lib/gamebox/gamebox_generator.rb +0 -32
  156. data/lib/gamebox/generators/actor_generator.rb +0 -43
  157. data/lib/gamebox/generators/view_generator.rb +0 -42
  158. data/lib/gamebox/physical_director.rb +0 -17
  159. data/lib/gamebox/physics.rb +0 -32
  160. data/lib/gamebox/spatial_bucket.rb +0 -9
  161. data/lib/gamebox/spatial_hash.rb +0 -194
  162. data/lib/gamebox/spatial_stagehand.rb +0 -80
  163. data/lib/gamebox/templates/template_app/Gemfile +0 -6
  164. data/lib/gamebox/templates/template_app/config/environment.rb +0 -23
  165. data/lib/gamebox/templates/template_app/config/stage_config.yml +0 -2
  166. data/lib/gamebox/templates/template_app/script/generate +0 -7
  167. data/lib/gamebox/templates/template_app/src/demo_stage.rb +0 -11
  168. data/lib/gamebox/templates/template_app/src/game.rb +0 -19
  169. data/lib/gamebox/templates/template_app/src/my_actor.rb +0 -14
  170. data/script/perf_spatial_hash.rb +0 -64
  171. data/spec/actor_factory_spec.rb +0 -61
  172. data/spec/actor_spec.rb +0 -71
  173. data/spec/actor_view_spec.rb +0 -61
  174. data/spec/animated_spec.rb +0 -83
  175. data/spec/backstage_spec.rb +0 -45
  176. data/spec/behavior_spec.rb +0 -28
  177. data/spec/collidable_spec.rb +0 -135
  178. data/spec/emitter_spec.rb +0 -20
  179. data/spec/input_manager_spec.rb +0 -134
  180. data/spec/resource_manager_spec.rb +0 -13
  181. data/spec/spatial_hash_spec.rb +0 -119
  182. data/spec/spatial_stagehand_spec.rb +0 -93
  183. data/spec/stage_manager_spec.rb +0 -25
  184. data/spec/stage_spec.rb +0 -65
@@ -16,7 +16,7 @@ The reason I wrote Gamebox is twofold: first, to aid in 48 hour game writing com
16
16
  - required gems
17
17
  - bundler
18
18
  - gosu
19
- - constructor
19
+ - conject
20
20
  - publisher
21
21
  - rspec for unit tests
22
22
 
data/gamebox.gemspec CHANGED
@@ -19,12 +19,14 @@ Gem::Specification.new do |s|
19
19
  s.require_paths = ["lib"]
20
20
 
21
21
  s.add_dependency "gosu"
22
- s.add_dependency "constructor"
23
22
  s.add_dependency "publisher"
24
- s.add_dependency "diy"
23
+ s.add_dependency "conject", ">= 0.0.5"
25
24
  s.add_dependency "tween"
25
+ s.add_dependency "activesupport", '3.0.0'
26
+ s.add_dependency "i18n"
27
+ s.add_dependency "rubigen"
26
28
  s.add_dependency "require_all"
27
- s.add_dependency "kvo"
29
+ s.add_dependency "kvo", ">= 0.0.2"
28
30
 
29
31
  s.add_development_dependency "pry", '~>0.9.7'
30
32
  s.add_development_dependency "pry-remote"
@@ -35,6 +37,7 @@ Gem::Specification.new do |s|
35
37
  s.add_development_dependency "rspec"
36
38
  s.add_development_dependency "rake"
37
39
  s.add_development_dependency "polaris"
38
-
40
+ s.add_development_dependency "simplecov"
41
+ s.add_development_dependency "chipmunk"
39
42
  end
40
43
 
data/lib/gamebox.rb CHANGED
@@ -1,6 +1,5 @@
1
1
  GAMEBOX_PATH = File.join(File.dirname(__FILE__),"gamebox/")
2
- require 'diy'
3
- require 'constructor'
2
+ require 'conject'
4
3
  require 'kvo'
5
4
  require 'tween'
6
5
  require 'gosu'
@@ -19,5 +18,9 @@ begin
19
18
  rescue LoadError
20
19
  end
21
20
 
21
+ require "#{GAMEBOX_PATH}/version.rb"
22
22
  require 'require_all'
23
- require_all Dir.glob("#{GAMEBOX_PATH}/**/*.rb").reject { |f| f.match("template_app") || f.match("spec") || f.match("gamebox_application.rb")}
23
+ directory_load_order = %w(lib core actors behaviors views stagehands)
24
+ directory_load_order.each do |dir|
25
+ require_all "#{GAMEBOX_PATH}/#{dir}/**/*.rb"
26
+ end
@@ -1,27 +1,25 @@
1
+ Actor.define :collidable_debugger do
2
+ has_behaviors :collider_container
3
+ end
1
4
 
2
-
3
-
4
- class CollidableDebugger < Actor
5
-
6
- attr_reader :actor
7
- def setup
8
- @actor = opts[:collider]
9
- @actor.when :remove_me do
10
- remove_self
5
+ Behavior.define :collider_container do
6
+ setup do
7
+ actor.has_attributes collider: actor.opts[:collider]
8
+ actor.collider.when :remove do
9
+ actor.remove
11
10
  end
12
-
13
11
  end
14
12
  end
15
13
 
16
- class CollidableDebuggerView < ActorView
14
+ ActorView.define :collidable_debugger_view do
17
15
 
18
- def setup
16
+ configure do
19
17
  @color = Color::WHITE
20
18
  end
21
19
 
22
- def draw(target,x_off,y_off,z)
23
- collider = @actor.actor
24
- case collider.collidable_shape
20
+ draw do |target,x_off,y_off,z|
21
+ collider = actor.collider
22
+ case collider.shape_type
25
23
  when :circle
26
24
  target.draw_circle x_off+collider.center_x, y_off+collider.center_y, collider.radius, @color, z
27
25
  else
@@ -1,57 +1,58 @@
1
-
2
- require 'publisher'
3
-
4
-
5
- class CurtainView < ActorView
6
- def draw(target,x_off,y_off,z)
7
- target.fill 0,0,1024,800, [0,0,0,@actor.height], z
1
+ ActorView.define :curtain_view do
2
+ draw do |target,x_off,y_off,z|
3
+ target.fill 0,0,1024,800, [0,0,0,actor.height], z
8
4
  end
9
5
  end
10
6
 
11
- class Curtain < Actor
12
- extend Publisher
13
-
14
- can_fire :curtain_up, :curtain_down
7
+ FULL_CURTAIN = 255
8
+ NO_CURTAIN = 0
15
9
 
16
- has_behavior :updatable, :layered => {:layer => 99_999}
10
+ Behavior.define :curtain_operator do
11
+ requires :director
12
+ setup do
13
+ actor.has_attributes duration_in_ms: 1000
14
+ actor.has_attributes dir: :down
17
15
 
18
- attr_accessor :height
19
-
20
- FULL_CURTAIN = 255
21
- NO_CURTAIN = 0
22
- def setup
23
- @duration_in_ms = @opts[:duration]
24
- @duration_in_ms ||= 1000
25
-
26
- case @opts[:dir]
16
+ case actor.dir
27
17
  when :up
28
- @height = FULL_CURTAIN
29
- @dir = -1
18
+ height = FULL_CURTAIN
19
+ actor.dir = -1
30
20
  when :down
31
- @height = NO_CURTAIN
32
- @dir = 1
21
+ height = NO_CURTAIN
22
+ actor.dir = 1
33
23
  end
34
- end
35
24
 
36
- # Update curtain height 0-255 (alpha)
37
- def update(time)
38
- perc_change = time.to_f/@duration_in_ms
39
- amount = FULL_CURTAIN * perc_change * @dir
40
- @height += amount.floor
25
+ actor.has_attributes height: height
26
+ director.when :update do |time|
27
+ update time
28
+ end
29
+ end
41
30
 
42
- if @height < 0
43
- @height = 0
44
- if alive?
45
- fire :curtain_up
46
- remove_self
47
- end
48
- elsif @height > 255
49
- @height = 255
50
- if alive?
51
- fire :curtain_down
52
- remove_self
31
+ helpers do
32
+ # Update curtain height 0-255 (alpha)
33
+ def update(time)
34
+ perc_change = time.to_f/actor.duration_in_ms
35
+ amount = FULL_CURTAIN * perc_change * actor.dir
36
+ actor.height += amount.floor
37
+
38
+ if actor.height < 0
39
+ actor.height = 0
40
+ if actor.alive
41
+ actor.emit :curtain_up
42
+ actor.remove
43
+ end
44
+ elsif actor.height > 255
45
+ actor.height = 255
46
+ if actor.alive
47
+ actor.emit :curtain_down
48
+ actor.remove
49
+ end
53
50
  end
54
51
  end
55
-
56
52
  end
57
53
  end
54
+
55
+ Actor.define :curtain do
56
+ has_behavior layered: 99_999
57
+ has_behavior :curtain_operator
58
+ end
@@ -1,6 +1,5 @@
1
- # class used for particle emissions
2
- class Emitter < Actor
3
-
1
+ # actor used for particle emissions
2
+ Actor.define :emitter do
4
3
  # options:
5
4
  # delay: ms to wait between creating more particles
6
5
  # particle_actor: actor to spawn as particle
@@ -9,43 +8,5 @@ class Emitter < Actor
9
8
  # spawn_variance: dist in pixels to spawn away from emitter (default -10..10)
10
9
  # location_tween: tween object that will be used to move the emitter (optional)
11
10
  # follow: actor to follow (optional)
12
- def setup
13
- @variance = opts[:spawn_variance] || (-10..10)
14
-
15
- spawn_timer = "#{self.object_id}_spawn"
16
- self.when :remove_me do
17
- stage.remove_timer spawn_timer
18
- end
19
- stage.add_timer spawn_timer, opts[:delay] do
20
- spawn_particle
21
- end
22
- ttl = opts[:ttl]
23
- if ttl
24
- suicide_timer = "#{self.object_id}_ttl"
25
- stage.add_timer suicide_timer, ttl do
26
- stage.remove_timer suicide_timer
27
- remove_self
28
- end
29
- end
30
-
31
- target = opts[:follow]
32
- if target
33
- self.x = target.x
34
- self.y = target.y
35
- target.when :x_changed do
36
- self.x = target.x
37
- end
38
- target.when :y_changed do
39
- self.y = target.y
40
- end
41
- end
42
-
43
- end
44
-
45
- def spawn_particle
46
- spawn_x = self.x + @variance.sample
47
- spawn_y = self.y + @variance.sample
48
-
49
- spawn opts[:particle_actor], {x: spawn_x, y: spawn_y}.merge(opts[:particle_opts])
50
- end
11
+ has_behavior :emitting
51
12
  end
@@ -1,8 +1,15 @@
1
- class FpsView < LabelView; end
2
- class Fps < Label
3
-
4
- def text
5
- fps
1
+ Behavior.define :fps_label_updater do
2
+ requires :director, :stage
3
+ setup do
4
+ @label = stage.create_actor :label, actor.attributes
5
+ director.when :update do |time|
6
+ @label.text = Gosu.fps
7
+ end
6
8
  end
7
-
9
+ react_to do |msg, *args|
10
+ @label.remove if msg == :remove
11
+ end
12
+ end
13
+ Actor.define :fps do
14
+ has_behavior :fps_label_updater
8
15
  end
@@ -1,43 +1,51 @@
1
- class LabelView < ActorView
2
- def draw(target,x_off,y_off,z)
3
- @converted_color ||= target.convert_color(actor.color)
4
- actor.font.draw actor.text, actor.x, actor.y, z,
5
- 1,1, # x factor, y factor
6
- @converted_color
7
- end
8
- end
9
-
10
- class Label < Actor
11
- has_behavior layered: {layer: 1}
12
- attr_accessor :text, :font, :color
13
-
14
- def setup
15
- @text = @opts[:text]
16
- @size = @opts[:size]
17
- font_name = @opts[:font]
18
- @color = @opts[:color]
1
+ Behavior.define :label_stuff do
2
+ requires_behaviors :positioned
3
+ requires :font_style_factory
4
+
5
+ setup do
6
+ # will define attributes and set their values if no one else has
7
+ actor.has_attributes text: "",
8
+ font_size: 30,
9
+ font_name: "Asimov.ttf",
10
+ color: [250,250,250,255],
11
+ width: 0,
12
+ height: 0,
13
+ layer: 1
14
+
15
+
16
+ font_style = font_style_factory.build actor.font_name, actor.font_size, actor.color
17
+ actor.has_attributes font_style: font_style
18
+
19
+ actor.when :font_size_changed do
20
+ actor.font_style.reload
21
+ recalculate_size
22
+ end
23
+ actor.when :font_name_changed do
24
+ actor.font_style.reload
25
+ recalculate_size
26
+ end
27
+ actor.when :text_changed do
28
+ recalculate_size
29
+ end
19
30
 
20
- @text ||= ""
21
- @size ||= 30
22
- font_name ||= "Asimov.ttf"
23
- @color ||= [250,250,250,255]
24
- layer = opts[:layer]
25
- layered.layer = layer
26
-
27
- @font = resource_manager.load_font font_name, @size
28
31
  end
29
32
 
30
- def width
31
- font.text_width(text)
33
+ helpers do
34
+ def recalculate_size
35
+ actor.width = actor.font_style.calc_width actor.text
36
+ actor.height = actor.font_style.height
37
+ end
32
38
  end
39
+ end
33
40
 
34
- def height
35
- [font.text_width(text),font.height]
36
- end
41
+ Actor.define :label do
42
+ has_behavior layered: 1
43
+ has_behavior :label_stuff
37
44
 
38
- def resize(size)
39
- @size = size
40
- resource_manager.load_font @font, @size
45
+ view do
46
+ draw do |target,x_off,y_off,z|
47
+ target.print actor.text, actor.x, actor.y, z, actor.font_style
48
+ end
41
49
  end
42
50
 
43
51
  end
@@ -1,5 +1,3 @@
1
-
2
- class Logo < Actor
3
- has_behaviors :graphical,
4
- :layered => {:layer => 999}
1
+ define_actor :logo do
2
+ has_behaviors :graphical, layered: 999
5
3
  end
@@ -1,37 +1,47 @@
1
- class ScoreView < ActorView
2
- def draw(target,x_off,y_off,z)
3
- text = @actor.score.to_s
4
- text = '0'*(6-text.size)+text
1
+ define_behavior :score_keeper do
2
+ requires :backstage, :stage
5
3
 
6
- font = @stage.resource_manager.load_font 'Asimov.ttf', 30
7
- x = @actor.x
8
- y = @actor.y
9
- font.draw text, x,y,z#, 1,1,target.convert_color([250,250,250,255])
4
+ setup do
5
+ clear if score.nil?
6
+ # TODO helper for "attached subactor"
7
+ label = stage.create_actor(:label, actor.attributes)
8
+ actor.has_attributes label: label
9
+ actor.when :position_changed do
10
+ actor.label.x = actor.x
11
+ actor.label.y = actor.y
12
+ end
13
+ actor.when :remove_me do
14
+ label.remove
15
+ end
16
+ update_text
17
+ reacts_with :subtract, :add
10
18
  end
11
- end
12
- class Score < Actor
13
19
 
14
- has_behavior :layered => {:layer => 999}
20
+ helpers do
21
+ def score
22
+ backstage[:score]
23
+ end
15
24
 
16
- def setup
17
- clear if backstage[:score].nil?
18
- end
25
+ def clear
26
+ backstage[:score] = 0
27
+ end
19
28
 
20
- def score
21
- backstage[:score]
22
- end
29
+ def add(amount)
30
+ backstage[:score] += amount
31
+ update_text
32
+ end
23
33
 
24
- def clear
25
- backstage[:score] = 0
26
- end
34
+ def subtract(amount)
35
+ backstage[:score] -= amount
36
+ update_text
37
+ end
27
38
 
28
- def +(amount)
29
- backstage[:score] += amount
30
- self
39
+ def update_text
40
+ actor.label.text = score
41
+ end
31
42
  end
43
+ end
32
44
 
33
- def -(amount)
34
- backstage[:score] -= amount
35
- self
36
- end
45
+ define_actor :score do
46
+ has_behavior :score_keeper, layered: 999
37
47
  end