Exemplo n.º 1
0
 def applyGravity(self, ship, deltaTime):
     distance = tupleDistance(ship.getPos(),
                              vec3ToTuple(self.planet.getPos()))
     if distance > Game.GRAVITY_DISTANCE: return
     gravity = Game.GRAVITY / distance
     gravityVector = tupleNormalize(
         tupleSegment(ship.getPos(), vec3ToTuple(self.planet.getPos())))
     gravityVector = scaleTuple(gravityVector, gravity * deltaTime)
     ship.applyForce(gravityVector)
Exemplo n.º 2
0
 def applyGravity(self, ship, deltaTime):
   distance = tupleDistance(
     ship.getPos(),
     vec3ToTuple( self.planet.getPos() )
   )
   if distance > Game.GRAVITY_DISTANCE: return
   gravity = Game.GRAVITY/distance
   gravityVector = tupleNormalize(
     tupleSegment(
       ship.getPos(),
       vec3ToTuple( self.planet.getPos() )
     )
   )
   gravityVector = scaleTuple( gravityVector, gravity * deltaTime)
   ship.applyForce( gravityVector )
Exemplo n.º 3
0
    def warpShips(self, warpAxis):
        shipOne = self.ships[0]
        shipTwo = self.ships[1]

        shipOnePos = shipOne.getPos()
        shipTwoPos = shipTwo.getPos()

        furtherShip = None
        closerShip = None

        if shipOnePos == tupleFurthestDistance(
                vec3ToTuple(self.planet.getPos()), [shipOnePos, shipTwoPos]):
            furtherShip = shipOne
            closerShip = shipTwo
        else:
            closerShip = shipOne
            furtherShip = shipTwo

        furtherToCloser = tupleSegment(furtherShip.getPos(),
                                       closerShip.getPos())

        if warpAxis == 'x':
            furtherShip.setPos(
                (furtherShip.getPos()[0] + furtherToCloser[0] * 2,
                 furtherShip.getPos()[1]))
        elif warpAxis == 'y':
            furtherShip.setPos(
                (furtherShip.getPos()[0],
                 furtherShip.getPos()[1] + furtherToCloser[1] * 2))
Exemplo n.º 4
0
  def warpPlanet(self, warpAxis):
    planetPos = vec3ToTuple( self.planet.getPos() )
    planetToCamera = tupleSegment( planetPos, self.getCameraPos() )

    if warpAxis == 'x':
      self.planet.setPos(
        planetPos[0] + planetToCamera[0]*2,
        planetPos[1],
        0
      )
      self.planetCollGeom.setPosition(
        planetPos[0] + planetToCamera[0]*2,
        planetPos[1],
        0
      )
    elif warpAxis == 'y':
      self.planet.setPos(
        planetPos[0],
        planetPos[1] + planetToCamera[1]*2,
        0
      )
      self.planetCollGeom.setPosition(
        planetPos[0],
        planetPos[1] + planetToCamera[1]*2,
        0
      )
Exemplo n.º 5
0
 def updateStars(self, newCameraPos):
     # TODO: Add unit tests!
     self.stars.setPos(newCameraPos)
     cameraDeltaPos = tupleSegment(self.lastCameraPos,
                                   vec3ToTuple(newCameraPos))
     xRotation = Quat()
     xRotation.setFromAxisAngle(cameraDeltaPos[0] * .1, Vec3(0, 1, 0))
     yRotation = Quat()
     yRotation.setFromAxisAngle(-cameraDeltaPos[1] * .1, Vec3(1, 0, 0))
     newRotation = xRotation.multiply(yRotation)
     self.starsRotation *= newRotation
     self.stars.setQuat(self.starsRotation)
Exemplo n.º 6
0
    def warpPlanet(self, warpAxis):
        planetPos = vec3ToTuple(self.planet.getPos())
        planetToCamera = tupleSegment(planetPos, self.getCameraPos())

        if warpAxis == 'x':
            self.planet.setPos(planetPos[0] + planetToCamera[0] * 2,
                               planetPos[1], 0)
            self.planetCollGeom.setPosition(
                planetPos[0] + planetToCamera[0] * 2, planetPos[1], 0)
        elif warpAxis == 'y':
            self.planet.setPos(planetPos[0],
                               planetPos[1] + planetToCamera[1] * 2, 0)
            self.planetCollGeom.setPosition(
                planetPos[0], planetPos[1] + planetToCamera[1] * 2, 0)
