示例#1
0
class MapCamera:
    def __init__(self, x_init_world, y_init_world, width_view_camera, height_view_camera):

        # rect that represents the surface visible of the camera
        self.rect_view_camera = Rect(x_init_world, y_init_world, width_view_camera, height_view_camera)

        self.pos_dest_screen = (0, 0)  # Position to draw the map captured by this Camera
        self.map_handler = None  # Controller of the map

        self.map_tile_surface = None  # Surface Buffer that contains the map drawn
        self.rect_hidden_camera = None  # Rect of the surface buffer with pos on map

        self.following = None
        self.can_follow = {"left": False, "top": False, "bottom": False, "right": True}

    # Start the camera setting buffers
    def init(self):
        assert self.map_handler is not None, "There's not a " + str(MapHandler.__class__) + " instance "
        assert self.map_handler.has_map_loaded(), "MapHandler has no map loaded yet!"
        self.__create_buffer()

    def __create_buffer(self):

        self.rect_hidden_camera = create_camera_rect(self.rect_view_camera, self.map_handler.get_tile_size())
        self.map_tile_surface = self.map_handler.create_image(self.rect_hidden_camera)

    def set_pos_screen(self, x_screen, y_screen):  # Set position to draw the map on screen
        self.pos_dest_screen = (x_screen, y_screen)

    def set_maphandler(self, map_handler):
        self.map_handler = map_handler

    # Move camera x y pixels
    def move(self, x_world, y_world):

        rect_moved = self.rect_view_camera.move(x_world, y_world)

        if self.map_handler.is_rect_out_of_map_bounds(rect_moved):
            return

        self.rect_view_camera.move_ip(x_world, y_world)

        if not self.rect_hidden_camera.contains(self.rect_view_camera):
            # If new pos is out of the buffer, we create a new one!
            self.__create_buffer()

    def draw(self, screen):

        rect_area_visible = Rect(self.rect_view_camera.x - self.rect_hidden_camera.x,
                                 self.rect_view_camera.y - self.rect_hidden_camera.y,
                                 self.rect_view_camera.width, self.rect_view_camera.height)
        screen.blit(self.map_tile_surface, self.pos_dest_screen, rect_area_visible)

    def get_x(self):
        return self.rect_view_camera.x

    def get_y(self):
        return self.rect_view_camera.y

    def follow(self, player):
        self.following = player

    def update(self):
        if self.following:
            x_p, y_p = self.following.x, \
                       self.following.y

            if self.can_follow['right']:
                if x_p > self.rect_view_camera.x + self.rect_view_camera.width / 4:
                    rect_moved = self.rect_view_camera.copy()
                    rect_moved.x = x_p - self.rect_view_camera.width / 4

                    if self.map_handler.is_rect_out_of_map_bounds(rect_moved):
                        return

                    self.rect_view_camera = rect_moved

                    if not self.rect_hidden_camera.contains(self.rect_view_camera):
                        # If new pos is out of the buffer, we create a new one!
                        self.__create_buffer()

    def is_rect_out_of_camera_bounds(self, rect):
        return not self.rect_view_camera.contains(rect)
示例#2
0
class Character(object):
    """
    This class defines the characteristics of any character starting out
    """
    def __init__(self):
        # References to draw
        self.image_path = ('character', 'human_sprite.png')
        self.image = Assets.load_image(self.image_path)
        self.name = Assets.names_db.random_full_name()
        #frm = display.font.render(str(frame), True, text_color)
        #rect = frm.get_rect()
        self.rect = Rect(0, 0, 0, 0)
        self.location = Rect(0, 0, 1, 1)
        self.target = Rect(50, 20, 1, 1)
        self.pct = 0.0
        self.next = Rect(self.location.x, self.location.y, 1, 1)

        # Traits and skills. General idea
        self.traits = {
            "health": 100,
            "intelligence": 0,
            "strength": 0,
            "mining": 0,
            "shooting": 0,
            "armour": 0,
            "weight": 0,
            "rotation": 0,
            "speed": 0,
            "versatility:": 0,
            "weight-capacity:": 0
        }

        # What the character has equipped
        self.equips = {
            "body_part_upper": None,
            "body_part_lower": None
        }


        # Condition: [inflicted?, Damage, Duration, Duration Remaining]
        self.conditions = {
            "Burn": [False, 1, 10, 0],
            "Frost": [False, 1, 15, 0],
            "Stun": [False, 1, 5, 0],
            "Sleep": [False, 1, 20, 0],
            "Shock": [False, 1, 3, 0],
            "Poison": [False, 1, 15, 0],
            "Bleed": [False, 1, 3, 0]
        }

    # Draw character
    def draw(self, screen, world):
        if not world.view_grid.contains(self.location) and not world.view_grid.contains(self.next):
            return

        if world.grid_size != self.rect.width:
            self.rect.width = self.rect.height = world.grid_size
            self.image = Assets.load_image(self.image_path, (world.grid_size, world.grid_size))
        self.rect.x = (self.location.x - world.view_grid.x) * world.grid_size \
            + (self.next.x - self.location.x) * self.pct * world.grid_size
        self.rect.y = (self.location.y - world.view_grid.y) * world.grid_size \
            + (self.next.y - self.location.y) * self.pct * world.grid_size
        screen.blit(self.image, self.rect)


    # Equip component

    # Un-equip component

    # Equip Upper or Lower Body Part
    def body_part_equip(self, part):
        #If it's an upper body part
        if part.upper:
            self.equips["body_part_upper"] = part
        else:
            self.equips["body_part_lower"] = part

        #Pad character's stats with the stats of the part
        #Example: self.traits["weight"] = self.traits["weight"] + part.weight
        #...

    # Un-equip Armour
    def body_part_unequip(self, part):
        if part.upper:
            self.equips["body_part_upper"] = None
        else:
            self.equips["body_part_lower"] = None

        #Unpad character's stats with the stats of the part
        #...

    @property
    def weight(self):
        return self.traits["weight"] + sum(p.weight for p in self.equips.values() if p is not None)

    # Apply condition on the character.
    def inflict(self, condition):
        if condition not in self.conditions:
            return
        self.conditions[condition][0] = True
        self.conditions[condition][3] = self.conditions[condition][2]
        print(self.conditions)

    def set_target(self, pos):
        self.target.x, self.target.y = pos

    def tick(self):
        if self.target != self.location:
            if self.pct >= 0.9:
                self.location = self.next.copy()
                self.pct = 0.0

            if self.next == self.location:
                # pick a block to go to next
                if self.target.x > self.location.x:
                    self.next.x = self.location.x + 1
                elif self.target.x < self.location.x:
                    self.next.x = self.location.x - 1

                if self.target.y > self.location.y:
                    self.next.y = self.location.y + 1
                elif self.target.y < self.location.y:
                    self.next.y = self.location.y - 1

            self.pct += 0.2

        for condition in self.conditions:
            if self.conditions[condition][0]:
                if self.conditions[condition][3] == 0:  # character lived through the condition
                    self.conditions[condition][0] = False
                else:
                    self.traits["health"] -= self.conditions[condition][1]
                    self.conditions[condition][3] -= 1
                    print(self.conditions[condition][3])


    # Character died. (Need to know what happens when a character dies)
    def death(self):
        print("Character has died.")

        # Absolve character of all conditions
        self.conditions = self.conditions.fromkeys(self.conditions, False)
        return