Class: Metro::UI::Sprite

Inherits:
Model
  • Object
show all
Defined in:
lib/metro/models/ui/sprite.rb

Overview

A sprite is a Metro model that is specially designed to draw and manage an image. A sprite maintains an image, location information, and rotation.

Direct Known Subclasses

Hero, PhysicsSprite

Constant Summary

Constants included from Metro::Units

Metro::Units::Bounds

Instance Attribute Summary collapse

Attributes inherited from Model

#scene, #window

Instance Method Summary collapse

Methods inherited from Model

#_load, #_save, #after_initialize, #create, #draw_completed?, hierarchy, inherited, #initialize, metro_name, #model, model_name, models, #name, #notification, #saveable_to_view, #show, #to_hash, #update, #update_completed?

Methods included from HasEvents

included

Methods included from KeyValueCoding

#get, #set

Methods included from PropertyOwner

included, #properties

Constructor Details

This class inherits a constructor from Metro::Model

Instance Attribute Details

#angleObject

The angle at which the sprite should be drawn. This is by default 0.



38
# File 'lib/metro/models/ui/sprite.rb', line 38

property :angle

#center_xObject

The center, horizontal position, as expressed in a ratio, of the image.



30
# File 'lib/metro/models/ui/sprite.rb', line 30

property :center_x, type: :numeric, default: 0.5

#center_yObject

The center, vertical position, as expressed in a ratio, of the image.



34
# File 'lib/metro/models/ui/sprite.rb', line 34

property :center_y, type: :numeric, default: 0.5

#colorObject

This is the color of the spirte. The color usually remains white, and the color property is implemented by the ‘alpha` value is the one thing that is altered to fade in and fade out the sprite.



22
# File 'lib/metro/models/ui/sprite.rb', line 22

property :color

#dimensionsObject

The height and width of the sprite is based on the image of the sprite.



42
43
44
# File 'lib/metro/models/ui/sprite.rb', line 42

property :dimensions do
  image.dimensions
end

#imageObject

The image that will be drawn for the sprite



12
# File 'lib/metro/models/ui/sprite.rb', line 12

property :image

#positionObject

The point at which the sprite should be drawn



16
# File 'lib/metro/models/ui/sprite.rb', line 16

property :position

#scaleObject

The scale at which to draw the sprite. This is default scale of 1.



26
# File 'lib/metro/models/ui/sprite.rb', line 26

property :scale

Instance Method Details

#bottomFloat

Returns the bottom-most y position of the sprite.

Returns:

  • (Float)

    the bottom-most y position of the sprite



67
68
69
# File 'lib/metro/models/ui/sprite.rb', line 67

def bottom
  top + height * y_factor
end

#boundsRectangleBounds

Returns the bounds of the sprite.

Returns:



47
48
49
# File 'lib/metro/models/ui/sprite.rb', line 47

def bounds
  Bounds.new left: left, right: right, top: top, bottom: bottom
end

#drawObject

By default the sprite will draw the image defined for it.



74
75
76
# File 'lib/metro/models/ui/sprite.rb', line 74

def draw
  image.draw_rot x, y, z_order, angle, center_x, center_y, x_factor, y_factor, color
end

#leftFloat

Returns the left-most x position of the sprite.

Returns:

  • (Float)

    the left-most x position of the sprite



52
53
54
# File 'lib/metro/models/ui/sprite.rb', line 52

def left
  x - width * center_x * x_factor
end

#rightFloat

Returns the right-most x position of the sprite.

Returns:

  • (Float)

    the right-most x position of the sprite



57
58
59
# File 'lib/metro/models/ui/sprite.rb', line 57

def right
  left + width * x_factor
end

#topFloat

Returns the top-most y position of the sprite.

Returns:

  • (Float)

    the top-most y position of the sprite



62
63
64
# File 'lib/metro/models/ui/sprite.rb', line 62

def top
  y - height * center_y * y_factor
end