Class: CyberarmEngine::GameObject

Inherits:
Object
  • Object
show all
Includes:
Common
Defined in:
lib/cyberarm_engine/game_object.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Common

#current_state, #darken, #fill, #get_asset, #get_image, #get_sample, #get_song, #lighten, #opacity, #pop_state, #previous_state, #push_state, #show_cursor, #show_cursor=, #window

Constructor Details

#initialize(options = {}) ⇒ GameObject

Returns a new instance of GameObject.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/cyberarm_engine/game_object.rb', line 8

def initialize(options={})
  if options[:auto_manage] || options[:auto_manage] == nil
    $window.current_state.add_game_object(self)
  end

  @options = options
  @image = options[:image] ? image(options[:image]) : nil
  x = options[:x] ? options[:x] : 0
  y = options[:y] ? options[:y] : 0
  z = options[:z] ? options[:z] : 0
  @position = Vector.new(x, y, z)
  @velocity = Vector.new
  @last_position = Vector.new
  @angle = options[:angle] ? options[:angle] : 0

  @center_x = options[:center_x] ? options[:center_x] : 0.5
  @center_y = options[:center_y] ? options[:center_y] : 0.5

  @scale_x  = options[:scale_x] ? options[:scale_x] : 1
  @scale_y  = options[:scale_y] ? options[:scale_y] : 1

  @color    = options[:color] ? options[:color] : Gosu::Color.argb(0xff_ffffff)
  @alpha    = options[:alpha] ? options[:alpha] : 255
  @mode = options[:mode] ? options[:mode] : :default

  @paused = false
  @speed = 0
  @debug_color = Gosu::Color::GREEN
  @world_center_point = Vector.new(0,0)

  setup

  @debug_text = Text.new("", color: @debug_color, y: @position.y-(self.height*self.scale), z: 9999)
  @debug_text.x = @position.x
  if @radius == 0 || @radius == nil
    @radius = options[:radius] ? options[:radius] : defined?(@image.width) ? ((@image.width+@image.height)/4)*scale : 1
  end
end

Instance Attribute Details

#alphaObject

Returns the value of attribute alpha.



7
8
9
# File 'lib/cyberarm_engine/game_object.rb', line 7

def alpha
  @alpha
end

#angleObject

Returns the value of attribute angle.



5
6
7
# File 'lib/cyberarm_engine/game_object.rb', line 5

def angle
  @angle
end

#center_xObject

Returns the value of attribute center_x.



5
6
7
# File 'lib/cyberarm_engine/game_object.rb', line 5

def center_x
  @center_x
end

#center_yObject

Returns the value of attribute center_y.



5
6
7
# File 'lib/cyberarm_engine/game_object.rb', line 5

def center_y
  @center_y
end

#colorObject

Returns the value of attribute color.



5
6
7
# File 'lib/cyberarm_engine/game_object.rb', line 5

def color
  @color
end

#imageObject

Returns the value of attribute image.



5
6
7
# File 'lib/cyberarm_engine/game_object.rb', line 5

def image
  @image
end

#last_positionObject

Returns the value of attribute last_position.



5
6
7
# File 'lib/cyberarm_engine/game_object.rb', line 5

def last_position
  @last_position
end

#modeObject

Returns the value of attribute mode.



5
6
7
# File 'lib/cyberarm_engine/game_object.rb', line 5

def mode
  @mode
end

#optionsObject

Returns the value of attribute options.



5
6
7
# File 'lib/cyberarm_engine/game_object.rb', line 5

def options
  @options
end

#pausedObject

Returns the value of attribute paused.



5
6
7
# File 'lib/cyberarm_engine/game_object.rb', line 5

def paused
  @paused
end

#positionObject

Returns the value of attribute position.



5
6
7
# File 'lib/cyberarm_engine/game_object.rb', line 5

def position
  @position
end

#radiusObject

Returns the value of attribute radius.



5
6
7
# File 'lib/cyberarm_engine/game_object.rb', line 5

def radius
  @radius
end

#scale_xObject

Returns the value of attribute scale_x.



5
6
7
# File 'lib/cyberarm_engine/game_object.rb', line 5

def scale_x
  @scale_x
end

#scale_yObject

Returns the value of attribute scale_y.



5
6
7
# File 'lib/cyberarm_engine/game_object.rb', line 5

