metro 0.0.5 → 0.0.6

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.
@@ -7,6 +7,15 @@ module Metro
7
7
  #
8
8
  class Generic < Model
9
9
 
10
+ def draw
11
+ raise cannot_draw_message
12
+ end
13
+
14
+ def cannot_draw_message
15
+ [ "Unable to draw #{name} in #{scene}",
16
+ "The actor named '#{name}' could not find a suitable model so it could not be drawn in the scene #{scene}" ].join("\n")
17
+ end
18
+
10
19
  end
11
20
  end
12
21
  end
@@ -11,6 +11,13 @@ module Metro
11
11
 
12
12
  attr_accessor :angle, :center_x, :center_y, :x_factor, :y_factor
13
13
 
14
+ def after_initialize
15
+ @angle = 0
16
+ @center_x = @center_y = 0.5
17
+ @x_factor = @y_factor = 1
18
+ @z_order = 0
19
+ end
20
+
14
21
  def image
15
22
  @image ||= Gosu::Image.new(window,asset_path(path))
16
23
  end
@@ -11,7 +11,8 @@ module Metro
11
11
 
12
12
  attr_accessor :x, :y, :x_factor, :y_factor, :z_order
13
13
 
14
- def initialize
14
+ def after_initialize
15
+ @text = ""
15
16
  @x_factor = @y_factor = 1.0
16
17
  end
17
18
 
@@ -2,25 +2,25 @@ module Metro
2
2
  module Models
3
3
 
4
4
  #
5
- # Draws a a menu of options. It is called a Select as it is named after the HTML
6
- # element select. A select model also inserts itself into the scene as an event
5
+ # Draws a a menu of options. A menu model inserts itself into the scene as an event
7
6
  # target as it needs to maintain the state of the menu. When an option is selected
8
7
  # an event is fired based on the name of the option.
9
8
  #
10
- # @note Only one 'select' can be defined for a given scene.
9
+ # @note Only one 'menu' can be defined for a given scene.
11
10
  #
12
- class Select < Model
13
- attr_accessor :selected_index, :options
11
+ class Menu < Model
12
+ attr_reader :selected_index, :menu_options
14
13
 
15
- def initialize
14
+ def after_initialize
16
15
  @selected_index = 0
17
16
  end
18
17
 
19
18
  def window=(value)
20
19
  @window = value
20
+ @menu_options = options.map {|option| Option.new option }
21
21
  events
22
22
  end
23
-
23
+
24
24
  def events
25
25
  relay = EventRelay.new(self,window)
26
26
 
@@ -32,7 +32,7 @@ module Metro
32
32
  end
33
33
 
34
34
  def selection
35
- scene_method = options[selected_index].downcase.gsub(/\s/,'_')
35
+ scene_method = option_at_index(selected_index).method
36
36
  scene.send scene_method
37
37
  end
38
38
 
@@ -61,17 +61,44 @@ module Metro
61
61
  highlight_color.alpha = value.floor
62
62
  end
63
63
 
64
+ def option_at_index(index)
65
+ menu_options[index]
66
+ end
67
+
64
68
  def draw
65
69
  options.each_with_index do |option,index|
66
70
 
71
+ option_name = option_at_index(index).name
72
+
67
73
  draw_color = color
68
74
  draw_color = highlight_color if index == selected_index
69
75
 
70
76
  y_position = y + padding * index
71
- font.draw option, x, y_position, Metro::Game::UI, 1.0, 1.0, draw_color
77
+ font.draw option_name, x, y_position, Metro::Game::UI, 1.0, 1.0, draw_color
72
78
  end
73
79
  end
74
80
 
81
+ class Option
82
+
83
+ attr_reader :data
84
+
85
+ attr_accessor :name, :method
86
+
87
+ def initialize(data)
88
+ @data = data
89
+
90
+ if data.is_a?(Hash)
91
+ @name = data.keys.first
92
+ @method = data.values.first
93
+ else
94
+ @name = data
95
+ @method = data.to_s.downcase.gsub(/\s/,'_').gsub(/^[^a-zA-Z]*/,'').gsub(/[^a-zA-Z0-9\s_]/,'')
96
+ end
97
+
98
+ end
99
+
100
+ end
101
+
75
102
  end
76
103
  end
77
104
  end
@@ -100,7 +100,7 @@ module Metro
100
100
 
101
101
  options.each do |raw_key,value|
102
102
 
103
- key = raw_key.dup
103
+ key = raw_key.to_s.dup
104
104
  key.gsub!(/-/,'_')
105
105
  key.gsub!(/([A-Z\d]+)([A-Z][a-z])/,'\1_\2')
106
106
  key.gsub!(/([a-z\d])([A-Z])/,'\1_\2')
@@ -184,5 +184,5 @@ end
184
184
 
185
185
  require_relative 'generic'
186
186
  require_relative 'label'
187
- require_relative 'select'
187
+ require_relative 'menu'
188
188
  require_relative 'image'
@@ -254,6 +254,16 @@ module Metro
254
254
  @updaters << updater
255
255
  end
256
256
 
257
+ #
258
+ # A simplier syntax to enqueue an animation. At the moment this animation is going
259
+ # to be an implicit animation.
260
+ #
261
+ def animate(options,&block)
262
+ animation = Metro::ImplicitAnimation.new options.merge(context: self)
263
+ animation.on_complete(&block) if block
264
+ enqueue animation
265
+ end
266
+
257
267
  #
258
268
  # The `base_update` method is called by the Game Window. This is to allow for any
259
269
  # special update needs to be handled before calling the traditional `update` method
@@ -10,17 +10,22 @@ module Metro
10
10
 
11
11
  def create(contents = {})
12
12
  contents = {} unless contents
13
- actor_instance = class_for_actor(contents['model']).new
14
- actor_instance._load(contents)
15
- actor_instance
13
+ actor_class = class_for_actor(model_name(contents))
14
+ instance = actor_class.new
15
+ instance._load options.merge(contents)
16
+ instance
16
17
  end
17
18
 
18
19
  def load_from_previous_scene?
19
20
  options[:from] == :previous_scene
20
21
  end
21
22
 
23
+ def model_name(contents = {})
24
+ contents['model'] || contents[:model] || options['model'] || options[:model] || name
25
+ end
26
+
22
27
  def class_for_actor(model_name)
23
- Model.model(model_name || name)
28
+ Model.model(model_name)
24
29
  end
25
30
  end
26
31
  end
@@ -1,3 +1,3 @@
1
1
  module Metro
2
- VERSION = "0.0.5"
2
+ VERSION = "0.0.6"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: metro
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-10-21 00:00:00.000000000 Z
12
+ date: 2012-10-22 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: gosu
@@ -52,8 +52,8 @@ files:
52
52
  - lib/metro/models/generic.rb
53
53
  - lib/metro/models/image.rb
54
54
  - lib/metro/models/label.rb
55
+ - lib/metro/models/menu.rb
55
56
  - lib/metro/models/model.rb
56
- - lib/metro/models/select.rb
57
57
  - lib/metro/scene.rb
58
58
  - lib/metro/scene_actor.rb
59
59
  - lib/metro/scene_view/json_view.rb