Class: Sprite_Base

Inherits:
Sprite
  • Object
show all
Defined in:
Sprite_Base.rb

Overview

** Sprite_Base


A sprite class with animation display processing added.

Direct Known Subclasses

Sprite_Battler, Sprite_Character

Constant Summary

@@ani_checker =

  • Class Variable


[]
@@ani_spr_checker =
[]
@@_reference_count =
{}

Instance Method Summary (collapse)

Constructor Details

- (Sprite_Base) initialize(viewport = nil)


  • Object Initialization




17
18
19
20
21
# File 'Sprite_Base.rb', line 17

def initialize(viewport = nil)
  super(viewport)
  @use_sprite = true        # Sprite use flag
  @ani_duration = 0         # Remaining time of animation
end

Instance Method Details

- (Boolean) animation?


  • Determine if animation is being displayed


Returns:

  • (Boolean)


41
42
43
# File 'Sprite_Base.rb', line 41

def animation?
  @animation != nil
end

- (Object) animation_process_timing(timing)


  • SE and Flash Timing Processing

    timing : Timing data (RPG::Animation::Timing)



219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'Sprite_Base.rb', line 219

def animation_process_timing(timing)
  timing.se.play unless @ani_duplicated
  case timing.flash_scope
  when 1
    self.flash(timing.flash_color, timing.flash_duration * @ani_rate)
  when 2
    if viewport && !@ani_duplicated
      viewport.flash(timing.flash_color, timing.flash_duration * @ani_rate)
    end
  when 3
    self.flash(nil, timing.flash_duration * @ani_rate)
  end
end

- (Object) animation_set_sprites(frame)


  • Set Animation Sprite

    frame : Frame data (RPG::Animation::Frame)



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'Sprite_Base.rb', line 182

def animation_set_sprites(frame)
  cell_data = frame.cell_data
  @ani_sprites.each_with_index do |sprite, i|
    next unless sprite
    pattern = cell_data[i, 0]
    if !pattern || pattern < 0
      sprite.visible = false
      next
    end
    sprite.bitmap = pattern < 100 ? @ani_bitmap1 : @ani_bitmap2
    sprite.visible = true
    sprite.src_rect.set(pattern % 5 * 192,
      pattern % 100 / 5 * 192, 192, 192)
    if @ani_mirror
      sprite.x = @ani_ox - cell_data[i, 1]
      sprite.y = @ani_oy + cell_data[i, 2]
      sprite.angle = (360 - cell_data[i, 4])
      sprite.mirror = (cell_data[i, 5] == 0)
    else
      sprite.x = @ani_ox + cell_data[i, 1]
      sprite.y = @ani_oy + cell_data[i, 2]
      sprite.angle = cell_data[i, 4]
      sprite.mirror = (cell_data[i, 5] == 1)
    end
    sprite.z = self.z + 300 + i
    sprite.ox = 96
    sprite.oy = 96
    sprite.zoom_x = cell_data[i, 3] / 100.0
    sprite.zoom_y = cell_data[i, 3] / 100.0
    sprite.opacity = cell_data[i, 6] * self.opacity / 255.0
    sprite.blend_type = cell_data[i, 7]
  end
end

- (Object) dispose


  • Free




25
26
27
28
# File 'Sprite_Base.rb', line 25

def dispose
  super
  dispose_animation
end

- (Object) dispose_animation


  • Free Animation




132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'Sprite_Base.rb', line 132

def dispose_animation
  if @ani_bitmap1
    @@_reference_count[@ani_bitmap1] -= 1
    if @@_reference_count[@ani_bitmap1] == 0
      @ani_bitmap1.dispose
    end
  end
  if @ani_bitmap2
    @@_reference_count[@ani_bitmap2] -= 1
    if @@_reference_count[@ani_bitmap2] == 0
      @ani_bitmap2.dispose
    end
  end
  if @ani_sprites
    @ani_sprites.each {|sprite| sprite.dispose }
    @ani_sprites = nil
    @animation = nil
  end
  @ani_bitmap1 = nil
  @ani_bitmap2 = nil
