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

Instance Method Summary collapse

Methods included from Helpers::RotationCenter

#rotation_center, #rotation_center=

Instance Attribute Details

#angleObject

Returns the value of attribute angle



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

def angle
  @angle
end

#centerObject

Returns the value of attribute center



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

def center
  @center
end

#center_xObject

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_yObject

Returns the value of attribute center_y



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

def center_y
  @center_y
end

#colorObject

Returns the value of attribute color



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

def color
  @color
end

#factorObject 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_xObject

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_yObject

Returns the value of attribute factor_y



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

def factor_y
  @factor_y
end

#heightObject

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

#imageObject

Returns the value of attribute image



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

def image
  @image
end

#modeObject

Returns the value of attribute mode



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

def mode
  @mode
end

#visibleObject

kill this? force use of setter



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

def visible
  @visible
end

#widthObject

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

#xObject

Returns the value of attribute x



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

def x
  @x
end

#yObject

Returns the value of attribute y



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

def y
  @y
end

#zorderObject

Returns the value of attribute zorder



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

def zorder
  @zorder
end

Instance Method Details

#alphaObject

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

#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.



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

#drawObject

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

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


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(object_options = {})        
  @visible = true   unless options[:visible] == false
  self.image =  options[:image]  if options[:image]
  self.color =  options[:color] || ::Gosu::Color::WHITE.dup
  self.alpha =  options[:alpha]  if options[:alpha]
  self.mode =   options[:mode] || :default
  self.x =      options[:x] || 0
  self.y =      options[:y] || 0
  self.zorder = options[:zorder] || 100
  self.angle =  options[:angle] || 0
  
  self.factor = options[:factor] || options[:scale] || $window.factor || 1.0
  self.factor_x = options[:factor_x].to_f if options[:factor_x]
  self.factor_y = options[:factor_y].to_f if options[:factor_y]
  self.rotation_center = options[:rotation_center] || :center_center
  
  if self.image
    self.width  = options[:width]   if options[:width]
    self.height = options[:height]  if options[:height]
    self.size   = options[:size]    if options[: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

#sizeObject

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_sObject

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)

Returns:

  • (Boolean)


225
226
227
# File 'lib/chingu/traits/sprite.rb', line 225

def visible?
  @visible == true
end