Class: Chingu::GameObject

Inherits:
BasicGameObject show all
Includes:
Helpers::InputClient, Helpers::RotationCenter
Defined in:
lib/chingu/game_object.rb

Overview

GameObject inherits from BasicGameObject to get traits and some class-methods like .all and .destroy

On top of that, it encapsulates GOSUs Image#draw_rot and all its parameters.

In Chingu GameObject is a visual object, something to put on screen, centers around the .image-parameter.

If you wan’t a invisible object but with traits, use BasicGameObject.

Direct Known Subclasses

Parallax, ParallaxLayer, Particle, Text

Instance Attribute Summary collapse

Attributes inherited from BasicGameObject

#options, #parent, #paused, #visible

Instance Method Summary collapse

Methods included from Helpers::RotationCenter

#rotation_center, #rotation_center=

Methods included from Helpers::InputClient

#holding?, #input, #input=

Methods inherited from BasicGameObject

all, create, #destroy, destroy_all, destroy_if, #draw_trait, initialize_trait, #pause!, #paused?, #setup, #setup_trait, size, trait, #trait_options, traits, #unpause!, #update, #update_trait

Methods included from Helpers::ClassInheritableAccessor

included

Constructor Details

#initialize(options = {}) ⇒ GameObject

Returns a new instance of GameObject.



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/chingu/game_object.rb', line 40

def initialize(options = {})
  super
  
  #
  # All encapsulated Gosu::Image.draw_rot arguments can be set with hash-options at creation time
  #
  if options[:image].is_a?(Gosu::Image)
    @image = options[:image]
  elsif options[:image].is_a? String
    begin
      # 1) Try loading the image the normal way
      @image = Gosu::Image.new($window, options[:image])
    rescue
      # 2) Try looking up the picture using Chingus Image-cache
      @image = Gosu::Image[options[:image]]
    end
  end
  
  @x = options[:x] || 0
  @y = options[:y] || 0
  @angle = options[:angle] || 0
  
  self.factor = options[:factor] || options[:scale] || $window.factor || 1.0
  @factor_x = options[:factor_x].to_f if options[:factor_x]
  @factor_y = options[:factor_y].to_f if options[:factor_y]
  
  self.center = options[:center] || 0.5
  
  @rotation_center = options[:rotation_center]
  self.rotation_center(options[:rotation_center]) if options[:rotation_center]
  
  @center_x = options[:center_x] if options[:center_x]
  @center_y = options[:center_y] if options[:center_y]
  
  if options[:color].is_a?(Gosu::Color)
    @color = options[:color]
  else
    @color = Gosu::Color.new(options[:color] || 0xFFFFFFFF)
  end
  
  self.alpha = options[:alpha]  if options[:alpha]
  
  @mode = options[:mode] || :default # :additive is also available.
  @zorder = options[:zorder] || 100
  
  if @image
    self.width = options[:width]   if options[:width]
    self.height = options[:height] if options[:height]
  end

  ### super ## This crashes
  # Call setup, this class holds an empty setup() to be overriden
  # setup() will be an easier method to override for init-stuff since you don't need to do super etc..
  setup
  
end

Instance Attribute Details

#angleObject

Returns the value of attribute angle.



34
35
36
# File 'lib/chingu/game_object.rb', line 34

def angle
  @angle
end

#centerObject

Returns the value of attribute center.



35
36
37
# File 'lib/chingu/game_object.rb', line 35

def center
  @center
end

#center_xObject

Returns the value of attribute center_x.



34
35
36
# File 'lib/chingu/game_object.rb', line 34

def center_x
  @center_x
end

#center_yObject

Returns the value of attribute center_y.



34
35
36
# File 'lib/chingu/game_object.rb', line 34

def center_y
  @center_y
end

#colorObject

Returns the value of attribute color.



34
35
36
# File 'lib/chingu/game_object.rb', line 34

def color
  @color
end

#factorObject Also known as: scale

Returns the value of attribute factor.



35
36
37
# File 'lib/chingu/game_object.rb', line 35

def factor
  @factor
end

#factor_xObject

Returns the value of attribute factor_x.



34
35
36
# File 'lib/chingu/game_object.rb', line 34

def factor_x
  @factor_x
end

#factor_yObject

Returns the value of attribute factor_y.



34
35
36
# File 'lib/chingu/game_object.rb', line 34

def factor_y
  @factor_y
end

#heightObject

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



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

def height
  @height
end

#imageObject

Returns the value of attribute image.



34
35
36
# File 'lib/chingu/game_object.rb', line 34

def image
  @image
end

#modeObject

Returns the value of attribute mode.



34
35
36
# File 'lib/chingu/game_object.rb', line 34

def mode
  @mode
end

#widthObject

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



124
125
126
# File 'lib/chingu/game_object.rb', line 124

def width
  @width
end

#xObject

Returns the value of attribute x.



34
35
36
# File 'lib/chingu/game_object.rb', line 34

def x
  @x
end

#yObject

Returns the value of attribute y.



34
35
36
# File 'lib/chingu/game_object.rb', line 34

def y
  @y
end

#zorderObject

Returns the value of attribute zorder.



34
35
36
# File 'lib/chingu/game_object.rb', line 34

def zorder
  @zorder
end

Instance Method Details

#alphaObject

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



168
169
170
# File 'lib/chingu/game_object.rb', line 168

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.



174
175
176
177
178
# File 'lib/chingu/game_object.rb', line 174

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.



102
103
104
# File 'lib/chingu/game_object.rb', line 102

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



110
111
112
# File 'lib/chingu/game_object.rb', line 110

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

#distance_to(object) ⇒ Object

Calculates the distance from self to a given object



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

def distance_to(object)
  distance(self.x, self.y, object.x, object.y)
end

#drawObject

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



241
242
243
# File 'lib/chingu/game_object.rb', line 241

def draw
  @image.draw_rot(@x, @y, @zorder, @angle, @center_x, @center_y, @factor_x, @factor_y, @color, @mode) if @visible
end

#draw_at(x, y) ⇒ Object

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



255
256
257
# File 'lib/chingu/game_object.rb', line 255

def draw_at(x, y)
  @image.draw_rot(x, y, @zorder, @angle, @center_x, @center_y, @factor_x, @factor_y, @color, @mode) if @visible
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.



248
249
250
# File 'lib/chingu/game_object.rb', line 248

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

#filenameObject

Returns a filename-friendly string from the current class-name

“FireBall” -> “fire_ball”



234
235
236
# File 'lib/chingu/game_object.rb', line 234

def filename
  Chingu::Inflector.underscore(self.class.to_s)
end

#hide!Object

Disable automatic calling of draw and draw_trait each game loop



195
196
197
# File 'lib/chingu/game_object.rb', line 195

def hide!
  @visible = false
end

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

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

Returns:

  • (Boolean)


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

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)


220
221
222
# File 'lib/chingu/game_object.rb', line 220

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

#show!Object

Enable automatic calling of draw and draw_trait each game loop



202
203
204
# File 'lib/chingu/game_object.rb', line 202

def show!
  @visible = true
end

#sizeObject

Get objects width and height in an array



148
149
150
# File 'lib/chingu/game_object.rb', line 148

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

#size=(size) ⇒ Object

Set width and height in one swoop



143
144
145
# File 'lib/chingu/game_object.rb', line 143

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

#visible?Boolean

Returns true if visible (not hidden)

Returns:

  • (Boolean)


209
210
211
# File 'lib/chingu/game_object.rb', line 209

def visible?
  @visible == true
end