metro 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,65 @@
1
+ class Animation
2
+
3
+ attr_reader :current_step
4
+
5
+ def initialize(options)
6
+ @current_step = 0
7
+
8
+ options.each do |key,value|
9
+ send :instance_variable_set, "@#{key}".to_sym, value
10
+ self.class.send :define_method, key do
11
+ instance_variable_get("@#{key}")
12
+ end
13
+ end
14
+ after_initialize
15
+ end
16
+
17
+ def completed?
18
+ current_step >= interval
19
+ end
20
+
21
+ def step!
22
+ return if completed?
23
+
24
+ execute_step
25
+ next_step
26
+
27
+ complete! if completed?
28
+ end
29
+
30
+ def next_step
31
+ @current_step = current_step + step_interval
32
+ end
33
+
34
+ def step_interval
35
+ 1
36
+ end
37
+
38
+ def execute_block_in_context(block_name)
39
+ if respond_to? block_name
40
+ block_to_execute = send(block_name)
41
+ context.instance_eval(&block_to_execute)
42
+ else
43
+ instance_variable_get("@#{block_name}").call
44
+ end
45
+ end
46
+
47
+ def execute_step
48
+ execute_block_in_context(:step_block)
49
+ end
50
+
51
+ def complete!
52
+ execute_block_in_context(:completed)
53
+ end
54
+
55
+ def step(&block)
56
+ @step_block = block if block
57
+ end
58
+
59
+ def completed(&block)
60
+ @completed = block if block
61
+ end
62
+
63
+ end
64
+
65
+ require_relative 'implicit_animation'
@@ -0,0 +1,28 @@
1
+ module Easing
2
+ module Linear
3
+ extend self
4
+
5
+ def linear(moment,start,change,interval)
6
+ change * moment / interval + start
7
+ end
8
+
9
+ def calculate(start,final,interval)
10
+ change = final - start
11
+ (1..interval).map { |time| linear(time,start,change,interval) }
12
+ end
13
+ end
14
+
15
+ module EaseIn
16
+ extend self
17
+
18
+ def ease_in_quad(moment,start,change,interval)
19
+ change * (moment = moment / interval) * moment + start
20
+ end
21
+
22
+ def calculate(start,final,interval)
23
+ change = final - start
24
+ (1..interval).map { |time| ease_in_quad(time,start,change,interval) }
25
+ end
26
+ end
27
+ end
28
+
@@ -0,0 +1,43 @@
1
+ require_relative 'easing'
2
+
3
+ class ImplicitAnimation < Animation
4
+
5
+ attr_reader :attributes
6
+ attr_reader :deltas
7
+
8
+ def delta_for_step(attribute)
9
+ deltas[attribute].at(current_step)
10
+ end
11
+
12
+ def stepping(stepping)
13
+ @steppings ||= begin
14
+ hash = Hash.new(Easing::Linear)
15
+ hash.merge! linear: Easing::Linear,
16
+ ease_in: Easing::EaseIn
17
+ end
18
+ @steppings[stepping]
19
+ end
20
+
21
+ def easing
22
+ @easing || :linear
23
+ end
24
+
25
+ def after_initialize
26
+ @deltas = {}
27
+
28
+ @attributes = to.map { |attribute,final| attribute }
29
+
30
+ to.each do |attribute,final|
31
+ start = actor.send(attribute)
32
+ deltas[attribute] = stepping(easing).calculate(start,final,interval)
33
+ end
34
+
35
+ step do
36
+ attributes.each do |attribute|
37
+ actor.send "#{attribute}=", delta_for_step(attribute)
38
+ end
39
+ end
40
+
41
+ end
42
+
43
+ end
data/lib/metro/game.rb CHANGED
@@ -33,6 +33,10 @@ module Metro
33
33
  def fullscreen?
34
34
  !!config.fullscreen
35
35
  end
36
+
37
+ def debug?
38
+ !!config.debug
39
+ end
36
40
 
37
41
  # TODO: ZOrder related constants that belong to Starry Knight
38
42
  Background, Stars, Players, UI = *0..3
@@ -27,6 +27,10 @@ module Metro
27
27
  def fullscreen(set_fullscreen = nil)
28
28
  set_fullscreen.nil? ? @fullscreen : @fullscreen = set_fullscreen
29
29
  end
30
+
31
+ def debug(set_debug = nil)
32
+ set_debug.nil? ? @debug : @debug = set_debug
33
+ end
30
34
 
31
35
  end
32
36
  end
data/lib/metro/scene.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require_relative 'scene_view/scene_view'
2
2
  require_relative 'event_relay'
3
+ require_relative 'animation/animation'
3
4
 
4
5
  module Metro
5
6
 
@@ -64,7 +65,19 @@ module Metro
64
65
  # @param [Scene] new_scene this is the instance of the scene that is about to replace
