Ejemplo n.º 1
0
    def on_event(self, event):
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE:
                if self.state == TextboxState.OPENING:
                    self.set_reading()
                elif self.state == TextboxState.READING:
                    self.text_index += 1
                    if self.text_index < self.text_max:
                        self.set_text(self.text[self.text_index][1:4])

                        if self.owner_entity != None:
                            self.world.delete_entity(self.owner_entity)
                            self.owner_entity = None
                        owner = self.text[self.text_index][0]
                        if owner != None:
                            self.owner_entity = self.world.create_entity(
                                Element({
                                    'name':
                                    self.element.name + "_owner",
                                    'pos':
                                    Vector2(self.element.pos.x + 8,
                                            self.element.pos.y)
                                }),
                                Sprite(
                                    self.font.render(owner, False, (0, 0, 0))),
                                Transform({'layer': 25}))
                            self.anim.current = 'named'
                        else:
                            self.anim.current = 'noname'
                    else:
                        self.set_closing()
Ejemplo n.º 2
0
    def load_chunk(self, chunk_x, chunk_y):
        self.loaded_chunks.append((chunk_x, chunk_y))
        try:
            chunk_name = next(filter(lambda ch: ch['pos']==[chunk_x, chunk_y], self.world_info.info['chunks']))['name']
        except StopIteration:
            print("No chunk defined for chunk position ({}, {})".format(chunk_x, chunk_y))
            raise

        chunk_path = paths.get_chunk(self.world_info.name, chunk_name)
        chunk_json = open(chunk_path)
        chunk = json.load(chunk_json)
        chunk_json.close()

        self.loaded_entities[(chunk_x, chunk_y)] = []

        chunk_surface = pygame.Surface((int(constants.CHUNK_SIZE_PIXELS),
                                       int(constants.CHUNK_SIZE_PIXELS)))
        for layout in chunk['layouts']:
            for y in range(0, constants.CHUNK_SIZE):
                for x in range(0, constants.CHUNK_SIZE):
                    tile_id = layout[y][x]
                    tile_info = self.world_info.mappings[str(tile_id)]
                    if isinstance(tile_info[0], list):
                        weights = list(map(lambda x: x[2], tile_info))
                        tile_info = random.choices(tile_info, weights)[0]
                    
                    tileset_name = tile_info[0]
                    tile_name = tile_info[1]

                    tileset = self.tilesets[tileset_name]
                    tile_img_pos = tileset['info'][tile_name]
                    tile_size = tileset['info']['tile_size']

                    chunk_surface.blit(tileset['img'],
                                    (x * tile_size, y * tile_size),
                                    pygame.Rect(tile_img_pos[0],
                                                tile_img_pos[1],
                                                tile_size,
                                                tile_size))
        
        spr = Sprite(chunk_surface, pygame.Rect(0, 0, constants.CHUNK_SIZE_PIXELS * self.scale, constants.CHUNK_SIZE_PIXELS * self.scale))
        transform = Transform({
            'pos': Vector2(chunk_x * constants.CHUNK_SIZE_PIXELS * self.scale, chunk_y * constants.CHUNK_SIZE_PIXELS * self.scale),
            'scale': Vector2(self.scale, self.scale)
        })
        self.loaded_entities[(chunk_x, chunk_y)].append(self.world.create_entity(spr, transform))

        objects = chunk['objects']
        object_mappings = chunk['object_mappings']
        for y in range(0, constants.CHUNK_SIZE):
            for x in range(0, constants.CHUNK_SIZE):
                obj_id = objects[y][x]
                obj_info = object_mappings[str(obj_id)]

                obj_x = int(chunk_x * constants.CHUNK_SIZE + x)
                obj_y = int(chunk_y * constants.CHUNK_SIZE + y)

                self.loaded_objects[(obj_x, obj_y)] = self.create_object(obj_info, obj_x, obj_y)
