development #8

Merged
s452701 merged 6 commits from development into master 2021-05-24 13:20:01 +02:00
9 changed files with 99 additions and 21 deletions
Showing only changes of commit be25865123 - Show all commits

BIN
assets/ui.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 712 B

View File

@ -2,15 +2,19 @@ import pygame
from settings import SCREEN_WIDTH, SCREEN_HEIGHT from settings import SCREEN_WIDTH, SCREEN_HEIGHT
from survival.camera import Camera from survival.camera import Camera
from survival.components.inventory_component import InventoryComponent
from survival.game_map import GameMap from survival.game_map import GameMap
from survival.generators.building_generator import BuildingGenerator from survival.generators.building_generator import BuildingGenerator
from survival.generators.player_generator import PlayerGenerator from survival.generators.player_generator import PlayerGenerator
from survival.generators.resource_generator import ResourceGenerator from survival.generators.resource_generator import ResourceGenerator
from survival.generators.world_generator import WorldGenerator from survival.generators.world_generator import WorldGenerator
from survival.systems.draw_system import DrawSystem
if __name__ == '__main__': if __name__ == '__main__':
pygame.init() pygame.init()
pygame.font.init()
win = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) win = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("AI Project") pygame.display.set_caption("AI Project")
@ -21,9 +25,10 @@ if __name__ == '__main__':
world = WorldGenerator().create_world(camera, game_map) world = WorldGenerator().create_world(camera, game_map)
player = PlayerGenerator().create_player(world, game_map) player = PlayerGenerator().create_player(world, game_map)
world.get_processor(DrawSystem).initialize_interface(world.component_for_entity(player, InventoryComponent))
building = BuildingGenerator().create_home(world, game_map) building = BuildingGenerator().create_home(world, game_map)
ResourceGenerator(world, game_map).generate_resources() ResourceGenerator(world, game_map).generate_resources(player)
run = True run = True

View File

@ -1,9 +1,9 @@
class InventoryComponent: class InventoryComponent:
def __init__(self, maxitems = 10): def __init__(self, maxitems=10):
self.maxitems = maxitems self.maxitems = maxitems
self.items = {} self.items = {}
def addItem(self, item, count): def add_item(self, item, count):
if item not in self.items: if item not in self.items:
self.items[item] = count self.items[item] = count
else: else:
@ -11,14 +11,11 @@ class InventoryComponent:
if self.items[item] > self.maxitems: if self.items[item] > self.maxitems:
self.items[item] = self.maxitems self.items[item] = self.maxitems
def removeItem(self, item, count): def remove_item(self, item, count):
if self.items: if item in self.items:
self.items[item] = self.items[item] - count self.items[item] = self.items[item] - count
if self.items[item] < 0: if self.items[item] < 0:
self.items[item] = 0 self.items[item] = 0
def hasItem(self, item): def has_item(self, item):
if self.items[item] != 0: return item in self.items and self.items[item] != 0
return True
else:
return False

View File

@ -0,0 +1,3 @@
class ResourceComponent:
def __init__(self, resource_type):
self.resource_type = resource_type

View File

@ -1,6 +1,7 @@
from survival.components.OnCollisionComponent import OnCollisionComponent from survival.components.OnCollisionComponent import OnCollisionComponent
from survival.components.camera_target_component import CameraTargetComponent from survival.components.camera_target_component import CameraTargetComponent
from survival.components.input_component import InputComponent from survival.components.input_component import InputComponent
from survival.components.inventory_component import InventoryComponent
from survival.components.movement_component import MovementComponent from survival.components.movement_component import MovementComponent
from survival.components.position_component import PositionComponent from survival.components.position_component import PositionComponent
from survival.components.sprite_component import SpriteComponent from survival.components.sprite_component import SpriteComponent
@ -16,6 +17,7 @@ class PlayerGenerator:
world.add_component(player, MovementComponent()) world.add_component(player, MovementComponent())
world.add_component(player, InputComponent()) world.add_component(player, InputComponent())
world.add_component(player, OnCollisionComponent()) world.add_component(player, OnCollisionComponent())
world.add_component(player, InventoryComponent())
camera_target = CameraTargetComponent(pos) camera_target = CameraTargetComponent(pos)
world.add_component(player, camera_target) world.add_component(player, camera_target)
game_map.add_entity(player, pos) game_map.add_entity(player, pos)

