Day Night System

Day Night System


Introduction
This script is for RMXP. It creates a day night system. Although this is not the only day night system script, hopefully it can gives something special.:)


Features
• Day Night System
• With effect available from Light Circle Script
• Minute, hour, day, month and year are recorded accordingly

  1.  
  2. #=begin
  3. #==============================================================================
  4. #==============================================================================
  5. #==============================================================================
  6. #Day Night System updated(12-07-2011)
  7. #==============================================================================
  8. #==============================================================================
  9. #==============================================================================
  10.  
  11.  
  12.  
  13.  
  14. #=begin
  15. #==============================================================================
  16. #==============================================================================
  17. #==============================================================================
  18. #Light Circle Script v1.2b updated(29-07-2011)
  19. #==============================================================================
  20. #==============================================================================
  21. #==============================================================================
  22. class Scene_Save < Scene_File
  23.      
  24.   alias alias_write_save_data write_save_data
  25.      
  26.   def write_save_data(file)
  27.     alias_write_save_data(file)
  28.     Marshal.dump($sensitivity,file)
  29.     Marshal.dump($sensitivity_2,file)
  30.     Marshal.dump($light_switch,file)
  31.     Marshal.dump($flick,file)
  32.   end
  33. end
  34. #==============================================================================
  35. class Scene_Load < Scene_File
  36.   alias t_read_save_data read_save_data
  37.   def read_save_data(file)
  38.     t_read_save_data(file)
  39.     $sensitivity = Marshal.load(file)
  40.     $sensitivity_2 = Marshal.load(file)
  41.     $light_switch = Marshal.load(file)
  42.     $flick = Marshal.load(file)
  43.   end
  44. end
  45. #==============================================================================
  46. class Game_Player< Game_Character
  47.   def change_sensitivity(value,type)
  48.     case type
  49.     when "sensitivity"
  50.       $sensitivity=value
  51.     when "sensitivity_2"
  52.       $sensitivity_2[0]=value
  53.     end
  54.   end
  55. end
  56. #==============================================================================
  57. class Scene_Map
  58.   def spriteset
  59.     return @spriteset
  60.   end
  61. end
  62. #==============================================================================
  63. #==============================================================================
  64. #==============================================================================
  65. class Scene_Title
  66.  
  67.   alias alias_main main
  68. #--------------------------------------------------------------------------
  69. # * change initial sensitivity here
  70. #--------------------------------------------------------------------------  
  71.   def main
  72.     $light_switch=1
  73.     $sensitivity_2=[]
  74.     $sensitivity_2[0]=100
  75.     $flick=0
  76.     alias_main
  77.   end
  78. #--------------------------------------------------------------------------
  79. # * change initial sensitivity here
  80. #--------------------------------------------------------------------------  
  81. end
  82. #==============================================================================
  83. #==============================================================================
  84. #==============================================================================
  85. # ** Spriteset_Map
  86. #------------------------------------------------------------------------------
  87. #   This class brings together map screen sprites, tilemaps, etc.
  88. #   It's used within the Scene_Map class.
  89. #==============================================================================
  90. #==============================================================================
  91. #==============================================================================
  92. class Spriteset_Map
  93.   attr_accessor :tone
  94.   attr_accessor :light_limit
  95.   attr_accessor :x_limit
  96.   attr_accessor :y_limit
  97.    
  98.   alias alias_update update
  99.   alias alias_initialize initialize
  100.   alias alias_dispose dispose
  101. #--------------------------------------------------------------------------
  102. # * Object Initialization
  103. #--------------------------------------------------------------------------
  104.   def initialize
  105. #--------------------------------------------------------------------------
  106. # * change initial setting
  107. #--------------------------------------------------------------------------      
  108.     @tone=40
  109.     @light_limit=15
  110.     @x_limit=20
  111.     @y_limit=15
  112. #--------------------------------------------------------------------------
  113. # * change initial setting
  114. #--------------------------------------------------------------------------
  115.     @opacity=[]
  116.     @random=[]
  117.     @r_value=[]
  118.     @x_x=[]
  119.     @y_y=[]
  120.     @xx=[]
  121.     @yy=[]
  122.     @event_rx=[]
  123.     @event_ry=[]
  124.     @event_coor_x=[]
  125.     @event_coor_y=[]
  126.     @sensitivity=[]
  127.     @viewport=[]
  128.     @viewport[0]=[]
  129.     @viewport[1]=[]
  130.     @viewport[2]=[]
  131.     @viewport[3]=[]
  132.     @viewport[4]=[]
  133.     @d=6
  134.     @frame=3
  135.     @area_divide=14
  136.     @event_frame_x_highlimit=[]
  137.     @event_frame_y_highlimit=[]
  138.     @event_frame_x_lowlimit=[]
  139.     @event_frame_y_lowlimit=[]
  140.     get_event_data
  141. #------------------------------------
  142.     for @y in 0..15
  143.       for @x in 0..20
  144.         @viewport[0][@y*21+@x] = Viewport.new(@x*32-16, @y*32-16, 32, 32)
  145.         @viewport[0][@y*21+@x].tone = Tone.new(-255, -255, -255, 0)
  146.         @viewport[0][@y*21+@x].z = 199
  147.         @viewport[1][@y*21+@x] = Viewport.new(@x*32-16+@d, @y*32+@d-16, 32, 32)
  148.         @viewport[1][@y*21+@x].tone = Tone.new(-255, -255, -255, 0)
  149.         @viewport[1][@y*21+@x].z = 199
  150.         @viewport[2][@y*21+@x] = Viewport.new(@x*32-16+@d*2, @y*32+@d*2-16, 32, 32)
  151.         @viewport[2][@y*21+@x].tone = Tone.new(-255, -255, -255, 0)
  152.         @viewport[2][@y*21+@x].z = 199
  153.         @viewport[3][@y*21+@x] = Viewport.new(@x*32-16-@d, @y*32-@d-16, 32, 32)
  154.         @viewport[3][@y*21+@x].tone = Tone.new(-255, -255, -255, 0)
  155.         @viewport[3][@y*21+@x].z = 199
  156.         @viewport[4][@y*21+@x] = Viewport.new(@x*32-16-@d*2, @y*32-@d*2-16, 32, 32)
  157.         @viewport[4][@y*21+@x].tone = Tone.new(-255, -255, -255, 0)
  158.         @viewport[4][@y*21+@x].z = 199
  159.         @opacity[@y*21+@x]=-@tone
  160.       end
  161.     end
  162.     alias_initialize
  163.  
  164.   end
  165. #--------------------------------------------------------------------------
  166. # * Object Initialization
  167. #--------------------------------------------------------------------------  
  168. #--------------------------------------------------------------------------
  169. # * Dispose
  170. #--------------------------------------------------------------------------
  171.   def dispose
  172.     alias_dispose
  173.     for @y in 0..15
  174.       for @x in 0..20
  175.         for i in 0..4
  176.           @viewport[i][@y*21+@x].dispose
  177.         end
  178.       end
  179.     end
  180.  
  181.   end
  182. #--------------------------------------------------------------------------
  183. # * Dispose
  184. #--------------------------------------------------------------------------  
  185. #--------------------------------------------------------------------------
  186. # * Event Data Setup(get information from events)
  187. #--------------------------------------------------------------------------
  188.   def get_event_data
  189.     for event in $game_map.events.values
  190.       if event.list && event.list[0].code == 108
  191.         i=1
  192.         loop do
  193.           if event.list[0].parameters.to_s == i.to_s
  194.             @xx[i]=event.x*32
  195.             @yy[i]=event.y*32
  196.             $sensitivity_2[i]=event.list[1].parameters.to_s .to_i
  197.             @r_value[i]=rand(event.list[2].parameters.to_s .to_i+1)
  198.           end
  199.           break if i== @light_limit
  200.           i+=1
  201.         end
  202.          
  203.       end
  204.     end
  205.   end
  206.  
  207. #--------------------------------------------------------------------------
  208. # * Event Data Setup(get information from events)
  209. #--------------------------------------------------------------------------
  210. #--------------------------------------------------------------------------
  211. # * Get Frame Rate
  212. #--------------------------------------------------------------------------
  213. def get_frame_rate
  214.    t = Time.now
  215. @compare1=t.sec if @compare1==nil
  216.   if @compare1 != t.sec
  217.     @compare1= t.sec
  218.     count_on=true
  219.     end
  220.  
  221.   @count_rate=0 if @count_rate==nil
  222.   if count_on==true
  223.     count_on=false
  224.     frame_rate,@count_rate=@count_rate,0
  225.   else
  226.     @count_rate+=1
  227.   end
  228.  
  229.   return frame_rate
  230.   end
  231. #--------------------------------------------------------------------------
  232. # * Get Frame Rate
  233. #--------------------------------------------------------------------------
  234. #--------------------------------------------------------------------------
  235. # * Frame Update (perform algorithms that calculate opacity of each coordinations)
  236. #--------------------------------------------------------------------------
  237.   def update
  238.    
  239.     frame_rate=get_frame_rate
  240.     @frame_rate==nil ? @frame_rate=0:nil
  241.     @frame_rate=frame_rate == nil ?  @frame_rate: frame_rate
  242. if @frame_rate>37
  243.       @frame=2
  244.       @area_divide=12
  245.     elsif @frame_rate<35
  246.       @frame=4
  247.       @area_divide=16
  248.     else
  249.          @frame=3
  250.       @area_divide=14
  251. end
  252.    # $frame=@frame_rate
  253.     @ccc=0 if @ccc==nil
  254.     @ccc2=0 if @ccc2==nil
  255.     if @ccc2%@frame==0
  256.    if @ccc==0  
  257.       if $light_switch==1
  258.         #@random[0]=rand($flick+1)
  259.  #------------------
  260.  get_event_data
  261. #-------------------  
  262.      #   i=1
  263.        # loop do
  264.          # break if @r_value[i]==nil
  265.           #@random[i]=rand(@r_value[i]+1)
  266.           #i+=1
  267.         #end
  268. #-------------------
  269.         @player_rx=($game_player.real_x-$game_map.display_x)/4
  270.         @player_ry=($game_player.real_y-$game_map.display_y)/4
  271.         @player_coor_x= (@player_rx+16)/32
  272.         @player_coor_y= (@player_ry+16)/32
  273.         @sensitivity[0]=$sensitivity_2[0]+rand($flick+1)
  274.       i=1
  275.       loop do
  276.           break if @xx[i]==nil
  277.           @event_rx[i]=(@xx[i]-$game_map.display_x/4)
  278.           @event_ry[i]=(@yy[i]-$game_map.display_y/4)
  279.           @event_coor_x[i]=(@event_rx[i]+16)/32
  280.           @event_coor_y[i]=(@event_ry[i]+16)/32
  281.           @sensitivity[i]=[$sensitivity_2[i]+@r_value[i],150].min
  282.           i+=1
  283.         end
  284. #-------------------
  285. @event_seq=1
  286.  
  287. for @y in 0..15
  288.   for @x in 0..20
  289.     @opacity[@y*21+@x] =@sensitivity[0]/((@x-@player_coor_x)**2+(@y-@player_coor_y)**2+5)
  290.   end
  291.   end
  292.  
  293.  
  294.  
  295.  
  296. loop do
  297.   break if @xx[@event_seq]==nil  
  298.   if (@player_coor_x-@event_coor_x[@event_seq]).abs<@x_limit && (@player_coor_y-@event_coor_y[@event_seq]).abs<@y_limit
  299. #++++++  
  300.  
  301.   @area_limit=(@sensitivity[@event_seq]/@area_divide).to_i
  302.  
  303. @event_frame_x_highlimit[@event_seq]=[@event_coor_x[@event_seq]+@area_limit,20].min
  304. @event_frame_x_lowlimit[@event_seq]=[@event_coor_x[@event_seq]-@area_limit,0].max
  305.  
  306.  
  307. for @x in @event_frame_x_lowlimit[@event_seq]..@event_frame_x_highlimit[@event_seq]
  308.   @event_frame_y_lowlimit[@event_seq]=(@event_coor_y[@event_seq]-Math.sqrt(@area_limit**2-(@event_coor_x[@event_seq]-@x)**2)).to_i
  309.  @event_frame_y_highlimit[@event_seq]=[2*@event_coor_y[@event_seq]-@event_frame_y_lowlimit[@event_seq],15].min
  310.  @event_frame_y_lowlimit[@event_seq]=[0,@event_frame_y_lowlimit[@event_seq]].max
  311.  for @y in @event_frame_y_lowlimit[@event_seq]..@event_frame_y_highlimit[@event_seq]
  312.    @opacity[@y*21+@x] +=@sensitivity[@event_seq]/((@x-@event_coor_x[@event_seq])**2+(@y-@event_coor_y[@event_seq])**2+5)
  313.    end
  314.    end
  315.  
  316.   end
  317.  
  318. #++++++  
  319.   @event_seq+=1
  320. end
  321.  
  322. #++++++
  323. for @y in 0..15
  324.   for @x in 0..20
  325.     a=[[@opacity[@y*21+@x]-@tone,0].min,-@tone].max
  326.     @viewport[0][@y*21+@x].tone=Tone.new(a, a, a)
  327.   end
  328.   end
  329. #++++++
  330. #--------------------
  331.         @on=nil if @on != nil
  332.       else
  333. #-------------------        
  334.         if @on==nil
  335.           for @y in 0..15
  336.             for @x in 0..20
  337.               a=Tone.new(0,0,0)
  338.               for i in 0..4
  339.                 @viewport[i][@y*21+@x].tone =a
  340.               end
  341.             end
  342.           end
  343.           @on=true
  344.         end
  345. #-------------------        
  346.       end
  347.     else
  348.               for @y in 0..15
  349.           @x=0
  350.           loop do
  351.             id=@y*21+@x
  352.               @viewport[@ccc][id].tone=@viewport[@ccc-1][id].tone
  353.             break if @x==20
  354.             @x+=1
  355.           end
  356.         end
  357. #-------------------
  358. end
  359. @ccc+=1
  360.     @ccc=0 if @ccc==5
  361.     end
  362. @ccc2+=1
  363.     @ccc2=0 if @ccc2==@frame
  364.  
  365.  
  366.     alias_update
  367.   end
  368. #--------------------------------------------------------------------------
  369. # * Frame Update (perform algorithms that calculate opacity of each coordinations)
  370. #--------------------------------------------------------------------------
  371. end
  372. #==============================================================================
  373. #==============================================================================
  374. #==============================================================================
  375. #=end
  376.  
  377.  
  378.  
  379. #==============================================================================
  380. # ■ Window_PlayTime
  381. #------------------------------------------------------------------------------
  382. #  菜单画面显示游戏时间的窗口。
  383. #==============================================================================
  384.  
  385. class Window_PlayTime < Window_Base
  386.   #--------------------------------------------------------------------------
  387.   # ● 初始化对像
  388.   #--------------------------------------------------------------------------
  389.   def initialize
  390.     super(0, 0, 160, 96)
  391.     self.contents = Bitmap.new(width - 32, height - 32)
  392.     refresh
  393.   end
  394.   #--------------------------------------------------------------------------
  395.   # ● 刷新
  396.   #--------------------------------------------------------------------------
  397.   def refresh
  398.     self.contents.clear
  399.     self.contents.font.size = 15
  400.     self.contents.font.color = system_color
  401.     self.contents.draw_text(4, 0, 120, 32, "Game Time")
  402.     @total_sec = Graphics.frame_count / Graphics.frame_rate
  403.     year = $day_night_system.year
  404.     month = $day_night_system.month
  405.     day = $day_night_system.day
  406.     hour = $day_night_system.hour
  407.     min = $day_night_system.min
  408.     text = sprintf("Time: %02d:%02d", hour, min)
  409.     self.contents.font.color = normal_color
  410.     self.contents.draw_text(-24, 16, 120, 32, text, 2)
  411.      text = sprintf("Date: %02d-%02d-%04d",day,month,year)
  412.     self.contents.font.color = normal_color
  413.     self.contents.draw_text(4, 32, 120, 32, text, 2)
  414.   end
  415.   #--------------------------------------------------------------------------
  416.   # ● 刷新画面
  417.   #--------------------------------------------------------------------------
  418.   def update
  419.     super
  420.     if Graphics.frame_count / Graphics.frame_rate != @total_sec
  421.       refresh
  422.     end
  423.   end
  424. end
  425.  
  426.  
  427. #==============================================================================
  428. #==============================================================================
  429. #==============================================================================
  430.  
  431.  
  432.  
  433.  
  434.  
  435. class Scene_Save < Scene_File
  436.      
  437.   alias alias_write_save_data_t write_save_data
  438.      
  439.   def write_save_data(file)
  440.     alias_write_save_data_t(file)
  441.     Marshal.dump($day_night_system,file)
  442.  
  443.   end
  444. end
  445. #==============================================================================
  446. class Scene_Load < Scene_File
  447.   alias t_read_save_data_t read_save_data
  448.   def read_save_data(file)
  449.     t_read_save_data_t(file)
  450.     $day_night_system = Marshal.load(file)
  451.   end
  452. end
  453. #==============================================================================
  454.  
  455.  
  456. class Scene_Title
  457.  
  458.   alias alias_main_t main
  459. #--------------------------------------------------------------------------
  460. # * change initial sensitivity here
  461. #--------------------------------------------------------------------------  
  462.   def main
  463.     $day_night_system=Day_Night_System.new(0,0,0,0,0)
  464.     alias_main_t
  465.   end
  466.   end
  467. #==============================================================================
  468.  
  469. class Scene_Map
  470.  
  471.   alias alias_update_t update
  472. #--------------------------------------------------------------------------
  473. # * change initial sensitivity here
  474. #--------------------------------------------------------------------------  
  475.   def update
  476.     $day_night_system.update
  477.     alias_update_t
  478.   end
  479.  
  480.  
  481. end
  482. #==============================================================================
  483. #==============================================================================
  484. #==============================================================================
  485. #==============================================================================
  486. #Day Night Script updated(15-07-2011)
  487. #==============================================================================
  488. #==============================================================================
  489. #==============================================================================
  490. class Day_Night_System
  491.  
  492. attr_accessor :year
  493. attr_accessor :month
  494. attr_accessor :day
  495. attr_accessor :hour
  496. attr_accessor :min
  497. attr_accessor :count
  498. attr_accessor :time_factor
  499. attr_accessor :system_mode
  500.  
  501.  
  502. #--------------------------------------------------------------------------
  503. # * Initialize
  504. #--------------------------------------------------------------------------
  505.  
  506.   def initialize(year,month,day,hour,min)
  507.     @year=year
  508.     @month=month
  509.     @day=day
  510.     @hour=hour
  511.     @min=min
  512.     @count=0
  513.     @time_factor=1
  514.     @system_mode=0
  515.     @time_tone={
  516.     12=>9,#12 o clock afternoon>>9 represents the darkness of environment
  517.     11=>11,#11 o clock morning
  518.     10=>13,#10 o clock morning
  519.     9=>15,#9 o clock morning
  520.     8=>17,#8 o clock morning
  521.     7=>21,#7 o clock morning
  522.     6=>27,#6 o clock morning
  523.     5=>30,#5 o clock morning
  524.     4=>34,#4 o clock midnight
  525.     3=>38,#3 o clock midnight
  526.     2=>38,#2 o clock midnight
  527.     1=>39,#1 o clock midnight
  528.     0=>39,#12 o clock midnight
  529.     13=>11,#1 o clock afternoon
  530.     14=>13,#2 o clock afternoon
  531.     15=>15,#3 o clock afternoon
  532.     16=>17,#4 o clock afternoon
  533.     17=>21,#5 o clock evening
  534.     18=>27,#6 o clock evening
  535.     19=>30,#7 o clock evening
  536.     20=>34,#8 o clock night
  537.     21=>38,#9 o clock night
  538.     22=>38,#10 o clock night
  539.     23=>39,#11 o clock night
  540.     98=>9,#day sight
  541.     99=>45#dark sight
  542.     }
  543.   end
  544.  
  545.  #--------------------------------------------------------------------------
  546. # * Initialize
  547. #--------------------------------------------------------------------------
  548.  
  549.  #--------------------------------------------------------------------------
  550. # * Initialize
  551. #--------------------------------------------------------------------------
  552.  
  553.   #--------------------------------------------------------------------------
  554. # * Update
  555. #--------------------------------------------------------------------------  
  556.  
  557. def update
  558.  
  559.   if @count<=40
  560.   @count+=@time_factor
  561. else
  562.   @min+=1
  563.   if @min>=60
  564.   @hour+=1
  565.    if @hour>=24
  566.   @day+=1
  567.      if @day>= day_number(@month,@year)
  568.   @month+=1
  569.      if @month>=12
  570.   @year+=1
  571.   @month=0
  572. end
  573.   @day=0
  574. end
  575.   @hour=0
  576. end
  577.   @min=0
  578. end
  579.   @count=0
  580.   end
  581.    #--------------------------------------------------------------------------
  582. # * Update
  583. #--------------------------------------------------------------------------  
  584.  
  585.  
  586. #-------------------
  587. #update light
  588. #-------------------
  589. day_night_cycle
  590. #--------------------
  591. end
  592.   #--------------------------------------------------------------------------
  593. # * Update
  594. #--------------------------------------------------------------------------  
  595.   #--------------------------------------------------------------------------
  596. # * Month
  597. #--------------------------------------------------------------------------  
  598. def day_number(month,year)
  599.   if month == 1||3||5||7||8||10||12
  600.     return 31
  601.   elsif month==2
  602.     #----------------
  603.     if year%4==0
  604.     return 29
  605.   else
  606.     return 28
  607.   end
  608.     #----------------
  609.     else
  610.     return 30
  611.   end
  612.  
  613.  
  614.   end
  615.   #--------------------------------------------------------------------------
  616. # * Month
  617. #--------------------------------------------------------------------------  
  618.     #--------------------------------------------------------------------------
  619. # * Day Night Cycle
  620. #--------------------------------------------------------------------------  
  621.  
  622. def day_night_cycle
  623. if $scene.is_a?(Scene_Map)
  624.  
  625.   if @system_mode==0
  626. $scene.spriteset.tone=@time_tone[@hour]
  627.                     elsif @system_mode==1
  628.                       $scene.spriteset.tone=@time_tone[98]
  629.                     elsif @system_mode==2
  630.                       $scene.spriteset.tone=@time_tone[99]
  631.                       end
  632. end
  633.  end
  634.   #--------------------------------------------------------------------------
  635. # * Day Night Cycle
  636. #--------------------------------------------------------------------------  
  637. end
  638.  
