Module: Chingu::Traits::Sprite

Includes:
Helpers::OptionsSetter
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

Constant Summary collapse

DEFAULTS =

default settings for all variables unless set in constructor

{
  :x => 0, :y => 0, :angle => 0,
  :factor_x => 1.0, :factor_y => 1.0,
  :zorder => 100, :center_x => 0.5, :center_y => 0.5,
  :mode => :default,
  :color => nil
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#angleObject

Returns the value of attribute angle.



40
41
42
# File 'lib/chingu/traits/sprite.rb', line 40

def angle
  @angle
end

#center_xObject

Returns the value of attribute center_x.



40
41
42
# File 'lib/chingu/traits/sprite.rb', line 40

def center_x
  @center_x
end

#center_yObject

Returns the value of attribute center_y.



40
41
42
# File 'lib/chingu/traits/sprite.rb', line 40

def center_y
  @center_y
end

#factor_xObject

Returns the value of attribute factor_x.



40
41
42
# File 'lib/chingu/traits/sprite.rb', line 40

def factor_x
  @factor_x
end

#factor_yObject

Returns the value of attribute factor_y.



40
41
42
# File 'lib/chingu/traits/sprite.rb', line 40

def factor_y
  @factor_y
end

#modeObject

Returns the value of attribute mode.



40
41
42
# File 'lib/chingu/traits/sprite.rb', line 40

def mode
  @mode
end

#visibleObject

Returns the value of attribute visible.



40
41
42
# File 'lib/chingu/traits/sprite.rb', line 40

def visible
  @visible
end

#xObject

Returns the value of attribute x.



40
41
42
# File 'lib/chingu/traits/sprite.rb', line 40

def x
  @x
end

#yObject

Returns the value of attribute y.



40
41
42
# File 'lib/chingu/traits/sprite.rb', line 40

def y
  @y
end

#zorderObject

Returns the value of attribute zorder.



40
41
42
# File 'lib/chingu/traits/sprite.rb', line 40

def zorder
  @zorder
end

Instance Method Details

#alphaObject

Get objects alpha-value (internally stored in @color.alpha)



156
157
158
# File 'lib/chingu/traits/sprite.rb', line 156

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.



162
163
164
165
166
# File 'lib/chingu/traits/sprite.rb', line 162

def alpha=(value)
  value = 0   if value < 0
  value = 255 if value > 255
  @color.alpha = value
end

#attributesObject

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.



92
93
94
# File 'lib/chingu/traits/sprite.rb', line 92

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



100
101
102
# File 'lib/chingu/traits/sprite.rb', line 100

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

#center=(center) ⇒ Object

Quick way of setting both center_x and center_y



151
152
153
# File 'lib/chingu/traits/sprite.rb', line 151

def center=(center)
  @center_x = @center_y = center
end

#color=(color) ⇒ Object



56
57
58
# File 'lib/chingu/traits/sprite.rb', line 56

def color=(color)
  @color = color.is_a?(Gosu::Color) ? color : Gosu::Color.new(color ||  0xFFFFFFFF)
end

#drawObject

Our encapsulation of GOSU’s image.draw_rot, uses the objects variables to draw it on screen if @visible is true



215
216
217
# File 'lib/chingu/traits/sprite.rb', line 215

def draw
  draw_relative
end

#draw_at(x, y) ⇒ Object

Works as #draw() but takes x/y arguments. Used among others by the edit-game state.



229
230
231
# File 'lib/chingu/traits/sprite.rb', line 229

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.



222
223
224
# File 'lib/chingu/traits/sprite.rb', line 222

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 @visible
end

#factor=(factor) ⇒ Object Also known as: scale=

Quick way of setting both factor_x and factor_y



144
145
146
# File 'lib/chingu/traits/sprite.rb', line 144

def factor=(factor)
  @factor_x = @factor_y = factor
end

#heightObject

Get effective on heightby calculating it from image-width and factor



128
129
130
# File 'lib/chingu/traits/sprite.rb', line 128

def height
  (@image.height.to_f * @factor_y).abs
end

#height=(height) ⇒ Object

Set an effective height for the object on screen. Chingu does this by setting factor_x depending on imge.width and width given. Usually better to have a large image and make it smaller then the other way around.



123
124
125
# File 'lib/chingu/traits/sprite.rb', line 123

def height=(height)
  @factor_y = height.to_f / @image.height.to_f
end

#hide!Object

Disable automatic calling of draw and draw_trait each game loop



183
184
185
# File 'lib/chingu/traits/sprite.rb', line 183

def hide!
  @visible = false
end

#image=(image) ⇒ Object

Accepts String, callable object or any-other non-nil capable of drawing itself on screen.

Examples:

image = 'rocket.png'
image = Gosu::Image.new($window, 'rocket.png')

 image = lambda do
   # TexPlay is library for Gosu image generation
   TexPlay.create_image($window,10,10).paint { circle(5,5,5, :color => :red) }
 end

Raises:

  • (ArgumentError)


73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/chingu/traits/sprite.rb', line 73

def image=(image)
  raise ArgumentError.new("No image set") if image.nil?
  
  @image = if String === image
             # 1) Try loading the image the normal way
             # 2) Try looking up the picture using Chingus Image-cache
             Gosu::Image.new($window, image,false) rescue Gosu::Image[image]
           elsif image.respond_to? :call
             image.call
           else
             image
           end
end

#inside_window?(x = @x, y = @y) ⇒ Boolean

Returns true if object is inside the game window, false if outside

Returns:

  • (Boolean)


203
204
205
# File 'lib/chingu/traits/sprite.rb', line 203

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

Returns:

  • (Boolean)


208
209
210
# File 'lib/chingu/traits/sprite.rb', line 208

def outside_window?(x = @x, y = @y)
  not inside_window?(x,y)
end

#setup_trait(object_options = {}) ⇒ Object



51
52
53
54
# File 'lib/chingu/traits/sprite.rb', line 51

def setup_trait(object_options = {})
  set_options(trait_options[:sprite].merge(object_options), DEFAULTS)
  super
end

#show!Object

Enable automatic calling of draw and draw_trait each game loop



190
191
192
# File 'lib/chingu/traits/sprite.rb', line 190

def show!
  @visible = true
end

#sizeObject

Get objects width and height in an array



138
139
140
# File 'lib/chingu/traits/sprite.rb', line 138

def size
  [self.width, self.height]
end

#size=(size) ⇒ Object

Set width and height in one swoop



133
134
135
# File 'lib/chingu/traits/sprite.rb', line 133

def size=(size)
  self.width, self.height = *size
end

#visible?Boolean

Returns true if visible (not hidden)

Returns:

  • (Boolean)


197
198
199
# File 'lib/chingu/traits/sprite.rb', line 197

def visible?
  @visible == true
end

#widthObject

Get effective on width by calculating it from image-width and factor



114
115
116
# File 'lib/chingu/traits/sprite.rb', line 114

def width
  (@image.width * @factor_x).abs
end

#width=(width) ⇒ Object

Set an effective width for the object on screen. Chingu does this by setting factor_x depending on imge.width and width given. Usually better to have a large image and make it smaller then the other way around.



109
110
111
# File 'lib/chingu/traits/sprite.rb', line 109

def width=(width)
  @factor_x = width.to_f / @image.width.to_f
end