Class: Sprite

Inherits:
Object
  • Object
show all
Includes:
RGSS::Drawable
Defined in:
lib/openrgss/sprite.rb

Overview

The sprite class. Sprites are the basic concept used to display characters and other objects on the game screen.

Instance Attribute Summary collapse

Attributes included from RGSS::Drawable

#created_at, #visible, #x, #y, #z

Instance Method Summary collapse

Methods included from RGSS::Drawable

#<=>, #>, #dispose, #disposed?

Constructor Details

#initialize(viewport = nil) ⇒ Sprite

Creates a new sprite object. Specifies a viewport (Viewport) when necessary.



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/openrgss/sprite.rb', line 72

def initialize(viewport=nil)
  @src_rect = Rect.new(0, 0, 0, 0)
  @x        = 0
  @y        = 0
  @z        = 0
  @ox       = 0
  @oy       = 0
  @zoom_x   = 1
  @zoom_y   = 1
  @angle    = 0
  @src_rect = Rect.new
  @color    = Color.new
  @tone     = Tone.new
  @opacity  = 255
  @visible  = true
  super(viewport)
end

Instance Attribute Details

#angleObject

The sprite’s angle of rotation. Specifies up to 360 degrees of counterclockwise rotation. However, drawing a rotated sprite is time-consuming, so avoid overuse.



27
28
29
# File 'lib/openrgss/sprite.rb', line 27

def angle
  @angle
end

#bitmapObject

Refers to the bitmap (Bitmap) used for the sprite’s starting point.



5
6
7
# File 'lib/openrgss/sprite.rb', line 5

def bitmap
  @bitmap
end

#blend_typeObject

The sprite’s blending mode (0: normal, 1: addition, 2: subtraction).



58
59
60
# File 'lib/openrgss/sprite.rb', line 58

def blend_type
  @blend_type
end

#bush_depthObject

The bush depth and opacity of a sprite. This can be used to represent a situation such as the character’s legs being hidden by bushes.

For bush_depth, the number of pixels for the bush section is specified. The default value is 0.

For bush_opacity, the opacity of the bush section from 0 to 255 is specified. Out-of-range values will be corrected automatically. The default value is 128.

The bush_opacity value will be multiplied by opacity. For example, if both opacity and bush_opacity are set to 128, it will be handled as a transparency on # top of a transparency, for an actual opacity of 64.



51
52
53
# File 'lib/openrgss/sprite.rb', line 51

def bush_depth
  @bush_depth
end

#bush_opacityObject

Returns the value of attribute bush_opacity.



52
53
54
# File 'lib/openrgss/sprite.rb', line 52

def bush_opacity
  @bush_opacity
end

#colorObject

The color (Color) to be blended with the sprite. Alpha values are used in the blending ratio.

Handled separately from the color blended into a flash effect. However, the color with the higher alpha value when displayed will have the higher priority when blended.



63
64
65
# File 'lib/openrgss/sprite.rb', line 63

def color
  @color
end

#mirrorObject

A flag denoting the sprite has been flipped horizontally. If TRUE, the sprite will be drawn flipped. The default is false.



42
43
44
# File 'lib/openrgss/sprite.rb', line 42

def mirror
  @mirror
end

#opacityObject

The sprite’s opacity (0-255). Out-of-range values are automatically corrected.



55
56
57
# File 'lib/openrgss/sprite.rb', line 55

def opacity
  @opacity
end

#oxObject

The x-coordinate of the sprite’s starting point.



15
16
17
# File 'lib/openrgss/sprite.rb', line 15

def ox
  @ox
end

#oyObject

The y-coordinate of the sprite’s starting point.



18
19
20
# File 'lib/openrgss/sprite.rb', line 18

def oy
  @oy
end

#src_rectObject

The box (Rect) taken from a bitmap.



8
9
10
# File 'lib/openrgss/sprite.rb', line 8

def src_rect
  @src_rect
end

#toneObject

The sprite’s color tone (Tone).



66
67
68
# File 'lib/openrgss/sprite.rb', line 66

def tone
  @tone
end

#viewportObject

Refers to the viewport (Viewport) associated with the sprite.



12
13
14
# File 'lib/openrgss/sprite.rb', line 12

def viewport
  @viewport
end

#wave_ampObject

Defines the amplitude, frequency, speed, and phase of the wave effect. A raster scroll effect is achieved by using a sinusoidal function to draw the sprite with each line’s horizontal position slightly different from the last.

wave_amp is the wave amplitude and wave_length is the wave frequency, and each is specified by a number of pixels.

wave_speed specifies the speed of the wave animation. The default is 360, and the larger the value, the faster the effect.

wave_phase specifies the phase of the top line of the sprite using an angle of up to 360 degrees. This is updated each time the update method is called. It is not necessary to use this property unless it is required for two sprites to have their wave effects synchronized.



36
37
38
# File 'lib/openrgss/sprite.rb', line 36

def wave_amp
  @wave_amp
end

#wave_lengthObject

Returns the value of attribute wave_length.



37
38
39
# File 'lib/openrgss/sprite.rb', line 37

def wave_length
  @wave_length
end

#wave_phaseObject

Returns the value of attribute wave_phase.



39
40
41
# File 'lib/openrgss/sprite.rb', line 39

def wave_phase
  @wave_phase
end

#wave_speedObject

Returns the value of attribute wave_speed.



38
39
40
# File 'lib/openrgss/sprite.rb', line 38

def wave_speed
  @wave_speed
end

#zoom_xObject

The sprite’s x-axis zoom level. 1.0 denotes actual pixel size.



21
22
23
# File 'lib/openrgss/sprite.rb', line 21

def zoom_x
  @zoom_x
end

#zoom_yObject

The sprite’s y-axis zoom level. 1.0 denotes actual pixel size.



24
25
26
# File 'lib/openrgss/sprite.rb', line 24

def zoom_y
  @zoom_y
end

Instance Method Details

#draw(destination = Graphics) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/openrgss/sprite.rb', line 121

def draw(destination=Graphics)
  return unless bitmap and opacity > 0

  base_x = @x-@ox
  base_y = @y-@oy
  if viewport
    destination.entity.set_clip_rect(viewport.rect.x, viewport.rect.y, viewport.rect.width, viewport.rect.height)
    base_x += viewport.rect.x - viewport.ox
    base_y += viewport.rect.y - viewport.oy
  end

  SDL::Surface.blit(@bitmap.entity, @src_rect.x, @src_rect.y, @src_rect.width, @src_rect.height, destination.entity, base_x, base_y)
  destination.entity.set_clip_rect(0, 0, destination.width, destination.height) if viewport
end

#flash(color, duration) ⇒ Object

Begins flashing the sprite. duration specifies the number of frames flashing will last.

If color is set to nil, the sprite will disappear while flashing.



99
100
101
# File 'lib/openrgss/sprite.rb', line 99

def flash(color, duration)

end

#heightObject

Gets the height of the sprite. Equivalent to src_rect.height.



117
118
119
# File 'lib/openrgss/sprite.rb', line 117

def height
  bitmap.height
end

#updateObject

Advances the sprite flash or wave phase. As a general rule, this method is called once per frame.

It is not necessary to call this if a flash or wave is not needed.



107
108
109
# File 'lib/openrgss/sprite.rb', line 107

def update

end

#widthObject

Gets the width of the sprite. Equivalent to src_rect.width.



112
113
114
# File 'lib/openrgss/sprite.rb', line 112

def width
  bitmap.width
end