View File

@ -1,33 +1,47 @@
import random import random
from enum import Enum
from survival import GameMap from survival import GameMap
from survival.components.OnCollisionComponent import OnCollisionComponent from survival.components.OnCollisionComponent import OnCollisionComponent
from survival.components.inventory_component import InventoryComponent
from survival.components.position_component import PositionComponent from survival.components.position_component import PositionComponent
from survival.components.resource_component import ResourceComponent
from survival.components.sprite_component import SpriteComponent from survival.components.sprite_component import SpriteComponent
from survival.esper import World from survival.esper import World
from survival.settings import RESOURCES_AMOUNT from survival.settings import RESOURCES_AMOUNT
class ResourceType(Enum):
FOOD = 1
WATER = 2
WOOD = 3
class ResourceGenerator: class ResourceGenerator:
def __init__(self, world, game_map): def __init__(self, world, game_map):
self.world = world self.world = world
self.map = game_map self.map = game_map
def generate_resources(self): def generate_resources(self, player: int):
for x in range(RESOURCES_AMOUNT): for x in range(RESOURCES_AMOUNT):
obj = self.world.create_entity() obj = self.world.create_entity()
sprites = ['apple.png', 'water.png', 'wood.png'] sprites = {
ResourceType.FOOD: 'apple.png',
ResourceType.WATER: 'water.png',
ResourceType.WOOD: 'wood.png'
}
empty_grid_pos = self.get_empty_grid_position() empty_grid_pos = self.get_empty_grid_position()
empty_pos = [empty_grid_pos[0] * 32, empty_grid_pos[1] * 32] empty_pos = [empty_grid_pos[0] * 32, empty_grid_pos[1] * 32]
pos = PositionComponent(empty_pos, empty_grid_pos) pos = PositionComponent(empty_pos, empty_grid_pos)
sprite = SpriteComponent(random.choice(sprites)) resource_type = random.choice(list(ResourceType))
sprite = SpriteComponent(sprites[resource_type])
col = OnCollisionComponent() col = OnCollisionComponent()
col.addCallback(self.remove_resource, world=self.world, game_map=self.map, entity=obj) col.addCallback(self.remove_resource, world=self.world, game_map=self.map, entity=obj, player=player)
self.world.add_component(obj, pos) self.world.add_component(obj, pos)
self.world.add_component(obj, sprite) self.world.add_component(obj, sprite)
self.world.add_component(obj, col) self.world.add_component(obj, col)
self.world.add_component(obj, ResourceComponent(resource_type))
self.map.add_entity(obj, pos) self.map.add_entity(obj, pos)
def get_empty_grid_position(self): def get_empty_grid_position(self):
@ -37,7 +51,10 @@ class ResourceGenerator:
return free_pos return free_pos
@staticmethod @staticmethod
def remove_resource(world: World, game_map: GameMap, entity: int): def remove_resource(world: World, game_map: GameMap, entity: int, player: int):
pos = world.component_for_entity(entity, PositionComponent) pos = world.component_for_entity(entity, PositionComponent)
resource = world.component_for_entity(entity, ResourceComponent)
inventory = world.component_for_entity(player, InventoryComponent)
inventory.add_item(resource.resource_type, 1)
game_map.remove_entity(pos.grid_position) game_map.remove_entity(pos.grid_position)
world.delete_entity(entity, immediate=True) world.delete_entity(entity, immediate=True)

View File

