Browse files

refactor: move shared files to lib

  • Loading branch information...
1 parent 1c3a3d2 commit bce4b854450ec77b4702122021979b828f4ac7af @siuying committed May 11, 2012
View
5 Rakefile
@@ -6,9 +6,12 @@ Motion::Project::App.setup do |app|
# Use `rake config' to see complete project settings.
app.name = 'SparrowDemo'
app.files += Dir.glob(File.join(app.project_dir, 'vendor/BubbleWrap/lib/**/*.rb'))
+ app.files += Dir.glob(File.join(app.project_dir, 'lib/sparrow/**/*.rb'))
+
app.pods do
dependency 'Sparrow-Framework'
end
app.frameworks += ['OpenGLES', 'OpenAL', 'QuartzCore', 'AudioToolbox', 'AVFoundation']
- app.files_dependencies "app/game/game.rb" => "app/supports/number_ext.rb"
+ app.files_dependencies "app/game/game.rb" => "lib/sparrow/supports/number_ext.rb"
+ app.files_dependencies "app/app_delegate.rb" => "lib/sparrow/events/resize_event.rb"
end
View
4 app/game/game.rb
@@ -1,4 +1,6 @@
class Game < SPSprite
+ include Sparrow
+
attr_accessor :game_width, :game_height
def initWithWidth(width, height:height)
@@ -29,7 +31,7 @@ def setup
self.addChild(@egg)
@egg.addEventListener(:"onEggTouched:", atObject:self, forType:"touch")
- @tween = SPTween.tweenWithTarget(@egg, time:5.0, transition: Transitions::EASE_IN_OUT)
+ @tween = SPTween.tweenWithTarget(@egg, time:5.0, transition:Transitions::EASE_IN_OUT)
@tween.animateProperty("rotation", targetValue:360.0.to_rad)
@tween.loop = SPLoopTypeRepeat
SPStage.mainStage.juggler.addObject(@tween)
View
2 app/game/game_controller.rb
@@ -1,4 +1,6 @@
class GameController < SPStage
+ include Sparrow
+
attr_accessor :game
def self.new(width, height)
View
63 app/models/media.rb
@@ -1,63 +0,0 @@
-module Media
- module_function
-
- @atlas = nil
- @sounds = nil
-
- def init_atlas
- return if @atlas
- @atlas = SPTextureAtlas.alloc.initWithContentsOfFile("atlas.xml")
- end
-
- def release_atlas
- @atlas = nil
- end
-
- def atlas_texture(name)
- self.init_atlas unless @atlas
- @atlas.textureByName(name)
- end
-
- def atlas_textures_with_prefix(prefix)
- self.initAtlas unless @atlas
- @atlas.texturesStartingWith(prefix)
- end
-
- def init_sound
- return if @sounds
-
- SPAudioEngine.start
- @sounds = {}
-
- soundDir = resources_path
- dirEnum = NSFileManager.defaultManager.enumeratorAtPath(soundDir)
-
- while filename = dirEnum.nextObject do
- if filename.pathExtension == "caf"
- sound = SPSound.alloc.initWithContentsOfFile(filename)
- @sounds[filename] = sound
- end
- end
- end
-
- def release_sound
- @sounds = nil
- SPAudioEngine.stop
- end
-
- def play_sound(soundName)
- sound = @sounds[soundName]
- if sound
- sound.play
- else
- SPSound.soundWithContentsOfFile(soundName).play
- end
- end
-
- def sound_channel(soundName)
- sound = @sounds[soundName]
- sound = SPSound.soundWithContentsOfFile(soundName) unless sound
- sound.createChannel
- end
-
-end
View
21 app/supports/number_ext.rb
@@ -1,21 +0,0 @@
-module GameNumbers
- PI = 3.14159265359
- PI_HALF = 1.57079632679
- TWO_PI = 6.28318530718
-
- def to_degree
- ((self) / PI * 180.0)
- end
-
- def to_rad
- ((self) / 180.0 * PI)
- end
-end
-
-class Fixnum
- include GameNumbers
-end
-
-class Float
- include GameNumbers
-end
View
17 app/supports/resize_event.rb
@@ -1,17 +0,0 @@
-class ResizeEvent < SPEvent
- SP_EVENT_TYPE_RESIZE = "resize"
-
- attr_accessor :width, :height, :animation_time
-
- def self.event(width, height, time)
- e = ResizeEvent.alloc.initWithType(SP_EVENT_TYPE_RESIZE, bubbles:false)
- e.width = width
- e.height = height
- e.animation_time = time
- e
- end
-
- def portrait?
- self.height > self.width
- end
-end
View
24 app/supports/transitions.rb
@@ -1,24 +0,0 @@
-module Transitions
- LINEAR = "linear"
- RANDOMIZE = "randomize"
-
- EASE_IN = "easeIn"
- EASE_OUT = "easeOut"
- EASE_IN_OUT = "easeInOut"
- EASE_OUT_IN = "easeOutIn"
-
- EASE_IN_BACK = "easeInBack"
- EASE_OUT_BACK = "easeOutBack"
- EASE_IN_OUT_BACK = "easeInOutBack"
- EASE_OUT_IN_BACK = "easeOutInBack"
-
- EASE_IN_ELASTIC = "easeInElastic"
- EASE_OUT_ELASTIC = "easeOutElastic"
- EASE_IN_OUT_ELASTIC = "easeInOutElastic"
- EASE_OUT_IN_ELASTIC = "easeOutInElastic"
-
- EASE_IN_BOUNCE = "easeInBounce"
- EASE_OUT_BOUNCE = "easeOutBounce"
- EASE_IN_OUT_BOUNCE = "easeInOutBounce"
- EASE_OUT_IN_BOUNCE = "easeOutInBounce"
-end
View
2 app/view_controller.rb
@@ -23,7 +23,7 @@ def didReceiveMemoryWarning
end
def loadView
- self.view = OverlayView.alloc.initWithFrame(App.frame)
+ self.view = Sparrow::OverlayView.alloc.initWithFrame(App.frame)
end
def shouldAutorotateToInterfaceOrientation(interfaceOrientation)
View
14 app/views/overlay_view.rb
@@ -1,14 +0,0 @@
-# This class makes a UIView work just like a sprite in Sparrow:
-# it will react only on touches of child objects, and won't block touches on
-# empty areas any longer. That makes it perfect for an overlay view, on which
-# we can then add all kinds of UIKit elements: textfields, iAd banners, etc.
-class OverlayView < UIView
- def pointInside(point, withEvent: event)
- self.subviews.each do |subview|
- innerPoint = [point.x - subview.frame.origin.x,
- point.y - subview.frame.origin.y]
- return true if (subview.pointInside(innerPoint, withEvent: event))
- end
- return false
- end
-end
View
3 lib/sparrow.rb
@@ -0,0 +1,3 @@
+module Sparrow
+ VERSION = "0.1.1"
+end
View
19 lib/sparrow/events/resize_event.rb
@@ -0,0 +1,19 @@
+module Sparrow
+ class ResizeEvent < SPEvent
+ SP_EVENT_TYPE_RESIZE = "resize"
+
+ attr_accessor :width, :height, :animation_time
+
+ def self.event(width, height, time)
+ e = ResizeEvent.alloc.initWithType(SP_EVENT_TYPE_RESIZE, bubbles:false)
+ e.width = width
+ e.height = height
+ e.animation_time = time
+ e
+ end
+
+ def portrait?
+ self.height > self.width
+ end
+ end
+end
View
64 lib/sparrow/supports/media.rb
@@ -0,0 +1,64 @@
+module Sparrow
+ module Media
+ module_function
+
+ @atlas = nil
+ @sounds = nil
+
+ def init_atlas
+ return if @atlas
+ @atlas = SPTextureAtlas.alloc.initWithContentsOfFile("atlas.xml")
+ end
+
+ def release_atlas
+ @atlas = nil
+ end
+
+ def atlas_texture(name)
+ self.init_atlas unless @atlas
+ @atlas.textureByName(name)
+ end
+
+ def atlas_textures_with_prefix(prefix)
+ self.initAtlas unless @atlas
+ @atlas.texturesStartingWith(prefix)
+ end
+
+ def init_sound
+ return if @sounds
+
+ SPAudioEngine.start
+ @sounds = {}
+
+ soundDir = resources_path
+ dirEnum = NSFileManager.defaultManager.enumeratorAtPath(soundDir)
+
+ while filename = dirEnum.nextObject do
+ if filename.pathExtension == "caf"
+ sound = SPSound.alloc.initWithContentsOfFile(filename)
+ @sounds[filename] = sound
+ end
+ end
+ end
+
+ def release_sound
+ @sounds = nil
+ SPAudioEngine.stop
+ end
+
+ def play_sound(soundName)
+ sound = @sounds[soundName]
+ if sound
+ sound.play
+ else
+ SPSound.soundWithContentsOfFile(soundName).play
+ end
+ end
+
+ def sound_channel(soundName)
+ sound = @sounds[soundName]
+ sound = SPSound.soundWithContentsOfFile(soundName) unless sound
+ sound.createChannel
+ end
+ end # Media
+end # Sparrow
View
17 lib/sparrow/supports/number_ext.rb
@@ -0,0 +1,17 @@
+module Sparrow
+ module GameNumbers
+ PI = 3.14159265359
+ PI_HALF = 1.57079632679
+ TWO_PI = 6.28318530718
+
+ def to_degree
+ ((self) / PI * 180.0)
+ end
+
+ def to_rad
+ ((self) / 180.0 * PI)
+ end
+ end
+end
+
+Numeric.send :include, Sparrow::GameNumbers
View
26 lib/sparrow/supports/transitions.rb
@@ -0,0 +1,26 @@
+module Sparrow
+ module Transitions
+ LINEAR = "linear"
+ RANDOMIZE = "randomize"
+
+ EASE_IN = "easeIn"
+ EASE_OUT = "easeOut"
+ EASE_IN_OUT = "easeInOut"
+ EASE_OUT_IN = "easeOutIn"
+
+ EASE_IN_BACK = "easeInBack"
+ EASE_OUT_BACK = "easeOutBack"
+ EASE_IN_OUT_BACK = "easeInOutBack"
+ EASE_OUT_IN_BACK = "easeOutInBack"
+
+ EASE_IN_ELASTIC = "easeInElastic"
+ EASE_OUT_ELASTIC = "easeOutElastic"
+ EASE_IN_OUT_ELASTIC = "easeInOutElastic"
+ EASE_OUT_IN_ELASTIC = "easeOutInElastic"
+
+ EASE_IN_BOUNCE = "easeInBounce"
+ EASE_OUT_BOUNCE = "easeOutBounce"
+ EASE_IN_OUT_BOUNCE = "easeInOutBounce"
+ EASE_OUT_IN_BOUNCE = "easeOutInBounce"
+ end
+end
View
16 lib/sparrow/views/overlay_view.rb
@@ -0,0 +1,16 @@
+# This class makes a UIView work just like a sprite in Sparrow:
+# it will react only on touches of child objects, and won't block touches on
+# empty areas any longer. That makes it perfect for an overlay view, on which
+# we can then add all kinds of UIKit elements: textfields, iAd banners, etc.
+module Sparrow
+ class OverlayView < UIView
+ def pointInside(point, withEvent: event)
+ self.subviews.each do |subview|
+ innerPoint = [point.x - subview.frame.origin.x,
+ point.y - subview.frame.origin.y]
+ return true if (subview.pointInside(innerPoint, withEvent: event))
+ end
+ return false
+ end
+ end
+end

0 comments on commit bce4b85

Please sign in to comment.