Example #1
0
 def getDraggable(self, actor):
     """Return the draggable for the actor"""
     try:
         return self.draggables[actor]
     except KeyError:
         raise NotDragging('The actor %s is not tracked as a draggable' %
                           actor.getNiceName())
Example #2
0
 def removeDropTarget(self, actor):
     """Remove an actor as a drop target"""
     try:
         del (self.targets[actor])
     except KeyError:
         raise NotATarget('The actor %s was not a target in %s' %
                          (actor.getNiceName(), self.getNiceName()))
Example #3
0
 def addActor(self, actor, start=None, stop=None):
     """Add an actor to be controlled and callback to be called when dragging start and stops"""
     if actor in self.draggables:
         raise DuplicateActor('The actor %s is already controlled by %s' % (actor.getNiceName(), self.getNiceName()))
     self.draggables.append(actor)
     actor.linkEvent(serge.events.E_LEFT_MOUSE_DOWN, self.mouseDown, (actor, start))
     actor.linkEvent(serge.events.E_LEFT_CLICK, self.clickedActor, (actor, stop))
 def actorSelected(self, obj, actor):
     """An actor was selected"""
     self.log.debug("Focus set to %s" % actor.getNiceName())
     # Defocus
     self.getChildren().forEach().loseFocus()
     # Refocus
     actor.getFocus()
Example #5
0
 def __call__(self, world, actor, interval):
     """Check the actor is in range"""
     if not serge.geometry.Point(actor.x, actor.y).isInside(self.space):
         self.log.debug('Removed %s because it was out of range' %
                        actor.getNiceName())
         world.scheduleActorRemoval(actor)
         return B_COMPLETED
 def updateActor(self, interval, world):
     """Update the bullet position"""
     #
     # Move the bullet
     dx, dy = (
         self.speed*math.cos(math.radians(self.angle)), 
         -self.speed*math.sin(math.radians(self.angle)) + 
             (G('bullet-fragment-force')*interval/1000.0 if self.has_gravity else 0)
     )
     self.move(dx, dy)
     self.setAngle(-Vec2d(dx, dy).get_angle_degrees())
     #
     if G('bullet-offscreen-highy') < self.y or G('bullet-offscreen-lowy') > self.y or \
        G('bullet-offscreen-highx') < self.x or G('bullet-offscreen-lowx') > self.x:
         world.scheduleActorRemoval(self)
         self.log.debug('Bullet off screen')
     #
     # Look for collisions
     for actor in self.world.findActorsByTag(self.target):
         if actor.active and self.isOverlapping(actor):
             self.log.info('Bullet %s collided with %s' % (self.getNiceName(), actor.getNiceName()))
             self.broadcaster.processEvent((self.event, (self, actor)))
     #
     if self.direction != -1:
         self.speed -= self.direction*interval/1000.0
 def _redoLocations(self):
     """Reset the locations of the objects within us"""
     self.log.debug("Resetting locations")
     if self.children:
         for i, actor in enumerate(self.children):
             actor.moveTo(*self.getCoords(i))
             self.log.debug("Set %s to %d, %d" % (actor.getNiceName(), actor.x, actor.y))
Example #8
0
 def actorSelected(self, obj, actor):
     """An actor was selected"""
     self.log.debug('Focus set to %s' % actor.getNiceName())
     # Defocus
     self.getChildren().forEach().loseFocus()
     # Refocus
     actor.getFocus()
Example #9
0
 def _redoLocations(self):
     """Reset the locations of the objects within us"""
     self.log.debug('Resetting locations')
     if self.children:
         for i, actor in enumerate(self.children):
             actor.moveTo(*self.getCoords(i))
             self.log.debug('Set %s to %d, %d' %
                            (actor.getNiceName(), actor.x, actor.y))
