def lamp_builder(registry, name: str, config: Dict, settings: Dict) -> Entity: entity = Entity(name, EntityType.LAMP) handler = registry.core.io.build_handler( config[CONTROL_SERVICE], *sanitize_component_config(config[SWITCH])) entity.add_component(SWITCH, registry.components[SWITCH]({}, handler, entity)) entity.settings = settings return entity
def blinds_builder(registry, name: str, config: Dict, settings: Dict): entity = Entity(name, EntityType.BLINDS) handler = registry.core.io.build_handler( config[CONTROL_SERVICE], *sanitize_component_config(config[BLINDS])) entity.add_component(BLINDS, registry.components[BLINDS]({}, handler, entity)) entity.settings = settings print(entity.components) return entity
def get_blocks(self): blocks = [] tile_keys = globs.tiles.data.keys() rows = globs.config.height // self.tilesize cols = globs.config.width // self.tilesize for row in range(rows): for col in range(cols): tile_id = str(pyxel.tilemap(self.index).get(self.offsetx + col, self.offsety + row)) if tile_id in tile_keys: tile_data = globs.tiles.data[tile_id] x = col * self.tilesize y = row * self.tilesize w = 8 h = 8 if type(tile_data) is dict: w = tile_data['hitbox_w'] h = tile_data['hitbox_h'] blocks.append(Entity(x, y, w, h)) return blocks
def rgb_lamp_builder(registry, name: str, config: Dict, settings: Dict) -> Entity: entity = Entity(name, EntityType.LAMP_RGB) _switch_handler = registry.core.io.build_handler( config[CONTROL_SERVICE], *sanitize_component_config(config[SWITCH])) _brightness_handler = registry.core.io.build_handler( config[CONTROL_SERVICE], *sanitize_component_config(config[BRIGHTNESS])) _color_handler = registry.core.io.build_handler( config[CONTROL_SERVICE], *sanitize_component_config(config[COLOR])) async def switch_handler(target, context): if target and entity.components[BRIGHTNESS].state == 0: await entity.call_method(BRIGHTNESS, "set", 100, context) elif not target: await entity.call_method(BRIGHTNESS, "set", 0, context) await _switch_handler(target, context) async def brightness_handler(target, context): if target == 0 and entity.components[SWITCH].state: await entity.call_method(SWITCH, "turn_off", None, context) elif target != 0 and not entity.components[SWITCH].state: await entity.call_method(SWITCH, "turn_on", None, context) await _brightness_handler(target, context) async def color_handler(target, context): if target == Colors.BLACK: await entity.call_method(SWITCH, "turn_off", None, context) elif target != Colors.BLACK and not entity.components[SWITCH].state: await entity.call_method(SWITCH, "turn_on", None, context) await _color_handler(target, context) entity.add_component( SWITCH, registry.components[SWITCH]({}, switch_handler, entity)) entity.add_component( BRIGHTNESS, registry.components[BRIGHTNESS]({}, brightness_handler, entity)) entity.add_component(COLOR, registry.components[COLOR]({}, color_handler, entity)) entity.settings = settings return entity
def __init__(self): self._fonts = {} self._lastMousePos = None self._onGuiElement = None self._guiElementId = 0 # 5 layers by default self._maxLayer = 4 self._layers = [{} for x in range(0, self._maxLayer + 1)] self._highestLayerDisplayed = 0 self._lastHighest = 0 self._selectedCases = None # tmp self._player = Entity('toto', baseStats={ StatsEnum.HP: 100, StatsEnum.MANA: 500 }) self._player._experience = 30 self._player._currentState[StatsEnum.HP] = 60
def dimmable_lamp_builder(registry, name: str, config: Dict, settings: Dict) -> Entity: entity = Entity(name, EntityType.LAMP_BRIGHTNESS) _switch_handler: Callable = registry.core.io.build_handler( config[CONTROL_SERVICE], *sanitize_component_config(config[SWITCH])) _brightness_handler: Callable = registry.core.io.build_handler( config[CONTROL_SERVICE], *sanitize_component_config(config[BRIGHTNESS])) async def switch_handler(target, context): if target and entity.components[BRIGHTNESS].state == 0: await entity.call_method(BRIGHTNESS, "set", 100, context) elif not target: await entity.call_method(BRIGHTNESS, "set", 0, context) await _switch_handler(target, context) async def brightness_handler(target, context): if target == 0 and entity.components[SWITCH].state: await entity.call_method(SWITCH, "turn_off", None, context) elif target != 0 and not entity.components[SWITCH].state: await entity.call_method(SWITCH, "turn_on", None, context) await _brightness_handler(target, context) entity.add_component( SWITCH, registry.components[SWITCH]({}, switch_handler, entity)) entity.add_component( BRIGHTNESS, registry.components[BRIGHTNESS]({}, brightness_handler, entity)) registry.core.add_job(entity.call_method, BRIGHTNESS, "increase", 10, Context.admin()) registry.core.add_job(entity.call_method, BRIGHTNESS, "increase", 10, Context.admin()) registry.core.add_job(entity.call_method, SWITCH, "turn_off", None, Context.admin()) entity.settings = settings return entity
class GuiManager(object): def __init__(self): self._fonts = {} self._lastMousePos = None self._onGuiElement = None self._guiElementId = 0 # 5 layers by default self._maxLayer = 4 self._layers = [{} for x in range(0, self._maxLayer + 1)] self._highestLayerDisplayed = 0 self._lastHighest = 0 self._selectedCases = None # tmp self._player = Entity('toto', baseStats={ StatsEnum.HP: 100, StatsEnum.MANA: 500 }) self._player._experience = 30 self._player._currentState[StatsEnum.HP] = 60 def init(self): pygame.font.init() self._fonts['default'] = pygame.font.Font( os.path.join(settings.FONT_PATH, 'VCR_OSD.ttf'), 15) self.guiLoader(StatusPanelGUI) self._layers[0]['openButton'].addEventHandler( 'click', self._layers[1]['statusPanel'].toggleShow, 'displayBox') statusPanel = self._layers[1]['statusPanel'] statusPanel.getChildNamedInHierarchy('closeButton').addEventHandler( 'click', statusPanel.toggleShow, 'close') statusPanel.getChildNamedInHierarchy('linkedLevel').addReference( 'player', self._player) charData = statusPanel.getChildNamedInHierarchy('charData') statusPanel.addEventHandler('show', charData.redrawChildren, 'redrawChildren') statusPanel.getChildNamedInHierarchy('hpProgressBar').addReferences({ 'current': self._player.getCurrentHealth, 'max': self._player.getMaxHealth }) statusPanel.getChildNamedInHierarchy('manaProgressBar').addReferences({ 'current': self._player.getCurrentMana, 'max': self._player.getMaxMana }) statusPanel.getChildNamedInHierarchy('xpProgressBar').addReferences({ 'current': self._player.getCurrentXp, 'max': self._player.getNextLevelXp }) frontManaBar = self._layers[0][ 'statusFrontElem'].getChildNamedInHierarchy('manaProgressBar') frontManaBar.addReferences({ 'current': self._player.getCurrentMana, 'max': self._player.getMaxMana }) # better solution ? self._player.setCurrentMana = frontManaBar.addWatcher( self._player.setCurrentMana, 'manaWatcher') frontManaBar = self._layers[0][ 'statusFrontElem'].getChildNamedInHierarchy('hpProgressBar') frontManaBar.addReferences({ 'current': self._player.getCurrentHealth, 'max': self._player.getMaxHealth }) # better solution ? self._player.setCurrentHp = frontManaBar.addWatcher( self._player.setCurrentHp, 'hpWatcher') frontManaBar = self._layers[0][ 'statusFrontElem'].getChildNamedInHierarchy('xpProgressBar') frontManaBar.addReferences({ 'current': self._player.getCurrentXp, 'max': self._player.getNextLevelXp }) # better solution ? self._player.getCurrentXp = frontManaBar.addWatcher( self._player.getCurrentXp, 'xpWatcher') def getElementGuiId(self): self._guiElementId += 1 return self._guiElementId def addToLayer(self, guiElem, layer): if layer > self._maxLayer: return self._layers[layer][guiElem.getName()] = guiElem def checkMousePosition(self, pixel): # tmp self._player.setCurrentMana(random.randint(0, 100)) if self._onGuiElement or self._highestLayerDisplayed > 0: return # Check cases map = modules.gameManager.gameManager.getMap() case = map.getCaseAtPixel(pixel) # deselect previous cases if self._selectedCases is not None: for c in self._selectedCases: c.toggleSelected() self._selectedCases = None if case is None: print("Le curseur n'est pas sur une case") return None # Test getNeighbours #neighbours = map.getCaseNeighbours(case) # Test getSpiralRing #ring = map.getCaseSpiralRing(case, 2) # Test getShortestPath # path = map.getShortestPath(case.getPosition(), map.getCaseAtPos((2, 2)).getPosition()) # Test getVisibleCases self._selectedCases = map.getVisibleCases(case, 3) # Change neighbours type # if a path has been found # if visible_cases is not None: # type = random.choice([BiomesTypes.PLAIN, BiomesTypes.FOREST, BiomesTypes.DESERT]) # for _case in visible_cases: # _case._type = type # Select new cases if self._selectedCases is not None: for c in self._selectedCases: c.toggleSelected() def checkOnClick(self, value): if self._onGuiElement is not None: self._onGuiElement.onClick(value) def updateMousePos(self, mousePos): if self._lastMousePos != mousePos or self._lastHighest != self._highestLayerDisplayed: # small bug here showing new element without moving (Maybe force when new display) # enough to fix it ? smth better ? if self._lastHighest != self._highestLayerDisplayed and self._onGuiElement is not None: self._onGuiElement.resetInside() self._lastMousePos = mousePos self._onGuiElement = None self._lastHighest = self._highestLayerDisplayed for guiElem in self._layers[self._highestLayerDisplayed].values(): self._onGuiElement = guiElem.checkInside(mousePos) if self._onGuiElement is not None: return # for l in range(self._maxLayer, -1, -1): # for guiElem in self._layers[l].values(): # self._onGuiElement = guiElem.checkInside(mousePos) # if self._onGuiElement is not None: # return def updateGui(self): highestLayer = 0 for l in range(0, self._maxLayer + 1): for guiElem in self._layers[l].values(): if guiElem.getShow(): guiElem.update() highestLayer = l if l > highestLayer else highestLayer self._highestLayerDisplayed = highestLayer def displayGui(self, screen): for l in range(0, self._maxLayer + 1): for guiElem in self._layers[l].values(): guiElem.display(screen) def guiLoader(self, dictio): for layer, elements in dictio.items(): currentLayer = int(layer) if layer > self._maxLayer or layer < 0: raise ValueError('Invalid layer value') for elem in elements: guiElem = self.guiNodeCreator(elem) self.addToLayer(guiElem, currentLayer) def guiNodeCreator(self, node, parent=None): if 'elemType' not in node.keys(): raise KeyError('No key named "elemType"') elemType = node['elemType'] # Add white list for elem type ? del node['elemType'] if not issubclass(elemType, GuiElement): raise TypeError('Invalid element type') children = None if 'children' in node.keys(): children = node['children'] del node['children'] guiElem = elemType(parent=parent, fonts=self._fonts, **node) if children is not None: for child in children: self.guiNodeCreator(child, parent=guiElem) return guiElem
def create_monster(self, char, color, x, y, size=30): self.potwor = Entity('S', (255,50,50), size, x, y, passable=False) self.enemies.append(self.potwor)