def __init__(self): self.log = l.getLogger('model') self.conf = EC.EngineConfig.Instance() # A Batch is a collection of vertex lists for batched rendering. self.batch = pyglet.graphics.Batch() # Mapping from sector to a list of positions inside that sector. self.sectors = {} # Simple function queue implementation. The queue is populated with # _show_block() and _hide_block() calls self.queue = deque() self._materialFactory = Materials.MaterialFactory.Instance() # all shown blocks. self.visibleWorld = {} # This defines all the blocks that are currently in the world. try: (self.world, self.player) = Savegame.Savegame.load() # make blocks visible after loading for position in self.world.getBlockPositions(): # sectorize blocks self.sectors.setdefault(Transform.sectorize(position, self.conf.getConfValue('sectorSize')), []).append(position) self.show_sector(0) except Exception, e: self.log.debug("Couldn't load a savegame. Creating new world ...") self.world = World.World() self.player = Player.Player() self.visibleWorld = {} self._initialize()
def remove_block(self, position, immediate=True): """ Remove the block at the given `position`. Parameters ---------- position : tuple of len 3 The (x, y, z) position of the block to remove. immediate : bool Whether or not to immediately remove block from canvas. """ # if block is dead hide it and remove it if not self.world.getBlock(position).isAlive(): transparent = self._materialFactory.getMaterial(self.world.getBlock(position).getMaterial()).transparent if immediate: self.hide_block(position) self.world.removeBlock(position) self.sectors[Transform.sectorize(position, self.conf.getConfValue('sectorSize'))].remove(position) # show newly exposed blocks self.check_neighbors(position, transparent) else: self.world.getBlock(position).decreaseLife()
def update(self, dt): """ This method is scheduled to be called repeatedly by the pyglet clock. Parameters ---------- dt : float The change in time since the last call. """ self.model.process_queue() sector = Transform.sectorize(self._player.position, self.conf.getConfValue('sectorSize')) if sector != self.sector: self.model.change_sectors(self.sector, sector) if self.sector is None: self.model.process_entire_queue() self.sector = sector m = 8 dt = min(dt, 0.2) for _ in xrange(m): self._update(dt / m)
def add_block(self, position, material, immediate=True): """ Add a block with the given `texture` and `position` to the world. Parameters ---------- position : tuple of len 3 The (x, y, z) position of the block to add. texture : list of len 3 The coordinates of the texture squares. Use `tex_coords()` to generate. immediate : bool Whether or not to draw the block immediately. """ self.world.addBlock(position, material) self.sectors.setdefault(Transform.sectorize(position, self.conf.getConfValue('sectorSize')), []).append(position) # hide newly hidden blocks transparent = self._materialFactory.getMaterial(self.world.getBlock(position).getMaterial()).transparent self.check_neighbors(position, transparent) if immediate: if self.exposed(position): self.show_block(position)