Class: Battle::Logic::WeatherChangeHandler

Inherits:
ChangeHandlerBase show all
Includes:
Hooks
Defined in:
scripts/01600 Alpha 25 Battle Engine/00200 Battle_Logic/00001 Handlers/01007 WeatherChangeHandler.rb

Overview

Handler responsive of answering properly weather changes requests

Constant Summary collapse

WEATHER_SYM_TO_MSG =

Mapping between weather symbol & message_id

{
  none: 97,
  rain: 88,
  sunny: 87,
  sandstorm: 89,
  hail: 90,
  fog: 91,
  hardsun: 271,
  hardrain: 269,
  wind: 273
}

Instance Attribute Summary

Attributes inherited from ChangeHandlerBase

#logic, #scene

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Hooks

#exec_hooks, #force_return, included, register, remove, remove_without_name

Methods inherited from ChangeHandlerBase

#prevent_change, #process_prevention_reason, #reset_prevention_reason

Constructor Details

#initialize(logic, scene, env = $env) ⇒ WeatherChangeHandler

Create a new Weather Change Handler

Parameters:



23
24
25
26
# File 'scripts/01600 Alpha 25 Battle Engine/00200 Battle_Logic/00001 Handlers/01007 WeatherChangeHandler.rb', line 23

def initialize(logic, scene, env = $env)
  super(logic, scene)
  @env = env
end

Class Method Details

.register_post_weather_change_hook(reason) {|handler, weather_type, last_weather| ... }

Function that registers a on_post_weather_change hook

Parameters:

  • reason (String)

    reason of the on_post_weather_change registration

Yield Parameters:

  • handler (WeatherChangeHandler)
  • weather_type (Symbol)

    :none, :rain, :sunny, :sandstorm, :hail, :fog, :hardsun, :hardrain

  • last_weather (Symbol)

    :none, :rain, :sunny, :sandstorm, :hail, :fog, :hardsun, :hardrain



99
100
101
102
103
104
105
106
107
# File 'scripts/01600 Alpha 25 Battle Engine/00200 Battle_Logic/00001 Handlers/01007 WeatherChangeHandler.rb', line 99

def register_post_weather_change_hook(reason)
  Hooks.register(WeatherChangeHandler, :on_post_weather_change, reason) do |hook_binding|
    yield(
      self,
      hook_binding.local_variable_get(:weather_type),
      hook_binding.local_variable_get(:last_weather)
    )
  end
end

.register_weather_prevention_hook(reason) {|handler, weather_type, last_weather| ... }

Function that registers a weather_prevention hook

Parameters:

  • reason (String)

    reason of the weather_prevention registration

Yield Parameters:

  • handler (WeatherChangeHandler)
  • weather_type (Symbol)

    :none, :rain, :sunny, :sandstorm, :hail, :fog, :hardsun, :hardrain

  • last_weather (Symbol)

    :none, :rain, :sunny, :sandstorm, :hail, :fog, :hardsun, :hardrain

Yield Returns:

  • (:prevent, nil)

    :prevent if the status cannot be applied



83
84
85
86
87
88
89
90
91
92
# File 'scripts/01600 Alpha 25 Battle Engine/00200 Battle_Logic/00001 Handlers/01007 WeatherChangeHandler.rb', line 83

def register_weather_prevention_hook(reason)
  Hooks.register(WeatherChangeHandler, :weather_prevention, reason) do |hook_binding|
    result = yield(
      self,
      hook_binding.local_variable_get(:weather_type),
      hook_binding.local_variable_get(:last_weather)
    )
    force_return(false) if result == :prevent
  end
end

Instance Method Details

#weather_appliable?(weather_type) ⇒ Boolean

Function telling if a weather can be applyied

Parameters:

  • weather_type (Symbol)

    :none, :rain, :sunny, :sandstorm, :hail, :fog, :hardsun, :hardrain

Returns:

  • (Boolean)


31
32
33
34
35
36
37
38
39
40
# File 'scripts/01600 Alpha 25 Battle Engine/00200 Battle_Logic/00001 Handlers/01007 WeatherChangeHandler.rb', line 31

def weather_appliable?(weather_type)
  log_data("# weather_appliable?(#{weather_type})")
  reset_prevention_reason
  last_weather = @env.current_weather_db_symbol
  exec_hooks(WeatherChangeHandler, :weather_prevention, binding)
  return true
rescue Hooks::ForceReturn => e
  log_data("# FR: weather_appliable? #{e.data} from #{e.hook_name} (#{e.reason})")
  return e.data
end

#weather_change(weather_type, nb_turn)

Function that actually change the weather

Parameters:

  • weather_type (Symbol)

    :none, :rain, :sunny, :sandstorm, :hail, :fog, :hardsun, :hardrain

  • nb_turn (Integer, nil)

    Number of turn, use nil for Infinity



45
46
47
48
49
50
51
52
53
54
# File 'scripts/01600 Alpha 25 Battle Engine/00200 Battle_Logic/00001 Handlers/01007 WeatherChangeHandler.rb', line 45

def weather_change(weather_type, nb_turn)
  log_data("# weather_change(#{weather_type}, #{nb_turn})")
  last_weather = @env.current_weather_db_symbol
  @env.apply_weather(weather_type, nb_turn)
  show_weather_message(last_weather, weather_type)
  exec_hooks(WeatherChangeHandler, :on_post_weather_change, binding)
rescue Hooks::ForceReturn => e
  log_data("# FR: weather_change #{e.data} from #{e.hook_name} (#{e.reason})")
  return e.data
end

#weather_change_with_process(weather_type, nb_turn)

Function that test if the change is possible and perform the change if so

Parameters:

  • weather_type (Symbol)

    :none, :rain, :sunny, :sandstorm, :hail, :fog, :hardsun, :hardrain

  • nb_turn (Integer, nil)

    Number of turn, use nil for Infinity



59
60
61
62
63
# File 'scripts/01600 Alpha 25 Battle Engine/00200 Battle_Logic/00001 Handlers/01007 WeatherChangeHandler.rb', line 59

def weather_change_with_process(weather_type, nb_turn)
  return process_prevention_reason unless weather_appliable?(weather_type)

  weather_change(weather_type, nb_turn)
end