Exemplo n.º 1
0
 def __init__(self, base):
     self.base = base
     if settings.mouse_over:
         taskMgr.add(self.mouse_task, 'mouse-task')
     self.picker = CollisionTraverser()
     self.pq = CollisionHandlerQueue()
     self.pickerNode = CollisionNode('mouseRay')
     self.pickerNP = self.base.cam.attachNewNode(self.pickerNode)
     self.pickerNode.setFromCollideMask(
         CollisionNode.getDefaultCollideMask()
         | GeomNode.getDefaultCollideMask())
     self.pickerRay = CollisionRay()
     self.pickerNode.addSolid(self.pickerRay)
     self.picker.addCollider(self.pickerNP, self.pq)
     #self.picker.showCollisions(render)
     self.over = None
Exemplo n.º 2
0
    def SetupOnClick(self):

        pickerNode = CollisionNode("mouseRay")
        pickerNP = self.base.camera.attachNewNode(pickerNode)
        pickerNode.setFromCollideMask(CollisionNode.getDefaultCollideMask()
                                      )  # GeomNode.getDefaultCollideMask())
        self.pickerRay = CollisionRay()
        pickerNode.addSolid(self.pickerRay)

        self.myTraverser = CollisionTraverser("mouseCollisionTraverser")

        # self.myTraverser.showCollisions(self.render) #uncomment to see collision point

        self.myHandler = CollisionHandlerQueue()

        self.myTraverser.addCollider(pickerNP, self.myHandler)
Exemplo n.º 3
0
 def set_clickable(self, clickable):
     self.clickable = clickable
     if self.use_collision_solid and self.collision_solid is not None:
         if clickable:
             self.collision_solid.node().setIntoCollideMask(
                 CollisionNode.getDefaultCollideMask())
         else:
             self.collision_solid.node().setIntoCollideMask(
                 BitMask32.all_off())
         #The instance itself is not clickable
         self.instance.setCollideMask(BitMask32.all_off())
     else:
         if clickable:
             self.instance.setCollideMask(GeomNode.getDefaultCollideMask())
         else:
             self.instance.setCollideMask(BitMask32.all_off())
