Class: Chingu::GameObject

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

Overview

GameObject is our BasisGameObject (class with framespecific stuff)

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

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

Methods included from Helpers::InputClient

#input, #input=

Methods inherited from BasicGameObject

all, create, #destroy, destroy_all, destroy_if, #draw_trait, has_trait, has_traits, #hide!, #pause!, #setup_trait, #show!, size, #unpause!, #update, #update_trait

Constructor Details

#initialize(options = {}) ⇒ GameObject

Returns a new instance of GameObject.



36
37
38
39
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
# File 'lib/chingu/game_object.rb', line 36

def initialize(options = {})
  super

  # All encapsulated 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
    @image = Image[options[:image]]
  end
  
  @x = options[:x] || 0
  @y = options[:y] || 0
  @angle = options[:angle] || 0
  
  self.center = options[:center] || 0.5
  self.factor = options[:factor] || 1.0
  @center_x = options[:center_x] if options[:center_x]
  @center_y = options[:center_y] if options[:center_y]
  @factor_x = options[:factor_x] if options[:factor_x]
  @factor_y = options[:factor_y] if options[:factor_y]

  if options[:color].is_a?(Gosu::Color)
    @color = options[:color]
  else
    @color = Gosu::Color.new(options[:color] || 0xFFFFFFFF)
  end      
  
  @mode = options[:mode] || :default # :additive is also available.
  @zorder = options[:zorder] || 100
                          
  setup_trait(options)  if respond_to?(:setup_trait)
end

Instance Attribute Details

#angleObject

Returns the value of attribute angle.



30
31
32
# File 'lib/chingu/game_object.rb', line 30

def angle
  @angle
end

#centerObject

Returns the value of attribute center.



31
32
33
# File 'lib/chingu/game_object.rb', line 31

def center
  @center
end

#center_xObject

Returns the value of attribute center_x.



30
31
32
# File 'lib/chingu/game_object.rb', line 30

def center_x
  @center_x
end

#center_yObject

Returns the value of attribute center_y.



30
31
32
# File 'lib/chingu/game_object.rb', line 30

def center_y
  @center_y
end

#colorObject

Returns the value of attribute color.



30
31
32
# File 'lib/chingu/game_object.rb', line 30

def color
  @color
end

#factorObject

Returns the value of attribute factor.



31
32
33
# File 'lib/chingu/game_object.rb', line 31

def factor
  @factor
end

#factor_xObject

Returns the value of attribute factor_x.



30
31
32
# File 'lib/chingu/game_object.rb', line 30

def factor_x
  @factor_x
end

#factor_yObject

Returns the value of attribute factor_y.



30
31
32
# File 'lib/chingu/game_object.rb', line 30

def factor_y
  @factor_y
end

#imageObject

Returns the value of attribute image.



30
31
32
# File 'lib/chingu/game_object.rb', line 30

def image
  @image
end

#modeObject

Returns the value of attribute mode.



30
31
32
# File 'lib/chingu/game_object.rb', line 30

def mode
  @mode
end

#xObject

Returns the value of attribute x.



30
31
32
# File 'lib/chingu/game_object.rb', line 30

def x
  @x
end

#yObject

Returns the value of attribute y.



30
31
32
# File 'lib/chingu/game_object.rb', line 30

def y
  @y
end

#zorderObject

Returns the value of attribute zorder.



30
31
32
# File 'lib/chingu/game_object.rb', line 30

def zorder
  @zorder
end

Instance Method Details

#distance_to(object) ⇒ Object

Calculates the distance from self to a given objevt



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

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

#drawObject



96
97
98
# File 'lib/chingu/game_object.rb', line 96

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

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

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

Returns:

  • (Boolean)


82
83
84
# File 'lib/chingu/game_object.rb', line 82

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)


87
88
89
# File 'lib/chingu/game_object.rb', line 87

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