65
66
  # the current scene.
66
67
  #
67
- def prepare_transition(new_scene) ; end
68
+ def prepare_transition_to(new_scene) ; end
69
+
70
+ #
71
+ # Before a scene is transisitioned to it is called with the previous scene. This
72
+ # allows for the new scene to rerieve any data from the previous scene to assist
73
+ # with the layout of the current scene.
74
+ #
75
+ # @note This method should be implemented in the Scene subclass.
76
+ #
77
+ # @param [Scene] old_scene this is the instance of the scene that is being moved
78
+ # away from.
79
+ #
80
+ def prepare_transition_from(old_scene) ; end
68
81
 
69
82
  #
70
83
  # The window is the main instance of the game. Using window can access a lot of
@@ -120,7 +133,14 @@ module Metro
120
133
  # or for generating the appropriate view information.
121
134
  #
122
135
  def self.scene_name(scene_name=nil)
123
- @scene_name ||= to_s[/^(.+)Scene$/,1].downcase
136
+ @scene_name ||= begin
137
+ root_name = to_s[/^(.+)Scene$/,1]
138
+ root_name.gsub!(/([A-Z\d]+)([A-Z][a-z])/,'\1_\2')
139
+ root_name.gsub!(/([a-z\d])([A-Z])/,'\1_\2')
140
+ root_name.downcase!
141
+ root_name
142
+ end
143
+
124
144
  scene_name ? @scene_name = scene_name.to_s : @scene_name
125
145
  end
126
146
 
@@ -187,7 +207,8 @@ module Metro
187
207
  #
188
208
  def _prepare_transition(new_scene)
189
209
  log.debug "Preparing to transition from scene #{self} to #{new_scene}"
190
- prepare_transition(new_scene)
210
+ prepare_transition_to(new_scene)
211
+ new_scene.prepare_transition_from(self)
191
212
  end
192
213
 
193
214
  #
@@ -18,13 +18,19 @@ module Metro
18
18
  #
19
19
  class CompositeDrawer < Drawer
20
20
 
21
+ attr_accessor :draw_debug
22
+
21
23
  #
22
24
  # Render all the view elements defined that are supported by this drawer.
23
25
  #
24
26
  def draw(view)
25
27
  view.each do |name,content|
26
28
  type = content['type']
27
- drawers[type].draw(content.merge 'name' => name)
29
+
30
+ if not content.key?('debug') or (content['debug'] and draw_debug)
31
+ drawers[type].draw(content.merge 'name' => name)
32
+ end
33
+
28
34
  end
29
35
  end
30
36
 
@@ -21,7 +21,8 @@ module Metro
21
21
  end
22
22
 
23
23
  def draw(view)
24
- images[view['name']].draw(view['x'],view['y'],1)
24
+ image = images[view['name']]
25
+ image.draw(view['x'] - image.width / 2.0,view['y'] - image.height / 2.0,1)
25
26
  end
26
27
 
27
28
  end
@@ -51,7 +51,11 @@ module Metro
51
51
  # @return an instance of a SceneView::Drawer that is capable of drawing
52
52
  # the Scene.
53
53
  def view_drawer
54
- @view_drawer ||= SceneView::CompositeDrawer.new(self)
54
+ @view_drawer ||= begin
55
+ drawer = SceneView::CompositeDrawer.new(self)
56
+ drawer.draw_debug = Game.debug?
57
+ drawer
58
+ end
55
59
  end
56
60
 
57
61
  module ClassMethods
data/lib/metro/scenes.rb CHANGED
@@ -31,7 +31,15 @@ module Metro
31
31
  # @return the Scene class that is found matching the specified scene name.
32
32
  #
33
33
  def find(scene_name)
34
- scenes_hash[scene_name]
34
+ found_scene = scenes_hash[scene_name]
35
+ log.error missing_scene_error_message(scene_name) unless found_scene
36
+ found_scene
37
+ end
38
+
39
+ def missing_scene_error_message(scene_name)
40
+ scene_names = Scene.scenes.map(&:scene_name).join(", ")
41
+ [ "Could not find scene with name '#{scene_name}'",
42
+ "Known scenes: #{scene_name}" ].join("\n")
35
43
  end
36
44
 
37
45
  #
data/lib/metro/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Metro
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
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.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -42,6 +42,9 @@ files:
42
42
  - Rakefile
43
43
  - bin/metro
44
44
  - lib/metro.rb
45
+ - lib/metro/animation/animation.rb
46
+ - lib/metro/animation/easing.rb
47
+ - lib/metro/animation/implicit_animation.rb
45
48
  - lib/metro/event_relay.rb
46
49
  - lib/metro/game.rb
47
50
  - lib/metro/game/dsl.rb