Example #10
0
 def _redoLocations(self):
     """Reset the locations of the objects within us"""
     self.log.info('Resetting locations')
     width = float(self.width) / len(self._actors)
     left, top, _, _ = self.getSpatial()
     for i, actor in enumerate(self._actors):
         actor.moveTo(left + width*(i+0.5), top + self.height*0.5)
         self.log.debug('Set %s to %d, %d' % (actor.getNiceName(), actor.x, actor.y))
 def findActorLocation(self, actor):
     """Find the location of an actor"""
     for x, row in enumerate(self._grid):
         for y, occupants in enumerate(row):
             if actor in occupants:
                 return x, y
     else:
         raise UnknownActor("The actor %s was not found in grid %s" % (actor.getNiceName(), self.getNiceName()))
 def findActorLocation(self, actor):
     """Find the location of an actor"""
     for x, row in enumerate(self._grid):
         for y, test_actor in enumerate(row):
             if actor == test_actor:
                 return (x, y)
     else:
         raise UnknownActor('The actor %s was not found in grid %s' % (actor.getNiceName(), self.getNiceName()))
Example #13
0
 def addDropTarget(self, actor, fn=None):
     """Add a target to drop to"""
     if actor in self.targets.keys():
         raise AlreadyATarget(
             'The target %s is already a drop target for %s' %
             (actor.getNiceName(), self.getNiceName()))
     else:
         self.targets[actor] = fn
Example #14
0
 def findActorLocation(self, actor):
     """Find the location of an actor"""
     for x, row in enumerate(self._grid):
         for y, occupants in enumerate(row):
             if actor in occupants:
                 return (x, y)
     else:
         raise UnknownActor('The actor %s was not found in grid %s' %
                            (actor.getNiceName(), self.getNiceName()))
Example #15
0
 def _redoLocations(self):
     """Reset the locations of the objects within us"""
     self.log.info('Resetting locations')
     width = float(self.width) / len(self._actors)
     left, top, _, _ = self.getSpatial()
     for i, actor in enumerate(self._actors):
         actor.moveTo(left + width * (i + 0.5), top + self.height * 0.5)
         self.log.debug('Set %s to %d, %d' %
                        (actor.getNiceName(), actor.x, actor.y))
Example #16
0
 def _redoLocations(self):
     """Reset the locations of the objects within us"""
     self.log.debug('Resetting locations')
     if self.children:
         height = float(self.height) / len(self.children)
         left, top, _, _ = self.getSpatial()
         for i, actor in enumerate(self.children):
             actor.moveTo(left + self.width*0.5, top + height*(i+0.5))
             self.log.debug('Set %s to %d, %d' % (actor.getNiceName(), actor.x, actor.y))
Example #17
0
 def removeBehaviourByName(self, actor, name):
     """Remove the named behaviour for an actor based on its name"""
     for record in self._behaviours.values():
         if record.matches(actor, name):
             self.removeBehaviour(record)
             return
     raise MissingBehaviour(
         'Not behaviour named "%s" found for actor "%s"' %
         (name, actor.getNiceName()))
 def _redoLocations(self):
     """Reset the locations of the objects within us"""
     self.log.debug("Resetting locations")
     if self.children:
         height = self.item_height if self.item_height else float(self.height) / len(self.children)
         left = self.getSpatial()[0]
         top = self.y if self.item_height else self.getSpatial()[1]
         for i, actor in enumerate(self.children):
             actor.moveTo(left + self.width * 0.5, top + height * (i + 0.5))
             self.log.debug("Set %s to %d, %d" % (actor.getNiceName(), actor.x, actor.y))