@ -4,12 +4,12 @@ import pygame
class Image: class Image:
def __init__(self, filename): def __init__(self, filename, pos=(0, 0), scale=1):
self.texture = pygame.image.load(os.path.join('..', 'assets', filename)).convert_alpha() self.texture = pygame.image.load(os.path.join('..', 'assets', filename)).convert_alpha()
self.image = self.texture self.image = self.texture
self.origin = (0, 0) self.origin = (0, 0)
self.pos = (0, 0) self.pos = pos
self.scale = 1 self.set_scale(scale)
def set_scale(self, scale): def set_scale(self, scale):
self.image = pygame.transform.scale(self.texture, self.image = pygame.transform.scale(self.texture,
@ -20,3 +20,8 @@ class Image:
window.blit(self.image, camera.apply(self.pos), window.blit(self.image, camera.apply(self.pos),
pygame.Rect(self.origin[0] * self.scale, self.origin[1] * self.scale, 32 * self.scale, pygame.Rect(self.origin[0] * self.scale, self.origin[1] * self.scale, 32 * self.scale,
32 * self.scale)) 32 * self.scale))
def draw_static(self, window):
window.blit(self.image, self.pos,
pygame.Rect(self.origin[0] * self.scale, self.origin[1] * self.scale, 32 * self.scale,
32 * self.scale))

View File

@ -1,14 +1,21 @@
from survival import esper from survival import esper
from survival.components.position_component import PositionComponent from survival.components.position_component import PositionComponent
from survival.components.sprite_component import SpriteComponent from survival.components.sprite_component import SpriteComponent
from survival.user_interface import UserInterface
class DrawSystem(esper.Processor): class DrawSystem(esper.Processor):
def __init__(self, camera): def __init__(self, camera):
self.camera = camera self.camera = camera
self.ui = None
def initialize_interface(self, inventory):
self.ui = UserInterface(self.camera.window, inventory)
def process(self, dt): def process(self, dt):
for ent, (sprite, pos) in self.world.get_components(SpriteComponent, PositionComponent): for ent, (sprite, pos) in self.world.get_components(SpriteComponent, PositionComponent):
sprite.image.pos = pos.position sprite.image.pos = pos.position
sprite.image.origin = (32 * pos.direction.value, 0) sprite.image.origin = (32 * pos.direction.value, 0)
self.camera.draw(sprite.image) self.camera.draw(sprite.image)
self.ui.update()
self.ui.draw()

View File

@ -0,0 +1,42 @@
import pygame.font
from survival import settings
from survival.components.inventory_component import InventoryComponent
from survival.generators.resource_generator import ResourceType
from survival.image import Image
class UserInterface:
def __init__(self, window, inventory: InventoryComponent):
self.width = settings.SCREEN_WIDTH
self.height = settings.SCREEN_HEIGHT
self.window = window
self.pos = (self.width - 240, 50)
self.scale = 2
self.inventory = inventory
self.images = {
ResourceType.FOOD: Image('apple.png', self.pos, self.scale),
ResourceType.WATER: Image('water.png', self.pos, self.scale),
ResourceType.WOOD: Image('wood.png', self.pos, self.scale)
}
i = 0
for key, value in self.images.items():
self.images[key].pos = (self.pos[0] + i * 32 * self.scale + 8 * i, self.pos[1])
i += 1
self.slot_image = Image('ui.png', self.pos, scale=2)
self.font = pygame.font.SysFont('Comic Sans MS', 20)
def update(self):
pass
def draw(self):
for key, image in self.images.items():
items_count = self.inventory.items[key] if self.inventory.has_item(key) else 0
self.slot_image.pos = image.pos
self.slot_image.draw_static(self.window)
image.draw_static(self.window)
textsurface = self.font.render(str(items_count), False, (255, 255, 255))
self.window.blit(textsurface, (image.pos[0] + 48, image.pos[1] + 36))