Module: NuriYuri::DynamicLight

Defined in:
scripts/02000 Nuri Yuri/00001 DynamicLight/00001 DynamicLight.rb,
scripts/02000 Nuri Yuri/00001 DynamicLight/00002 DynamicLightSprite.rb,
scripts/02000 Nuri Yuri/00001 DynamicLight/00004 ScalableDynamicLight.rb

Overview

Module managing the dynamic light system for PSDK

Defined Under Namespace

Classes: DynamicLightSprite, ScalableDLS

Constant Summary collapse

LIGHTS =

Returns List of light The first array component tell if it's a normal light (:normal, centered on the character) or if it's a directed light (:direction, in the direction of the character) The second component is the sprite shown under the map (light mask) The third component is the sprite shown in the map viewport for special effect mostly on top of the character.

Returns:

  • (Array<Array>)

    List of light The first array component tell if it's a normal light (:normal, centered on the character) or if it's a directed light (:direction, in the direction of the character) The second component is the sprite shown under the map (light mask) The third component is the sprite shown in the map viewport for special effect mostly on top of the character

[
  [:normal, 'dynamic_light/circle_320'],
  [:direction, 'dynamic_light/flash_light_mask', 'dynamic_light/flash_light_color'],
  [:normal, 'dynamic_light/circle_96']
]
ANIMATIONS =

Returns List of light animation, Hash key are opacity: & zoom: (array of value).

Returns:

  • (Array<Hash>)

    List of light animation, Hash key are opacity: & zoom: (array of value)

[
  { zoom: [1], opacity: [255] },
  { zoom: Array.new(120) { |i| 0.70 + 0.05 * Math.cos(6.28 * i / 120) },
    opacity: Array.new(120) { |i| 240 + (15 * Math.cos(6.28 * i / 120)).to_i } },
  { zoom: Array.new(120) { |i| 0.95 + 0.05 * Math.cos(6.28 * i / 120) }, opacity: [255] }
]

Class Method Summary collapse

Class Method Details

.add(chara_id, light_type, animation_type = 0, zoom_count = 0, opacity_count = 0, *args, type: DynamicLightSprite) ⇒ Integer

Add a new light to the stack

Parameters:

  • chara_id (Integer)

    ID of the character (0 = player, -n = follower n, +n = event id)

  • light_type (Integer)

    Type of the light we'll display on the character

  • animation_type (Integer) (defaults to: 0)

    Type of the animation performed on the light

  • zoom_count (Integer) (defaults to: 0)

    initial value of the zoom_count

  • opacity_count (Integer) (defaults to: 0)

    initial value of the opacity_count

  • args (Array)

    extra parameters

  • type (Class) (defaults to: DynamicLightSprite)

    Type of the sprite used to simulate the light

Returns:

  • (Integer)

    ID of the light in the light stack, if -1 => one of the parameter is invalid



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'scripts/02000 Nuri Yuri/00001 DynamicLight/00001 DynamicLight.rb', line 88

def add(chara_id, light_type, animation_type = 0, zoom_count = 0, opacity_count = 0, *args, type: DynamicLightSprite)
  return -2 unless $scene.is_a?(Scene_Map) && @viewport
  return -1 unless light_type.between?(0, LIGHTS.size - 1)
  return -1 unless animation_type.between?(0, ANIMATIONS.size - 1)

  if chara_id < 0
    character = nil # Not supported now.
  elsif chara_id == 0
    character = $game_player
  else
    character = $game_map.events[chara_id]
  end
  return -1 unless character

  @stack << type.new(character, light_type, animation_type, zoom_count, opacity_count, *args)
  light_id = @stack.last.light_id = @stack.size - 1
  PFM.game_state.nuri_yuri_dynamic_light << { params: [chara_id, light_type, animation_type, zoom_count, opacity_count, *args], on: true, type: type }
  return light_id
end

.clear_stack

Clear the light task



160
161
162
163
164
165
# File 'scripts/02000 Nuri Yuri/00001 DynamicLight/00001 DynamicLight.rb', line 160

def clear_stack
  @stack ||= []
  @stack.each { |light| light.dispose unless light.disposed? }
ensure
  @stack.clear
end

.create_viewport

Create the viewport



168
169
170
171
# File 'scripts/02000 Nuri Yuri/00001 DynamicLight/00001 DynamicLight.rb', line 168

def create_viewport
  @viewport = Viewport.create(:main, 1) if !@viewport || @viewport.disposed?
  Graphics.sort_z
end

.dispose_viewport

Dispose the light viewport



174
175
176
# File 'scripts/02000 Nuri Yuri/00001 DynamicLight/00001 DynamicLight.rb', line 174

def dispose_viewport
  @viewport.dispose
end

.light_sprite(light_id) ⇒ DynamicLightSprite?

Retrieve the light sprite

Parameters:

  • light_id

    ID of the light in the light stack

Returns:



137
138
139
140
141
142
# File 'scripts/02000 Nuri Yuri/00001 DynamicLight/00001 DynamicLight.rb', line 137

def light_sprite(light_id)
  return nil unless @stack
  return nil if light_id < 0

  @stack[light_id]
end

.load_blendmode

Load the light blend_mode



179
180
181
182
183
184
185
186
187
188
# File 'scripts/02000 Nuri Yuri/00001 DynamicLight/00001 DynamicLight.rb', line 179

def load_blendmode
  shader = BlendMode.new
  shader.color_src_factor = BlendMode::DstColor
  shader.color_dest_factor = BlendMode::OneMinusSrcColor
  shader.color_equation = BlendMode::Subtract
  shader.alpha_src_factor = BlendMode::SrcAlpha
  shader.alpha_dest_factor = BlendMode::DstAlpha
  shader.alpha_equation = BlendMode::Add
  @viewport.shader = shader