def scale_y
  @scale_y
end

#velocityObject

Returns the value of attribute velocity.



5
6
7
# File 'lib/cyberarm_engine/game_object.rb', line 5

def velocity
  @velocity
end

Class Method Details

.destroy_allObject



246
247
248
249
250
251
252
253
254
255
# File 'lib/cyberarm_engine/game_object.rb', line 246

def self.destroy_all
  INSTANCES.clear
  if $window.current_state
    $window.current_state.game_objects.each do |o|
      if o.is_a?(self.class)
        $window.current_state.game_objects.delete(o)
      end
    end
  end
end

.each_circle_collision(object, resolve_with = :width, &block) ⇒ Object



225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/cyberarm_engine/game_object.rb', line 225

def self.each_circle_collision(object, resolve_with = :width, &block)
  if object.class != Class && object.instance_of?(object.class)
    $window.current_state.game_objects.select {|i| i.class == self}.each do |o|
      distance = Gosu.distance(o.x, o.y, object.x, object.y)
      if distance <= o.radius+object.radius
        block.call(o, object) if block
      end
    end
  else
    lista = $window.current_state.game_objects.select {|i| i.class == self}
    listb = $window.current_state.game_objects.select {|i| i.class == object}
    lista.product(listb).each do |o, o2|
      next if o == o2
      distance = Gosu.distance(o.x, o.y, o2.x, o2.y)
      if distance <= o.radius+o2.radius
        block.call(o, o2) if block
      end
    end
  end
end

Instance Method Details

#_x_visibleObject



99
100
101
102
# File 'lib/cyberarm_engine/game_object.rb', line 99

def _x_visible
  self.x.between?(($window.width/2)-(@world_center_point.x), ($window.width/2)+@world_center_point.x) ||
    self.x.between?(((@world_center_point.x)-$window.width/2), ($window.width/2)+@world_center_point.x)
end

#_y_visibleObject



104
105
106
107
# File 'lib/cyberarm_engine/game_object.rb', line 104

def _y_visible
  self.y.between?(($window.height/2)-(@world_center_point.y), ($window.height/2)+@world_center_point.y) ||
    self.y.between?((@world_center_point.y)-($window.height/2), ($window.height/2)+@world_center_point.y)
end

#allObject

NOTE: This could be implemented more reliably



221
222
223
# File 'lib/cyberarm_engine/game_object.rb', line 221

def all
  INSTANCES.select {|i| i.class == self}
end

#button_down(id) ⇒ Object



158
159
# File 'lib/cyberarm_engine/game_object.rb', line 158

def button_down(id)
end

#button_up(id) ⇒ Object



155
156
# File 'lib/cyberarm_engine/game_object.rb', line 155

def button_up(id)
end

#circle_collision?(object) ⇒ Boolean

Returns:

  • (Boolean)


180
181
182
183
184
185
186
187
# File 'lib/cyberarm_engine/game_object.rb', line 180

def circle_collision?(object)
  distance = Gosu.distance(self.x, self.y, object.x, object.y)
  if distance <= self.radius+object.radius
    true
  else
    false
  end
end

#debug_text(text) ⇒ Object



65
66
67
68
69
# File 'lib/cyberarm_engine/game_object.rb', line 65

def debug_text(text)
  @debug_text.text = text
  @debug_text.x = @position.x-(@debug_text.width / 2)
  @debug_text.y = @position.y-(@debug_text.height + self.radius + self.height)
end

#destroyObject



210
211
212
213
214
215
216
217
218
# File 'lib/cyberarm_engine/game_object.rb', line 210

def destroy
  if $window.current_state
    $window.current_state.game_objects.each do |o|
      if o.is_a?(self.class) && o == self
        $window.current_state.game_objects.delete(o)
      end
    end
  end
end

#drawObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/cyberarm_engine/game_object.rb', line 47

def draw
  if @image
    @image.draw_rot(@position.x, @position.y, @position.z, @angle, @center_x, @center_y, @scale_x, @scale_y, @color, @mode)
  end

  if $debug
    show_debug_heading
    $window.draw_circle(@position.x, @position.y, radius, 9999, @debug_color)
    if @debug_text.text != ""
      $window.draw_rect(@debug_text.x-10, (@debug_text.y-10), @debug_text.width+20, @debug_text.height+20, Gosu::Color.rgba(0,0,0,200), 9999)
      @debug_text.draw
    end
  end