end

- (Object) end_animation


  • End Animation




175
176
177
# File 'Sprite_Base.rb', line 175

def end_animation
  dispose_animation
end

- (Object) load_animation_bitmap


  • Read (Load) Animation Graphics




68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'Sprite_Base.rb', line 68

def load_animation_bitmap
  animation1_name = @animation.animation1_name
  animation1_hue = @animation.animation1_hue
  animation2_name = @animation.animation2_name
  animation2_hue = @animation.animation2_hue
  @ani_bitmap1 = Cache.animation(animation1_name, animation1_hue)
  @ani_bitmap2 = Cache.animation(animation2_name, animation2_hue)
  if @@_reference_count.include?(@ani_bitmap1)
    @@_reference_count[@ani_bitmap1] += 1
  else
    @@_reference_count[@ani_bitmap1] = 1
  end
  if @@_reference_count.include?(@ani_bitmap2)
    @@_reference_count[@ani_bitmap2] += 1
  else
    @@_reference_count[@ani_bitmap2] = 1
  end
  Graphics.frame_reset
end

- (Object) make_animation_sprites


  • Create Animation Spirtes




90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'Sprite_Base.rb', line 90

def make_animation_sprites
  @ani_sprites = []
  if @use_sprite && !@@ani_spr_checker.include?(@animation)
    16.times do
      sprite = ::Sprite.new(viewport)
      sprite.visible = false
      @ani_sprites.push(sprite)
    end
    if @animation.position == 3
      @@ani_spr_checker.push(@animation)
    end
  end
  @ani_duplicated = @@ani_checker.include?(@animation)
  if !@ani_duplicated && @animation.position == 3
    @@ani_checker.push(@animation)
  end
end

- (Object) set_animation_origin


  • Set Animation Origin




110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'Sprite_Base.rb', line 110

def set_animation_origin
  if @animation.position == 3
    if viewport == nil
      @ani_ox = Graphics.width / 2
      @ani_oy = Graphics.height / 2
    else
      @ani_ox = viewport.rect.width / 2
      @ani_oy = viewport.rect.height / 2
    end
  else
    @ani_ox = x - ox + width / 2
    @ani_oy = y - oy + height / 2
    if @animation.position == 0
      @ani_oy -= height / 2
    elsif @animation.position == 2
      @ani_oy += height / 2
    end
  end
end

- (Object) set_animation_rate


  • Set Animation Speed




62
63
64
# File 'Sprite_Base.rb', line 62

def set_animation_rate
  @ani_rate = 4     # Fixed value by default
end

- (Object) start_animation(animation, mirror = false)


  • Start Animation




47
48
49
50
51
52
53
54
55
56
57
58
# File 'Sprite_Base.rb', line 47

def start_animation(animation, mirror = false)
  dispose_animation
  @animation = animation
  if @animation
    @ani_mirror = mirror
    set_animation_rate
    @ani_duration = @animation.frame_max * @ani_rate + 1
    load_animation_bitmap
    make_animation_sprites
    set_animation_origin
  end
end

- (Object) update


  • Frame Update




32
33
34
35
36
37
# File 'Sprite_Base.rb', line 32

def update
  super
  update_animation
  @@ani_checker.clear
  @@ani_spr_checker.clear
end

- (Object) update_animation


  • Update Animation




156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'Sprite_Base.rb', line 156

def update_animation
  return unless animation?
  @ani_duration -= 1
  if @ani_duration % @ani_rate == 0
    if @ani_duration > 0
      frame_index = @animation.frame_max
      frame_index -= (@ani_duration + @ani_rate - 1) / @ani_rate
      animation_set_sprites(@animation.frames[frame_index])
      @animation.timings.each do |timing|
        animation_process_timing(timing) if timing.frame == frame_index
      end
    else
      end_animation
    end
  end
end