项目作者: seanohue

项目描述 :
A way of handling time and time-related state changes in a highly customizable way, for use with games and virtual worlds
高级语言: JavaScript
项目地址: git://github.com/seanohue/fantasy-time-crunch.git
创建时间: 2019-03-20T00:43:10Z
项目社区:https://github.com/seanohue/fantasy-time-crunch

开源协议:

下载


fantasy-time-crunch

I needed a way of handling a made-up time system in a game, so I made this.

Sample Configuration

  1. const time = {
  2. // One and only one unit is the smallest measured subdivision of time, defined as a 'tick'
  3. second: {
  4. tick: true,
  5. makes: {minute: 60},
  6. },
  7. // Larger units are defined by how many of a smaller unit they are subdivided into
  8. minute: {
  9. makes: {hour: 60},
  10. },
  11. // Optionally one can define a function to be called when
  12. // a unit increments (having a bell toll, etc.)
  13. hour: {
  14. makes: {day: 24},
  15. onIncrement() {
  16. console.log('BING!');
  17. console.log('Hour: ', this.time.hour);
  18. },
  19. // Units can be divided into multiple state changes. For example, a day has two or more states defined
  20. // by how many hours have passed.
  21. // 'states' can be an object or a function.
  22. // In this case, dawn and dusk are different depending
  23. // on the season
  24. states() {
  25. if (this.states.is('winter')) {
  26. return {
  27. day: 9, // 0900 -- by default this number represents hours. however, each property returned can also be a function that has access to any time unit.
  28. night: 20, // 2000
  29. };
  30. }
  31. return {
  32. day: 6, // 0900
  33. night: 23, // 2200
  34. };
  35. }
  36. },
  37. day: {
  38. makes() {
  39. const days = this.time.month % 2 ? 31 : 30;
  40. return {month: days};
  41. },
  42. onIncrement() {
  43. const names = 'MTWHFSU';
  44. console.log('Day:', names[this.time.day - 1]);
  45. },
  46. },
  47. month: {
  48. // The 'of' property can also be a function called
  49. // In this case, even months have 30 days and odd have 31.
  50. makes: { year: 12},
  51. // For simpler state changes, an object marking the
  52. // transitional thresholds is fine.
  53. states: {
  54. winter: 11,
  55. spring: 3,
  56. summer: 5,
  57. fall: 9
  58. }
  59. }
  60. };