Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #==============================================================================
- # MS - Timer Actions
- #------------------------------------------------------------------------------
- # por Masked
- #==============================================================================
- #==============================================================================
- # Instruções
- #------------------------------------------------------------------------------
- # Para criar uma tarefa:
- # MBS::Timer.schedule(:nome_da_tarefa, tempo, 'script')
- #
- # Sendo o tempo em segundos e o nome da tarefa um nome único que será dado a
- # cada tarefa, criar duas tarefas com o mesmo nome retorna erro. O script
- # será o código executado ao fim do tempo estimado.
- #
- # Se quiser obter o tempo decorrido de uma tarefa:
- # MBS::Timer.time_elapsed(:nome_da_tarefa)
- #==============================================================================
- ($imported ||= {})[:ms_timer] = true
- #==============================================================================
- # >> MBS
- #==============================================================================
- module MBS
- #============================================================================
- # >> Timer
- #============================================================================
- module Timer
- # Texto de segurança
- SECURITY = [0xC, 0xA, 0xF, 0xE].pack('C')
- # Constantes de tempo
- S = 1
- MIN = 60 * S
- H = 60 * MIN
- D = 24 * H
- # Lista de tarefas para acontecer
- @@_tasks = {}
- # Se mudou ou não
- @@_changed = false
- # Estrutura das tarefas
- Task = Struct.new(:time, :action) do
- #----------------------------------------------------------------------
- # * Definição dos atributos
- #----------------------------------------------------------------------
- attr_reader :time, :action, :start, :end
- #----------------------------------------------------------------------
- # * Inicialização do objeto
- #----------------------------------------------------------------------
- def initialize(time, action)
- @time = time
- @action = action
- @start = Time.now
- @end = Time.now + time
- end
- end
- #------------------------------------------------------------------------
- # * Registro de uma tarefa (tempo real)
- #------------------------------------------------------------------------
- def self.schedule(sym, time, action)
- raise('(MS - Timer Actions) Duas tarefas não podem ter o mesmo nome!') if @@_tasks.has_key?(sym)
- @@_tasks[sym] = Task.new(time, action)
- end
- #------------------------------------------------------------------------
- # * Aquisição das tarefas que estão sendo atualizadas
- #------------------------------------------------------------------------
- def self.current
- @@_tasks
- end
- #------------------------------------------------------------------------
- # * Aquisição do tempo decorrido de uma tarefa
- #------------------------------------------------------------------------
- def self.time_elapsed(sym)
- return 0 unless @@_tasks.has_key?(sym)
- return Time.now - @@_tasks[sym].start
- end
- #------------------------------------------------------------------------
- # * Atualização do módulo
- #------------------------------------------------------------------------
- def self.update
- @@_tasks.each do |key, task|
- if (task.end - Time.now) <= 0
- Object.__send__(:class_eval, task.action)
- @@_tasks.delete(key)
- @@_changed = true
- end
- end
- end
- #------------------------------------------------------------------------
- # * Verificação de alterações no timer
- #------------------------------------------------------------------------
- def self.changed?
- @@_changed
- end
- #------------------------------------------------------------------------
- # * Salvamento dos dados do timer em um arquivo
- #------------------------------------------------------------------------
- def self.save(filename)
- file = File.open(filename, 'wb')
- file.write(SECURITY)
- Marshal.dump(@@_tasks, file)
- file.close
- @@_changed = false
- end
- #------------------------------------------------------------------------
- # * Carregamento dos dados do timer em um arquivo
- #------------------------------------------------------------------------
- def self.load(filename)
- file = File.open(filename, 'rb')
- return if file.read(SECURITY.size) != SECURITY
- @@_tasks = Marshal.load(file)
- file.close
- @@_changed = false
- end
- end
- end
- #==============================================================================
- # ** Scene_Base
- #-----------------------------------------------------------------------------
- # Aqui são feitas as modificações para atualizar constantemente o timer
- #==============================================================================
- class Scene_Base
- alias mbstimer_updatebasic update_basic
- #--------------------------------------------------------------------------
- # * Atualização da tela (básico)
- #--------------------------------------------------------------------------
- def update_basic
- mbstimer_updatebasic
- MBS::Timer.update
- end
- end
- #==============================================================================
- # ** DataManager
- #------------------------------------------------------------------------------
- # Aqui são feitas as modificações para salvar o timer
- #==============================================================================
- module DataManager
- #--------------------------------------------------------------------------
- # Alias
- #--------------------------------------------------------------------------
- class << self
- alias mbstimer_savegame save_game
- alias mbstimer_loadgame load_game
- end
- #--------------------------------------------------------------------------
- # * Salvamento do jogo
- #--------------------------------------------------------------------------
- def self.save_game(*a, &b)
- mbstimer_savegame(*a, &b)
- MBS::Timer.save(sprintf("Timer%03d.rvdata2", a[0]))
- end
- #--------------------------------------------------------------------------
- # * Carregamento do jogo
- #--------------------------------------------------------------------------
- def self.load_game(*a, &b)
- mbstimer_loadgame(*a, &b)
- MBS::Timer.load(sprintf("Timer%03d.rvdata2", a[0]))
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement