Loading

Paste #pn0vpa58b

  1. local states = {
  2.   -- Initial state, sets up the plant to full health, then drops to normal state.
  3.   "start": {
  4.     { entry = setup_full_health,   -- Also needs registering?!
  5.       initial = true,
  6.       events = { "tick_day": { next_state = "decreasing" } }
  7.     }
  8.   },
  9.  
  10.   -- Plant in normal state, loosing water, and slowly dying.
  11.   "decreasing":
  12.     { events =
  13.         { "tick_day":
  14.             { { condition = function(self): return self.current_state < 5 end,
  15.                 action = decrease_plant,
  16.                 next_state = "decreasing"
  17.               },
  18.  
  19.               { -- Plant already dead, current state does not need managing.
  20.                 next_state = "decreasing"
  21.               }
  22.             },
  23.           "watering":
  24.             { { next_state = "restoring" }
  25.             },
  26.           "pick_up":
  27.             { { action = unregister_plant,
  28.                 next_state = "picked_up"
  29.               }
  30.             }
  31.         }
  32.     },
  33.  
  34.   -- Plant got watered, and is recovering to full health.
  35.   "restoring":
  36.     { entry = setup_restore,
  37.       events =
  38.         { "tick_day":
  39.             { { condition = function(self): return self.current_state > 1 end,
  40.                 action = restore_plant,
  41.                 next_state = "restoring"
  42.               },
  43.               { -- Restored to full health, go back to decreasing
  44.                 action = function(self)
  45.                   self.days_left = DAYS_BETWEEN_STATES
  46.                 end,
  47.                 next_state = "decreasing"
  48.               }
  49.             }
  50.           "watering":
  51.             { { next_state = "restoring" } -- Already restoring, do nothing.
  52.             }
  53.           "pick_up":
  54.             { { action = unregister_plant,
  55.                 next_state = "picked_up"
  56.               }
  57.             }
  58.         }
  59.     },
  60.   "picked_up" :
  61.     { events =
  62.         { "drop":
  63.             { { action = register_plant,
  64.                 next_state = "decreasing"
  65.               }
  66.             }
  67.         }
  68.     }
  69. }

Comments