Class: Chingu::ClassicGameObject
- Inherits:
-
BasicGameObject
- Object
- BasicGameObject
- Chingu::ClassicGameObject
- Includes:
- Helpers::InputClient, Helpers::RotationCenter
- Defined in:
- lib/chingu/classic_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.
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 on heightby calculating it from image-width and factor.
-
#image ⇒ Object
Returns the value of attribute image.
-
#mode ⇒ Object
Returns the value of attribute mode.
-
#width ⇒ Object
Get effective on width by calculating it from image-width and factor.
-
#x ⇒ Object
Returns the value of attribute x.
-
#y ⇒ Object
Returns the value of attribute y.
-
#zorder ⇒ Object
Returns the value of attribute zorder.
Attributes inherited from BasicGameObject
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.
-
#distance_to(object) ⇒ Object
Calculates the distance from self to a given object.
-
#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.
-
#filename ⇒ Object
Returns a filename-friendly string from the current class-name.
-
#hide! ⇒ Object
(also: #hide)
Disable automatic calling of draw and draw_trait each game loop.
-
#initialize(options = {}) ⇒ ClassicGameObject
constructor
A new instance of ClassicGameObject.
-
#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.
-
#show! ⇒ Object
(also: #show)
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
-
#visible? ⇒ Boolean
Returns true if visible (not hidden).
Methods included from Helpers::RotationCenter
#rotation_center, #rotation_center=
Methods included from Helpers::InputClient
#add_inputs, #holding?, #holding_all?, #holding_any?, #input, #input=, #on_input
Methods inherited from BasicGameObject
all, create, #destroy, destroy_all, destroy_if, #draw_trait, each, each_with_index, initialize_trait, #pause!, #paused?, select, #setup, #setup_trait, size, trait, #trait_options, traits, #unpause!, #update, #update_trait
Methods included from Helpers::ClassInheritableAccessor
Constructor Details
#initialize(options = {}) ⇒ ClassicGameObject
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/classic_game_object.rb', line 40 def initialize( = {}) super # # All encapsulated Gosu::Image.draw_rot arguments can be set with hash-options at creation time # if [:image].is_a?(Gosu::Image) @image = [:image] elsif [:image].is_a? String begin # 1) Try loading the image the normal way @image = Gosu::Image.new($window, [:image]) rescue # 2) Try looking up the picture using Chingus Image-cache @image = Gosu::Image[[:image]] end end @x = [:x] || 0 @y = [:y] || 0 @angle = [:angle] || 0 self.factor = [:factor] || [:scale] || $window.factor || 1.0 @factor_x = [:factor_x].to_f if [:factor_x] @factor_y = [:factor_y].to_f if [:factor_y] self.center = [:center] || 0.5 @rotation_center = [:rotation_center] self.rotation_center([:rotation_center]) if [:rotation_center] @center_x = [:center_x] if [:center_x] @center_y = [:center_y] if [:center_y] if [:color].is_a?(Gosu::Color) @color = [:color] else @color = Gosu::Color.new([:color] || 0xFFFFFFFF) end self.alpha = [:alpha] if [:alpha] @mode = [:mode] || :default # :additive is also available. @zorder = [:zorder] || 100 if @image self.width = [:width] if [:width] self.height = [:height] if [: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
#angle ⇒ Object
Returns the value of attribute angle.
34 35 36 |
# File 'lib/chingu/classic_game_object.rb', line 34 def angle @angle end |
#center ⇒ Object
Returns the value of attribute center.
35 36 37 |
# File 'lib/chingu/classic_game_object.rb', line 35 def center @center end |
#center_x ⇒ Object
Returns the value of attribute center_x.
34 35 36 |
# File 'lib/chingu/classic_game_object.rb', line 34 def center_x @center_x end |
#center_y ⇒ Object
Returns the value of attribute center_y.
34 35 36 |
# File 'lib/chingu/classic_game_object.rb', line 34 def center_y @center_y end |
#color ⇒ Object
Returns the value of attribute color.
34 35 36 |
# File 'lib/chingu/classic_game_object.rb', line 34 def color @color end |
#factor ⇒ Object Also known as: scale
Returns the value of attribute factor.
35 36 37 |
# File 'lib/chingu/classic_game_object.rb', line 35 def factor @factor end |
#factor_x ⇒ Object
Returns the value of attribute factor_x.
34 35 36 |
# File 'lib/chingu/classic_game_object.rb', line 34 def factor_x @factor_x end |
#factor_y ⇒ Object
Returns the value of attribute factor_y.
34 35 36 |
# File 'lib/chingu/classic_game_object.rb', line 34 def factor_y @factor_y end |
#height ⇒ Object
Get effective on heightby calculating it from image-width and factor
144 145 146 |
# File 'lib/chingu/classic_game_object.rb', line 144 def height @height end |
#image ⇒ Object
Returns the value of attribute image.
34 35 36 |
# File 'lib/chingu/classic_game_object.rb', line 34 def image @image end |
#mode ⇒ Object
Returns the value of attribute mode.
34 35 36 |
# File 'lib/chingu/classic_game_object.rb', line 34 def mode @mode end |
#width ⇒ Object
Get effective on width by calculating it from image-width and factor
130 131 132 |
# File 'lib/chingu/classic_game_object.rb', line 130 def width @width end |
#x ⇒ Object
Returns the value of attribute x.
34 35 36 |
# File 'lib/chingu/classic_game_object.rb', line 34 def x @x end |
#y ⇒ Object
Returns the value of attribute y.
34 35 36 |
# File 'lib/chingu/classic_game_object.rb', line 34 def y @y end |
#zorder ⇒ Object
Returns the value of attribute zorder.
34 35 36 |
# File 'lib/chingu/classic_game_object.rb', line 34 def zorder @zorder end |
Instance Method Details
#alpha ⇒ Object
Get objects alpha-value (internally stored in @color.alpha)
174 175 176 |
# File 'lib/chingu/classic_game_object.rb', line 174 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.
180 181 182 183 184 |
# File 'lib/chingu/classic_game_object.rb', line 180 def alpha=(value) value = 0 if value < 0 value = 255 if value > 255 @color.alpha = value 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.
108 109 110 |
# File 'lib/chingu/classic_game_object.rb', line 108 def attributes [@x, @y, @angle, @center_x, @center_y, @factor_x, @factor_y, @color.dup, @mode, @zorder] end |
#attributes=(attributes) ⇒ Object
Set all attributes on 1 line Mainly used in combination with game_object1.attributes = game_object2.attributes
116 117 118 |
# File 'lib/chingu/classic_game_object.rb', line 116 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
233 234 235 |
# File 'lib/chingu/classic_game_object.rb', line 233 def distance_to(object) distance(self.x, self.y, object.x, object.y) end |
#draw ⇒ Object
Our encapsulation of GOSU's image.draw_rot, uses the objects variables to draw it on screen if @visible is true
249 250 251 |
# File 'lib/chingu/classic_game_object.rb', line 249 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.
263 264 265 |
# File 'lib/chingu/classic_game_object.rb', line 263 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.
256 257 258 |
# File 'lib/chingu/classic_game_object.rb', line 256 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 |
#filename ⇒ Object
Returns a filename-friendly string from the current class-name
"FireBall" -> "fire_ball"
242 243 244 |
# File 'lib/chingu/classic_game_object.rb', line 242 def filename Chingu::Inflector.underscore(Chingu::Inflector.demodulize(self.class.to_s)) end |
#hide! ⇒ Object Also known as: hide
Disable automatic calling of draw and draw_trait each game loop
201 202 203 |
# File 'lib/chingu/classic_game_object.rb', line 201 def hide! @visible = false end |
#inside_window?(x = @x, y = @y) ⇒ Boolean
Returns true if object is inside the game window, false if outside
223 224 225 |
# File 'lib/chingu/classic_game_object.rb', line 223 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
228 229 230 |
# File 'lib/chingu/classic_game_object.rb', line 228 def outside_window?(x = @x, y = @y) not inside_window?(x,y) end |
#show! ⇒ Object Also known as: show
Enable automatic calling of draw and draw_trait each game loop
209 210 211 |
# File 'lib/chingu/classic_game_object.rb', line 209 def show! @visible = true end |
#size ⇒ Object
Get objects width and height in an array
154 155 156 |
# File 'lib/chingu/classic_game_object.rb', line 154 def size [self.width, self.height] end |
#size=(size) ⇒ Object
Set width and height in one swoop
149 150 151 |
# File 'lib/chingu/classic_game_object.rb', line 149 def size=(size) self.width, self.height = *size end |
#to_s ⇒ Object
97 98 99 100 101 |
# File 'lib/chingu/classic_game_object.rb', line 97 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)
217 218 219 |
# File 'lib/chingu/classic_game_object.rb', line 217 def visible? @visible == true end |