Class: Yuki::Particle_Object

Inherits:
Object
  • Object
show all
Defined in:
scripts/01450 Systems/00003 Map Engine/00001 Graphics/00400 Particles/00503 Particle_Object.rb,
scripts/01450 Systems/00003 Map Engine/00001 Graphics/00400 Particles/00502 Yuki_Particles Action Handlers.rb

Overview

The object that describe a particle

Author:

  • Nuri Yuri

Constant Summary collapse

TILE_ZOOM =

Zoom of a tile to adjust coordinate

Configs.display.tilemap_settings.character_tile_zoom
ACTION_HANDLERS =
{}
ACTION_HANDLERS_ORDER =
[]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(character, data, on_tp = false, params = {}) ⇒ Particle_Object

Create a particle object

Parameters:

  • character (Game_Character)

    the character on which the particle displays

  • data (Hash{Symbol => Hash})

    the data of the particle field of the data hash :

    enter: the particle animation when character enter on the tile
    stay: the particle animation when character stay on the tile
    leave: the particle animation when character leave the tile
    

    field of the particle animation

    max_counter: the number of frame on the animation
    loop: Boolean # if the animation loops or not
    data: an Array of animation instructions (Hash)
    

    field of an animation instruction

    state: Symbol # the new state of the particle
    zoom: Numeric # the zoom of the particle
    position: Symbol # the position type of the particle (:center_pos, :character_pos)
    file: String # the filename of the particle in Graphics/Particles/
    angle: Numeric # the angle of the particle
    add_z: Integer # The z offset relatively to the character
    oy_offset: Integer # The offset in oy
    opacity: Integer # The opacity of the particle
    chara: Boolean # If the particle Texture is treaten like the Character bitmap
    rect: Array(Integer, Integer, Integer, Integer) # the parameter of the #set function of Rect (src_rect)
    
  • on_tp (Boolean) (defaults to: false)

    tells the particle to skip the :enter animation or not

  • params (Hash) (defaults to: {})

    additional params for the animation

Options Hash (params):

  • :flow (Symbol)

    define the kind of flow to use for the animation

  • :radius (Integer)

    define the radius to use for the :update_radius_flow flow



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'scripts/01450 Systems/00003 Map Engine/00001 Graphics/00400 Particles/00503 Particle_Object.rb', line 36

def initialize(character, data, on_tp = false, params = {})
  @character = character
  init_map_data(character)
  @x = character.x + @map_data.offset_x
  @y = character.y + @map_data.offset_y
  @z = character.z
  @sprite = ::Sprite.new(Particles.viewport)
  @data = data
  @counter = 0
  @position_type = :center_pos
  @state = (on_tp ? :stay : :enter)
  @state = params[:state] if params.key?(:state)
  init_zoom
  @ox = 0
  @oy = 0
  @oy_off = 0
  @ox_off = 0
  @wait_count = 0
  @params = params
  @flow = params[:flow] || :update_default_flow
end

Instance Attribute Details

#disposedBoolean (readonly) Also known as: disposed?

if the particle is disposed

Returns:

  • (Boolean)


9
10
11
# File 'scripts/01450 Systems/00003 Map Engine/00001 Graphics/00400 Particles/00503 Particle_Object.rb', line 9

def disposed
  @disposed
end

Class Method Details

.add_handler(name, before = nil, &block)

Add a new action handler

Parameters:

  • name (Symbol)

    name of the action

  • before (Symbol, nil) (defaults to: nil)

    tell to put this handler before another handler



9
10
11
12
13
14
15
16
# File 'scripts/01450 Systems/00003 Map Engine/00001 Graphics/00400 Particles/00502 Yuki_Particles Action Handlers.rb', line 9

def self.add_handler(name, before = nil, &block)
  unless ACTION_HANDLERS_ORDER.include?(name)
    index = before ? ACTION_HANDLERS_ORDER.index(before) : nil
    index ||= ACTION_HANDLERS_ORDER.size
    ACTION_HANDLERS_ORDER.insert(index, name)
  end
  ACTION_HANDLERS[name] = block
end

Instance Method Details

#dispose

Dispose the particle



183
184
185
186
187
188
189
190
# File 'scripts/01450 Systems/00003 Map Engine/00001 Graphics/00400 Particles/00503 Particle_Object.rb', line 183

def dispose
  return if disposed?

  @sprite.dispose unless @sprite.disposed?
  @sprite = nil
  @disposed = true
  Yuki::Particles.clean_stack
end

#exectute_action(action)

Execute an animation instruction

Parameters:

  • action (Hash)

    the animation instruction



146
147
148
149
150
151
152
# File 'scripts/01450 Systems/00003 Map Engine/00001 Graphics/00400 Particles/00503 Particle_Object.rb', line 146

