Example #1
0
    def setLightLocation(self, x, y, input = 'pygame'):
        """
        @x,y: light coordinates
        @input: can be either pygame ((0,0) topleft) or pymunk((0,0) bottomleft)
        """
        if input == 'pygame':
            self.center = Vec2d(x, y)
        elif input == 'pymunk':
            x, y = helper.toPygame(Vec2d(x, y))
            self.center = Vec2d(x, y)
        else:
            raise ValueError('bad input argument')

        for segment in self.segments:
            dx = 0.5 * (segment.p1.x + segment.p2.x) - x
            dy = 0.5 * (segment.p1.y + segment.p2.y) - y
            segment.d = dx*dx + dy*dy

            segment.p1.angle = math.atan2(segment.p1.y - y, segment.p1.x - x)
            segment.p2.angle = math.atan2(segment.p2.y - y, segment.p2.x - x)

            d_angle = segment.p2.angle - segment.p1.angle
            if d_angle <= -math.pi:
                d_angle += math.pi * 2
            if d_angle > math.pi:
                d_angle -= math.pi * 2

            segment.p1.begin = (d_angle > 0.0)
            segment.p2.begin = not segment.p1.begin  
Example #2
0
 def setBorderWithSize(self, x, y, size, input = 'pygame'):
     """
     Adds the outer limit of the light.
     @x,y: coordinates of top_left (see input)
     @size: int
     @input: can be either 'pygame' or 'pymunk' (0,0 top_left, 0,0 bottom_left)
     """
     if input == 'pymunk':
         x, y = helper.toPygame(Vec2d(x,y))
     x = x - size//2
     y = y - size//2       
     border_rect = pygame.Rect(x, y, size, size, border=True)
     self.addRect(border_rect)
Example #3
0
    def update(self):
        #### JUST FOR DEBUG PURPOSES
        has_level_info = self.group_manager.doesGroupExist('level_info')
        if has_level_info:
            level_info = self.group_manager.get('level_info')[0]
            level_info_comp = self.entity_manager.getComponent(level_info, LevelInfoComponent.__name__)
        ####

        list_entities = self.entity_manager.getEntitiesHavingComponents(
                            PositionComponent.__name__,
                            RenderComponent.__name__)

        has_camera = self.group_manager.doesGroupExist('camera')
        if has_camera:
            camera_entity = self.group_manager.get('camera')[0]
            camera_position = self.entity_manager.getComponent(camera_entity, PositionComponent.__name__)
            camera_x = camera_position.x
            camera_y = camera_position.y

        for entity in list_entities:
            position_comp = self.entity_manager.getComponent(entity, PositionComponent.__name__)
            render_comp = self.entity_manager.getComponent(entity, RenderComponent.__name__)

            x,y = helper.xyToPygame(position_comp.x, position_comp.y)
            if has_camera:
                x -= camera_x
                y += camera_y
            position_comp.rect.centerx = x
            position_comp.rect.centery = y
            self.surface.blit(render_comp.sprite, position_comp.rect)







        #####
        #####
        ## DEBUG THINGS
        if not has_level_info:
            return
        if level_info_comp.debug:
            # paint collision
            list_entities = self.group_manager.get('collision')
            for entity in list_entities:
                # collision rectangle
                physics_comp = self.entity_manager.getComponent(entity, PhysicsComponent.__name__)
                shape = physics_comp.shape
                vertices = shape.get_points() 
                x,y = helper.toPygame(vertices[0])
                width = helper.toPygame(vertices[1])[0] - x
                height = y - helper.toPygame(vertices[3])[1]
                x = int(x - camera_x)
                y = int(y + camera_y - height)   
                rect = pygame.Rect(x, y, width, height)
                pygame.draw.rect(DISPLAYSURF, RED, rect, 3) 

                # gravity center
                center = helper.toPygame(physics_comp.body.position)
                center = (int(center[0] - camera_x), int(center[1] + camera_y))
                pygame.draw.circle(DISPLAYSURF, BLUE, center, 3)

            # paint hero center of gravity
            hero = self.group_manager.get('hero')[0]
            physics_comp = self.entity_manager.getComponent(hero, PhysicsComponent.__name__)
            center = helper.toPygame(physics_comp.body.position)
            center = (int(center[0] - camera_x), int(center[1] + camera_y))
            pygame.draw.circle(DISPLAYSURF, BLUE, center, 3)