AI-Project/survival/systems/consumption_system.py
2021-06-20 16:27:38 +02:00

65 lines
2.8 KiB
Python

import random
from survival import esper
from survival.components.consumption_component import ConsumptionComponent
from survival.components.inventory_component import InventoryComponent
from survival.components.learning_component import LearningComponent
from survival.components.moving_component import MovingComponent
from survival.generators.resource_type import ResourceType
class ConsumeComponent:
def __init__(self, cost):
self.cost = cost
class ConsumptionSystem(esper.Processor):
CONSUMPTION_FACTOR = 0.05
CONSUMPTION_RANGE = 0.07
def __init__(self, callback):
self.callback = callback
def process(self, dt):
cons: ConsumptionComponent
inventory: InventoryComponent
c: ConsumeComponent
for ent, (cons, inventory, c) in self.world.get_components(ConsumptionComponent, InventoryComponent,
ConsumeComponent):
for resource in cons.status.keys():
cons.status[resource] -= c.cost * self.CONSUMPTION_FACTOR + random.uniform(-self.CONSUMPTION_RANGE,
self.CONSUMPTION_RANGE)
if cons.status[resource] < 0:
inventory.items[resource] -= 1
cons.status[resource] = 1
if self.world.has_component(ent, LearningComponent):
for resource in cons.status.keys():
if inventory.items[resource] <= 0 and self.world.has_component(ent, LearningComponent):
# If entity has run out of items
learning: LearningComponent = self.world.component_for_entity(ent, LearningComponent)
learning.reward -= 1
learning.done = True
break
else:
self.callback(ent)
self.world.remove_component(ent, ConsumeComponent)
# cons.timer -= dt
# if cons.timer > 0:
# continue
# cons.timer = cons.timer_value
#
# if self.world.has_component(ent, LearningComponent):
# # If no item was picked up
# if cons.last_inventory_state == inventory.total_items_count():
# learning: LearningComponent = self.world.component_for_entity(ent, LearningComponent)
# learning.reward += -10
# learning.done = True
# cons.last_inventory_state = inventory.total_items_count()
# else:
# if inventory.has_item(ResourceType.FOOD):
# inventory.remove_item(ResourceType.FOOD, 1)
# else:
# self.callback()