SzybciorSmartTraktor/plant.py
Emil Gutkiewicz ac32a5e0b7 added plants
2022-03-25 06:52:06 +01:00

109 lines
2.6 KiB
Python

from enum import Enum
class PlantGrowthStages(Enum):
PLANTED = 0
GROWING = 1
UNRIPE = 2
RIPE = 3
@staticmethod
def can_be_harvested(_type):
return _type == PlantGrowthStages.RIPE
@staticmethod
def next_growth_type(_type):
return {
PlantGrowthStages.PLANTED: PlantGrowthStages.GROWING,
PlantGrowthStages.GROWING: PlantGrowthStages.UNRIPE,
PlantGrowthStages.UNRIPE: PlantGrowthStages.RIPE,
PlantGrowthStages.RIPE: PlantGrowthStages.RIPE,
}[_type]
class Plant:
tick_decrease_amount: 1
tick_increase_amount: 1
def __init__(self):
self.growth = PlantGrowthStages.PLANTED
self.canBeHarvested = False
self.water_level = 0
self.fertilizer_level = 0
self.growth_stage_level = 0
self.growth_stage_threshold = 10000
def grow(self):
self.growth_stage_level = 0
previous_growth = self.growth
self.growth = PlantGrowthStages.next_growth_type(self.growth)
if self.growth == previous_growth:
self.canBeHarvested = True
def is_watered(self):
return self.water_level > 0
def water(self):
self.water_level = 100
def is_fertilized(self):
return self.fertilizer_level > 0
def fertilize(self):
self.fertilizer_level = 1000
def tick(self):
self.water_level -= self.tick_decrease_amount
self.fertilizer_level -= self.tick_decrease_amount
self.growth_stage_level += self.tick_increase_amount
if self.growth_stage_threshold == self.growth_stage_level:
self.grow()
def harvest(self):
if PlantGrowthStages.can_be_harvested(self.growth):
return self
@staticmethod
def sow(seed):
return {
PlantGrowthStages.PLANTED: PlantGrowthStages.GROWING,
PlantGrowthStages.GROWING: PlantGrowthStages.UNRIPE,
PlantGrowthStages.UNRIPE: PlantGrowthStages.RIPE,
PlantGrowthStages.RIPE: PlantGrowthStages.RIPE,
}[seed]
class Carrot(Plant):
def __init__(self):
super(Carrot, self).__init__()
self.growth_stage_threshold = 5000
class Potato(Plant):
def __init__(self):
super(Potato, self).__init__()
class Wheat(Plant):
def __init__(self):
super(Wheat, self).__init__()
class Seeds(Enum):
Carrot = 0
Potato = 1
Wheat = 2
@staticmethod
def seed_to_plant(seed):
return {
Seeds.Carrot: Carrot(),
Seeds.Potato: Potato(),
Seeds.Wheat: Wheat(),
}[seed]