class Pointer(): def __init__(self): #----------------------------------------------------------------------------------------------------------------------------------------------- #---------------------------------------------------VARIABLE DECLARATION------------------------------------------------------------------------ #----------------------------------------------------------------------------------------------------------------------------------------------- self.bitmap = OnscreenImage(image="./tex/sun_cursor.png", pos=(0, 0, 0), hpr=None, scale=0.05, color=None, parent=None, sort=200) self.bitmap.setTransparency(TransparencyAttrib.MAlpha) #end __init__ #----------------------------------------------------------------------------------------------------------------------------------------------- #----------------------------------------------------------------FUNCTIONS---------------------------------------------------------------------- #----------------------------------------------------------------------------------------------------------------------------------------------- # It sets the x position given def setPosX(self, x): self.posX = x * 1.3 self.bitmap.setX(self.posX) #end setPosX # It sets the y position given def setPosY(self, y): self.posY = y self.bitmap.setZ(self.posY)
class LifeBar: def __init__(self): ## Life bar self.image = OnscreenImage(image="image/life_bar.png",pos=(0, 0, 0), scale=(256.0/base.win.getXSize(),1,32.0/base.win.getYSize()), parent=render2d) self.image.setTransparency(TransparencyAttrib.MAlpha) self.image.setZ(-0.9) self.image.setY(2) def __del__(self): self.image.destroy()
class LifeBar: def __init__(self): ## Life bar self.image = OnscreenImage(image="image/life_bar.png", pos=(0, 0, 0), scale=(256.0 / base.win.getXSize(), 1, 32.0 / base.win.getYSize()), parent=render2d) self.image.setTransparency(TransparencyAttrib.MAlpha) self.image.setZ(-0.9) self.image.setY(2) def __del__(self): self.image.destroy()
class English(HUDElement): def __init__(self): HUDElement.__init__(self) self.dir = ani.model_dir / 'hud' / 'english' self.text_scale = 0.2 self.text_color = (1, 1, 1, 1) self.circle = OnscreenImage(image=panda_path(self.dir / 'circle.png'), parent=self.dummy_right, scale=0.15) self.circle.setTransparency(TransparencyAttrib.MAlpha) autils.alignTo(self.circle, self.dummy_right, autils.CL, autils.C) self.circle.setZ(-0.65) self.crosshairs = OnscreenImage(image=panda_path(self.dir / 'crosshairs.png'), pos=(0, 0, 0), parent=self.circle, scale=0.14) self.crosshairs.setTransparency(TransparencyAttrib.MAlpha) self.text = OnscreenText( text="(0.00, 0.00)", pos=(0, -1.15), scale=self.text_scale, fg=self.text_color, align=TextNode.ACenter, mayChange=True, parent=self.circle, ) def set(self, a, b): self.crosshairs.setPos(-a, 0, b) self.text.setText(f"({a:.2f},{b:.2f})") def init(self): self.show() def show(self): self.circle.show() def hide(self): self.circle.hide() def destroy(self): self.hide() del self.text del self.crosshairs del self.circle
class Pointer(): def __init__(self): #----------------------------------------------------------------------------------------------------------------------------------------------- #---------------------------------------------------VARIABLE DECLARATION------------------------------------------------------------------------ #----------------------------------------------------------------------------------------------------------------------------------------------- self.bitmap = OnscreenImage(image="./tex/sun_cursor.png", pos = (0, 0, 0),hpr=None, scale=0.05, color=None, parent=None, sort=200) self.bitmap.setTransparency(TransparencyAttrib.MAlpha) #end __init__ #----------------------------------------------------------------------------------------------------------------------------------------------- #----------------------------------------------------------------FUNCTIONS---------------------------------------------------------------------- #----------------------------------------------------------------------------------------------------------------------------------------------- # It sets the x position given def setPosX(self, x): self.posX = x*1.3 self.bitmap.setX(self.posX) #end setPosX # It sets the y position given def setPosY(self, y): self.posY = y self.bitmap.setZ(self.posY) #end setPosY
class Jack(HUDElement): def __init__(self): HUDElement.__init__(self) self.dir = ani.model_dir / 'hud' / 'jack' self.text_scale = 0.4 self.text_color = (1, 1, 1, 1) self.arc = OnscreenImage(image=panda_path(self.dir / 'arc.png'), pos=(1.4, 0, -0.45), parent=aspect2d, scale=0.075) self.arc.setTransparency(TransparencyAttrib.MAlpha) self.cue_cartoon = OnscreenImage( image=panda_path(self.dir / 'cue.png'), parent=aspect2d, pos=(0, 0, 0), scale=(0.15, 1, 0.01), ) self.cue_cartoon.setTransparency(TransparencyAttrib.MAlpha) autils.alignTo(self.cue_cartoon, self.dummy_right, autils.CL, autils.C) self.cue_cartoon.setZ(-0.40) autils.alignTo(self.arc, self.cue_cartoon, autils.LR, autils.CR) self.rotational_point = OnscreenImage(image=panda_path( ani.model_dir / 'hud' / 'english' / 'circle.png'), parent=self.arc, scale=0.15) self.rotational_point.setTransparency(TransparencyAttrib.MAlpha) autils.alignTo(self.rotational_point, self.arc, autils.C, autils.LR) self.cue_cartoon.wrtReparentTo(self.rotational_point) self.text = OnscreenText( text="0 deg", pos=(-1, -1.4), scale=self.text_scale, fg=self.text_color, align=TextNode.ACenter, mayChange=True, parent=self.arc, ) def set(self, theta): self.text.setText(f"{theta:.1f} deg") self.rotational_point.setR(theta) def init(self): self.show() def show(self): self.arc.show() self.cue_cartoon.show() def hide(self): self.arc.hide() self.cue_cartoon.hide() def destroy(self): self.hide() del self.arc
class SMGUICounter(): def __init__(self, icon, maxValue): self.value = 0 self.life = 0.0 self.basePos = Vec2(0.7, POS_TOP) self.maxValue = maxValue # Must be 1 or higher to activate. self.iconImg = OnscreenImage(image = ("../res/icons/gui_" + str(icon) + ".png"), pos = (self.basePos.getX(), 0, self.basePos.getY()), scale = 0.1) self.iconImg.setTransparency(TransparencyAttrib.MAlpha) strValue = str(self.value) if(self.maxValue > 0): strValue += ("/" + str(self.maxValue)) self.textObj = OnscreenText(text = strValue, style = 1, fg = (0,0,0,1), pos = (self.basePos.getX() + COUNT_OFFSET_X, self.basePos.getY() + COUNT_OFFSET_Y), align = TextNode.ARight, scale = .2) self.state = ST_IDLE #------------------------------------------------------------------------------------------------------------------------------------------------------------ # Returns the GUI element type. #------------------------------------------------------------------------------------------------------------------------------------------------------------ def getGUIType(self): return 1 #------------------------------------------------------------------------------------------------------------------------------------------------------------ # Summons a 100-foot tall unicorn with chainsaws strapped to its back. #------------------------------------------------------------------------------------------------------------------------------------------------------------ def updateGUI(self): dt = globalClock.getDt() state = self.state # State-based control if(state == ST_PEEK_IN): y = self.getYPos() if(y <= MIN_PEEK_HEIGHT): self.setState(ST_PEEK_WAIT) else: self.setYPos(y - PEEK_RATE * dt) elif(state == ST_PEEK_OUT): y = self.getYPos() if(y >= MAX_PEEK_HEIGHT): self.setState(ST_IDLE) self.life = 0.0 else: self.setYPos(y + PEEK_RATE * dt) elif(state == ST_PEEK_WAIT): if(self.getLife() > PEEK_TIME): self.setState(ST_PEEK_OUT) else: self.addLife() self.updateText() self.updatePos() def updatePos(self): x = self.basePos.getX() y = self.basePos.getY() self.iconImg.setX(x) self.iconImg.setZ(y) self.textObj.setX(x + COUNT_OFFSET_X) self.textObj.setY(y + COUNT_OFFSET_Y) def updateText(self): txt = str(self.value) if(self.maxValue > 0): txt += ("/" + str(self.maxValue)) self.textObj.setText(txt) #------------------------------------------------------------------------------------------------------------------------------------------------------------ # Returns the peek state as an integer. #------------------------------------------------------------------------------------------------------------------------------------------------------------ def getState(self): return self.state #------------------------------------------------------------------------------------------------------------------------------------------------------------ # Sets the peek state. # setState(int a {0 - 3}) #------------------------------------------------------------------------------------------------------------------------------------------------------------ def setState(self, state): self.state = state; def setMaxValue(self, value): self.maxValue = value #------------------------------------------------------------------------------------------------------------------------------------------------------------ # Adds 1 to the peek life counter. #------------------------------------------------------------------------------------------------------------------------------------------------------------ def addLife(self): self.life += globalClock.getDt() #------------------------------------------------------------------------------------------------------------------------------------------------------------ # Resets the peek life counter to 0. #------------------------------------------------------------------------------------------------------------------------------------------------------------ def resetLife(self): self.life = 0.0 #------------------------------------------------------------------------------------------------------------------------------------------------------------ # Makes this class get a freaking life and stop sitting on its lazy a- Wait, what? Oh, returns the current peek life. #------------------------------------------------------------------------------------------------------------------------------------------------------------ def getLife(self): return self.life #------------------------------------------------------------------------------------------------------------------------------------------------------------ # Gets the value of the tracked variable. #------------------------------------------------------------------------------------------------------------------------------------------------------------ def getValue(self): return self.value #------------------------------------------------------------------------------------------------------------------------------------------------------------ # Changes the variable behind the scenes with no peeking. #------------------------------------------------------------------------------------------------------------------------------------------------------------ def setValue(self, value): self.value = value #------------------------------------------------------------------------------------------------------------------------------------------------------------ # Adds a value to the tracked variable and peeks the counter out. Use negatives to subtract. #------------------------------------------------------------------------------------------------------------------------------------------------------------ def increment(self): self.setState(ST_PEEK_IN) self.value += 1 #------------------------------------------------------------------------------------------------------------------------------------------------------------ # Returns the OnScreenText object. #------------------------------------------------------------------------------------------------------------------------------------------------------------ def getTextObj(self): return self.textObj #------------------------------------------------------------------------------------------------------------------------------------------------------------ # Gets the vertical position of the text. #------------------------------------------------------------------------------------------------------------------------------------------------------------ def getYPos(self): return self.basePos.getY() #------------------------------------------------------------------------------------------------------------------------------------------------------------ # Sets the Y position of both the label and value. #------------------------------------------------------------------------------------------------------------------------------------------------------------ def setYPos(self, ypos): self.basePos = Vec2(self.basePos.getX(), ypos)
class Unit: def __init__(self, unitID, position, mainClass): # self = copy.copy(mainClass.unitHandler.unitDict[mainClass.unitHandler.main['units'][unitID]]) self.model = mainClass.unitHandler.unitDict[mainClass.unitHandler.main["units"][unitID]].model self.fullName = copy.copy(mainClass.unitHandler.unitDict[mainClass.unitHandler.main["units"][unitID]].fullName) self.HP = copy.copy(mainClass.unitHandler.unitDict[mainClass.unitHandler.main["units"][unitID]].HP) self.info = copy.copy(mainClass.unitHandler.unitDict[mainClass.unitHandler.main["units"][unitID]].info) self.moveType = copy.copy(mainClass.unitHandler.unitDict[mainClass.unitHandler.main["units"][unitID]].moveType) self.model = copy.copy(mainClass.unitHandler.unitDict[mainClass.unitHandler.main["units"][unitID]].model) self.radius = copy.copy(mainClass.unitHandler.unitDict[mainClass.unitHandler.main["units"][unitID]].radius) self.mass = copy.copy(mainClass.unitHandler.unitDict[mainClass.unitHandler.main["units"][unitID]].mass) self.startForce = copy.copy( mainClass.unitHandler.unitDict[mainClass.unitHandler.main["units"][unitID]].startForce ) self.maxForce = copy.copy(mainClass.unitHandler.unitDict[mainClass.unitHandler.main["units"][unitID]].maxForce) self.dig = copy.copy(mainClass.unitHandler.unitDict[mainClass.unitHandler.main["units"][unitID]].dig) self.reinforce = copy.copy( mainClass.unitHandler.unitDict[mainClass.unitHandler.main["units"][unitID]].reinforce ) self.shovel = copy.copy(mainClass.unitHandler.unitDict[mainClass.unitHandler.main["units"][unitID]].shovel) self.hold = copy.copy(mainClass.unitHandler.unitDict[mainClass.unitHandler.main["units"][unitID]].hold) self.modelHeight = copy.copy( mainClass.unitHandler.unitDict[mainClass.unitHandler.main["units"][unitID]].modelHeight ) self.selectScale = copy.copy( mainClass.unitHandler.unitDict[mainClass.unitHandler.main["units"][unitID]].selectScale ) self.job = False self.uID = mainClass.unitHandler.unitUniqueID print self.uID self.modelNode = loader.loadModel(self.model) self.modelNode.setName("unit " + str(self.uID).zfill(3)) self.modelNode.reparentTo(render) self.modelNode.setPos(position) self.modelNode.setCollideMask(BitMask32.bit(1)) self.select = OnscreenImage(image="data/models/game/selected.png") self.select.setScale(float(self.selectScale)) self.select.reparentTo(self.modelNode) self.select.setZ(float(self.modelHeight) / 2) self.select.setTransparency(TransparencyAttrib.MAlpha) self.select.setBillboardPointEye() self.select.hide() self.groundRay = CollisionRay() self.groundRay.setOrigin(0, 0, 100) self.groundRay.setDirection(0, 0, -1) self.groundCol = CollisionNode("unit Ray") self.groundCol.addSolid(self.groundRay) self.groundCol.setTag("units", "ray1") self.groundCol.setFromCollideMask(BitMask32.bit(0)) self.groundCol.setIntoCollideMask(BitMask32.allOff()) self.groundColNp = self.modelNode.attachNewNode(self.groundCol) self.groundColNp.setPos(0, 0, 0) self.groundHandler = CollisionHandlerFloor() self.groundHandler.setMaxVelocity(100) base.cTrav2.addCollider(self.groundColNp, self.groundHandler) self.groundHandler.addCollider(self.groundColNp, self.modelNode) self.AI = AICharacter(self.fullName, self.modelNode, self.mass * 2, self.startForce * 2, self.maxForce * 2) mainClass.unitHandler.AIWorld.addAiChar(self.AI) self.AIBehaviors = self.AI.getAiBehaviors() if self.moveType == "ground": self.aStar = astar.aStar(mainClass.unitHandler.meshes.landMesh, mainClass) elif self.moveType == "water": self.aStar = astar.aStar(mainClass.unitHandler.meshes.waterMesh, mainClass) elif self.moveType == "air": self.aStar = astar.aStar(mainClass.unitHandler.meshes.airMesh, mainClass) print self
class World(DirectObject): def __init__(self): self.start = False self.mainScreen = OnscreenImage(image = 'mainScreen.png') #self.mainScreen.setScale(1.25) self.accept("mouse1", self.begin) def begin(self): if self.start == False: self.start = True self.mainScreen.destroy() self.__init__1() def __init__1(self): #load physics base.enableParticles() #create a traverser base.cTrav = CollisionTraverser() self.cevent = CollisionHandlerEvent() self.cevent.addInPattern('into-%in') self.cevent.addOutPattern('outof-%in') #load all the models self.w_terrain = Terrain() self.p_bike = PlayerBike(base.cTrav) #disable mouse base.disableMouse() self.dead = False self.deadCount = 0 taskMgr.add(self.gameOverDead, 'deadTask') self.win = False self.winCount = 0 taskMgr.add(self.gameOverWin, 'winTask') #load and play background sound backgroundSound = base.loader.loadSfx('Modern_Battlefield.mp3') backgroundSound.setLoop(True) backgroundSound.play() #parent the camera to the player bike and offset the initial location base.camera.reparentTo(self.p_bike.bike) base.camera.setZ(6) base.camera.setP(-8) base.camera.setY(-32) #set up accept tasks #close the game self.accept("escape", sys.exit) #handle movement self.accept("arrow_up", self.p_bike.setDirection, ["forward", 1]) self.accept("arrow_right", self.p_bike.setDirection, ["right", 1]) self.accept("arrow_left", self.p_bike.setDirection, ["left", 1]) self.accept("arrow_up-up", self.p_bike.setDirection, ["forward", 0]) self.accept("arrow_right-up", self.p_bike.setDirection, ["right", 0]) self.accept("arrow_left-up", self.p_bike.setDirection, ["left", 0]) #handle shooting #self.accept("space", self.p_bike.setShoot, [1]) #self.accept("space-up", self.p_bike.setShoot, [0]) self.accept("z", self.p_bike.setShoot, [1]) self.accept("z-up", self.p_bike.setShoot, [0]) #powerup collisions self.accept("p_bike-powerup1", self.powerupCollision) self.accept("p_bike-powerup2", self.powerupCollision) self.accept("p_bike-powerup3", self.powerupCollision) self.accept("p_bike-powerup4", self.powerupCollision) self.accept("p_bike-powerup5", self.powerupCollision) #bullet collision with player self.accept("p_bike-bullet", self.bulletCollision) self.accept("e_bike-bullet", self.bulletCollision) #setup basic environment lighting self.ambientLight = AmbientLight("ambientLight") self.ambientLight.setColor((.25, .25, .25, 1)) self.ambientLightNP = render.attachNewNode(self.ambientLight) render.setLight(self.ambientLightNP) render.setShaderAuto() self.playerHealth = OnscreenImage(image = 'PlayerHealthBar.png') self.playerHealth.setX(-1) self.playerHealth.setZ(-1.95) #enemy health self.enemyHealth = OnscreenImage(image = 'EnemyHealthBar.png') self.enemyHealth.setX(1) self.enemyHealth.setZ(-1.95) self.initAI() self.e_bikes = [self.addEnemy()] base.cTrav.addCollider(self.p_bike.cNodePath1, self.e_bikes[0].cevent) def gameOverDead(self, task): if self.dead == True: self.deadCount += 1 if self.deadCount > 200: self.gameOver = OnscreenImage(image = 'gameOver.png') self.gameOver.setScale(1.35) return Task.cont def gameOverWin(self, task): if self.win == True: self.winCount += 1 if self.winCount > 200: self.gameOverWin = OnscreenImage(image = 'win.png') self.gameOverWin.setScale(1.35) return Task.cont def powerupCollision(self, cEntry): #check powerup1 if cEntry.getIntoNodePath() == self.w_terrain.cPowerNode1Path: self.w_terrain.powerUp1 = False self.p_bike.invin = True #print('I WIN') #check powerup2 elif cEntry.getIntoNodePath() == self.w_terrain.cPowerNode2Path: self.w_terrain.powerUp2 = False self.p_bike.shotgun = True self.p_bike.p_up_timer = 0 #check powerup3 elif cEntry.getIntoNodePath() == self.w_terrain.cPowerNode3Path: self.w_terrain.powerUp3 = False self.p_bike.shotgun = True self.p_bike.p_up_timer = 0 #check power4 elif cEntry.getIntoNodePath() == self.w_terrain.cPowerNode4Path: self.w_terrain.powerUp4 = False self.p_bike.health_up = True #check powerup5 elif cEntry.getIntoNodePath() == self.w_terrain.cPowerNode5Path: self.w_terrain.powerUp5 = False self.p_bike.health_up = True cEntry.getIntoNodePath().remove() def bulletCollision(self, cEntry): #check which bike is being hit if self.p_bike.invin == False and (cEntry.getFromNodePath() == self.p_bike.cNodePath1 or cEntry.getFromNodePath() == self.p_bike.cNodePath1 or cEntry.getFromNodePath() == self.p_bike.cNodePath3): #print('player bike!') self.p_bike.hp -= 1 self.playerHealth.setX(self.playerHealth.getX() - .1) #print('player hp:', self.p_bike.hp) if self.p_bike.hp <= 0: #kill player bike and reset camera taskMgr.remove("moveTask") taskMgr.remove("bulletTask") taskMgr.remove("shootTask") taskMgr.remove("powerupTask") x = base.camera.getX() + self.p_bike.bike.getX() y = base.camera.getY() + self.p_bike.bike.getY() h = self.p_bike.bike.getH() angle = deg2Rad(self.p_bike.bike.getH()) dy = -math.cos(angle) dx = math.sin(angle) dx = -dx * 32 dy = -dy * 32 base.camera.reparentTo(render) base.camera.setPos(0,0,0) base.camera.setPosHpr( self.p_bike.bike.getX() - dx, self.p_bike.bike.getY() - dy, 7, self.p_bike.bike.getH(), -8, 0) x = self.p_bike.bike.getX() y = self.p_bike.bike.getY() z = self.p_bike.bike.getZ() h = self.p_bike.bike.getH() self.p_bike.bike.delete() self.death_player = Actor("moto2_deadActor.egg", {"death":"moto2_deadAnim.egg"}) self.death_player.reparentTo(render) self.death_player.setPos(x,y,z) self.death_player.setH(h) self.animcontrol = self.death_player.getAnimControl('death') self.death_player.play("death") self.dead = True #UNCOMMENT WHEN ENEMY BIKES ARE FIXED else: for enemy in self.e_bikes: #print('in enemy list') #print(cEntry.getFromNodePath()) #print(cEntry.getFromNodePath().getParent()) #print(cEntry.getFromNodePath().getParent()) #print(enemy.cNodePath) #print(cEntry.getIntoNodePath()) #print(cEntry.getIntoNodePath().getParent()) if cEntry.getFromNodePath() == enemy.cNodePath1 or cEntry.getFromNodePath() == enemy.cNodePath2 or cEntry.getFromNodePath() == enemy.cNodePath3: #print('enemy hit') enemy.hp -= 1 self.enemyHealth.setX(self.enemyHealth.getX() + .1) #print('enemy hp:', enemy.hp) if enemy.hp <= 0: #print('Game Over. You Win!') #kill enemy bike x = enemy.cNodePath.getParent().getX() y = enemy.cNodePath.getParent().getY() z = enemy.cNodePath.getParent().getZ() h = enemy.cNodePath.getParent().getH() #print(x,y,z,h) enemy.cNodePath.getParent().remove() self.death_enemy = Actor("moto1_deadActor.egg", {"death":"moto1_deadAnim.egg"}) self.death_enemy.reparentTo(render) self.death_enemy.setPos(x,y,z) self.death_enemy.setH(h) self.death_enemy.play("death") self.win = True break #destroy the bullet for i in range(len(self.p_bike.bullet.bulletList)): if cEntry.getIntoNodePath().getParent() == self.p_bike.bullet.bulletList[i]: #print('erased') self.p_bike.bullet.bulletTime.remove(self.p_bike.bullet.bulletTime[i]) self.p_bike.bullet.bulletList.remove(self.p_bike.bullet.bulletList[i]) cEntry.getIntoNodePath().getParent().remove() break #UNCOMMENT WHEN ENEMY BIKES ARE FIXED #cycle through the enemy bullets for enemy in self.e_bikes: for i in range(len(enemy.bullet.bulletList)): if cEntry.getIntoNodePath().getParent() == enemy.bullet.bulletList[i]: #print('erased') enemy.bullet.bulletTime.remove(enemy.bullet.bulletTime[i]) enemy.bullet.bulletList.remove(enemy.bullet.bulletList[i]) cEntry.getIntoNodePath().getParent().remove() break def initAI(self): self.AIworld = AIWorld(render) #self.AIworld.addObstacle(self.w_terrain.wall_terrain) #self.AIworld.addObstacle(self.w_terrain.cNodePathWall1) #self.AIworld.addObstacle(self.w_terrain.cNodePathWall2) #self.AIworld.addObstacle(self.w_terrain.cNodePathWall3) #self.AIworld.addObstacle(self.w_terrain.cNodePathWall4) #self.AIworld.addObstacle(self.w_terrain.bounds) #AI World update taskMgr.add(self.AIUpdate,"AIUpdate") def AIUpdate(self, task): self.AIworld.update() for i in self.e_bikes: i.update() i.bike.setHpr(i.bike.getH(), 0, i.bike.getR()) #i.bike.setZ(max(0.0, i.bike.getZ())) #i.bike.setZ(0.0) #print i.bike.getHpr() return Task.cont def addEnemy(self): enemy = EnemyBike(base.cTrav, self.cevent) enemy.target = self.p_bike enemy.setMode('scan') self.AIworld.addAiChar(enemy.AIchar) base.cTrav.addCollider(self.p_bike.cNodePath1, enemy.cevent) return enemy
class Unit: def __init__(self, unitID, position, mainClass): # self = copy.copy(mainClass.unitHandler.unitDict[mainClass.unitHandler.main['units'][unitID]]) self.model = mainClass.unitHandler.unitDict[ mainClass.unitHandler.main['units'][unitID]].model self.fullName = copy.copy(mainClass.unitHandler.unitDict[ mainClass.unitHandler.main['units'][unitID]].fullName) self.HP = copy.copy(mainClass.unitHandler.unitDict[ mainClass.unitHandler.main['units'][unitID]].HP) self.info = copy.copy(mainClass.unitHandler.unitDict[ mainClass.unitHandler.main['units'][unitID]].info) self.moveType = copy.copy(mainClass.unitHandler.unitDict[ mainClass.unitHandler.main['units'][unitID]].moveType) self.model = copy.copy(mainClass.unitHandler.unitDict[ mainClass.unitHandler.main['units'][unitID]].model) self.radius = copy.copy(mainClass.unitHandler.unitDict[ mainClass.unitHandler.main['units'][unitID]].radius) self.mass = copy.copy(mainClass.unitHandler.unitDict[ mainClass.unitHandler.main['units'][unitID]].mass) self.startForce = copy.copy(mainClass.unitHandler.unitDict[ mainClass.unitHandler.main['units'][unitID]].startForce) self.maxForce = copy.copy(mainClass.unitHandler.unitDict[ mainClass.unitHandler.main['units'][unitID]].maxForce) self.dig = copy.copy(mainClass.unitHandler.unitDict[ mainClass.unitHandler.main['units'][unitID]].dig) self.reinforce = copy.copy(mainClass.unitHandler.unitDict[ mainClass.unitHandler.main['units'][unitID]].reinforce) self.shovel = copy.copy(mainClass.unitHandler.unitDict[ mainClass.unitHandler.main['units'][unitID]].shovel) self.hold = copy.copy(mainClass.unitHandler.unitDict[ mainClass.unitHandler.main['units'][unitID]].hold) self.modelHeight = copy.copy(mainClass.unitHandler.unitDict[ mainClass.unitHandler.main['units'][unitID]].modelHeight) self.selectScale = copy.copy(mainClass.unitHandler.unitDict[ mainClass.unitHandler.main['units'][unitID]].selectScale) self.job = False self.uID = mainClass.unitHandler.unitUniqueID print self.uID self.modelNode = loader.loadModel(self.model) self.modelNode.setName('unit ' + str(self.uID).zfill(3)) self.modelNode.reparentTo(render) self.modelNode.setPos(position) self.modelNode.setCollideMask(BitMask32.bit(1)) self.select = OnscreenImage(image='data/models/game/selected.png') self.select.setScale(float(self.selectScale)) self.select.reparentTo(self.modelNode) self.select.setZ(float(self.modelHeight) / 2) self.select.setTransparency(TransparencyAttrib.MAlpha) self.select.setBillboardPointEye() self.select.hide() self.groundRay = CollisionRay() self.groundRay.setOrigin(0, 0, 100) self.groundRay.setDirection(0, 0, -1) self.groundCol = CollisionNode('unit Ray') self.groundCol.addSolid(self.groundRay) self.groundCol.setTag('units', 'ray1') self.groundCol.setFromCollideMask(BitMask32.bit(0)) self.groundCol.setIntoCollideMask(BitMask32.allOff()) self.groundColNp = self.modelNode.attachNewNode(self.groundCol) self.groundColNp.setPos(0, 0, 0) self.groundHandler = CollisionHandlerFloor() self.groundHandler.setMaxVelocity(100) base.cTrav2.addCollider(self.groundColNp, self.groundHandler) self.groundHandler.addCollider(self.groundColNp, self.modelNode) self.AI = AICharacter(self.fullName, self.modelNode, self.mass * 2, self.startForce * 2, self.maxForce * 2) mainClass.unitHandler.AIWorld.addAiChar(self.AI) self.AIBehaviors = self.AI.getAiBehaviors() if (self.moveType == 'ground'): self.aStar = astar.aStar(mainClass.unitHandler.meshes.landMesh, mainClass) elif (self.moveType == 'water'): self.aStar = astar.aStar(mainClass.unitHandler.meshes.waterMesh, mainClass) elif (self.moveType == 'air'): self.aStar = astar.aStar(mainClass.unitHandler.meshes.airMesh, mainClass) print self # mainClass.unitHandler.unitUniqueID += 1
class Minimap: def __init__(self, path='/models', scale = 0.0, posX = -0.9, posZ = -0.65, mapimageName = None): taskMgr.add(self.step,"MinimapTask") self.path = path self.posX = posX self.posZ = posZ b = 500 pos = Vec3(self.posX,0,self.posZ)#Aspect2D coordinates for the location of the center of the minimap. if mapimageName is None: mapimageName = '%s/battlemap.png' % self.path#mapimage is the image to place in the minimap self.scale = scale #this value is the number of pixels wide the map will be. ratio = b/self.scale self.dotScale = (ratio) * b self.dotSize = 0.005 / ratio self.map = aspect2d.attachNewNode('Map') mapimage = self.getMapImage(mapimageName) props = base.win.getProperties( ) self.Height = float(props.getYSize()) self.Hscale = (1/self.Height) self.map.setScale(self.Hscale) #Sets scale to the screen resolution. self.map.setPos(pos) #this is the location on aspect2d for the minimap. self.dots = [] self.dots.append([]) self.targets = [] self.targets.append([]) self.teamImage = [] #a list of paths to the image that each team will use. self.setNumTeams(9) self.mapimage = OnscreenImage(image = mapimage, scale = self.scale, parent = self.map) self.mousePosition = OnscreenImage(image = self.path + 'select3.png', scale = self.dotSize*10, pos = (0,0,0), parent = aspect2d) self.totaltargets = 0 def destroy(self): """Remove Minimap from game""" for team in range(len(self.dots)): #will cycle through each team for i in range(len(self.dots[team])): #will cycle through each member of the team dot = self.dots[team][i] dot.removeNode() self.mousePosition.removeNode() self.mapimage.removeNode() self.map.removeNode() def getMapImage(self, mapimageName): texImage=PNMImage() texImage.read(Filename(mapimageName)) tex=Texture() tex.load(texImage) return tex def setTargets(self, targets, team): for i in range(len(targets)): self.targets[team].append(targets[i]) for i in range(len(self.targets[team])): self.dots[team].append(OnscreenImage(image = self.teamImage[team], scale = self.dotSize, pos = (0,0,0), parent = aspect2d)) def setBezel(self, image = None, scale = None): if image is None: image = '%s/bezel.png' % self.path if scale is None: scale = 1 self.bezel = OnscreenImage(image = image, scale = self.scale * scale, parent = self.map) self.bezel.setTransparency(TransparencyAttrib.MAlpha) def setScale(self, scale): self.scale = scale def setPos(self,num): #must be a Vec3 self.pos = num def appendTarget(self, target = None, team = None): #target must be a nodepath, team must be an integer if target is not None: self.targets[team].append(target) x = len(self.targets[team]) self.dots[team].append(OnscreenImage(image = self.teamImage[team], scale = self.dotSize, pos = (0,0,0), parent = aspect2d)) def setNumTeams(self, num): #Num must be an integer. Sets the number of different groups for the map to track. Each group may be tracked using a different graphic newTargets = [] for x in range(num): newTargets.append([]) self.dots.append([]) self.teamImage.append('%s/dot%d.png' % (self.path, x)) self.targets = newTargets def removeTarget(self, target = None, team = None): for i in range(len(self.targets[team])): if self.targets[team][i] == target: #This means the object in question is the object to be removed self.dots[team][i].stash() def step(self, task): try: for team in range(len(self.targets)): #will cycle through each team for i in range(len(self.targets[team])): #will cycle through each member of the team target = self.targets[team][i] if target.isEmpty() == False: x = (target.getX()/self.dotScale) + self.posX z = (target.getZ()/self.dotScale) + self.posZ self.dots[team][i].setX(x) self.dots[team][i].setZ(z) self.mousePosition.setX((camera.getX()/self.dotScale + self.posX)) self.mousePosition.setZ((camera.getZ()/self.dotScale + self.posZ)) return Task.cont except: pass