Module: Chingu::Traits::Sprite
- Includes:
- Helpers::RotationCenter
- Defined in:
- lib/chingu/traits/sprite.rb
Overview
A Chingu trait providing ability to be drawn as an image.
Example:
class Rocket < BasicGameObject
trait :sprite, :image => 'rocket.png'
end
Rocket.create
Options:
:image - actual sprite to draw
- see #image= for details as this method is used to set this option
Introducing Variables:
:x, :y, :angle, :factor_x, :factor_y, :center_x, :center_y, :zorder, :mode, :visible
Defined Under Namespace
Modules: ClassMethods
Instance Attribute Summary collapse
-
#angle ⇒ Object
Returns the value of attribute angle.
-
#center ⇒ Object
Returns the value of attribute center.
-
#center_x ⇒ Object
Returns the value of attribute center_x.
-
#center_y ⇒ Object
Returns the value of attribute center_y.
-
#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, angle = 0, center_x = 0, center_y = 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).
Methods included from Helpers::RotationCenter
#rotation_center, #rotation_center=
Instance Attribute Details
#angle ⇒ Object
Returns the value of attribute angle
39 40 41 |
# File 'lib/chingu/traits/sprite.rb', line 39 def angle @angle end |
#center ⇒ Object
Returns the value of attribute center
40 41 42 |
# File 'lib/chingu/traits/sprite.rb', line 40 def center @center end |
#center_x ⇒ Object
Returns the value of attribute center_x
39 40 41 |
# File 'lib/chingu/traits/sprite.rb', line 39 def center_x @center_x end |
#center_y ⇒ Object
Returns the value of attribute center_y
39 40 41 |
# File 'lib/chingu/traits/sprite.rb', line 39 def center_y @center_y end |
#color ⇒ Object
Returns the value of attribute color
39 40 41 |
# File 'lib/chingu/traits/sprite.rb', line 39 def color @color end |
#factor ⇒ Object Also known as: scale
Returns the value of attribute factor
40 41 42 |
# File 'lib/chingu/traits/sprite.rb', line 40 def factor @factor end |
#factor_x ⇒ Object
Returns the value of attribute factor_x
39 40 41 |
# File 'lib/chingu/traits/sprite.rb', line 39 def factor_x @factor_x end |
#factor_y ⇒ Object
Returns the value of attribute factor_y
39 40 41 |
# File 'lib/chingu/traits/sprite.rb', line 39 def factor_y @factor_y end |
#height ⇒ Object
Get effective height by calculating it from image-width and factor
155 156 157 |
# File 'lib/chingu/traits/sprite.rb', line 155 def height @height end |
#image ⇒ Object
Returns the value of attribute image
40 41 42 |
# File 'lib/chingu/traits/sprite.rb', line 40 def image @image end |
#mode ⇒ Object
Returns the value of attribute mode
39 40 41 |
# File 'lib/chingu/traits/sprite.rb', line 39 def mode @mode end |
#visible ⇒ Object
kill this? force use of setter
41 42 43 |
# File 'lib/chingu/traits/sprite.rb', line 41 def visible @visible end |
#width ⇒ Object
Get effective width by calculating it from image-width and factor_x
139 140 141 |
# File 'lib/chingu/traits/sprite.rb', line 139 def width @width end |
#x ⇒ Object
Returns the value of attribute x
39 40 41 |
# File 'lib/chingu/traits/sprite.rb', line 39 def x @x end |
#y ⇒ Object
Returns the value of attribute y
39 40 41 |
# File 'lib/chingu/traits/sprite.rb', line 39 def y @y end |
#zorder ⇒ Object
Returns the value of attribute zorder
39 40 41 |
# File 'lib/chingu/traits/sprite.rb', line 39 def zorder @zorder end |
Instance Method Details
#alpha ⇒ Object
Get objects alpha-value (internally stored in @color.alpha)
182 183 184 |
# File 'lib/chingu/traits/sprite.rb', line 182 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.
188 189 190 191 192 |
# File 'lib/chingu/traits/sprite.rb', line 188 def alpha=(value) value = 0 if value < 0 value = 255 if value > 255 @color.alpha = value.to_i 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.
115 116 117 |
# File 'lib/chingu/traits/sprite.rb', line 115 def attributes [@x, @y, @angle, @center_x, @center_y, @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
123 124 125 |
# File 'lib/chingu/traits/sprite.rb', line 123 def attributes=(attributes) self.x, self.y, self.angle, self.center_x, self.center_y, 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
243 244 245 |
# File 'lib/chingu/traits/sprite.rb', line 243 def draw @image.draw_rot(@x, @y, @zorder, @angle, @center_x, @center_y, @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.
257 258 259 |
# File 'lib/chingu/traits/sprite.rb', line 257 def draw_at(x, y) draw_relative(x,y) end |
#draw_relative(x = 0, y = 0, zorder = 0, angle = 0, center_x = 0, center_y = 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.
250 251 252 |
# File 'lib/chingu/traits/sprite.rb', line 250 def draw_relative(x=0, y=0, zorder=0, angle=0, center_x=0, center_y=0, factor_x=0, factor_y=0) @image.draw_rot(@x+x, @y+y, @zorder+zorder, @angle+angle, @center_x+center_x, @center_y+center_y, @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
209 210 211 212 |
# File 'lib/chingu/traits/sprite.rb', line 209 def hide! @parent.game_objects.hide_game_object(self) if @parent && @visible @visible = false end |
#inside_window?(x = @x, y = @y) ⇒ Boolean
Returns true if object is inside the game window, false if outside
231 232 233 |
# File 'lib/chingu/traits/sprite.rb', line 231 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
236 237 238 |
# File 'lib/chingu/traits/sprite.rb', line 236 def outside_window?(x = @x, y = @y) not inside_window?(x,y) end |
#setup_trait(object_options = {}) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/chingu/traits/sprite.rb', line 43 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.angle = [:angle] || 0 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] self.rotation_center = [:rotation_center] || :center_center 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
217 218 219 220 |
# File 'lib/chingu/traits/sprite.rb', line 217 def show! @parent.game_objects.show_game_object(self) if @parent && !@visible @visible = true end |
#size ⇒ Object
Get objects width and height in an array
165 166 167 |
# File 'lib/chingu/traits/sprite.rb', line 165 def size [self.width, self.height] end |
#size=(size) ⇒ Object
Set width and height in one swoop
160 161 162 |
# File 'lib/chingu/traits/sprite.rb', line 160 def size=(size) self.width, self.height = *size end |
#to_s ⇒ Object
Let's have some useful information in to_s()
71 72 73 74 75 76 77 |
# File 'lib/chingu/traits/sprite.rb', line 71 def to_s "#{self.class.to_s} @ #{x.to_i} / #{y.to_i} " << "(#{width.to_i} x #{height.to_i}) - " << "ratio: #{sprintf("%.2f",width.to_f/height.to_f)} " << "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)
225 226 227 |
# File 'lib/chingu/traits/sprite.rb', line 225 def visible? @visible == true end |