def entity_follow(self, entity): self.__ef = entity if entity is not None: if entity.rect: self.offset = Vec2( self.world.window.size[0] / 2 - entity.rect.width / 2, self.world.window.size[1] / 2 - entity.rect.height / 2) else: self.offset = Vec2(self.world.window.size[0] / 2, self.world.window.size[1] / 2)
def movebykey(self, eventkey): if eventkey == self.controles[Controls.LEFT]: if self.controltype == ControlType.FOURDIRECTION or self.controltype == ControlType.CLASSICJUMP \ or self.controltype == ControlType.LEFTRIGHT: if (self.entity.has_component(PhysicsComponent) and self.entity.get_component( PhysicsComponent).affectbygravity): self.force = (-self.speed, 0) elif self.entity.has_component(PositionComponent): pos = self.entity.get_component(PositionComponent) pos.position = Vec2(pos.position.x - self.speed, pos.position.y) pos.update_phys() elif eventkey == self.controles[Controls.RIGHT]: if self.controltype == ControlType.FOURDIRECTION or self.controltype == ControlType.CLASSICJUMP \ or self.controltype == ControlType.LEFTRIGHT: if (self.entity.has_component(PhysicsComponent) and self.entity.get_component( PhysicsComponent).affectbygravity): self.force = (self.speed, 0) elif self.entity.has_component(PositionComponent): pos = self.entity.get_component(PositionComponent) pos.position = Vec2(pos.position.x + self.speed, pos.position.y) pos.update_phys() elif eventkey == self.controles[Controls.UPJUMP]: if self.controltype == ControlType.FOURDIRECTION or self.controltype == ControlType.UPDOWN: if (self.entity.has_component(PhysicsComponent) and self.entity.get_component( PhysicsComponent).affectbygravity): self.force = (0, self.speed * 20) elif self.entity.has_component(PositionComponent): pos = self.entity.get_component(PositionComponent) pos.position = Vec2(pos.position.x, pos.position.y - self.speed) pos.update_phys() elif self.controltype == ControlType.CLASSICJUMP: if self.entity.has_component(PhysicsComponent): phys = self.entity.get_component(PhysicsComponent) grounding = phys.check_grounding() if grounding['body'] is not None and abs(grounding['normal'].x / grounding['normal'].y) < \ phys.shape.friction: self.force = (0, self.speed * 20) elif eventkey == self.controles[Controls.DOWN]: if self.controltype == ControlType.FOURDIRECTION or self.controltype == ControlType.UPDOWN: if (self.entity.has_component(PhysicsComponent) and self.entity.get_component( PhysicsComponent).affectbygravity): self.force = (0, -self.speed) elif self.entity.has_component(PositionComponent): pos = self.entity.get_component(PositionComponent) pos.position = Vec2(pos.position.x, pos.position.y + self.speed) pos.update_phys()
def __init__(self, window, pos=Vec2(), width=600): pos = Vec2(pos.x, pos.y + 20) super(Console, self).__init__(pos, width) self.window = window self.commands = {"print": print_command, "debug": debug_command} self.lastscommands = [] self.current_command = len(self.lastscommands) self.font = Font(size=18) self.retour = Label(Vec2(pos.x, pos.y - 21), ">", Colors.BLACK.value, Font(size=18), Colors.WHITE.value) self.retour.parent = self
def __init__(self, position: Vec2, text: str, command: Union[None, Callable[[], None]] = None, size: Vec2 = Vec2(100, 40), sprite: Union[None, str] = None): super(Button, self).__init__(position) if sprite is None: self.image = pygame.Surface(size.coords) self.image.fill((50, 50, 50)) else: image = pygame.image.load(sprite) self.image = pygame.transform.scale(image, size.coords) self.label = Label(position, text) self.label.parent = self self.rect = self.image.get_rect() self.position = position self.size = size self.sprite = sprite self.ishover = False self.__enabled = True self.command = command self.update_render()
def position(self, position): self.__position = position self.rect.x = self.position.x self.rect.y = self.position.y self.label.position = Vec2( self.rect.x + self.rect.width / 2 - self.label.rect.width / 2, self.rect.y + self.rect.height / 2 - self.label.rect.height / 2)
def __init__(self, position: Vec2, text: str, color: Color = Colors.WHITE.value, font: Font = Font(), background: Union[None, Color] = None): super(MultilineLabel, self).__init__(position) if not isinstance(font, Font): raise TypeError("Font have not a Font type") if not isinstance(color, Color): raise TypeError("Color have not a Color type") if not isinstance(background, Color) and background is not None: raise TypeError("Background must be a Color") if "\n" not in text: loggers.get_logger("PyEngine").info( "MultilineLabel without Line break is useless. Use Label.") self.__text = text self.labels = [ Label(Vec2(self.position.x, self.position.y + 20 * (k + 1)), v) for k, v in enumerate(text.split("\n")) ] self.__color = color self.__font = font self.__background = background self.update_render()
def strings(self, val: (list, tuple)): if not isinstance(val, (list, tuple)) or len(val) == 0: raise ValueError("Strings must be a list with minimum one str.") self.__strings = val self.current_index = 0 self.label.text = self.strings[0] self.bnext.position = Vec2( self.position.x + 35 + self.maxsize_string(), self.position.y)
def scale(self, val): for i in self.tiles: i.get_component(SpriteComponent).scale = val i.get_component(PositionComponent).offset = Vec2( i.pos_in_grid[0] * self.tilewidth * val, i.pos_in_grid[1] * self.tileheight * val + (self.tileheight - i.image.get_height())) self.__scale = val
def __init__(self, pos, offset, sprite, pos_in_grid, tileheight): super(Tile, self).__init__() self.pos_in_grid = pos_in_grid poscomp = self.add_component(PositionComponent(pos, offset)) self.add_component(SpriteComponent(sprite)) poscomp.offset = Vec2( offset.x, offset.y + (tileheight - self.image.get_height())) self.add_component(PhysicsComponent(False))
def create_tile(self, datas, idtiles, pos, x, y): if datas["layers"][0]["data"][y * self.width + x] - 1 != -1: offset = Vec2(x * self.tilewidth, y * self.tileheight) idtile = str(datas["layers"][0]["data"][y * self.width + x] - 1) tilesetfolder = "/".join( datas["tilesets"][0]["source"].split("/")[:-1]) + "/" return Tile(pos, offset, self.folder + tilesetfolder + idtiles[idtile], [x, y], self.tileheight)
def __init__(self, pos: Vec2, size: Vec2 = Vec2(150, 10), sprites: Tuple[str, str] = None): super(ProgressBar, self).__init__(pos) self.__value = 0 self.__size = size self.sprites = sprites
def __init__(self, position: Vec2, offset: Vec2 = Vec2()): self.__entity = None if not isinstance(position, pygame.Vector2): raise TypeError("Position must be a Vec2") if not isinstance(offset, pygame.Vector2): raise TypeError("Offset must be a Vec2") self.__offset = offset self.__position = position
def position(self, position): if not isinstance(position, pygame.Vector2): raise TypeError("Position must be a Vec2") self.__position = position for i in self.world.get_system(EntitySystem).entities: pos = i.get_component(PositionComponent) pos.position = Vec2( pos.position.x - self.position.x + self.offset.x, pos.position.y - self.position.y + self.offset.y)
def __init__(self, position: Vec2, strings: (list, tuple)): super(Selector, self).__init__(position) if not isinstance(strings, (list, tuple)) or len(strings) == 0: raise ValueError("Strings must be a list with minimum one str.") self.__strings = strings self.current_index = 0 self.bprecedent = Button(position, "<", self.precedent, size=Vec2(25, 25)) self.label = Label(Vec2(position.x + 30, position.y), self.strings[0], Colors.BLACK.value, Font(size=18)) self.bnext = Button(Vec2(position.x + 35 + self.maxsize_string(), position.y), ">", self.next, size=Vec2(25, 25)) self.bprecedent.parent = self self.label.parent = self self.bnext.parent = self self.update_render()
def __init__(self, controltype: ControlType, speed: int = 20): self.entity = None self.controltype = controltype self.speed = speed self.goto = Vec2(-1, -1) self.force = (0, 0) self.keypressed = set() self.controles = { Controls.UPJUMP: const.K_UP, Controls.LEFT: const.K_LEFT, Controls.RIGHT: const.K_RIGHT, Controls.DOWN: const.K_DOWN }
def update(self): if not self.can_rot: self.body.angular_velocity = 0 self.body.angle = 0 if self.entity.has_component(PositionComponent): if str(self.body.position[0]) != "nan" and str( self.body.position[1]) != "nan": pos = Vec2(self.flipy(self.body.position)) self.entity.get_component(PositionComponent).position = pos if self.entity.has_component(SpriteComponent): self.entity.get_component(SpriteComponent).make_rotation( math.degrees(self.body.angle))
def text(self, text): if len(self.labels): [self.system.remove_widget(i) for i in self.labels] if "\n" not in text: loggers.get_logger("PyEngine").info( "MultilineLabel without Line break is useless. Use Label.") self.__text = text self.labels = [ Label(Vec2(self.position.x, self.position.y + 20 * (k + 1)), v) for k, v in enumerate(text.split("\n")) ] [self.system.add_widget(i) for i in self.labels] self.update_render()
def movebymouse(self): if self.entity.has_component(PositionComponent): position = self.entity.get_component(PositionComponent).position if position.x - 10 < self.goto.x < position.x + 10 and position.y - 10 < self.goto.y < position.y + 10: self.goto = Vec2(-1, -1) else: if (self.entity.has_component(PhysicsComponent) and self.entity.get_component( PhysicsComponent).affectbygravity): if position.x - 10 > self.goto.x: self.force = (self.force[0] - self.speed, self.force[1]) elif position.x + 10 < self.goto.x: self.force = (self.force[0] + self.speed, self.force[1]) if position.y - 10 > self.goto.y: self.force = (self.force[0], self.force[1] + self.speed * 20) elif position.y + 10 < self.goto.y: self.force = (self.force[0], self.force[1] - self.speed * 20) else: pos = Vec2() if position.x - 10 > self.goto.x: pos.x = position.x - self.speed elif position.x + 10 < self.goto.x: pos.x = position.x + self.speed else: pos.x = position.x if position.y - 10 > self.goto.y: pos.y = position.y - self.speed elif position.y + 10 < self.goto.y: pos.y = position.y + self.speed else: pos.y = position.y self.entity.get_component(PositionComponent).position = pos
def update_render(self): self.label.position = Vec2( self.position.x + 30 + self.maxsize_string() / 2 - self.label.font.rendered_size(self.label.text)[0] / 2, self.position.y)
def __init__(self, world: World): self.world = world self.__position = Vec2() self.offset = Vec2() self.entity_follow = None self.__zoom = 1
def size(self): return Vec2(self.rect.width, self.rect.height)
def update_render(self): self.update_rect() self.label.position = Vec2( self.rect.x + self.rect.width / 2 - self.label.rect.width / 2, self.rect.y + self.rect.height / 2 - self.label.rect.height / 2)
def update_render(self): self.update_rect() self.label.position = Vec2(self.rect.x + 20 * self.scale + 5, self.rect.y + self.rect.height / 2 - self.label.rect.height / 2)
def mousepress(self, evt): if self.controltype == ControlType.CLICKFOLLOW and evt.button == MouseButton.LEFTCLICK.value: self.goto = Vec2(evt.pos[0], evt.pos[1])
def mousemotion(self, evt): if self.controltype == ControlType.MOUSEFOLLOW: self.goto = Vec2(evt.pos[0], evt.pos[1])