Exemplo n.º 7
0
 def loadShips(self):
     shipOne = Ship(Game.NAME_SHIP_ONE, Game.START_POS_SHIP_ONE,
                    Game.START_HEADING_SHIP_ONE)
     shipTwo = Ship(Game.NAME_SHIP_TWO, Game.START_POS_SHIP_TWO,
                    Game.START_HEADING_SHIP_TWO)
     offlimits = [(vec3ToTuple(self.planet.getPos()), Game.GRAVITY_DISTANCE)
                  ]
     shipOne.setPos(self.generateRandomStartPos(offlimits))
     shipOne.heading = random.random() * 360
     shipTwo.heading = random.random() * 360
     offlimits.append((shipOne.getPos(), 150))
     shipTwo.setPos(self.generateRandomStartPos(offlimits))
     self.ships = []
     self.ships.append(shipOne)
     self.ships.append(shipTwo)
Exemplo n.º 8
0
    def restartGame(self):
        self.planet.setPos(Vec3())
        self.planetCollGeom.setPosition(Vec3())

        offlimits = [(vec3ToTuple(self.planet.getPos()), 102)]

        # Reset ship one
        self.ships[0].setPos(self.generateRandomStartPos(offlimits))
        self.ships[0].heading = random.random() * 360
        self.ships[0].setVel(Ship.VEL_DEFAULT)
        self.ships[0].isAlive = True
        self.ships[0].health = Ship.HEALTH
        self.ships[0].visualNode.show()
        for bullet in self.ships[0].bullets:
            bullet['isAlive'] = False

        offlimits.append((self.ships[0].getPos(), 128))

        # Reset ship two
        self.ships[1].setPos(self.generateRandomStartPos(offlimits))
        self.ships[1].heading = random.random() * 360
        self.ships[1].setVel(Ship.VEL_DEFAULT)
        self.ships[1].isAlive = True
        self.ships[1].health = Ship.HEALTH
        self.ships[1].visualNode.show()
        for bullet in self.ships[1].bullets:
            bullet['isAlive'] = False

        for s in self.ships:
            s.update(1 / 60.)

        self.gameFrames = 0

        playerOneScore = self.players[0].score
        playerTwoScore = self.players[1].score
        if playerOneScore >= Game.MATCH_MAX_POINTS:
            self.showGameWinnerText(self.players[0])
            self.players[0].score = 0
            self.players[1].score = 0
        if playerTwoScore >= Game.MATCH_MAX_POINTS:
            self.showGameWinnerText(self.players[1])
            self.players[0].score = 0
            self.players[1].score = 0
        playerOneScore = self.players[0].score
        playerTwoScore = self.players[1].score
        self.scorePlayerOne.setText('Score: ' + str(playerOneScore))
        self.scorePlayerTwo.setText('Score: ' + str(playerTwoScore))
Exemplo n.º 9
0
  def restartGame(self):
    self.planet.setPos( Vec3() )
    self.planetCollGeom.setPosition( Vec3() )

    offlimits = [ ( vec3ToTuple( self.planet.getPos() ), 102 ) ]
    
    # Reset ship one
    self.ships[0].setPos( self.generateRandomStartPos( offlimits ) )
    self.ships[0].heading = random.random()*360
    self.ships[0].setVel( Ship.VEL_DEFAULT )
    self.ships[0].isAlive = True
    self.ships[0].health = Ship.HEALTH
    self.ships[0].visualNode.show()
    for bullet in self.ships[0].bullets:
      bullet['isAlive'] = False

    offlimits.append( ( self.ships[0].getPos(), 128 ) )
    
    # Reset ship two
    self.ships[1].setPos( self.generateRandomStartPos( offlimits ) )
    self.ships[1].heading = random.random()*360
    self.ships[1].setVel( Ship.VEL_DEFAULT )
    self.ships[1].isAlive = True
    self.ships[1].health = Ship.HEALTH
    self.ships[1].visualNode.show()
    for bullet in self.ships[1].bullets:
      bullet['isAlive'] = False

    for s in self.ships:
      s.update( 1/60. )

    self.gameFrames = 0

    playerOneScore = self.players[0].score 
    playerTwoScore = self.players[1].score 
    if playerOneScore >= Game.MATCH_MAX_POINTS:
      self.showGameWinnerText(self.players[0])
      self.players[0].score = 0
      self.players[1].score = 0
    if playerTwoScore >= Game.MATCH_MAX_POINTS:
      self.showGameWinnerText(self.players[1])
      self.players[0].score = 0
      self.players[1].score = 0
    playerOneScore = self.players[0].score 
    playerTwoScore = self.players[1].score 
    self.scorePlayerOne.setText( 'Score: ' + str(playerOneScore) )
    self.scorePlayerTwo.setText( 'Score: ' + str(playerTwoScore) )