Expand to see the code.


Day Night System
The time system will start at 0 minute, 0 hour, 0 day, 0 month and 0 year. The demo shown is time fasten already, but can be changed as you like. Change the parameter $day_night_system.time_factor; the higher value of it, the faster time goes.


Light Circle Script
put 1st comment of number on each event to represent their id. Then, put 2nd comment of number to represent the strength of light radius of each event. Next, put the 3rd comment of number to represent the power of flickering.(Both the 3 comments can be put as what number as you want, no more limitation^^ but the 1st comment must be put according to the sequence, which mean must start with value of '1', then '2', then '3'... if you do not wish the event to flick, just set the 3rd comment value as '0' or don't create 3rd comment)

@tone represents how dark is the screen in the game, the higher value of @tone, the darker the screen.@light_limit represents the number of lights are allowed in the screen. for example, if @light_limit equals 5, the maximum light events in the screen is 5. @x_limit is where the light source being switched on or off. If the distance of x between hero and light source is more than @x_limit, the light source will be switched off. The same thing goes with @y_limit.

To set radius of single event or only hero, use $sensitivity_2[A]=value where A represents each event's id. If A equals 0, it represents hero. When $light_switch is set to 0, the script is being ignored. When $light_switch is set to 1, the script is in used. To let hero's light flick, set $flick to a value. The bigger the value $flick is, the stronger the flickering effect.

To change the darkest tone in the game, call $scene.spriteset.tone=A, where A is a number. The bigger the number, the darker the tone is.

Place this script just above Main.^^





Note: To avoid confusion, I combined the day night system script with light circle script together, so if the light circle script existed before this script, delete it as the script is already included.



Image
The image is more or less similar to what shown in light circle script post.

Demo download link(Day Night System)
http://www.2shared.com/file/Weous3q0/Day_Night_System.html

FAQ



not sure to SDK



Credits and Thanks


Please give comments for this.TQ!!!



no need to be credited, but you are welcome to give me credits

Original thread: https://www.hbgames.org/forums/viewtopic.php?t=75037