Example #19
0
 def _redoLocations(self):
     """Reset the locations of the objects within us"""
     self.log.debug('Resetting locations')
     if self.children:
         height = float(self.height) / len(self.children)
         left, top, _, _ = self.getSpatial()
         for i, actor in enumerate(self.children):
             actor.moveTo(left + self.width * 0.5, top + height * (i + 0.5))
             self.log.debug('Set %s to %d, %d' %
                            (actor.getNiceName(), actor.x, actor.y))
 def assignBehaviour(self, actor, behaviour, name):
     """Assign a behaviour to an actor"""
     record = BehaviourRecord(actor, behaviour, name)
     if record._getId() in self._behaviours:
         raise DuplicateBehaviour('The behaviour %s,%s was already recorded' % (actor, behaviour))
     nice_name = 'the game' if actor is None else actor.getNiceName()
     self.log.debug('Assigned behaviour %s to %s' % (behaviour, nice_name))
     self._behaviours[record._getId()] = record
     if actor:
         actor.linkEvent(serge.events.E_REMOVED_FROM_WORLD, self._actorRemoved)
     return record
Example #21
0
 def actorSelected(self, obj, actor):
     """An actor was selected"""
     self.log.debug('Focus set to %s' % actor.getNiceName())
     #
     # Defocus
     self.getChildren().forEach().loseFocus()
     if self._last_focus:
         self.processEvent((serge.events.E_LOST_FOCUS, self._last_focus))
     #
     # Refocus
     if actor:
         actor.getFocus()
         self.processEvent((serge.events.E_GOT_FOCUS, actor))
     self._last_focus = actor
 def actorSelected(self, obj, actor):
     """An actor was selected"""
     self.log.debug('Focus set to %s' % actor.getNiceName())
     #
     # Defocus
     self.getChildren().forEach().loseFocus()
     if self._last_focus:
         self.processEvent((serge.events.E_LOST_FOCUS, self._last_focus))
     #
     # Refocus
     if actor:
         actor.getFocus()
         self.processEvent((serge.events.E_GOT_FOCUS, actor))
     self._last_focus = actor
Example #23
0
 def assignBehaviour(self, actor, behaviour, name):
     """Assign a behaviour to an actor"""
     record = BehaviourRecord(actor, behaviour, name)
     if record._getId() in self._behaviours:
         raise DuplicateBehaviour(
             'The behaviour %s,%s was already recorded' %
             (actor, behaviour))
     nice_name = 'the game' if actor is None else actor.getNiceName()
     self.log.debug('Assigned behaviour %s to %s' % (behaviour, nice_name))
     self._behaviours[record._getId()] = record
     if actor:
         actor.linkEvent(serge.events.E_REMOVED_FROM_WORLD,
                         self._actorRemoved)
     return record
 def addActor(self, actor, start=None, stop=None, x_constraint=None, y_constraint=None):
     """Add an actor to be controlled and callback to be called when dragging start and stops"""
     #
     # Reality checks
     if actor in self.draggables:
         raise DuplicateActor('The actor %s is already controlled by %s' % (actor.getNiceName(), self.getNiceName()))
     if x_constraint is not None and not isinstance(x_constraint, tuple):
         raise BadConstraint('The x_constraint was not a tuple')
     if y_constraint is not None and not isinstance(y_constraint, tuple):
         raise BadConstraint('The y_constraint was not a tuple')
     #
     # Add the draggable
     self.draggables[actor] = DragItem(actor, start, stop, x_constraint, y_constraint)
     actor.linkEvent(serge.events.E_LEFT_MOUSE_DOWN, self.mouseDown, (actor, start))
     actor.linkEvent(serge.events.E_LEFT_CLICK, self.clickedActor, (actor, stop))
