Make consumption use resources
This commit is contained in:
parent
40f620b3ec
commit
bbfaedf925
@ -8,6 +8,7 @@ from survival.components.position_component import PositionComponent
|
|||||||
from survival.components.resource_component import ResourceComponent
|
from survival.components.resource_component import ResourceComponent
|
||||||
from survival.game.enums import Direction
|
from survival.game.enums import Direction
|
||||||
from survival.esper import World
|
from survival.esper import World
|
||||||
|
from survival.systems.consumption_system import ConsumeComponent
|
||||||
|
|
||||||
|
|
||||||
class Action(Enum):
|
class Action(Enum):
|
||||||
@ -35,9 +36,11 @@ class Action(Enum):
|
|||||||
if action == Action.ROTATE_LEFT:
|
if action == Action.ROTATE_LEFT:
|
||||||
world.add_component(entity, DirectionChangeComponent(
|
world.add_component(entity, DirectionChangeComponent(
|
||||||
Direction.rotate_left(world.component_for_entity(entity, PositionComponent).direction)))
|
Direction.rotate_left(world.component_for_entity(entity, PositionComponent).direction)))
|
||||||
|
world.add_component(entity, ConsumeComponent(0.2))
|
||||||
elif action == Action.ROTATE_RIGHT:
|
elif action == Action.ROTATE_RIGHT:
|
||||||
world.add_component(entity, DirectionChangeComponent(
|
world.add_component(entity, DirectionChangeComponent(
|
||||||
Direction.rotate_right(world.component_for_entity(entity, PositionComponent).direction)))
|
Direction.rotate_right(world.component_for_entity(entity, PositionComponent).direction)))
|
||||||
|
world.add_component(entity, ConsumeComponent(0.2))
|
||||||
else:
|
else:
|
||||||
world.add_component(entity, MovingComponent())
|
world.add_component(entity, MovingComponent())
|
||||||
return action
|
return action
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
|
from survival.generators.resource_type import ResourceType
|
||||||
|
|
||||||
|
|
||||||
class ConsumptionComponent:
|
class ConsumptionComponent:
|
||||||
def __init__(self, inventory_state=0):
|
def __init__(self):
|
||||||
self.timer_value: float = 2000
|
self.status = {ResourceType.FOOD: 1, ResourceType.WOOD: 1, ResourceType.WATER: 1}
|
||||||
self.timer: float = self.timer_value
|
|
||||||
self.last_inventory_state = inventory_state
|
|
||||||
|
@ -17,6 +17,9 @@ class InventoryComponent:
|
|||||||
if self.items[item] < 0:
|
if self.items[item] < 0:
|
||||||
self.items[item] = 0
|
self.items[item] = 0
|
||||||
|
|
||||||
|
def count(self, item):
|
||||||
|
return self.items[item]
|
||||||
|
|
||||||
def has_item(self, item):
|
def has_item(self, item):
|
||||||
return item in self.items and self.items[item] != 0
|
return item in self.items and self.items[item] != 0
|
||||||
|
|
||||||
|
@ -25,7 +25,7 @@ class PlayerGenerator:
|
|||||||
inv = InventoryComponent()
|
inv = InventoryComponent()
|
||||||
for resource in ResourceType:
|
for resource in ResourceType:
|
||||||
inv.add_item(resource, STARTING_RESOURCES_AMOUNT)
|
inv.add_item(resource, STARTING_RESOURCES_AMOUNT)
|
||||||
world.add_component(player, ConsumptionComponent(inv.total_items_count()))
|
world.add_component(player, ConsumptionComponent())
|
||||||
world.add_component(player, inv)
|
world.add_component(player, inv)
|
||||||
camera_target = CameraTargetComponent(pos)
|
camera_target = CameraTargetComponent(pos)
|
||||||
world.add_component(player, camera_target)
|
world.add_component(player, camera_target)
|
||||||
|
@ -5,6 +5,7 @@ from survival.components.on_collision_component import OnCollisionComponent
|
|||||||
from survival.components.moving_component import MovingComponent
|
from survival.components.moving_component import MovingComponent
|
||||||
from survival.components.position_component import PositionComponent
|
from survival.components.position_component import PositionComponent
|
||||||
from survival.game.enums import Direction
|
from survival.game.enums import Direction
|
||||||
|
from survival.systems.consumption_system import ConsumeComponent
|
||||||
|
|
||||||
|
|
||||||
class CollisionSystem(esper.Processor):
|
class CollisionSystem(esper.Processor):
|
||||||
@ -34,6 +35,7 @@ class CollisionSystem(esper.Processor):
|
|||||||
|
|
||||||
else:
|
else:
|
||||||
self.map.move_entity(pos.grid_position, moving.target)
|
self.map.move_entity(pos.grid_position, moving.target)
|
||||||
|
self.world.add_component(ent, ConsumeComponent(self.map.get_cost(moving.target)))
|
||||||
pos.grid_position = moving.target
|
pos.grid_position = moving.target
|
||||||
|
|
||||||
def check_collision(self, pos):
|
def check_collision(self, pos):
|
||||||
|
@ -1,30 +1,64 @@
|
|||||||
|
import random
|
||||||
|
|
||||||
from survival import esper
|
from survival import esper
|
||||||
from survival.components.consumption_component import ConsumptionComponent
|
from survival.components.consumption_component import ConsumptionComponent
|
||||||
from survival.components.inventory_component import InventoryComponent
|
from survival.components.inventory_component import InventoryComponent
|
||||||
from survival.components.learning_component import LearningComponent
|
from survival.components.learning_component import LearningComponent
|
||||||
|
from survival.components.moving_component import MovingComponent
|
||||||
from survival.generators.resource_type import ResourceType
|
from survival.generators.resource_type import ResourceType
|
||||||
|
|
||||||
|
|
||||||
|
class ConsumeComponent:
|
||||||
|
def __init__(self, cost):
|
||||||
|
self.cost = cost
|
||||||
|
|
||||||
|
|
||||||
class ConsumptionSystem(esper.Processor):
|
class ConsumptionSystem(esper.Processor):
|
||||||
|
CONSUMPTION_FACTOR = 0.05
|
||||||
|
CONSUMPTION_RANGE = 0.07
|
||||||
|
|
||||||
def __init__(self, callback):
|
def __init__(self, callback):
|
||||||
self.callback = callback
|
self.callback = callback
|
||||||
|
|
||||||
def process(self, dt):
|
def process(self, dt):
|
||||||
for ent, (cons, inventory) in self.world.get_components(ConsumptionComponent, InventoryComponent):
|
cons: ConsumptionComponent
|
||||||
cons.timer -= dt
|
inventory: InventoryComponent
|
||||||
if cons.timer > 0:
|
c: ConsumeComponent
|
||||||
continue
|
for ent, (cons, inventory, c) in self.world.get_components(ConsumptionComponent, InventoryComponent,
|
||||||
cons.timer = cons.timer_value
|
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):
|
if self.world.has_component(ent, LearningComponent):
|
||||||
# If no item was picked up
|
for resource in cons.status.keys():
|
||||||
if cons.last_inventory_state == inventory.total_items_count():
|
if inventory.items[resource] <= 0 and self.world.has_component(ent, LearningComponent):
|
||||||
learning: LearningComponent = self.world.component_for_entity(ent, LearningComponent)
|
# If entity has run out of items
|
||||||
learning.reward += -10
|
learning: LearningComponent = self.world.component_for_entity(ent, LearningComponent)
|
||||||
learning.done = True
|
learning.reward -= 1
|
||||||
cons.last_inventory_state = inventory.total_items_count()
|
learning.done = True
|
||||||
|
break
|
||||||
else:
|
else:
|
||||||
if inventory.has_item(ResourceType.FOOD):
|
self.callback(ent)
|
||||||
inventory.remove_item(ResourceType.FOOD, 1)
|
|
||||||
else:
|
self.world.remove_component(ent, ConsumeComponent)
|
||||||
self.callback()
|
# 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()
|
||||||
|
Loading…
Reference in New Issue
Block a user