end

#draw_rect(x, y, width, height, color, z = 0) ⇒ Object



151
152
153
# File 'lib/cyberarm_engine/game_object.rb', line 151

def draw_rect(x, y, width, height, color, z = 0)
  $window.draw_rect(x,y,width,height,color,z)
end

#each_circle_collision(object, resolve_with = :width, &block) ⇒ Object

Duplication… so DRY.



190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/cyberarm_engine/game_object.rb', line 190

def each_circle_collision(object, resolve_with = :width, &block)
  if object.class != Class && object.instance_of?(object.class)
    $window.current_state.game_objects.select {|i| i.class == object.class}.each do |o|
      distance = Gosu.distance(self.x, self.y, object.x, object.y)
      if distance <= self.radius+object.radius
        block.call(o, object) if block
      end
    end
  else
    list = $window.current_state.game_objects.select {|i| i.class == object}
    list.each do |o|
      next if self == o
      distance = Gosu.distance(self.x, self.y, o.x, o.y)
      if distance <= self.radius+o.radius
        block.call(self, o) if block
      end
    end
  end
end

#find_closest(game_object_class) ⇒ Object



161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/cyberarm_engine/game_object.rb', line 161

def find_closest(game_object_class)
  best_object = nil
  best_distance = 100_000_000_000 # Huge default number


  game_object_class.all.each do |object|
    distance = Gosu::distance(self.x, self.y, object.x, object.y)
    if distance <= best_distance
      best_object = object
      best_distance = distance
    end
  end

  return best_object
end

#heading(ahead_by = 100, object = nil, angle_only = false) ⇒ Object



109
110
111
112
113
114
115
116
117
# File 'lib/cyberarm_engine/game_object.rb', line 109

def heading(ahead_by = 100, object = nil, angle_only = false)
  direction = Gosu.angle(@last_position.x, @last_position.x, @position.x, position.y).gosu_to_radians

  _x = @position.x + (ahead_by * Math.cos(direction))
  _y = @position.y + (ahead_by * Math.sin(direction))

  return direction if angle_only
  return Vector.new(_x, _y) unless angle_only
end

#heightObject



128
129
130
# File 'lib/cyberarm_engine/game_object.rb', line 128

def height
  @image ? @image.height * self.scale : 0
end

#look_at(object) ⇒ Object



176
177
178
# File 'lib/cyberarm_engine/game_object.rb', line 176

def look_at(object)
  # TODO: Implement

end

#pauseObject



132
133
134
# File 'lib/cyberarm_engine/game_object.rb', line 132

def pause
  @paused = true
end

#rotate(int) ⇒ Object



140
141
142
143
# File 'lib/cyberarm_engine/game_object.rb', line 140

def rotate(int)
  self.angle+=int
  self.angle%=360
end

#scaleObject



71
72
73
74
75
76
77
78
# File 'lib/cyberarm_engine/game_object.rb', line 71

def scale
  if @scale_x == @scale_y
    return @scale_x
  else
    false
    # maths?

  end
end

#scale=(int) ⇒ Object



80
81
82
83
84
# File 'lib/cyberarm_engine/game_object.rb', line 80

def scale=(int)
  self.scale_x = int
  self.scale_y = int
  self.radius = ((@image.width+@image.height)/4)*self.scale
end

#show_debug_headingObject



119
120
121
122
# File 'lib/cyberarm_engine/game_object.rb', line 119

def show_debug_heading
  _heading = heading
  Gosu.draw_line(@position.x, @position.y, @debug_color, _heading.x, _heading.y, @debug_color, 9999)
end

#unpauseObject



136
137
138
# File 'lib/cyberarm_engine/game_object.rb', line 136

def unpause
  @paused = false
end

#updateObject



62
63
# File 'lib/cyberarm_engine/game_object.rb', line 62

def update
end

#visibleObject



86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/cyberarm_engine/game_object.rb', line 86

def visible
  true
  # if _x_visible

  #   if _y_visible

  #     true

  #   else

  #     false

  #   end

  # else

  #   false

  # end

end

#widthObject



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

def width
  @image ? @image.width * self.scale : 0
end