def exectute_action(action)
  ACTION_HANDLERS_ORDER.each do |name|
    if (data = action[name])
      instance_exec(data, &ACTION_HANDLERS[name])
    end
  end
end

#screen_z

Function that process screen z depending on original screen_y (without zoom)



178
179
180
# File 'scripts/01450 Systems/00003 Map Engine/00001 Graphics/00400 Particles/00503 Particle_Object.rb', line 178

def screen_z
  (y * 128 - $game_map.display_y + 3) / 4 + 32 * @z + 31
end

#update

Update the particle animation



59
60
61
62
63
64
65
66
67
68
# File 'scripts/01450 Systems/00003 Map Engine/00001 Graphics/00400 Particles/00503 Particle_Object.rb', line 59

def update
  return if disposed?
  return dispose unless @map_linker.map_datas.include?(@map_data)

  if @wait_count > 0
    @wait_count -= 1
    return update_sprite_position
  end
  update_particle_info(@data[@state]) && update_sprite_position
end

#update_default_flow(data) ⇒ Boolean

Update the default particle state flow

Parameters:

  • data (Hash)

    the data related to the current state

Returns:

  • (Boolean)


103
104
105
106
107
108
109
110
111
112
113
114
# File 'scripts/01450 Systems/00003 Map Engine/00001 Graphics/00400 Particles/00503 Particle_Object.rb', line 103

def update_default_flow(data)
  if @state == :enter
    @state = :stay
    @counter = 0
  elsif @state == :stay
    @state = :leave if x != @character.x || y != @character.y
    @counter = 0
  else
    return false
  end
  return true
end

#update_particle_info(data) ⇒ Boolean

Update the particle info

Parameters:

  • data (Hash)

    the data related to the current state

Returns:

  • (Boolean)

    if the update_sprite_position can be done



85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'scripts/01450 Systems/00003 Map Engine/00001 Graphics/00400 Particles/00503 Particle_Object.rb', line 85

def update_particle_info(data)
  if @counter < data[:max_counter]
    (action = data[:data][@counter]) && exectute_action(action)
    @counter += 1
  elsif send(@flow, data)
    return true
  elsif !data[:loop]
    dispose
    return false
  else
    @counter = 0
  end
  return true
end

#update_radius_flow(data) ⇒ Boolean

Update the radius particle kind flow

Parameters:

  • data (Hash)

    the data related to the current state

Returns:

  • (Boolean)


119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'scripts/01450 Systems/00003 Map Engine/00001 Graphics/00400 Particles/00503 Particle_Object.rb', line 119

def update_radius_flow(data)
  radius = Math.sqrt(($game_player.x - @character.x)**2 + ($game_player.y - @character.y)**2)
  if @state == :enter
    @state = :stay
    @counter = 0
  elsif @state == :stay
    return false if @map_data.map_id != $game_map.map_id

    if radius > @params[:radius]
      @state = :leave
      @counter = 0
    end
  elsif @state == :leave
    return false if @map_data.map_id != $game_map.map_id

    if (@sprite.visible = (radius <= @params[:radius]))
      @state = :enter
      @counter = 0
    end
  else
    return false
  end
  return true
end

#update_sprite_position

Update the position of the particle sprite



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'scripts/01450 Systems/00003 Map Engine/00001 Graphics/00400 Particles/00503 Particle_Object.rb', line 155

def update_sprite_position
  case @position_type
  when :center_pos, :grass_pos
    @sprite.x = (((x * 128 - $game_map.display_x + 3) / 4 + 16) * @tile_zoom).floor
    @sprite.y = ((y * 128 - $game_map.display_y + 3) / 4 + 32)
    if @position_type == :center_pos || @sprite.y >= @character.screen_y
      @sprite.z = (screen_z + @add_z)
    else
      @sprite.z = (screen_z - 1)
    end
    @sprite.y = (@sprite.y * @tile_zoom).floor
    @sprite.ox = @ox + @ox_off
    @sprite.oy = @oy + @oy_off
  when :character_pos
    @sprite.x = @character.screen_x * @tile_zoom
    @sprite.y = @character.screen_y * @tile_zoom
    @sprite.z = (@character.screen_z(0) + @add_z)
    @sprite.ox = @ox + @ox_off
    @sprite.oy = @oy + @oy_off
  end
end

#xInteger

Get the real x of the particle on the map

Returns:



72
73
74
# File 'scripts/01450 Systems/00003 Map Engine/00001 Graphics/00400 Particles/00503 Particle_Object.rb', line 72

def x
  return @x - @map_data.offset_x
end

#yInteger

Get the real y of the particle on the map

Returns:



78
79
80
# File 'scripts/01450 Systems/00003 Map Engine/00001 Graphics/00400 Particles/00503 Particle_Object.rb', line 78

def y
  return @y - @map_data.offset_y
end