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

Instance Method Summary collapse

Instance Attribute Details

#angleObject

Returns the value of attribute angle



37
38
39
# File 'lib/chingu/traits/simple_sprite.rb', line 37

def angle
  @angle
end

#centerObject (readonly)

Returns the value of attribute center



38
39
40
# File 'lib/chingu/traits/simple_sprite.rb', line 38

def center
  @center
end

#colorObject

Returns the value of attribute color



37
38
39
# File 'lib/chingu/traits/simple_sprite.rb', line 37

def color
  @color
end

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

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_yObject

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

#heightObject

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

#imageObject

Returns the value of attribute image



38
39
40
# File 'lib/chingu/traits/simple_sprite.rb', line 38

def image
  @image
end

#modeObject

Returns the value of attribute mode



37
38
39
# File 'lib/chingu/traits/simple_sprite.rb', line 37

def mode
  @mode
end

#visibleObject

kill this? force use of setter



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

def visible
  @visible
end

#widthObject

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

#xObject

Returns the value of attribute x



37
38
39
# File 'lib/chingu/traits/simple_sprite.rb', line 37

def x
  @x
end

#yObject

Returns the value of attribute y



37
38
39
# File 'lib/chingu/traits/simple_sprite.rb', line 37

def y
  @y
end

#zorderObject

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

#alphaObject

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



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

#drawObject

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

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


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(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.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]
  
  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



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

#sizeObject

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_sObject

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)

Returns:

  • (Boolean)


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

def visible?
  @visible == true
end