end

.on_map_init

Part called when Scene_Map init itself (in order to reload all the lights)



191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'scripts/02000 Nuri Yuri/00001 DynamicLight/00001 DynamicLight.rb', line 191

def on_map_init
  PFM.game_state.nuri_yuri_dynamic_light ||= [] # Safe init the stack
  light_info_stack = PFM.game_state.nuri_yuri_dynamic_light.clone
  unless light_info_stack.empty?
    @delay = proc do
      start
      light_info_stack.each do |light_info|
        id = add(*light_info[:params], type: light_info[:type])
        switch_off(id) unless light_info[:on]
      end
    end
    Scheduler.add_message(:on_transition, Scene_Map, 'NuriYuri::DynamicLight', 100, self, :update)
  end
end

.on_soft_reset

Clean everything on soft reset



213
214
215
216
217
# File 'scripts/02000 Nuri Yuri/00001 DynamicLight/00001 DynamicLight.rb', line 213

def on_soft_reset
  unregister
  @stack ||= []
  @stack.clear
end

.register

Register the update task



145
146
147
148
149
# File 'scripts/02000 Nuri Yuri/00001 DynamicLight/00001 DynamicLight.rb', line 145

def register
  Scheduler.add_message(:on_update, Scene_Map, 'NuriYuri::DynamicLight', 100, self, :update)
  Scheduler.add_message(:on_warp_end, Scene_Map, 'NuriYuri::DynamicLight', 100, self, :update)
  nil
end

.start(&block)

Start the dynamic light processing

Parameters:

  • block (Proc)

    given_block that tells to start the DynamicLight process on the next update



27
28
29
30
31
32
33
34
35
36
# File 'scripts/02000 Nuri Yuri/00001 DynamicLight/00001 DynamicLight.rb', line 27

def start(&block)
  return unless $scene.is_a?(Scene_Map)
  return start_delay(&block) if block

  stop(true)
  create_viewport
  load_blendmode
  register
  @delay = nil
end

.start_delay {|dl| ... }

Start delayed to the warp_end process

Yields:

  • (dl)

    given_block that tells to start the DynamicLight process on the next update

Yield Parameters:



52
53
54
55
56
57
58
59
60
61
62
# File 'scripts/02000 Nuri Yuri/00001 DynamicLight/00001 DynamicLight.rb', line 52

def start_delay
  return unless $scene.is_a?(Scene_Map)

  @stack ||= []
  unregister
  @delay = proc do
    start
    yield(self)
  end
  Scheduler.add_message(:on_warp_end, Scene_Map, 'NuriYuri::DynamicLight', 100, self, :update)
end

.stop(from_start = false)

Stop the dynamic light processing

Parameters:

  • from_start (Boolean) (defaults to: false)

    tell if stop was called from start to prevent useless viewport dispose



40
41
42
43
44
45
46
47
# File 'scripts/02000 Nuri Yuri/00001 DynamicLight/00001 DynamicLight.rb', line 40

def stop(from_start = false)
  return unless $scene.is_a?(Scene_Map)

  PFM.game_state.nuri_yuri_dynamic_light.clear
  unregister
  clear_stack
  dispose_viewport unless from_start
end

.stop_delay

Stop delayed to the warp_end process



65
66
67
68
69
70
71
# File 'scripts/02000 Nuri Yuri/00001 DynamicLight/00001 DynamicLight.rb', line 65

def stop_delay
  return unless $scene.is_a?(Scene_Map)

  @stack ||= []
  @delay = proc { stop }
  Scheduler.__remove_task(:on_update, Scene_Map, 'NuriYuri::DynamicLight', 100)
end

.switch_off(light_id)

Switch a light off

Parameters:

  • light_id (Integer)

    ID of the light in the light stack



123
124
125
126
127
128
129
130
131
132
# File 'scripts/02000 Nuri Yuri/00001 DynamicLight/00001 DynamicLight.rb', line 123

def switch_off(light_id)
  return unless @stack
  return if light_id < 0

  light = @stack[light_id]
  if light
    light.on = false
    PFM.game_state.nuri_yuri_dynamic_light[light_id][:on] = false
  end
end

.switch_on(light_id)

Switch a light on

Parameters:

  • light_id (Integer)

    ID of the light in the light stack



110
111
112
113
114
115
116
117
118
119
# File 'scripts/02000 Nuri Yuri/00001 DynamicLight/00001 DynamicLight.rb', line 110

def switch_on(light_id)
  return unless @stack
  return if light_id < 0

  light = @stack[light_id]
  if light
    light.on = true
    PFM.game_state.nuri_yuri_dynamic_light[light_id][:on] = true
  end
end

.unregister

Unregister the update task



152
153
154
155
156
157
# File 'scripts/02000 Nuri Yuri/00001 DynamicLight/00001 DynamicLight.rb', line 152

def unregister
  Scheduler.__remove_task(:on_update, Scene_Map, 'NuriYuri::DynamicLight', 100)
  Scheduler.__remove_task(:on_transition, Scene_Map, 'NuriYuri::DynamicLight', 100)
  Scheduler.__remove_task(:on_warp_end, Scene_Map, 'NuriYuri::DynamicLight', 100)
  nil
end

.update

Update the lights



74
75
76
77
# File 'scripts/02000 Nuri Yuri/00001 DynamicLight/00001 DynamicLight.rb', line 74

def update
  @delay&.call
  @stack.each(&:update)
end

.viewportViewport

Return the viewport of the DynamicLight system

Returns:



208
209
210
# File 'scripts/02000 Nuri Yuri/00001 DynamicLight/00001 DynamicLight.rb', line 208

def viewport
  @viewport
end