2021-06-20 16:27:38 +02:00
|
|
|
import random
|
|
|
|
|
2021-06-06 19:55:55 +02:00
|
|
|
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
|
2021-06-20 16:27:38 +02:00
|
|
|
from survival.components.moving_component import MovingComponent
|
2021-06-06 19:55:55 +02:00
|
|
|
from survival.generators.resource_type import ResourceType
|
|
|
|
|
|
|
|
|
2021-06-20 16:27:38 +02:00
|
|
|
class ConsumeComponent:
|
|
|
|
def __init__(self, cost):
|
|
|
|
self.cost = cost
|
|
|
|
|
|
|
|
|
2021-06-06 19:55:55 +02:00
|
|
|
class ConsumptionSystem(esper.Processor):
|
2021-06-20 16:27:38 +02:00
|
|
|
CONSUMPTION_FACTOR = 0.05
|
|
|
|
CONSUMPTION_RANGE = 0.07
|
|
|
|
|
2021-06-06 19:55:55 +02:00
|
|
|
def __init__(self, callback):
|
|
|
|
self.callback = callback
|
|
|
|
|
|
|
|
def process(self, dt):
|
2021-06-20 16:27:38 +02:00
|
|
|
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
|
2021-06-06 19:55:55 +02:00
|
|
|
|
|
|
|
if self.world.has_component(ent, LearningComponent):
|
2021-06-20 16:27:38 +02:00
|
|
|
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
|
2021-06-06 19:55:55 +02:00
|
|
|
else:
|
2021-06-20 16:27:38 +02:00
|
|
|
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()
|