Ejemplo n.º 3
0
 def set_text(self, lines):
     for i in range(0, 3):
         if self.text_entity[i] != None:
             self.world.delete_entity(self.text_entity[i])
         self.text_entity[i] = self.world.create_entity(
             Element({
                 'name':
                 self.element.name + "_text" + str(i),
                 'pos':
                 Vector2(self.element.pos.x + 8,
                         self.element.pos.y + TEXT_SIZE * i + 48)
             }), Sprite(self.font.render(lines[i], False, (0, 0, 0))),
             Transform({'layer': 25}))
Ejemplo n.º 4
0
    def update_text(self):
        from game.components import Sprite, Transform

        if self.text_entity != None:
            self.world.delete_entity(self.text_entity)
            self.text_entity = None

        self.last_text = self.text

        spr = self.font.render(self.text, False, (0, 0, 0))
        self.text_entity = self.world.create_entity(
            Element({
                'name': self.element.name + "_text",
                'pos': Vector2(self.element.pos.x, self.element.pos.y)
            }), Sprite(spr), Transform({'layer': 25}))
Ejemplo n.º 5
0
    def load(self):
        sprite_info = None
        with open(self.json_path) as file:
            sprite_info = json.load(file)

        surface = pygame.image.load(self.img_path)
        x = sprite_info.get('x', 0)
        y = sprite_info.get('y', 0)
        width = sprite_info.get('width', surface.get_width())
        height = sprite_info.get('height', surface.get_height())
        sprite = Sprite(surface, pygame.Rect(x, y, width, height))

        animation = None
        animation_groups = None

        animation_info = sprite_info.get('animation', None)
        if animation_info == None:
            animation = Animation(width, height, width, height, -1, 0, 1)
        else:
            s_width = animation_info.get('width', width)
            s_height = animation_info.get('height', height)
            delay = animation_info.get('delay', -1)
            start = animation_info.get('start', 0)
            end = animation_info.get(
                'end',
                int(width / s_width) * int(height / s_height))

            animation = Animation(width, height, s_width, s_height, delay,
                                  start, end)

            animation_groups_info = animation_info.get('groups', None)
            if animation_groups_info != None:
                groups = {}
                for key, value in animation_groups_info.items():
                    g_loop = value.get('loop', True)
                    g_start = value.get('start', 0)
                    g_end = value.get('end', end)
                    g_delay = value.get('delay', delay)

                    group = AnimationGroup(g_loop, g_start, g_end, g_delay)
                    groups[key] = group

                animation_groups = AnimationGroups(animation_info['default'],
                                                   groups)

        return (sprite, animation, animation_groups)
Ejemplo n.º 6
0
    def set_text(self, text):
        from game.components import Sprite, Transform

        if self.text_entity != None:
            self.world.delete_entity(self.text_entity)
            self.text_entity = None

        spr = self.font.render(text, False, (255, 255, 255))
        self.text_entity = self.world.create_entity(
            Element({
                'name':
                self.element.name + '_text',
                'pos':
                Vector2(
                    self.element.pos.x + (self.element.size.x / 2) -
                    (spr.get_width() / 2), self.element.pos.y +
                    (self.element.size.y / 2) - (spr.get_height() / 2))
            }), Sprite(spr), Transform({'layer': 25}))
Ejemplo n.º 7
0
    def set_reading(self):
        self.transform.scale.x = 1.0
        self.transform.scale.y = 1.0
        self.state = TextboxState.READING

        owner = self.text[self.text_index][0]
        if owner != None:
            self.owner_entity = self.world.create_entity(
                Element({
                    'name':
                    self.element.name + "_owner",
                    'pos':
                    Vector2(self.element.pos.x + 8, self.element.pos.y)
                }), Sprite(self.font.render(owner, False, (0, 0, 0))),
                Transform({'layer': 25}))
            self.anim.current = 'named'
        else:
            self.anim.current = 'noname'

        if self.text != None:
            self.set_text(self.text[self.text_index][1:4])