Advertisement
Guest User

MS - Timer Actions

a guest
Jun 28th, 2015
372
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 6.62 KB | None | 0 0
  1. #==============================================================================
  2. # MS - Timer Actions
  3. #------------------------------------------------------------------------------
  4. # por Masked
  5. #==============================================================================
  6. #==============================================================================
  7. # Instruções
  8. #------------------------------------------------------------------------------
  9. # Para criar uma tarefa:
  10. # MBS::Timer.schedule(:nome_da_tarefa, tempo, 'script')
  11. #
  12. # Sendo o tempo em segundos e o nome da tarefa um nome único que será dado a
  13. # cada tarefa, criar duas tarefas com o mesmo nome retorna erro. O script
  14. # será o código executado ao fim do tempo estimado.
  15. #
  16. # Se quiser obter o tempo decorrido de uma tarefa:
  17. # MBS::Timer.time_elapsed(:nome_da_tarefa)
  18. #==============================================================================
  19. ($imported ||= {})[:ms_timer] = true
  20. #==============================================================================
  21. # >> MBS
  22. #==============================================================================
  23. module MBS
  24.   #============================================================================
  25.   # >> Timer
  26.   #============================================================================
  27.   module Timer
  28.    
  29.     # Texto de segurança
  30.     SECURITY = [0xC, 0xA, 0xF, 0xE].pack('C')
  31.    
  32.     # Constantes de tempo
  33.     S = 1
  34.     MIN = 60 * S
  35.     H = 60 * MIN
  36.     D = 24 * H    
  37.    
  38.     # Lista de tarefas para acontecer
  39.     @@_tasks = {}
  40.    
  41.     # Se mudou ou não
  42.     @@_changed = false
  43.    
  44.     # Estrutura das tarefas
  45.     Task = Struct.new(:time, :action) do
  46.       #----------------------------------------------------------------------
  47.       # * Definição dos atributos
  48.       #----------------------------------------------------------------------
  49.       attr_reader :time, :action, :start, :end
  50.       #----------------------------------------------------------------------
  51.       # * Inicialização do objeto
  52.       #----------------------------------------------------------------------
  53.       def initialize(time, action)
  54.         @time = time
  55.         @action = action
  56.         @start = Time.now
  57.         @end = Time.now + time
  58.       end
  59.     end
  60.    
  61.     #------------------------------------------------------------------------
  62.     # * Registro de uma tarefa (tempo real)
  63.     #------------------------------------------------------------------------
  64.     def self.schedule(sym, time, action)
  65.       raise('(MS - Timer Actions) Duas tarefas não podem ter o mesmo nome!') if @@_tasks.has_key?(sym)
  66.       @@_tasks[sym] = Task.new(time, action)
  67.     end
  68.     #------------------------------------------------------------------------
  69.     # * Aquisição das tarefas que estão sendo atualizadas
  70.     #------------------------------------------------------------------------
  71.     def self.current
  72.       @@_tasks
  73.     end
  74.     #------------------------------------------------------------------------
  75.     # * Aquisição do tempo decorrido de uma tarefa
  76.     #------------------------------------------------------------------------
  77.     def self.time_elapsed(sym)
  78.       return 0 unless @@_tasks.has_key?(sym)
  79.       return Time.now - @@_tasks[sym].start
  80.     end
  81.     #------------------------------------------------------------------------
  82.     # * Atualização do módulo
  83.     #------------------------------------------------------------------------
  84.     def self.update
  85.       @@_tasks.each do |key, task|
  86.         if (task.end - Time.now) <= 0
  87.           Object.__send__(:class_eval, task.action)
  88.           @@_tasks.delete(key)
  89.           @@_changed = true
  90.         end
  91.       end
  92.     end
  93.     #------------------------------------------------------------------------
  94.     # * Verificação de alterações no timer
  95.     #------------------------------------------------------------------------
  96.     def self.changed?
  97.       @@_changed
  98.     end
  99.     #------------------------------------------------------------------------
  100.     # * Salvamento dos dados do timer em um arquivo
  101.     #------------------------------------------------------------------------
  102.     def self.save(filename)
  103.       file = File.open(filename, 'wb')
  104.       file.write(SECURITY)
  105.       Marshal.dump(@@_tasks, file)
  106.       file.close
  107.       @@_changed = false
  108.     end
  109.     #------------------------------------------------------------------------
  110.     # * Carregamento dos dados do timer em um arquivo
  111.     #------------------------------------------------------------------------
  112.     def self.load(filename)
  113.       file = File.open(filename, 'rb')
  114.       return if file.read(SECURITY.size) != SECURITY
  115.       @@_tasks = Marshal.load(file)
  116.       file.close
  117.       @@_changed = false
  118.     end
  119.   end
  120. end
  121. #==============================================================================
  122. # ** Scene_Base
  123. #-----------------------------------------------------------------------------
  124. # Aqui são feitas as modificações para atualizar constantemente o timer
  125. #==============================================================================
  126. class Scene_Base
  127.   alias mbstimer_updatebasic update_basic
  128.   #--------------------------------------------------------------------------
  129.   # * Atualização da tela (básico)
  130.   #--------------------------------------------------------------------------
  131.   def update_basic
  132.     mbstimer_updatebasic
  133.     MBS::Timer.update
  134.   end
  135. end
  136. #==============================================================================
  137. # ** DataManager
  138. #------------------------------------------------------------------------------
  139. # Aqui são feitas as modificações para salvar o timer
  140. #==============================================================================
  141. module DataManager
  142.   #--------------------------------------------------------------------------
  143.   # Alias
  144.   #--------------------------------------------------------------------------
  145.   class << self
  146.     alias mbstimer_savegame save_game
  147.     alias mbstimer_loadgame load_game
  148.   end
  149.   #--------------------------------------------------------------------------
  150.   # * Salvamento do jogo
  151.   #--------------------------------------------------------------------------
  152.   def self.save_game(*a, &b)
  153.     mbstimer_savegame(*a, &b)
  154.     MBS::Timer.save(sprintf("Timer%03d.rvdata2", a[0]))
  155.   end
  156.   #--------------------------------------------------------------------------
  157.   # * Carregamento do jogo
  158.   #--------------------------------------------------------------------------
  159.   def self.load_game(*a, &b)
  160.     mbstimer_loadgame(*a, &b)
  161.     MBS::Timer.load(sprintf("Timer%03d.rvdata2", a[0]))
  162.   end
  163. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement