예제 #1
0
    def update(self, timeElapsed):
        self.timeSinceLastShot += timeElapsed
        if self.timeSinceLastShot >= self.shotCooldown:
            self.timeSinceLastShot = 0
            tile = self.get_current_tile()
            target_tile = self.target.get_current_tile()
            x, y = tile.get_grid_position()
            target_x, target_y = target_tile.get_grid_position()
            if target_x == x or target_y == y:
                if target_x == x:
                    if y < target_y:
                        direction = (0, 1)
                    else:
                        direction = (0, -1)
                    agent_to_origination_point = vector.setMagnitude(
                        direction, self.height * .5)
                if target_y == y:
                    if x < target_x:
                        direction = (1, 0)
                    else:
                        direction = (-1, 0)
                    agent_to_origination_point = vector.setMagnitude(
                        direction, self.width * .5)

                origination_point = calculate.addPointAndVector(
                    self.position, agent_to_origination_point)

                shot = Shot(world=self.world,
                            direction=direction,
                            tile=tile,
                            owner=self,
                            position=origination_point)
                self.world.add_canvas_element(shot)

        super(Enemy, self).update(self)
예제 #2
0
def wander(agent):
    steeringController = agent.getSteeringController()
    centerToTargetVector = steeringController.centerToWanderTarget
    wanderDistance = steeringController.wanderDistance
    wanderJitter = steeringController.wanderJitter
    wanderRadius = steeringController.wanderRadius
    agentPosition = agent.getPosition()
    projectionVector = (wanderDistance, 0)
    
    targetAdjustment =\
        ((random.random() * 2 - 1) * wanderJitter, 
         (random.random() * 2 - 1) * wanderJitter)
    
    centerToTargetVector = calculate.addVectors(centerToTargetVector,
                                                targetAdjustment)
    
    centerToTargetVector = vector.setMagnitude(centerToTargetVector,
                                               wanderRadius)
    
    localTargetVector = calculate.addVectors(centerToTargetVector,
                                             projectionVector)
    worldTargetVector = convert.vectorToWorldSpace(localTargetVector,
                                                   agentPosition,
                                                   agent.getDirectionRadians())
    
    return worldTargetVector
예제 #3
0
    def enter(cls, owner):
        owner.update_path()
        path = owner.get_path()
        next_tile = owner.get_current_tile()

        while next_tile is owner.get_current_tile():
            if len(path) == 0:
                raise statemachine.StateChangeFailed()
            next_tile = path.pop(0)

        if next_tile.is_obstructed():
            raise statemachine.StateChangeFailed()

        owner_to_tile = calculate.subtractPoints(next_tile.getPosition(),
                                                 owner.getPosition())

        # Pick the cardinal direction that's closest to the
        # force vector
        cardinal_velocity = vector.pick_closest_vector(owner_to_tile, [(0, 1),
                                                                       (0, -1),
                                                                       (-1, 0),
                                                                       (1, 0)])

        owner.set_next_tile(next_tile)
        owner.setVelocity(
            vector.setMagnitude(cardinal_velocity, owner.single_speed))
예제 #4
0
파일: agents.py 프로젝트: krieghan/escape
 def fire(self,
          target):
     owner = self.owner
     world = owner.world
     position = self.getPosition()
     speed = owner.getSpeed()
     shotSpeed = speed + self.shotSpeed
     
     predictedPosition = self.predictFuturePosition(source=self,
                                                    target=target,
                                                    shotSpeed=shotSpeed)
                                                    
     shotToPredictedPosition = calculate.subtractPoints(predictedPosition,
                                                        position)
     shotVelocity = vector.setMagnitude(shotToPredictedPosition,
                                        shotSpeed)
     shot = Shot(fromTurret=self,
                 velocity=shotVelocity,
                 position=position,
                 maxDistance=self.firingRange,
                 render=self.shotRenderer,
                 height=self.shotHeight,
                 width=self.shotWidth,
                 color=(1, 1, 1),
                 world=world,
                 damage=self.damage,
                 target=target)
     world.addShot(shot)
     self.timeRecharged = world.current_time + self.rechargeTime
