2017-04-18 32 views
-4

我想爲我的遊戲代碼空中包廂排序的系統列表值匹配。它會在某個特定的時間開始變黑,並且在某個特定的時間開始變亮(在黑暗之後)。我在尋找的是如何檢查服務器時間是否在這些時間段之間?檢查,如果一個時間範圍,C#

using System.Collections.Generic; 

namespace Game.GameEnvironment 
{ 
    class GameEnvironmentHandler 
    { 
     /// <summary> 
     /// List of dark times. 
     /// </summary> 
     private List<string> _darkTimes; 

     /// <summary> 
     /// List of light times. 
     /// </summary> 
     private List<string> _lightTimes; 

     public GameEnvironmentHandler() 
     { 
      // any time before 19:00 and after 08:00 is also light. 

      _darkTimes = new List<string> 
      { 
       "19:00", // getting dark 
       "19:30", 
       "20:00", 
       "20:30", 
       "21:00" // fully dark 
      }; 

      _lightTimes = new List<string> 
      { 
       "06:00", // getting light 
       "06:30", 
       "07:00", 
       "07:30", 
       "08:00" // fully light 
      }; 
     } 

     /// <summary> 
     /// Checks if its hit the first stage of dark times. 
     /// </summary> 
     /// <returns></returns> 
     public bool IsGettingDark() 
     { 

     } 

     /// <summary> 
     /// Checks if its hit the first stage of light times. 
     /// </summary> 
     /// <returns></returns> 
     public bool IsGettingLight() 
     { 

     } 

     /// <summary> 
     /// Gets what stage of getting dark or light its at (1-5) 
     /// </summary> 
     /// <returns></returns> 
     public int GetTimeStage() 
     { 
      return 0; 
     } 
    } 
} 
+1

使用'Timespan'作爲你的存儲機制 –

+0

Timespan需要手動輸入時間,你不能以編程方式從列表中加載它(不是我所知道的) –

+1

有一個'Timespan'列表.... –

回答

1

如果您可以提供的當前時間的小時:

int darkeningStart = 19; 
int darkeningEnd = 21; 

int brighteningStart = 6; 
int brighteningEnd = 8; 

[...]

public bool IsGettingLight(int hour) 
{ 
    return (hour >= brighteningStart && hour < brighteningEnd); 
} 

同樣的事情也適用於IsGettingDark()。

您可以實施階段,如果您也可以提供當前時間的一分鐘,但我不知道你的遊戲是如何設置爲。

相關問題