Module: Chingu::Traits::SimpleSprite
- Defined in:
- lib/chingu/traits/simple_sprite.rb
Overview
A Chingu trait providing ability to be drawn as an image.
Example:
class Rocket < BasicGameObject
trait :simple_sprite
end
Rocket.create(:x => 100, :y => 200)
Options: :image - actual sprite to draw - see #image= for details as this method is used to set this option
Introducing Variables: :x, :y, :zorder, :factor_x, :factor_y, :mode, :color, :visible
Defined Under Namespace
Modules: ClassMethods
Instance Attribute Summary collapse
-
#angle ⇒ Object
Returns the value of attribute angle.
-
#center ⇒ Object
readonly
Returns the value of attribute center.
-
#color ⇒ Object
Returns the value of attribute color.
-
#factor ⇒ Object
(also: #scale)
Returns the value of attribute factor.
-
#factor_x ⇒ Object
Returns the value of attribute factor_x.
-
#factor_y ⇒ Object
Returns the value of attribute factor_y.
-
#height ⇒ Object
Get effective height by calculating it from image-width and factor.
-
#image ⇒ Object
Returns the value of attribute image.
-
#mode ⇒ Object
Returns the value of attribute mode.
-
#visible ⇒ Object
kill this? force use of setter.
-
#width ⇒ Object
Get effective width by calculating it from image-width and factor_x.
-
#x ⇒ Object
Returns the value of attribute x.
-
#y ⇒ Object
Returns the value of attribute y.
-
#zorder ⇒ Object
Returns the value of attribute zorder.
Instance Method Summary collapse
-
#alpha ⇒ Object
Get objects alpha-value (internally stored in @color.alpha).
-
#alpha=(value) ⇒ Object
Set objects alpha-value (internally stored in @color.alpha) If out of range, set to closest working value.
-
#attributes ⇒ Object
Get all settings from a game object in one array.
-
#attributes=(attributes) ⇒ Object
Set all attributes on 1 line Mainly used in combination with game_object1.attributes = game_object2.attributes.
-
#draw ⇒ Object
Our encapsulation of GOSU's image.draw_rot, uses the objects variables to draw it on screen if @visible is true.
-
#draw_at(x, y) ⇒ Object
Works as #draw() but takes x/y arguments.
-
#draw_relative(x = 0, y = 0, zorder = 0, factor_x = 0, factor_y = 0) ⇒ Object
Works as #draw() but takes offsets for all draw_rot()-arguments.
-
#hide! ⇒ Object
Disable automatic calling of draw and draw_trait each game loop.
-
#inside_window?(x = @x, y = @y) ⇒ Boolean
Returns true if object is inside the game window, false if outside.
-
#outside_window?(x = @x, y = @y) ⇒ Boolean
Returns true object is outside the game window.
- #setup_trait(object_options = {}) ⇒ Object
-
#show! ⇒ Object
Enable automatic calling of draw and draw_trait each game loop.
-
#size ⇒ Object
Get objects width and height in an array.
-
#size=(size) ⇒ Object
Set width and height in one swoop.
-
#to_s ⇒ Object
Let's have some useful information in to_s().
-
#visible? ⇒ Boolean
Returns true if visible (not hidden).
Instance Attribute Details
#angle ⇒ Object
Returns the value of attribute angle.
37 38 39 |
# File 'lib/chingu/traits/simple_sprite.rb', line 37 def angle @angle end |
#center ⇒ Object (readonly)
Returns the value of attribute center.
38 39 40 |
# File 'lib/chingu/traits/simple_sprite.rb', line 38 def center @center end |
#color ⇒ Object
Returns the value of attribute color.
37 38 39 |
# File 'lib/chingu/traits/simple_sprite.rb', line 37 def color @color end |
#factor ⇒ Object Also known as: scale
Returns the value of attribute factor.
38 39 40 |
# File 'lib/chingu/traits/simple_sprite.rb', line 38 def factor @factor end |
#factor_x ⇒ Object
Returns the value of attribute factor_x.
37 38 39 |
# File 'lib/chingu/traits/simple_sprite.rb', line 37 def factor_x @factor_x end |
#factor_y ⇒ Object
Returns the value of attribute factor_y.
37 38 39 |
# File 'lib/chingu/traits/simple_sprite.rb', line 37 def factor_y @factor_y end |
#height ⇒ Object
Get effective height by calculating it from image-width and factor
149 150 151 |
# File 'lib/chingu/traits/simple_sprite.rb', line 149 def height @height end |
#image ⇒ Object
Returns the value of attribute image.
38 39 40 |
# File 'lib/chingu/traits/simple_sprite.rb', line 38 def image @image end |
#mode ⇒ Object
Returns the value of attribute mode.
37 38 39 |
# File 'lib/chingu/traits/simple_sprite.rb', line 37 def mode @mode end |
#visible ⇒ Object
kill this? force use of setter
39 40 41 |
# File 'lib/chingu/traits/simple_sprite.rb', line 39 def visible @visible end |
#width ⇒ Object
Get effective width by calculating it from image-width and factor_x
133 134 135 |
# File 'lib/chingu/traits/simple_sprite.rb', line 133 def width @width end |
#x ⇒ Object
Returns the value of attribute x.
37 38 39 |
# File 'lib/chingu/traits/simple_sprite.rb', line 37 def x @x end |
#y ⇒ Object
Returns the value of attribute y.
37 38 39 |
# File 'lib/chingu/traits/simple_sprite.rb', line 37 def y @y end |
#zorder ⇒ Object
Returns the value of attribute zorder.
37 38 39 |
# File 'lib/chingu/traits/simple_sprite.rb', line 37 def zorder @zorder end |
Instance Method Details
#alpha ⇒ Object
Get objects alpha-value (internally stored in @color.alpha)
172 173 174 |
# File 'lib/chingu/traits/simple_sprite.rb', line 172 def alpha @color.alpha end |
#alpha=(value) ⇒ Object
Set objects alpha-value (internally stored in @color.alpha) If out of range, set to closest working value. this makes fading simpler.
178 179 180 181 182 |
# File 'lib/chingu/traits/simple_sprite.rb', line 178 def alpha=(value) value = 0 if value < 0 value = 255 if value > 255 @color.alpha = value end |
#attributes ⇒ Object
Get all settings from a game object in one array. Complemented by the GameObject#attributes= setter. Makes it easy to clone a objects x,y,angle etc.
109 110 111 |
# File 'lib/chingu/traits/simple_sprite.rb', line 109 def attributes [@x, @y, @angle, @factor_x, @factor_y, @color, @mode, @zorder] end |
#attributes=(attributes) ⇒ Object
Set all attributes on 1 line Mainly used in combination with game_object1.attributes = game_object2.attributes
117 118 119 |
# File 'lib/chingu/traits/simple_sprite.rb', line 117 def attributes=(attributes) self.x, self.y, self.angle, self.factor_x, self.factor_y, self.color, self.mode, self.zorder = *attributes end |
#draw ⇒ Object
Our encapsulation of GOSU's image.draw_rot, uses the objects variables to draw it on screen if @visible is true
233 234 235 |
# File 'lib/chingu/traits/simple_sprite.rb', line 233 def draw @image.draw(@x, @y, @zorder, @factor_x, @factor_y, @color, @mode) if @image end |
#draw_at(x, y) ⇒ Object
Works as #draw() but takes x/y arguments. Used among others by the edit-game state.
247 248 249 |
# File 'lib/chingu/traits/simple_sprite.rb', line 247 def draw_at(x, y) draw_relative(x,y) end |
#draw_relative(x = 0, y = 0, zorder = 0, factor_x = 0, factor_y = 0) ⇒ Object
Works as #draw() but takes offsets for all draw_rot()-arguments. Used among others by the viewport-trait.
240 241 242 |
# File 'lib/chingu/traits/simple_sprite.rb', line 240 def draw_relative(x=0, y=0, zorder=0, factor_x=0, factor_y=0) @image.draw(@x+x, @y+y, @zorder+zorder, @factor_x+factor_x, @factor_y+factor_y, @color, @mode) if @image end |
#hide! ⇒ Object
Disable automatic calling of draw and draw_trait each game loop
199 200 201 202 |
# File 'lib/chingu/traits/simple_sprite.rb', line 199 def hide! @parent.game_objects.hide_game_object(self) if @parent && @visible == true @visible = false end |
#inside_window?(x = @x, y = @y) ⇒ Boolean
Returns true if object is inside the game window, false if outside
221 222 223 |
# File 'lib/chingu/traits/simple_sprite.rb', line 221 def inside_window?(x = @x, y = @y) x >= 0 && x <= $window.width && y >= 0 && y <= $window.height end |
#outside_window?(x = @x, y = @y) ⇒ Boolean
Returns true object is outside the game window
226 227 228 |
# File 'lib/chingu/traits/simple_sprite.rb', line 226 def outside_window?(x = @x, y = @y) not inside_window?(x,y) end |
#setup_trait(object_options = {}) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/chingu/traits/simple_sprite.rb', line 41 def setup_trait( = {}) @visible = true unless [:visible] == false self.image = [:image] if [:image] self.color = [:color] || ::Gosu::Color::WHITE.dup self.alpha = [:alpha] if [:alpha] self.mode = [:mode] || :default self.x = [:x] || 0 self.y = [:y] || 0 self.zorder = [:zorder] || 100 self.factor = [:factor] || [:scale] || $window.factor || 1.0 self.factor_x = [:factor_x].to_f if [:factor_x] self.factor_y = [:factor_y].to_f if [:factor_y] if self.image self.width = [:width] if [:width] self.height = [:height] if [:height] self.size = [:size] if [:size] end super end |
#show! ⇒ Object
Enable automatic calling of draw and draw_trait each game loop
207 208 209 210 |
# File 'lib/chingu/traits/simple_sprite.rb', line 207 def show! @parent.game_objects.show_game_object(self) if @parent && @visible == false @visible = true end |
#size ⇒ Object
Get objects width and height in an array
159 160 161 |
# File 'lib/chingu/traits/simple_sprite.rb', line 159 def size [self.width, self.height] end |
#size=(size) ⇒ Object
Set width and height in one swoop
154 155 156 |
# File 'lib/chingu/traits/simple_sprite.rb', line 154 def size=(size) self.width, self.height = *size end |
#to_s ⇒ Object
Let's have some useful information in to_s()
67 68 69 70 71 |
# File 'lib/chingu/traits/simple_sprite.rb', line 67 def to_s "#{self.class.to_s} @ #{x.to_i} / #{y.to_i} " << "(#{width.to_i} x #{height.to_i}) - " << " ratio: #{sprintf("%.2f",width/height)} scale: #{sprintf("%.2f", factor_x)}/#{sprintf("%.2f", factor_y)} angle: #{angle.to_i} zorder: #{zorder} alpha: #{alpha}" end |
#visible? ⇒ Boolean
Returns true if visible (not hidden)
215 216 217 |
# File 'lib/chingu/traits/simple_sprite.rb', line 215 def visible? @visible == true end |