예제 #5
0
def hide(agent,
         attacker, 
         obstacles):
    distanceFromObstacleBoundary = 30
    closestDistance = None
    closestHidingPlace = None
    attackerPosition = attacker.getPosition()
    agentPosition = agent.getPosition()
    for obstacle in obstacles:
        obstaclePosition = obstacle.getPosition()
        hidingPlaceDistanceToObstacle = distanceFromObstacleBoundary + obstacle.getRadius()
        attackerToObstacle = calculate.subtractPoints(obstaclePosition,
                                                      attackerPosition)
        attackerDistanceToObstacle = vector.getMagnitude(attackerToObstacle)
        attackerDistanceToHidingPlace =  hidingPlaceDistanceToObstacle + attackerDistanceToObstacle
        attackerToHidingPlace = vector.setMagnitude(attackerToObstacle,
                                                    attackerDistanceToHidingPlace)

        hidingPlace = calculate.addPointAndVector(attackerPosition,
                                                  attackerToHidingPlace)
        
        agentToHidingPlace = calculate.subtractPoints(hidingPlace,
                                                      agentPosition)
        distanceToHidingPlace = vector.getMagnitude(agentToHidingPlace)
        
        if closestDistance is None or distanceToHidingPlace < closestDistance:
            closestDistance = distanceToHidingPlace
            closestHidingPlace = hidingPlace
        
    if closestHidingPlace is None:
        return evade.evade(agent,
                           attacker)
        
    return arrive.arrive(agent,
                         closestHidingPlace)
예제 #6
0
def seek(agent,
         targetPosition):
    maxSpeed = agent.getMaxSpeed()
    agentPosition = agent.getPosition()
    agentVelocity = agent.getVelocity()
    agentToTarget = calculate.subtractPoints(targetPosition,
                                             agentPosition)
    
    desiredVelocity = vector.setMagnitude(agentToTarget,
                                          maxSpeed)
    
    return calculate.subtractVectors(desiredVelocity,
                                     agentVelocity)
예제 #7
0
 def __init__(self,
              world,
              direction,
              owner,
              renderer=renderers.render_shot,
              height=.2,
              width=.2,
              single_speed=10,
              position=None,
              tile=None):
     super(Shot, self).__init__(world=world,
                                renderer=renderer,
                                height=height,
                                width=width,
                                position=position,
                                tile=tile)
     self.owner = owner
     self.single_speed = single_speed
     self.velocity = vector.setMagnitude(direction, self.single_speed)
예제 #8
0
def flee(agent,
         targetPosition,
         evadeDistanceSquared=None):
    steeringController = agent.getSteeringController()
    agentPosition = agent.getPosition()
    agentMaxSpeed = agent.getMaxSpeed()
    agentVelocity = agent.getVelocity()
    
    agentToTarget = calculate.subtractPoints(agentPosition,
                                             targetPosition)
    distanceSquaredToTarget = vector.getMagnitudeSquared(agentToTarget)

    if (evadeDistanceSquared is not None and 
        distanceSquaredToTarget > evadeDistanceSquared):
        return (0, 0)
    
    desiredVelocity = vector.setMagnitude(agentToTarget,
                                          agentMaxSpeed)
    
    return calculate.subtractVectors(desiredVelocity,
                                     agentVelocity)
예제 #9
0
 def test_scaleTwoToFive(self):
     startVector = (0, 2)
     newVector = vector.setMagnitude(startVector,
                                     5)
     self.assertEquals((0, 5),
                       newVector)
예제 #10
0
 def setSpeed(self, speed):
     self.velocity = vector.setMagnitude(self.velocity, speed)
예제 #11
0
 def setVelocityFromDirection(self, direction):
     self.velocity = vector.setMagnitude(direction, self.single_speed)