Class: MiniGL::GameObject

Inherits:
Sprite
  • Object
show all
Includes:
Movement
Defined in:
lib/minigl/game_object.rb

Overview

This class represents an object with a set of properties and methods commonly used in games. It defines an object with a rectangular bounding box, and having all the attributes required for using the Movement module.

Instance Attribute Summary

Attributes included from Movement

#bottom, #h, #left, #mass, #max_speed, #passable, #prev_speed, #right, #speed, #stored_forces, #top, #w, #x, #y

Attributes inherited from Sprite

#img, #img_index, #x, #y

Instance Method Summary collapse

Methods included from Movement

#bounds, #cycle, #move, #move_carrying, #move_free

Methods inherited from Sprite

#animate, #animate_once, #set_animation

Constructor Details

#initialize(x, y, w, h, img, img_gap = nil, sprite_cols = nil, sprite_rows = nil, mass = 1.0, retro = nil) ⇒ GameObject

Creates a new game object.

Parameters:

x

The x-coordinate of the object’s bounding box. This can be modified later via the x attribute.

y

The y-coordinate of the object’s bounding box. This can be modified later via the y attribute.

w

The width of the object’s bounding box.

h

The height of the object’s bounding box.

img

The image or spritesheet for the object.

img_gap

A Vector object representing the difference between the top left corner of the bounding box and the coordinates of the image. For example, an object with x = 100, y = 50 and img_gap = Vector.new(-5, -5) will be drawn at position (95, 45) of the screen.

sprite_cols

The number of columns in the spritesheet. Use nil if the image is not a spritesheet.

sprite_rows

The number of rows in the spritesheet. Use nil if the image is not a spritesheet.

mass

The mass of the object. Details on how it is used can be found in the Movement module.



219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/minigl/game_object.rb', line 219

def initialize(x, y, w, h, img, img_gap = nil, sprite_cols = nil, sprite_rows = nil, mass = 1.0, retro = nil)
  super x, y, img, sprite_cols, sprite_rows, retro
  @w = w; @h = h
  @img_gap =
    if img_gap.nil?
      Vector.new 0, 0
    else
      img_gap
    end
  @mass = mass
  @speed = Vector.new 0, 0
  @max_speed = Vector.new 15, 15
  @stored_forces = Vector.new 0, 0
end

Instance Method Details

#draw(map = nil, scale_x = 1, scale_y = 1, alpha = 0xff, color = 0xffffff, angle = nil, flip = nil, z_index = 0, round = false) ⇒ Object

Draws the game object in the screen.

Parameters:

map

A Map object, relative to which the object will be drawn (the x and y coordinates of the image will be changed according to the position of the camera).

scale_x

A scale factor to be applied horizontally to the image.

scale_y

A scale factor to be applied vertically to the image.

alpha

The opacity with which the image will be drawn. Valid values vary from 0 (fully transparent) to 255 (fully opaque).

color

A color filter to apply to the image. A white (0xffffff) filter will keep all colors unchanged, while a black (0x000000) filter will turn all colors to black. A red (0xff0000) filter will keep reddish colors with slight or no change, whereas bluish colors will be darkened, for example.

angle

A rotation, in degrees, to be applied to the image, relative to its center.

flip

Specify :horiz to draw the image horizontally flipped or :vert to draw it vertically flipped.

z_index

The z-order to draw the object. Objects with larger z-orders will be drawn on top of the ones with smaller z-orders.

round

Specify whether the drawing coordinates should be rounded to an integer before drawing, to avoid little distortions of the image. Only applies when the image is not rotated.

Obs.: This method accepts named parameters.



260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
# File 'lib/minigl/game_object.rb', line 260

def draw(map = nil, scale_x = 1, scale_y = 1, alpha = 0xff, color = 0xffffff, angle = nil, flip = nil, z_index = 0, round = false)
  if map.is_a? Hash
    scale_x = map.fetch(:scale_x, 1)
    scale_y = map.fetch(:scale_y, 1)
    alpha = map.fetch(:alpha, 0xff)
    color = map.fetch(:color, 0xffffff)
    angle = map.fetch(:angle, nil)
    flip = map.fetch(:flip, nil)
    z_index = map.fetch(:z_index, 0)
    round = map.fetch(:round, false)
    map = map.fetch(:map, nil)
  end

  color = (alpha << 24) | color
  img_gap_scale_x = scale_image_gap? ? scale_x : 1
  img_gap_scale_y = scale_image_gap? ? scale_y : 1
  if angle
    center_x = @x + @w * 0.5
    center_y = @y + @h * 0.5
    o_x = center_x - @x - @img_gap.x * img_gap_scale_x
    o_y = center_y - @y - @img_gap.y * img_gap_scale_y
    @img[@img_index].draw_rot @x + (flip == :horiz ? -1 : 1) * (@img_gap.x * img_gap_scale_x + o_x) - (map ? map.cam.x : 0),
                              @y + (flip == :vert ? -1 : 1) * (@img_gap.y * img_gap_scale_y + o_y) - (map ? map.cam.y : 0),
                              z_index, angle, o_x.to_f / (@img[0].width * scale_x), o_y.to_f / (@img[0].height * scale_y),
                              (flip == :horiz ? -scale_x : scale_x), (flip == :vert ? -scale_y : scale_y), color
  else
    x = @x + (flip == :horiz ? -1 : 1) * @img_gap.x * img_gap_scale_x - (map ? map.cam.x : 0) + (flip == :horiz ? @w : 0)
    y = @y + (flip == :vert ? -1 : 1) * @img_gap.y * img_gap_scale_y - (map ? map.cam.y : 0) + (flip == :vert ? @h : 0)
    @img[@img_index].draw (round ? x.round : x), (round ? y.round : y),
                          z_index, (flip == :horiz ? -scale_x : scale_x),
                          (flip == :vert ? -scale_y : scale_y), color
  end
end

#scale_image_gap?Boolean

override this if you don’t want the image_gap to be scaled when drawing the object scaled.

Returns:

  • (Boolean)


305
306
307
# File 'lib/minigl/game_object.rb', line 305

def scale_image_gap?
  true
end

#visible?(map = nil) ⇒ Boolean

Returns whether this object is visible in the given map (i.e., in the viewport determined by the camera of the given map). If no map is given, returns whether the object is visible on the screen.

Returns:

  • (Boolean)


297
298
299
300
301
# File 'lib/minigl/game_object.rb', line 297

def visible?(map = nil)
  r = Rectangle.new @x.round + @img_gap.x, @y.round + @img_gap.y, @img[0].width, @img[0].height
  return Rectangle.new(0, 0, G.window.width, G.window.height).intersect? r if map.nil?
  map.cam.intersect? r
end