2021-03-28 18:42:04 +02:00
|
|
|
import random
|
|
|
|
|
2021-05-24 13:10:10 +02:00
|
|
|
from enum import Enum
|
2021-05-23 14:33:21 +02:00
|
|
|
from survival import GameMap
|
2021-05-21 20:14:00 +02:00
|
|
|
from survival.components.OnCollisionComponent import OnCollisionComponent
|
2021-05-24 13:10:10 +02:00
|
|
|
from survival.components.inventory_component import InventoryComponent
|
2021-03-28 18:42:04 +02:00
|
|
|
from survival.components.position_component import PositionComponent
|
2021-05-24 13:10:10 +02:00
|
|
|
from survival.components.resource_component import ResourceComponent
|
2021-03-28 18:42:04 +02:00
|
|
|
from survival.components.sprite_component import SpriteComponent
|
2021-05-23 14:33:21 +02:00
|
|
|
from survival.esper import World
|
2021-03-28 18:56:32 +02:00
|
|
|
from survival.settings import RESOURCES_AMOUNT
|
2021-03-28 18:42:04 +02:00
|
|
|
|
|
|
|
|
2021-05-24 13:10:10 +02:00
|
|
|
class ResourceType(Enum):
|
|
|
|
FOOD = 1
|
|
|
|
WATER = 2
|
|
|
|
WOOD = 3
|
|
|
|
|
|
|
|
|
2021-03-28 18:42:04 +02:00
|
|
|
class ResourceGenerator:
|
|
|
|
def __init__(self, world, game_map):
|
|
|
|
self.world = world
|
|
|
|
self.map = game_map
|
|
|
|
|
2021-05-24 13:10:10 +02:00
|
|
|
def generate_resources(self, player: int):
|
2021-03-28 18:56:32 +02:00
|
|
|
for x in range(RESOURCES_AMOUNT):
|
2021-03-28 18:42:04 +02:00
|
|
|
obj = self.world.create_entity()
|
2021-05-24 13:10:10 +02:00
|
|
|
sprites = {
|
|
|
|
ResourceType.FOOD: 'apple.png',
|
|
|
|
ResourceType.WATER: 'water.png',
|
|
|
|
ResourceType.WOOD: 'wood.png'
|
|
|
|
}
|
2021-03-28 18:42:04 +02:00
|
|
|
|
|
|
|
empty_grid_pos = self.get_empty_grid_position()
|
|
|
|
empty_pos = [empty_grid_pos[0] * 32, empty_grid_pos[1] * 32]
|
|
|
|
pos = PositionComponent(empty_pos, empty_grid_pos)
|
2021-05-24 13:10:10 +02:00
|
|
|
resource_type = random.choice(list(ResourceType))
|
|
|
|
sprite = SpriteComponent(sprites[resource_type])
|
2021-05-21 20:14:00 +02:00
|
|
|
col = OnCollisionComponent()
|
2021-05-24 13:10:10 +02:00
|
|
|
col.addCallback(self.remove_resource, world=self.world, game_map=self.map, entity=obj, player=player)
|
2021-03-28 18:42:04 +02:00
|
|
|
self.world.add_component(obj, pos)
|
|
|
|
self.world.add_component(obj, sprite)
|
2021-05-21 20:14:00 +02:00
|
|
|
self.world.add_component(obj, col)
|
2021-05-24 13:10:10 +02:00
|
|
|
self.world.add_component(obj, ResourceComponent(resource_type))
|
2021-03-28 18:42:04 +02:00
|
|
|
self.map.add_entity(obj, pos)
|
|
|
|
|
|
|
|
def get_empty_grid_position(self):
|
|
|
|
free_pos = [random.randrange(self.map.width), random.randrange(self.map.height)]
|
|
|
|
while self.map.is_colliding(free_pos):
|
|
|
|
free_pos = [random.randrange(self.map.width), random.randrange(self.map.height)]
|
|
|
|
return free_pos
|
2021-05-23 14:33:21 +02:00
|
|
|
|
|
|
|
@staticmethod
|
2021-05-24 13:10:10 +02:00
|
|
|
def remove_resource(world: World, game_map: GameMap, entity: int, player: int):
|
2021-05-23 14:33:21 +02:00
|
|
|
pos = world.component_for_entity(entity, PositionComponent)
|
2021-05-24 13:10:10 +02:00
|
|
|
resource = world.component_for_entity(entity, ResourceComponent)
|
|
|
|
inventory = world.component_for_entity(player, InventoryComponent)
|
|
|
|
inventory.add_item(resource.resource_type, 1)
|
2021-05-23 14:33:21 +02:00
|
|
|
game_map.remove_entity(pos.grid_position)
|
|
|
|
world.delete_entity(entity, immediate=True)
|