Exemplo n.º 4
0
    def __init__(self):
        ShowBase.__init__(self)

        mySound = base.loader.loadSfx("sfx/tank_with_radar.ogg")
        self.mainShot_snd = base.loader.loadSfx("sfx/mainShot.ogg")
        self.enemyShot_snd = base.loader.loadSfx("sfx/enemyShot.ogg")
        self.enemyTankExplosion_snd = base.loader.loadSfx(
            "sfx/enemyTankExplosion.ogg")

        device_list = self.devices.getDevices()
        for device in device_list:
            print(device.device_class)
            # if device.device_class == DeviceClass.flight_stick:
            #    print("Have Joy stick")

        # render.setDepthTest(False)
        self.camLens.setFov(50)
        render.setAntialias(AntialiasAttrib.MLine)

        base.setBackgroundColor(0, 0, 0)
        base.disableMouse()
        props = WindowProperties()
        # props.setCursorHidden(True)
        base.win.requestProperties(props)

        # Load the environment models
        self.ground = self.loader.loadModel("models/ground_bl.egg")

        self.tank = self.loader.loadModel("models/tank_bl.egg")
        self.tank.setRenderModeWireframe()

        self.ground.setRenderModeWireframe()
        self.ground.setScale(100, 100, 1)

        # tank as lines
        #   set up explosion variables
        #   explosion intervals added in renderTanks() method
        for t in tanks_list:
            tanks_dict[t]["explosion"] = Parallel(
                name="Tank{}-Explosion".format(t))

        # group node for all enemy tanks
        self.tank_group = render.attachNewNode("Tanks")
        self.renderTanks(self.tank_group)

        # tank rounds
        with open('models/tank_round.json', "r") as f:
            data = json.load(f)
        lines = create_lineSegs_object(data, 0)
        lines.setThickness(3)

        gn_round = lines.create()
        np_round = NodePath(gn_round)

        self.tank_round = []
        #
        self.tank_round.append(render.attachNewNode("tank-round"))
        np_round.instanceTo(self.tank_round[0])
        # self.tank_round[0].hide()
        self.tank_round[0].setColorScale(0.3, 0.3, 1.0, 1.0)
        self.tank_round[0].setPos(0, 20, -0.2 - 10)
        self.tank_round[0].setHpr(self.tank_round[0], 0, 90, 0)
        self.tank_round[0].setScale(0.2, 0.2, 0.2)
        self.tank_round[0].reparentTo(camera)

        # render enemy tank round
        for t in tanks_list:
            tanks_dict[t]["round"] = render.attachNewNode(
                "tank{}-round".format(t))
            np_round.instanceTo(tanks_dict[t]["round"])
            tanks_dict[t]["round"].setPos(-0.4, 0, 1.61325)
            tanks_dict[t]["round"].setHpr(tanks_dict[t]["round"], 0, 0, 90)
            tanks_dict[t]["round"].setScale(0.14, 0.14, 0.14)
            tanks_dict[t]["round"].reparentTo(tanks_dict[t]["tank"])

        #
        # new mountain method
        self.render_mountains()

        ####################
        # collisions       #
        ####################
        print(CollisionNode.getDefaultCollideMask())

        # Initialize collision Handler
        self.collHandEvent = CollisionHandlerEvent()
        self.collHandEvent.addInPattern('into-%in')

        # collision spheres enemy tank
        for t in tanks_list:
            cs = CollisionSphere(0, 0, 0.9, tanks_dict[t]["coll_rad"])
            cnodePath = tanks_dict[t]["tank"].attachNewNode(
                CollisionNode('cTank' + t))
            cnodePath.node().addSolid(cs)

            if DEBUG:
                # cnodePath.show()
                pass
        self.tank_group.setCollideMask(BitMask32(0x10))

        # print(self.tank_group.getCollideMask())
        # print(tanks_dict['1']["tank"].getCollideMask())

        # collision sphere for round of main tank
        cs = CollisionSphere(0, 0, 0, 1)
        tr_cnodePath = self.tank_round[0].attachNewNode(
            CollisionNode('cTankRound'))
        tr_cnodePath.node().addSolid(cs)

        # collision spheres for enemy tank rounds
        cs = CollisionSphere(0, 0, 0, 1)
        for t in tanks_list:
            np = tanks_dict[t]["round"].attachNewNode(
                CollisionNode('ceTankRound' + t))
            np.node().addSolid(cs)
            np.node().setFromCollideMask(BitMask32(0x20))
            # np.show()

        # collision sphere main tank
        cs = CollisionSphere(0, 0, 0, 1)
        np = self.camera.attachNewNode(CollisionNode('cmTank'))
        np.node().addSolid(cs)
        # np.show()

        # Initialise Traverser
        traverser = CollisionTraverser('Main Traverser')
        if DEBUG:
            traverser.showCollisions(render)
        base.cTrav = traverser

        # from objects
        traverser.addCollider(tr_cnodePath, self.collHandEvent)

        np_list = render.findAllMatches("**/ceTankRound*")
        for np in np_list:
            traverser.addCollider(np, self.collHandEvent)

        # grid
        grid_lines = procedural_grid(-1000, 500, -1000, 500, 50)
        grid_lines.setThickness(1)
        node = grid_lines.create()
        self.grid = NodePath(node)
        self.grid.setColorScale(0.15, 0.2, 0.15, 1.0)
        self.grid.setPos(0, 0, -0.2)

        alight = AmbientLight('ambientLight')
        alight.setColor(Vec4(0, 0, 0, 0))  # ambient light is dim red
        # alightNP = self.render.attachNewNode(alight)

        # render sight
        self.render_sight()

        # Tasks
        for t in tanks_list:
            tanks_dict[t]["move"] = True

        self.taskMgr.add(self.spinCameraTask, "SpinCameraTask")
        self.taskMgr.add(self.moveTanksTask, "MoveTanksTask")
        self.taskMgr.add(self.moveTask, "MoveTask")
        self.taskMgr.add(self.enemy_shoot_task, "EnemyShoot")

        # base.messenger.toggleVerbose()

        self.accept('space', self.shoot)
        self.accept('space-up', self.shot_clear)
        self.accept('shot-done', self.reset_shot)

        self.accept('into-' + 'cmTank', self.struck)
        for t in tanks_list:
            self.accept('into-' + 'cTank' + t, self.tank0_round_hit)
            self.accept('explosion{}-done'.format(t),
                        self.explosion_cleanup,
                        extraArgs=[t])
            self.accept('shot{}-done'.format(t),
                        self.enemy_reset_shot,
                        extraArgs=[t])

        # on-screen text
        vect = self.camera.getHpr()
        self.textObject = OnscreenText(text=str(vect[0]),
                                       pos=(-0.5, -0.9),
                                       scale=(0.03, 0.05),
                                       fg=(0.4, 1.0, 0.4, 1),
                                       mayChange=True)
        self.textObject.reparentTo(self.render2d)

        mySound.setLoop(True)
        mySound.play()