def __updateNonlocalVehicle(self, task = None): self.curSpeed = self.smoother.getSmoothForwardVelocity() rotSpeed = -1 * self.smoother.getSmoothRotationalVelocity() self.leanAmount = self.curSpeed * rotSpeed / 500.0 self.leanAmount = clampScalar(self.leanAmount, -10, 10) self.__animate() return Task.cont
def getExtremeness(self, traitValue): percent = self._getTraitPercent(traitValue) if percent < 0.5: howExtreme = (0.5 - percent) * 2.0 else: howExtreme = (percent - 0.5) * 2.0 return clampScalar(howExtreme, 0.0, 1.0)
def __animate(self): speed = self.curSpeed self.spinWheels(speed / 10) enginePitch = clampScalar(speed / 120.0, 0.5, 15) self.kartLoopSfx.setPlayRate(enginePitch) if not self.localVehicle: dist = (self.getPos() - localAvatar.getPos()).length() if self.localVehicle: if self.lifter.isOnGround(): if self.offGround > 10: kart = self.geom[0].find('**/main*') bumpDown1 = kart.posInterval(0.1, Vec3(0, 0, -1)) bumpUp1 = kart.posInterval(0.15, Vec3(0, 0, 0)) bumpDown2 = kart.posInterval(0.5, Vec3(0, 0, -.4)) bumpUp2 = kart.posInterval(0.7, Vec3(0, 0, 0)) bumpSeq = Sequence(bumpDown1, bumpUp1, bumpDown2, bumpUp2) bumpSeq.start() self.offGround = 0 else: self.offGround += 1 if self.offGround == 0: modifier = self.surfaceModifiers[self.groundType]['shake'] * (speed / 50.0) else: modifier = 1 roll = self.leanAmount * 1.5 roll += (random.random() * 2 - 1) * modifier pitch = self.acceleration * -.005 pitch += (random.random() * 2 - 1) * modifier self.rollSuspension(roll) self.pitchSuspension(pitch) return Task.cont
def stickCarToGround(self): posList = [] nWheels = len(self.wheelData) for nWheel in xrange(nWheels): cQueue = self.cQueue[nWheel] cQueue.sortEntries() if cQueue.getNumEntries() == 0: return entry = cQueue.getEntry(0) self.shakeWheel(nWheel, entry.getIntoNodePath()) pos = entry.getSurfacePoint(render) wheelPos = self.wheelBases[nWheel].getPos(render) pos = wheelPos - pos posList.append(pos) cQueue.clearEntries() rf = posList[0].getZ() lf = posList[1].getZ() rr = posList[2].getZ() lr = posList[3].getZ() right = (rf + rr) / 2 left = (lf + lr) / 2 rollVal = right - left rollVal = clampScalar(rollVal, -1, 1) curRoll = self.getR() newRoll = curRoll + rollVal * 2.0 self.setR(newRoll) if not self.stopped: camera.setR(-newRoll) front = (rf + lf) / 2 rear = (rr + lr) / 2 center = (front + rear) / 2 pitchVal = front - rear pitchVal = clampScalar(pitchVal, -1, 1) curPitch = self.getP() newPitch = curPitch - pitchVal * 2.0 self.setP((newPitch + curPitch) / 2.0) if self.proRacer: self.cameraNode.setP(-newPitch) elif not self.stopped: self.cameraNode.setP(-newPitch)
def setTrickAptitude(self, trickId, aptitude, send = 1): aptitude = clampScalar(aptitude, 0.0, 1.0) aptitudes = self.trickAptitudes while len(aptitudes) - 1 < trickId: aptitudes.append(0.0) if aptitudes[trickId] != aptitude: aptitudes[trickId] = aptitude if send: self.b_setTrickAptitudes(aptitudes) else: self.setTrickAptitudes(aptitudes, local=1)
def setTrickAptitude(self, trickId, aptitude, send=1): aptitude = clampScalar(aptitude, 0.0, 1.0) aptitudes = self.trickAptitudes while len(aptitudes) - 1 < trickId: aptitudes.append(0.0) if aptitudes[trickId] != aptitude: aptitudes[trickId] = aptitude if send: self.b_setTrickAptitudes(aptitudes) else: self.setTrickAptitudes(aptitudes, local=1)
def __watchControls(self, task): dt = globalClock.getDt() curVelocity = self.actorNode.getPhysicsObject().getVelocity() curSpeed = curVelocity.length() fvec = self.forward.getPos(render) - self.getPos(render) fvec.normalize() dotProduct = curVelocity.dot(fvec) goingBack = -1 if dotProduct < 0: goingBack = 1 if self.proRacer: self.__computeTurnRatio(curSpeed) self.__updateWheelPos(dt, curSpeed) newHForTurning = self.getH() if self.proRacer or not self.stopped and self.arrowHorz and curSpeed > 1: if self.proRacer: turnHelp = 0 if self.hittingWall or goingBack == 1: turnHelp = self.arrowHorz effectiveSpeed = curSpeed if effectiveSpeed > self.speedMinTurn: effectiveSpeed = self.speedMinTurn rotation = -goingBack * (self.wheelPosition * dt * self.turnRatio * -1.8 * curSpeed / 100 + turnHelp * dt * self.turnRatio * -1.2) self.outPutCounter += 1 if self.outPutCounter > 5: self.outPutCounter = 0 else: rotation = self.arrowHorz * dt * self.turnRatio * -1.2 oldH = self.getH() newH = (oldH + rotation) % 360 self.setH(newH) newHForTurning = newH if self.groundType == 'ice': newHForTurning = (oldH + rotation * iceTurnFactor) % 360 pitch = -self.getP() + 5 accelBase = self.accelerationBase pitch += accelBase pitch = clampScalar(pitch, accelBase - 5, accelBase + 5) self.accelerationMult = pitch * 2 if self.groundType == 'ice': self.accelerationMult *= iceAccelFactor if self.stopped: self.acceleration = 0 else: self.acceleration = self.arrowVert * self.accelerationMult * self.cheatFactor if self.proRacer: if self.skidding: self.acceleration = self.arrowVert * self.accelerationMult * self.cheatFactor * 0.5 if self.turbo: self.acceleration += self.accelerationMult * 1.5 self.engine.setVector(Vec3(0, self.acceleration, 0)) if self.groundType == 'ice': rotMat = Mat3.rotateMatNormaxis(newHForTurning, Vec3.up()) else: rotMat = Mat3.rotateMatNormaxis(self.getH(), Vec3.up()) curHeading = rotMat.xform(Vec3.forward()) push = (3 - self.getP()) * 0.02 curHeading.setZ(curHeading.getZ() - min(0.2, max(-.2, push))) onScreenDebug.append('vehicle curHeading = %s\n' % curHeading.pPrintValues()) onScreenDebug.append('vehicle H = %s newHForTurning=%f\n' % (self.getH(), newHForTurning)) windResistance = self.surfaceModifiers[self.groundType]['windResistance'] self.windResistance.setCoef(windResistance) physicsFrame = int((globalClock.getFrameTime() - self.physicsEpoch) * self.physicsCalculationsPerSecond) numFrames = min(physicsFrame - self.lastPhysicsFrame, self.maxPhysicsFrames) self.lastPhysicsFrame = physicsFrame leanIncrement = self.arrowHorz * self.physicsDt * self.turnRatio if self.stopped: leanIncrement = 0 driftMin = self.surfaceModifiers[self.groundType]['driftMin'] if self.proRacer: driftMin = self.surfaceModifiers[self.groundType]['driftMin'] * 0.2 if self.skidding: driftMin = self.surfaceModifiers[self.groundType]['driftMin'] for i in xrange(int(numFrames)): self.physicsMgr.doPhysics(self.physicsDt) curVelocity = self.actorNode.getPhysicsObject().getVelocity() idealVelocity = curHeading * curSpeed curVelocity *= self.imHitMult driftVal = abs(self.leanAmount) * 16 / self.cheatFactor + 15 / self.cheatFactor curVelocity = Vec3((curVelocity * driftVal + idealVelocity) / (driftVal + 1)) curSpeed = curVelocity.length() curVelocity.normalize() curVelocity *= min(curSpeed, 600) self.actorNode.getPhysicsObject().setVelocity(curVelocity) curSpeed = curVelocity.length() speedFactor = min(curSpeed, 150) / 162.0 self.leanAmount = (self.leanAmount + leanIncrement) * speedFactor self.leanAmount = clampScalar(self.leanAmount, -10, 10) self.cWallTrav.traverse(render) self.curSpeed = curSpeed if self.proRacer: self.turnWheels(self.wheelPosition * -45) else: self.turnWheels(self.arrowHorz * -10) self.__animate() if self.proRacer: speedProporation = 1.0 if curSpeed < self.speedMaxTurn: speedProporation = 0.0 else: speedProporation = (curSpeed - self.speedMaxTurn) / self.speedMinTurn cameraDist = self.cameraArmBase + self.cameraArmExtend * speedProporation cameraOffset = Point3(0, -cameraDist, 0) self.cameraNode.setPos(cameraOffset) behindPos = render.getRelativePoint(self, Point3(0, -30, 0)) self.proCameraDummyNode.setPos(render, behindPos) self.proCameraDummyNode.lookAt(self) self.cameraNode.lookAt(self) dir1 = self.proCameraDummyNode.getH() dir2 = self.proCameraHeading if dir1 > 180: dir1 -= 360 elif dir1 < -180: dir1 += 360 if dir2 > 180: dir2 -= 360 elif dir2 < -180: dir2 += 360 self.proCameraHeading = dir2 dif = dir1 - dir2 if dif > 180: dif -= 360 elif dif < -180: dif += 360 speedDif = abs(dif) if speedDif > 90: speedDif = 90 cameraTightener = 1.0 if curSpeed > self.speedMinTurn: cameraTightener = self.cameraTightener else: cameraTightener = 1.0 + curSpeed / self.speedMinTurn * (self.cameraTightener - 1.0) swingSpeedRatio = speedDif / 90 swingSpeed = self.armSwingSpeedPerp * swingSpeedRatio + self.armSwingSpeedPara * (1 - swingSpeedRatio) self.proCameraHeading += dif * cameraTightener * (dt / swingSpeed) self.cameraArmNode.setH(self.proCameraHeading - self.getH()) elif not self.stopped: self.cameraNode.setH(self.leanAmount) self.updateParticles(self.leanAmount) if (self.leanAmount > 8 or self.leanAmount < -8) and self.offGround <= 0: self.startSkid() else: self.stopSkid() if self.speedometer: self.speedometer['text'] = str(int(curSpeed / 3)) self.speedGauge.setR(min(110, max(0, curSpeed / 3 / 120 * 110))) if not self.stopped: self.stickCarToGround() if self.__clampPosition(): self.notify.debug('did a clampPosition on %d' % self.doId) return Task.cont
def update(self, time): placeSorter = [] placeCount = 0 for key in self.racerDict.keys(): racer = self.racerDict[key] curvetime = racer.curvetime face = racer.face mapspot = racer.mapspot maxlaphit = racer.maxlaphit if not racer.finished and racer.enabled: placeSorter.append((curvetime, key)) if racer.finished or racer.enabled: placeCount += 1 pt = Vec3(0, 0, 0) mapT = (curvetime % 1 + self.race.startT / self.race.curve.getMaxT()) % 1 * self.race.curve.getMaxT() self.race.curve.getPoint(mapT, pt) self.race.curve.getPoint(mapT % self.race.curve.getMaxT(), pt) lapT = clampScalar(curvetime / self.race.lapCount, 0.0, 1.0) faceX = self.faceStartPos[0] * (1 - lapT) + self.faceEndPos[0] * lapT racer.update(faceX=faceX, mapspotPt=pt) t = time - self.race.baseTime - self.raceTimeDelta if key == localAvatar.doId: if self.race.laps > maxlaphit: racer.update(maxlaphit=self.race.laps) self.maxLapHit = racer.maxlaphit if self.maxLapHit < self.race.lapCount: for y in self.timeLabels[self.maxLapHit - 1]: y.configure( text_font=ToontownGlobals.getSignFont()) for y in self.timeLabels[self.maxLapHit]: y.show() for y in self.timeLabels[self.maxLapHit]: y.configure( text_font=ToontownGlobals.getSignFont()) self.raceTimeDelta = globalClock.getFrameTime( ) - self.race.baseTime lapNotice = DirectLabel() lapNotice.setScale(0.1) if self.maxLapHit == self.race.lapCount - 1: lapNotice[ 'text'] = TTLocalizer.KartRace_FinalLapText else: lapNotice[ 'text'] = TTLocalizer.KartRace_LapText % str( self.maxLapHit + 1) taskMgr.doMethodLater(2, lapNotice.remove, 'removeIt', extraArgs=[]) self.lapLabel['text'] = str( clampScalar(self.maxLapHit + 1, 1, self.race.lapCount)) + '/' + str( self.race.lapCount) suffix = { 1: TTLocalizer.KartRace_FirstSuffix, 2: TTLocalizer.KartRace_SecondSuffix, 3: TTLocalizer.KartRace_ThirdSuffix, 4: TTLocalizer.KartRace_FourthSuffix } placeSorter.sort() for x, p in zip(placeSorter, xrange(len(placeSorter), 0, -1)): self.racerDict[x[1]].update(place=p + placeCount - len(placeSorter)) localRacer = self.racerDict[localAvatar.doId] nearDiff, farDiff = RaceGlobals.TrackDict[self.race.trackId][8] if not localRacer.finished and self.faceEndPos[ 0] - localRacer.face.getX() < nearDiff: for racerId in self.racerDict.keys(): racer = self.racerDict[racerId] if not racer.enabled or racerId == localAvatar.doId or racer.face.getX( ) >= self.faceEndPos[0]: continue if self.faceEndPos[0] - racer.face.getX() < farDiff: self.photoFinish = True if self.photoFinish: self.photoFinishLabel.show() self.placeLabelNum['text'] = '' self.placeLabelStr['text'] = '' else: self.photoFinishLabel.hide() self.placeLabelNum['text'] = str( self.racerDict[localAvatar.doId].place) self.placeLabelStr['text'] = suffix[self.racerDict[ localAvatar.doId].place] minutes = int(t / 60) t -= minutes * 60 seconds = int(t) padding = (seconds < 10 and ['0'] or [''])[0] t -= seconds fraction = str(t)[2:4] fraction = fraction + '0' * (2 - len(fraction)) if self.timerEnabled and self.maxLapHit < self.race.lapCount: self.timeLabels[self.maxLapHit][0]['text'] = "%d'" % minutes self.timeLabels[self.maxLapHit][1]['text'] = "%s%d''" % (padding, seconds) self.timeLabels[self.maxLapHit][2]['text'] = '%s' % fraction if self.race.wrongWay and not self.wrongWaySeq.isPlaying(): self.wrongWaySeq.loop() elif not self.race.wrongWay and self.wrongWaySeq.isPlaying(): self.wrongWaySeq.finish()
def addToMood(self, component, delta): value = self.mood.getComponent(component) value += delta self.setMoodComponent(component, clampScalar(value, 0.0, 1.0))
def update(self, time): placeSorter = [] placeCount = 0 for key in self.racerDict.keys(): racer = self.racerDict[key] curvetime = racer.curvetime face = racer.face mapspot = racer.mapspot maxlaphit = racer.maxlaphit if not racer.finished and racer.enabled: placeSorter.append((curvetime, key)) if racer.finished or racer.enabled: placeCount += 1 pt = Vec3(0, 0, 0) mapT = (curvetime % 1 + self.race.startT / self.race.curve.getMaxT()) % 1 * self.race.curve.getMaxT() self.race.curve.getPoint(mapT, pt) self.race.curve.getPoint(mapT % self.race.curve.getMaxT(), pt) lapT = clampScalar(curvetime / self.race.lapCount, 0.0, 1.0) faceX = self.faceStartPos[0] * (1 - lapT) + self.faceEndPos[0] * lapT racer.update(faceX=faceX, mapspotPt=pt) t = time - self.race.baseTime - self.raceTimeDelta if key == localAvatar.doId: if self.race.laps > maxlaphit: racer.update(maxlaphit=self.race.laps) self.maxLapHit = racer.maxlaphit if self.maxLapHit < self.race.lapCount: for y in self.timeLabels[self.maxLapHit - 1]: y.configure(text_font=ToontownGlobals.getSignFont()) for y in self.timeLabels[self.maxLapHit]: y.show() for y in self.timeLabels[self.maxLapHit]: y.configure(text_font=ToontownGlobals.getSignFont()) self.raceTimeDelta = globalClock.getFrameTime() - self.race.baseTime lapNotice = DirectLabel() lapNotice.setScale(0.1) if self.maxLapHit == self.race.lapCount - 1: lapNotice['text'] = TTLocalizer.KartRace_FinalLapText else: lapNotice['text'] = TTLocalizer.KartRace_LapText % str(self.maxLapHit + 1) taskMgr.doMethodLater(2, lapNotice.remove, 'removeIt', extraArgs=[]) self.lapLabel['text'] = str(clampScalar(self.maxLapHit + 1, 1, self.race.lapCount)) + '/' + str(self.race.lapCount) suffix = {1: TTLocalizer.KartRace_FirstSuffix, 2: TTLocalizer.KartRace_SecondSuffix, 3: TTLocalizer.KartRace_ThirdSuffix, 4: TTLocalizer.KartRace_FourthSuffix} placeSorter.sort() for x, p in zip(placeSorter, xrange(len(placeSorter), 0, -1)): self.racerDict[x[1]].update(place=p + placeCount - len(placeSorter)) localRacer = self.racerDict[localAvatar.doId] nearDiff, farDiff = RaceGlobals.TrackDict[self.race.trackId][8] if not localRacer.finished and self.faceEndPos[0] - localRacer.face.getX() < nearDiff: for racerId in self.racerDict.keys(): racer = self.racerDict[racerId] if not racer.enabled or racerId == localAvatar.doId or racer.face.getX() >= self.faceEndPos[0]: continue if self.faceEndPos[0] - racer.face.getX() < farDiff: self.photoFinish = True if self.photoFinish: self.photoFinishLabel.show() self.placeLabelNum['text'] = '' self.placeLabelStr['text'] = '' else: self.photoFinishLabel.hide() self.placeLabelNum['text'] = str(self.racerDict[localAvatar.doId].place) self.placeLabelStr['text'] = suffix[self.racerDict[localAvatar.doId].place] minutes = int(t / 60) t -= minutes * 60 seconds = int(t) padding = (seconds < 10 and ['0'] or [''])[0] t -= seconds fraction = str(t)[2:4] fraction = fraction + '0' * (2 - len(fraction)) if self.timerEnabled and self.maxLapHit < self.race.lapCount: self.timeLabels[self.maxLapHit][0]['text'] = "%d'" % minutes self.timeLabels[self.maxLapHit][1]['text'] = "%s%d''" % (padding, seconds) self.timeLabels[self.maxLapHit][2]['text'] = '%s' % fraction if self.race.wrongWay and not self.wrongWaySeq.isPlaying(): self.wrongWaySeq.loop() elif not self.race.wrongWay and self.wrongWaySeq.isPlaying(): self.wrongWaySeq.finish()
def doDrift(curValue, timeToMedian, dt = float(dt)): newValue = curValue + dt / (timeToMedian * 7200) return clampScalar(newValue, 0.0, 1.0)