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)
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 )
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))
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 )
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)
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)
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)
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))
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) )
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 )
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)
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 ) )
def distanceToPlanetSquared(self, pos): return tupleDistanceSquared(pos, vec3ToTuple(self.planet.getPos()))
def testVec3ToTuple(self): self.failUnlessEqual( vec3ToTuple( Vec3(3, 7, 0) ), (3, 7) )
def testVec3ToTuple(self): self.failUnlessEqual(vec3ToTuple(Vec3(3, 7, 0)), (3, 7))
def distanceToPlanetSquared(self, pos): return tupleDistanceSquared( pos, vec3ToTuple( self.planet.getPos() ) )