Exemplo n.º 10
0
 def updateStars(self, newCameraPos):
   # TODO: Add unit tests!
   self.stars.setPos(newCameraPos)
   cameraDeltaPos = tupleSegment(
     self.lastCameraPos,
     vec3ToTuple( newCameraPos )
   )
   xRotation = Quat()
   xRotation.setFromAxisAngle(
     cameraDeltaPos[0] * .1,
     Vec3( 0, 1, 0 )
   )
   yRotation = Quat()
   yRotation.setFromAxisAngle(
     -cameraDeltaPos[1] * .1,
     Vec3( 1, 0, 0 )
   )
   newRotation = xRotation.multiply( yRotation )
   self.starsRotation *= newRotation
   self.stars.setQuat( self.starsRotation )
Exemplo n.º 11
0
 def loadShips(self):
   shipOne = Ship(
     Game.NAME_SHIP_ONE,
     Game.START_POS_SHIP_ONE,
     Game.START_HEADING_SHIP_ONE
   )
   shipTwo = Ship(
     Game.NAME_SHIP_TWO,
     Game.START_POS_SHIP_TWO,
     Game.START_HEADING_SHIP_TWO
   )
   offlimits = [ ( vec3ToTuple( self.planet.getPos() ), Game.GRAVITY_DISTANCE ) ]
   shipOne.setPos( self.generateRandomStartPos( offlimits ) )
   shipOne.heading = random.random()*360
   shipTwo.heading = random.random()*360
   offlimits.append( ( shipOne.getPos(), 150 ) )
   shipTwo.setPos( self.generateRandomStartPos( offlimits ) )
   self.ships = []
   self.ships.append(shipOne)
   self.ships.append(shipTwo)
Exemplo n.º 12
0
  def warpShips(self, warpAxis):
    shipOne = self.ships[0]
    shipTwo = self.ships[1]

    shipOnePos = shipOne.getPos()
    shipTwoPos = shipTwo.getPos()

    furtherShip = None
    closerShip = None

    if shipOnePos == tupleFurthestDistance(
      vec3ToTuple( self.planet.getPos() ),
      [shipOnePos, shipTwoPos]
    ):
      furtherShip = shipOne
      closerShip = shipTwo
    else:
      closerShip = shipOne
      furtherShip = shipTwo

    furtherToCloser = tupleSegment(
      furtherShip.getPos(), closerShip.getPos()
    )

    if warpAxis == 'x':
      furtherShip.setPos(
        (
          furtherShip.getPos()[0] + furtherToCloser[0]*2,
          furtherShip.getPos()[1]
        )
      )
    elif warpAxis == 'y':
      furtherShip.setPos(
        (
          furtherShip.getPos()[0],
          furtherShip.getPos()[1] + furtherToCloser[1]*2
        )
      )
Exemplo n.º 13
0
 def distanceToPlanetSquared(self, pos):
     return tupleDistanceSquared(pos, vec3ToTuple(self.planet.getPos()))
Exemplo n.º 14
0
 def testVec3ToTuple(self):
   self.failUnlessEqual( vec3ToTuple( Vec3(3, 7, 0) ), (3, 7) )
Exemplo n.º 15
0
 def testVec3ToTuple(self):
     self.failUnlessEqual(vec3ToTuple(Vec3(3, 7, 0)), (3, 7))
Exemplo n.º 16
0
 def distanceToPlanetSquared(self, pos):
   return tupleDistanceSquared(
     pos,
     vec3ToTuple( self.planet.getPos() )
   )