TheoAllen

Theo - Core Time System

Aug 28th, 2013
562
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 15.60 KB | None | 0 0
  1. # =============================================================================
  2. # TheoAllen Time System - Core Script
  3. # Version : 1.1
  4. # Contact : www.rpgmakerid.com (or) http://theolized.blogspot.com
  5. # (This script documentation is written in informal indonesian language)
  6. # =============================================================================
  7. ($imported ||= {})[:Theo_CoreTime] = true
  8. # =============================================================================
  9. # CHANGE LOGS:
  10. # -----------------------------------------------------------------------------
  11. # 2013.08.29 - Change update variables workflow + added week
  12. # 2013.08.27 - Finished script
  13. # =============================================================================
  14. =begin
  15.   =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  16.   Perkenalan :
  17.   ---------------------------------------------------------------------------
  18.   Script ini ngebikin game kamu punya time. Ini adalah core script dari
  19.   fitur-fitur tambahan lainnya ntar
  20.  
  21.   =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  22.   Cara penggunaan :
  23.   ---------------------------------------------------------------------------
  24.   Pasang dibawah material namun diatas material
  25.   Atur jalan timenya dengan style kamu di konfigurasi
  26.  
  27.   Angka menit, jam, dan hari gw masukin ke variable ID. Pastikan kamu gunain
  28.   itu untuk bikin conditional branch.
  29.  
  30.   Untuk mempercepat dan memperlambat waktu jangan gunain control variable.
  31.   Gunain script call yang udah wa jelasin dibawah
  32.  
  33.   Untuk munculin debug window (yang nampilin waktu) gunain ALT
  34.  
  35.   =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  36.   Notetag :
  37.   ---------------------------------------------------------------------------
  38.  
  39.   <no time tone>
  40.   ^
  41.   Pasang notetag ini pada note map untuk peta-peta yang ngga kepengaruh ama
  42.   tone waktu. Semisal di gua
  43.  
  44.   =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  45.   Script call :
  46.   ---------------------------------------------------------------------------
  47.   advanded(menit)
  48.   advanded(menit,jam)
  49.   advanded(menit,jam,hari)
  50.   ^
  51.   Untuk mempercepat waktu. Parameter yang wajib diisi adalah menit. Sedangkan
  52.   jam dan hari bisa diabaikan
  53.  
  54.   ---------------------------------
  55.   roll_back(menit)
  56.   roll_back(menit,jam)
  57.   roll_back(menit,jam,hari)
  58.   ^
  59.   Untuk ngembaliin waktu. Parameter yang wajib diisi sama kek forward
  60.  
  61.   ---------------------------------
  62.   set_time(menit)
  63.   set_time(menit,jam)
  64.   set_time(menit,jam,hari)
  65.   ^
  66.   Untuk ngeset waktu secara langsung. Semisal kamu dalam inn, kamu pengen
  67.   bikin si player bangun jam 5:30, maka kamu bisa menggunakan script call
  68.   beruntun seperti ini
  69.  
  70.   advanded(0,0,1)
  71.   set_time(30,5)
  72.  
  73.   ---------------------------------
  74.   time.stop
  75.   time.play
  76.   ^
  77.   Untuk menghentikan waktu ato menjalankan waktu
  78.  
  79.   ---------------------------------
  80.   time.disable_tone = false
  81.   time.disable_tone = true
  82.   ^
  83.   Untuk ngedisable time tone semisal kamu make Evented Tint Screen
  84.  
  85.   =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  86.   Terms of Use :
  87.   ---------------------------------------------------------------------------
  88.   Credit gw, TheoAllen. Kalo semisal u bisa ngedit2 script gw trus jadi lebih
  89.   keren, terserah. Ane bebasin. Asal ngga ngeklaim aja. Kalo semisal mau
  90.   dipake buat komersil, jangan lupa, gw dibagi gratisannya.  
  91.  
  92.   =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  93.   Author's Notes :
  94.   ---------------------------------------------------------------------------
  95.   Time system yang gw buat disini berdasarkan time system Open-World WRPG
  96.   macem Skyrim, Fallout, dan Kingdom of Amalur. Dimana time system mereka ngga
  97.   butuh system penanggalan dan nama hari. Tapi mereka mencatat sudah berapa
  98.   hari kah player berada dalam waktu di game tersebut
  99.  
  100.   Personally, gw ngga demen ama game HM like. Jadi time system ini emang gw
  101.   bikin untuk kelengkapan fitur game dengan tipe open-world RPG. Dengan kata
  102.   lain ini gw bikin untuk kebutuhan gw sendiri
  103.  
  104.   Kalo semisal kamu mengharapkan ada system penanggalan seperti nama hari,
  105.   bulan, dan bahkan musim, edit aja sendiri ato berharap aja gw mau bikinin
  106.   suatu saat.
  107.  
  108. =end
  109. # =============================================================================
  110. # Konfigurasi :
  111. # =============================================================================
  112. module THEO
  113.   module Time
  114.   # --------------------------------------------------------------------------
  115.   # Setting Umum
  116.   # --------------------------------------------------------------------------
  117.     StartMinute = 10  # Permulaan menit waktu new game
  118.     StartHour   = 8   # Permulaan jam waktu new game
  119.     StartDay    = 0   # Permulaan hari waktu new game
  120.  
  121.     Minute_ID = 10 # ID untuk variable menit
  122.     Hour_ID   = 11 # ID untuk variable jam
  123.     Day_ID    = 12 # ID untuk variable hari
  124.     Week_ID   = 13 # ID untuk variable minggu
  125.    
  126.   # Note :
  127.   # - Nilai variable diatas otomatis udah diupdate. Kamu tinggal make buat
  128.   #   conditional branch
  129.   # - Perhitungan minggu ngga ada limiternya kek jam max 24 dan hari max 30.
  130.   #   Jadi semisal udah jalan 70 hari ya 7 minggu tanpa direset
  131.  
  132.   # --------------------------------------------------------------------------
  133.   # Skala waktu
  134.   # --------------------------------------------------------------------------
  135.   # Settingan menit dalam detik realtime. Semisal kamu nulisnya 5, maka 5
  136.   # detik dalam realtime sama dengan satu menit dalam game
  137.     Default_MinuteScale = 0.1
  138.  
  139.   # --------------------------------------------------------------------------
  140.   # Settingan ini adalah skala menit kustom berdasar tileset ID. Kamu bisa
  141.   # membikin kalo player ada di worldmap (Tileset ID = 1) maka waktu dipercepat.
  142.   # Tapi kalo dalam indoor diberhentiin atau semacemnya. Jika tileset ID ngga
  143.   # dicatet disini otomatis akan make default minute scale diatas.
  144.  
  145.     Custom_MinuteScale  = { # <-- Jangan disentuh!
  146.     # Tileset ID => Skala menit,
  147.                1 => 0.1,
  148.                
  149.     # Tambahin sendiri          
  150.    
  151.     } # <-- Jangan disentuh!
  152.   # --------------------------------------------------------------------------
  153.   # Tone Waktu
  154.   # --------------------------------------------------------------------------
  155.   # List tone yg tersedia. Sama kek tint screen
  156.   # Time  = Tone.new(red,green,blue,gray)
  157.     Dawn  = Tone.new( 30,    0, -34,   0)  # Pagi
  158.     Noon  = Tone.new(  0,    0,   0,   0)  # Siang
  159.     Dusk  = Tone.new( 68,  -34, -34,   0)  # Sore
  160.     Nite  = Tone.new(-68,  -68,   0,  68)  # Malem
  161.  
  162.   Tones = { # <-- Jangan disentuh!
  163.   # List tone yg dipake di jam2 tertentu. Dimulai dari min dan diakhiri ampe
  164.   # max. Kalo kamu ngisinya [0,4] => Nite, maka itu berarti selama jam 0 - 4
  165.   # tone yg dipake adalah Nite.
  166.  
  167.   # [min,max] => time tone
  168.     [  0,  4] => Nite,
  169.     [  5,  7] => Dawn,
  170.     [  8, 14] => Noon,
  171.     [ 15, 18] => Dusk,
  172.     [ 19, 24] => Nite,
  173.    
  174.     } # <-- Jangan disentuh!
  175.   # --------------------------------------------------------------------------
  176.   # Lain-lain
  177.   # --------------------------------------------------------------------------
  178.   # Bagi kalian yg pernah maen harvest moon. Dalam game HM hari ngga akan
  179.   # ganti kalo ngga tidur. Setting ini untuk menyetting sampe manakah nominal
  180.   # maksimal jam. Semisal kamu tulis 22, maka kalo jam udah nyentuh 22 ngga
  181.   # akan diapdet lagi. Tulis 0 ato lebih dari 24 kalo pengen ngedisable
  182.     MaxHour = 0
  183.    
  184.   # Game pasti ada cutscenenya. Semisal omong-omongan ama orang atau scene yang
  185.   # lain. Settingan ini untuk stop waktu sementara kalo ada cutscene dan
  186.   # dijalanin lagi kalo scene udah beres
  187.     DisableAtScene = true
  188.    
  189.   end
  190. end
  191. # =============================================================================
  192. # Akhir dari konfig :
  193. # =============================================================================
  194. class Game_System
  195.   attr_reader :time
  196.  
  197.   alias theo_coretime_init initialize
  198.   def initialize
  199.     theo_coretime_init
  200.     @time = TimeSystem.new
  201.   end
  202.  
  203. end
  204.  
  205. class Game_Interpreter
  206.  
  207.   def advanced(minute, hour = 0, day = 0)
  208.     time.forward(minute)
  209.     time.forward_hour(hour)
  210.     time.forward_day(day)
  211.   end
  212.  
  213.   def roll_back(minute, hour = 0, day = 0)
  214.     time.backward(minute)
  215.     time.backward_hour(hour)
  216.     time.backward_day(day)
  217.   end
  218.  
  219.   def set_time(minute, hour = 0, day = 0)
  220.     time.forward(minute - time.minute)
  221.     time.forward_hour(hour - time.hour)
  222.     time.forward_day(day - time.day)
  223.   end
  224.  
  225.   def time
  226.     $game_system.time
  227.   end
  228.  
  229. end
  230.  
  231. class << DataManager
  232.  
  233.   alias theo_coretime_create_obj create_game_objects
  234.   def create_game_objects
  235.     theo_coretime_create_obj
  236.     $game_system.time.assign_variables
  237.   end
  238.  
  239. end
  240.  
  241. class TimeSystem
  242.   # --------------------------------------------------------------------------
  243.   # Public variables
  244.   # --------------------------------------------------------------------------
  245.   attr_accessor :disable_tone
  246.   attr_reader :tone
  247.   # --------------------------------------------------------------------------
  248.   # Module included
  249.   # --------------------------------------------------------------------------
  250.   include THEO::Time
  251.   # --------------------------------------------------------------------------
  252.   # Initializer methods
  253.   # --------------------------------------------------------------------------
  254.   def initialize
  255.     init_count
  256.     init_tone
  257.     @stop = false
  258.   end
  259.  
  260.   def init_count
  261.     @count = 0
  262.     @minute = 0
  263.   end
  264.  
  265.   def init_tone
  266.     @tone = check_tone.clone
  267.     @tone_target = @current_tone = check_tone
  268.     @tone_duration = 0
  269.     @disable_tone = false
  270.   end
  271.   # --------------------------------------------------------------------------
  272.   # General methods
  273.   # --------------------------------------------------------------------------
  274.   def start_tone_change(tone, duration)
  275.     @tone_target = tone.clone
  276.     @tone_duration = duration
  277.     @tone = @tone_target.clone if @tone_duration == 0
  278.   end
  279.  
  280.   def check_tone
  281.     Tones.each do |key, tone|
  282.       next unless check_hour(key[0],key[1])
  283.       return tone
  284.     end
  285.     return Tone.new
  286.   end
  287.  
  288.   def check_hour(min, max)
  289.     return hour >= min && hour <= max
  290.   end
  291.  
  292.   def play
  293.     @stop = false
  294.   end
  295.  
  296.   def stop
  297.     @stop = true
  298.   end
  299.  
  300.   def stopped?
  301.     @stop
  302.   end
  303.  
  304.   def time_stopped?
  305.     ($game_map.interpreter.running? && DisableAtScene) || stopped? ||
  306.     (MaxHour > 0 ? hour >= MaxHour : false)
  307.   end
  308.  
  309.   def var
  310.     $game_variables
  311.   end
  312.  
  313.   def assign_variables
  314.     var[Minute_ID] = minute
  315.     var[Hour_ID] = hour
  316.     var[Day_ID] = day
  317.     var[Week_ID] = week
  318.   end
  319.   # --------------------------------------------------------------------------
  320.   # Frame update methods
  321.   # --------------------------------------------------------------------------
  322.   def update
  323.     update_count
  324.     update_tone
  325.   end
  326.  
  327.   def update_count
  328.     return if time_stopped?
  329.     @count += calculate_count
  330.     update_time if @count >= 60
  331.   end
  332.  
  333.   def update_time
  334.     @minute += 1
  335.     @count = 0
  336.     on_change
  337.   end
  338.  
  339.   def on_change
  340.     update_variables
  341.   end
  342.  
  343.   def update_tone
  344.     @current_tone = check_tone
  345.     if @tone_target != @current_tone
  346.       @tone_target = @current_tone
  347.       start_tone_change(@tone_target, 60)
  348.     end
  349.     if @tone_duration > 0
  350.       d = @tone_duration
  351.       @tone.red = (@tone.red * (d - 1) + @tone_target.red) / d
  352.       @tone.green = (@tone.green * (d - 1) + @tone_target.green) / d
  353.       @tone.blue = (@tone.blue * (d - 1) + @tone_target.blue) / d
  354.       @tone.gray = (@tone.gray * (d - 1) + @tone_target.gray) / d
  355.       @tone_duration -= 1
  356.     end
  357.   end
  358.  
  359.   def calculate_count
  360.     rate = Custom_MinuteScale[$game_map.tileset.id]
  361.     return 1.0 / (rate ? rate : Default_MinuteScale) rescue 0
  362.   end
  363.  
  364.   def update_variables
  365.     if var[Minute_ID] != minute
  366.       on_minute_change
  367.     end
  368.     if var[Hour_ID] != hour
  369.       on_hour_change
  370.     end
  371.     if var[Day_ID] != day
  372.       on_day_change
  373.     end
  374.     if var[Week_ID] != week
  375.       on_week_change
  376.     end
  377.   end
  378.   # --------------------------------------------------------------------------
  379.   # On Changes Methods
  380.   # --------------------------------------------------------------------------
  381.   def on_minute_change
  382.     var[Minute_ID] = minute
  383.   end
  384.  
  385.   def on_hour_change
  386.     var[Hour_ID] = hour
  387.   end
  388.  
  389.   def on_day_change
  390.     var[Day_ID] = day
  391.   end
  392.  
  393.   def on_week_change
  394.     var[Week_ID] = week
  395.   end
  396.   # --------------------------------------------------------------------------
  397.   # Advancing and roll back methods
  398.   # --------------------------------------------------------------------------
  399.   def forward(minute)
  400.     @minute += minute
  401.     on_change
  402.   end
  403.  
  404.   def forward_hour(hour)
  405.     forward(hour * 60)
  406.   end
  407.  
  408.   def forward_day(day)
  409.     forward_hour(day * 24)
  410.   end
  411.  
  412.   def backward(minute)
  413.     forward(-minute)
  414.   end
  415.  
  416.   def backward_hour(hour)
  417.     backward(hour * 60)
  418.   end
  419.  
  420.   def backward_day(day)
  421.     backward_hour(day * 24)
  422.   end
  423.   # --------------------------------------------------------------------------
  424.   # Absolut time (without additional time)
  425.   # --------------------------------------------------------------------------
  426.   def abs_minute
  427.     @minute
  428.   end
  429.  
  430.   def abs_hour
  431.     abs_minute / 60
  432.   end
  433.  
  434.   def abs_day
  435.     abs_hour / 24
  436.   end
  437.   # --------------------------------------------------------------------------
  438.   # Total time (with additional time)
  439.   # --------------------------------------------------------------------------
  440.   def total_minute
  441.     abs_minute + StartMinute
  442.   end
  443.  
  444.   def total_hour
  445.     (total_minute / 60) + StartHour
  446.   end
  447.  
  448.   def total_day
  449.     (total_hour / 24) + StartDay
  450.   end
  451.  
  452.   def week
  453.     total_day / 7
  454.   end
  455.   # --------------------------------------------------------------------------
  456.   # Real time (with limiter)
  457.   # --------------------------------------------------------------------------
  458.   def minute
  459.     total_minute % 60
  460.   end
  461.  
  462.   def hour
  463.     total_hour % 24
  464.   end
  465.  
  466.   def day
  467.     (total_day % 30) + 1
  468.   end
  469.  
  470. end
  471.  
  472. class Game_Screen
  473.  
  474.   alias theo_coretime_tone tone
  475.   def tone
  476.     if $game_map.no_time_tone? || $game_system.time.disable_tone
  477.       return theo_coretime_tone
  478.     else
  479.       return $game_system.time.tone
  480.     end
  481.   end
  482.  
  483. end
  484.  
  485. class Game_Map
  486.  
  487.   def no_time_tone?
  488.     @map.note.include?("<no time tone>")
  489.   end
  490.  
  491. end
  492. # ----------------------------------------------------------------------------
  493. # Window Time just for debug if necessary
  494. # ----------------------------------------------------------------------------
  495. class Window_TestTime < Window_Base
  496.  
  497.   def initialize
  498.     super(0,0,130,fitting_height(1))
  499.     self.visible = false
  500.     refresh
  501.   end
  502.  
  503.   def time
  504.     $game_system.time
  505.   end
  506.  
  507.   def refresh
  508.     contents.clear
  509.     @minute = time.total_minute
  510.     text = sprintf("%02d:%02d",time.hour,time.minute)
  511.     draw_text(0,0,contents.width,line_height,text,1)
  512.   end
  513.  
  514.   def update
  515.     super
  516.     if Input.trigger?(:ALT) && $TEST
  517.       refresh
  518.       self.visible = !visible
  519.     end
  520.     refresh if visible && @minute != $game_system.time.total_minute
  521.   end
  522.  
  523. end
  524.  
  525. class Scene_Map < Scene_Base
  526.  
  527.   alias theo_coretime_start start
  528.   def start
  529.     theo_coretime_start
  530.     @dns = Window_TestTime.new
  531.   end
  532.  
  533.   alias theo_coretime_update update
  534.   def update
  535.     theo_coretime_update
  536.     $game_system.time.update
  537.   end
  538.  
  539. end
Advertisement
Add Comment
Please, Sign In to add comment