Example #25
0
 def addActor(self,
              actor,
              start=None,
              stop=None,
              x_constraint=None,
              y_constraint=None):
     """Add an actor to be controlled and callback to be called when dragging start and stops"""
     #
     # Reality checks
     if actor in self.draggables:
         raise DuplicateActor('The actor %s is already controlled by %s' %
                              (actor.getNiceName(), self.getNiceName()))
     if x_constraint is not None and not isinstance(x_constraint, tuple):
         raise BadConstraint('The x_constraint was not a tuple')
     if y_constraint is not None and not isinstance(y_constraint, tuple):
         raise BadConstraint('The y_constraint was not a tuple')
     #
     # Add the draggable
     self.draggables[actor] = DragItem(actor, start, stop, x_constraint,
                                       y_constraint)
     actor.linkEvent(serge.events.E_LEFT_MOUSE_DOWN, self.mouseDown,
                     (actor, start))
     actor.linkEvent(serge.events.E_LEFT_CLICK, self.clickedActor,
                     (actor, stop))
 def getDraggable(self, actor):
     """Return the draggable for the actor"""
     try:
         return self.draggables[actor]
     except KeyError:
         raise NotDragging('The actor %s is not tracked as a draggable' % actor.getNiceName())
            raise CellOccupied(
                "The cell %s is already occupied by %s in grid %s"
                % ((x, y), occupant.getNiceName(), self.getNiceName())
            )
        #
        # Add to the grid
        try:
            self._grid[x][y] = actor
        except IndexError:
            raise OutOfRange("%s is out of the range of this grid (%s)" % ((x, y), self.getNiceName()))
        #
        # Now make sure that we update everything
        self.addChild(actor)
        actor.setLayerName(self.getLayerName() if layer_name is None else layer_name)
        actor.moveTo(*self.getCoords((x, y)))
        self.log.debug("Set coords for %s to %d, %d" % (actor.getNiceName(), actor.x, actor.y))
        return actor

    def autoAddActor(self, actor):
        """Automatically add an actor to the next cell in the grid
        
        This fills horizontally and then vertically
        
        """
        self.addActor((self._added % len(self._grid), self._added // len(self._grid)), actor)
        self._added += 1
        return actor

    def moveActor(self, (x, y), actor):
        """Move an actor from wherever it is to the new location"""
        #
        except CellEmpty:
            pass
        else:
            raise CellOccupied('The cell %s is already occupied by %s in grid %s' % ((x, y), occupant.getNiceName(), self.getNiceName()))
        #
        # Add to the grid
        try:
            self._grid[x][y] = actor
        except IndexError:
            raise OutOfRange('%s is out of the range of this grid (%s)' % ((x, y), self.getNiceName()))
        #
        # Now make sure that we update everything
        self.addChild(actor)
        actor.setLayerName(self.getLayerName() if layer_name is None else layer_name)
        actor.moveTo(*self.getCoords((x, y)))
        self.log.debug('Set coords for %s to %d, %d' % (actor.getNiceName(), actor.x, actor.y))
        return actor

    def autoAddActor(self, actor):
        """Automatically add an actor to the next cell in the grid
        
        This fills horizontally and then vertically
        
        """
        self.addActor((self._added % len(self._grid), self._added // len(self._grid)), actor)
        self._added += 1
        return actor
    
    def moveActor(self, (x, y), actor):
        """Move an actor from wherever it is to the new location"""
        #
 def actorEntry(self, obj, actor):
     """An entry was accepted"""
     self.log.debug("Entry to %s" % actor.getNiceName())
     self.processEvent((serge.events.E_ACCEPT_ENTRY, actor))
     # Defocus
     self.getChildren().forEach().loseFocus()
Example #30
0
                ((x, y), occupant.getNiceName(), self.getNiceName()))
        #
        # Add to the grid
        try:
            self._grid[x][y] = actor
        except IndexError:
            raise OutOfRange('%s is out of the range of this grid (%s)' %
                             ((x, y), self.getNiceName()))
        #
        # Now make sure that we update everything
        self.addChild(actor)
        actor.setLayerName(
            self.getLayerName() if layer_name is None else layer_name)
        actor.moveTo(*self.getCoords((x, y)))
        self.log.debug('Set coords for %s to %d, %d' %
                       (actor.getNiceName(), actor.x, actor.y))
        return actor

    def autoAddActor(self, actor):
        """Automatically add an actor to the next cell in the grid
        
        This fills horizontally and then vertically
        
        """
        self.addActor(
            (self._added % len(self._grid), self._added // len(self._grid)),
            actor)
        self._added += 1
        return actor

    def moveActor(self, (x, y), actor):
 def __call__(self, world, actor, interval):
     """Check the actor is in range"""
     if not serge.geometry.Point(actor.x, actor.y).isInside(self.space):
         self.log.debug('Removed %s because it was out of range' % actor.getNiceName())
         world.scheduleActorRemoval(actor)
         return B_COMPLETED        
 def removeBehaviourByName(self, actor, name):
     """Remove the named behaviour for an actor based on its name"""
     for record in self._behaviours.values():
         if record.matches(actor, name):
             self.removeBehaviour(record)
             return
     raise MissingBehaviour('Not behaviour named "%s" found for actor "%s"' % (name, actor.getNiceName()))
 def addDropTarget(self, actor, fn=None):
     """Add a target to drop to"""
     if actor in self.targets.keys():
         raise AlreadyATarget('The target %s is already a drop target for %s' % (actor.getNiceName(), self.getNiceName()))
     else:
         self.targets[actor] = fn
Example #34
0
                'The cell %s is already occupied by %s in grid %s' %
                ((x, y), occupant.getNiceName(), self.getNiceName()))
        #
        # Add to the grid
        try:
            self._grid[x][y] = actor
        except IndexError:
            raise OutOfRange('%s is out of the range of this grid (%s)' %
                             ((x, y), self.getNiceName()))
        #
        # Now make sure that we update everything
        self._changes.append(('add', actor))
        actor.setLayerName(self.getLayerName())
        actor.moveTo(*self._getCoords((x, y)))
        self.log.debug('Set coords for %s to %d, %d' %
                       (actor.getNiceName(), actor.x, actor.y))

    def getActorAt(self, (x, y)):
        """Return the actor at a certain location"""
        try:
            occupant = self._grid[x][y]
        except IndexError:
            raise OutOfRange('%s is out of the range of this grid (%s)' %
                             ((x, y), self.getNiceName()))
        else:
            if occupant is None:
                raise CellEmpty('The cell %s in grid %s is empty' %
                                ((x, y), self.getNiceName()))
            else:
                return occupant
Example #35
0
 def actorEntry(self, obj, actor):
     """An entry was accepted"""
     self.log.debug('Entry to %s' % actor.getNiceName())
     self.processEvent((serge.events.E_ACCEPT_ENTRY, actor))
     # Defocus
     self.getChildren().forEach().loseFocus()
 def removeDropTarget(self, actor):
     """Remove an actor as a drop target"""
     try:
         del(self.targets[actor])
     except KeyError:
         raise NotATarget('The actor %s was not a target in %s' % (actor.getNiceName(), self.getNiceName()))
Example #37
0
        except CellEmpty:
            pass
        else:
            raise CellOccupied('The cell %s is already occupied by %s in grid %s' % ((x, y), occupant.getNiceName(), self.getNiceName()))
        #
        # Add to the grid
        try:
            self._grid[x][y] = actor
        except IndexError:
            raise OutOfRange('%s is out of the range of this grid (%s)' % ((x, y), self.getNiceName()))
        #
        # Now make sure that we update everything
        self._changes.append(('add', actor))
        actor.setLayerName(self.getLayerName())
        actor.moveTo(*self._getCoords((x, y)))
        self.log.debug('Set coords for %s to %d, %d' % (actor.getNiceName(), actor.x, actor.y))

    def getActorAt(self, (x, y)):
        """Return the actor at a certain location"""
        try:
            occupant = self._grid[x][y]
        except IndexError:
            raise OutOfRange('%s is out of the range of this grid (%s)' % ((x, y), self.getNiceName()))
        else:
            if occupant is None:
                raise CellEmpty('The cell %s in grid %s is empty' % ((x, y), self.getNiceName()))
            else:
                return occupant

    def findActorLocation(self, actor):
        """Find the location of an actor"""