示例#1
0
    def rotateCube(self, direction):
        """
        rotates the cube according to the direction passed as first argument. accepted values are "up" "down" "left" "right"
        """
        #first check if we are allowed to move the block at all..
        if self.isAnimated():
            print("canceling rotation of cube")
            return

        #cleaning up from last rotation (we cant clean those up at the end of this function cause the interval needs the dummynode for rotation)
        self.cube.wrtReparentTo(render)
        try:
            dummy.remove_node()
        except:
            pass

        self.setAnimated(True)
        duration = 0.2
        x1, y1, x2, y2 = self.getCubeTiles()

        self.level.animateTile(x1, y1)
        self.level.animateTile(x2, y2)

        dummy = render.attachNewNode("dummy")
        dummy.reparentTo(self.cube)
        dummy.setZ(render, 0)
        dummy.wrtReparentTo(render)
        dummy.setHpr(0, 0, 0)
        dest_hpr = Vec3(0)

        if self.cube.getZ(render) > .7:
            #case1 : cube is standing upright
            #ok... since we rotate relative there are rounding errors... since there !might! be some uebernoob playing the game
            #needing one gazillion rotations to reach the goal it might happen those rounding errors actually get visible
            #so let him enjoy and reset the cube every-time it's standing straight-up.

            self.cube.setZ(
                render, 1
            )  #how comes this is one?.. well.. i know.. because of my learnings..
            self.cube.setX(render, round(self.cube.getX(render), 0))
            self.cube.setY(render, round(self.cube.getY(render), 0))
            self.cube.setH(render, round(self.cube.getH(render), 0))
            self.cube.setP(render, round(self.cube.getP(render), 0))
            self.cube.setR(render, round(self.cube.getR(render), 0))

            if direction == "right":
                dummy.setY(dummy, .5)
                self.cube.wrtReparentTo(dummy)
                dest_hpr = Vec3(0, -90, 0)
                self.setCubeTiles(x1, y1 + 1, x1, y1 + 2)

            if direction == "left":
                dummy.setY(dummy, -.5)
                self.cube.wrtReparentTo(dummy)
                dest_hpr = Vec3(0, 90, 0)
                self.setCubeTiles(x1, y1 - 2, x1, y1 - 1)

            if direction == "up":
                dummy.setX(dummy, -.5)
                self.cube.wrtReparentTo(dummy)
                dest_hpr = Vec3(0, 0, -90)
                self.setCubeTiles(x1 - 2, y1, x1 - 1, y1)

            if direction == "down":
                dummy.setX(dummy, .5)
                self.cube.wrtReparentTo(dummy)
                dest_hpr = Vec3(0, 0, 90)
                self.setCubeTiles(x1 + 1, y1, x1 + 2, y1)

        elif x1 == x2:  #if aligned to y-axis
            if direction == "right":
                dummy.setY(dummy, 1)
                self.cube.wrtReparentTo(dummy)
                dest_hpr = Vec3(0, -90, 0)
                self.setCubeTiles(x1, y1 + 2)

            if direction == "left":
                dummy.setY(dummy, -1)
                self.cube.wrtReparentTo(dummy)
                dest_hpr = Vec3(0, 90, 0)
                self.setCubeTiles(x1, y1 - 1)

            if direction == "up":
                dummy.setX(dummy, -.5)
                self.cube.wrtReparentTo(dummy)
                dest_hpr = Vec3(0, 0, -90)
                self.setCubeTiles(x1 - 1, y1, x2 - 1, y2)

            if direction == "down":
                dummy.setX(dummy, .5)
                self.cube.wrtReparentTo(dummy)
                dest_hpr = Vec3(0, 0, 90)
                self.setCubeTiles(x1 + 1, y1, x2 + 1, y2)

        elif y1 == y2:  #if it is alligned to x-axis..

            if direction == "right":
                dummy.setY(dummy, .5)
                self.cube.wrtReparentTo(dummy)
                dest_hpr = Vec3(0, -90, 0)
                self.setCubeTiles(x1, y1 + 1, x2, y2 + 1)

            if direction == "left":
                dummy.setY(dummy, -.5)
                self.cube.wrtReparentTo(dummy)
                dest_hpr = Vec3(0, 90, 0)
                self.setCubeTiles(x1, y1 - 1, x2, y2 - 1)

            if direction == "up":
                dummy.setX(dummy, -1)
                self.cube.wrtReparentTo(dummy)
                dest_hpr = Vec3(0, 0, -90)
                self.setCubeTiles(x1 - 1, y1)

            if direction == "down":
                dummy.setX(dummy, 1)
                self.cube.wrtReparentTo(dummy)
                dest_hpr = Vec3(0, 0, 90)
                self.setCubeTiles(x1 + 2, y1)

        else:
            print("Invalid move. Waiting ..")

        print("Rotating!")
        anim = self.animate(
            LerpHprInterval(dummy, duration, dest_hpr, (0, 0, 0)))

        #this sorta.. doesnt belong here.. but i dunno where to put it yet.
        x1, y1, x2, y2 = self.getCubeTiles()
        # self.level.tintTile(x1,y1) #this is fun to play with if your cube is invisible...
        # self.level.tintTile(x2,y2)
        self.level.stopAnimatedTile(
            x1, y1)  #stops the tile-animation for the tiles below the block
        self.level.stopAnimatedTile(x2, y2)

        #cheking what consequences your move had... muhahaa... if you get a 1 .. i've got bad news for you
        checkresult = checkMove(self.level.levelNode, self.getCubeTiles(),
                                self.sounds)
        if checkresult == 1:
            self.falls += 1
            self.moves += 1

            # Force to the corner when the cuboid falls down
            side_force = 1.7

            force = Vec3(0, 0, 0)
            if direction == "up":
                force = Vec3(-side_force, 0, 0)
            elif direction == "down":
                force = Vec3(side_force, 0, 0)
            elif direction == "left":
                force = Vec3(0, -side_force, 0)
            elif direction == "right":
                force = Vec3(0, side_force, 0)

            dummy.set_hpr(render, Vec3(0, 0, 0))

            del anim

            self.setAnimated(True)
            final_hpr = dest_hpr * 3.0 + Vec3(random(), random(),
                                              random()) * 360.0 * 0.0
            anim = LerpFunc(self.animateCube,
                            fromData=0,
                            toData=1,
                            duration=1.3,
                            blendType='noBlend',
                            extraArgs=[
                                dummy.get_pos(render),
                                Vec3(0), final_hpr, dummy, force
                            ])
            taskMgr.doMethodLater(anim.getDuration(), self.resetCube,
                                  "resetTask")

        elif checkresult == 2:

            #ok.. once reached the goal, move the block down, fading it out. thenload the new level etc.
            anim.pop()
            anim.append(Func(lambda: self.level.fadeOutLevel()))

            Sequence(Wait(0.3), Func(lambda *args: self.cube.hide())).start()

            taskMgr.doMethodLater(anim.getDuration() + 2, self.levelUp,
                                  "lvlup")
            taskMgr.doMethodLater(anim.getDuration() + 2,
                                  lambda *args: self.cube.show(), "show cube")
            taskMgr.doMethodLater(
                anim.getDuration() + 2,
                lambda *args: self.shard_node.node().remove_all_children(),
                "clear shards")

            Sequence(Wait(0.2),
                     Func(lambda *args: self.sounds.playSound("finish.wav"))
                     ).start()

            self.moves = 0
            self.falls = 0

            cube_min, cube_max = Vec3(-0.5, -0.5, -1), Vec3(0.5, 0.5, 1)
            self.shard_node.set_pos(dummy.get_pos(render) + Vec3(-0.5, 0, 0))
            shard_size = (cube_max - cube_min) / 5.0

            self.shard_node.hide()
            Sequence(Wait(0.22),
                     Func(lambda *args: self.shard_node.show())).start()

            for i in range(5):
                for j in range(5):
                    for k in range(5):
                        shard = loader.loadModel("models/CubeShard.bam")
                        shard.reparent_to(self.shard_node)
                        shard.set_x(i * shard_size.x + 0.1)
                        shard.set_y(j * shard_size.y + 0.1)
                        shard.set_z(k * shard_size.z + 0.2)
                        shard.set_scale(0.8 + random())

                        force = Vec3(i - 2 - 0.15, j - 2 - 0.15, k - 2 + 2.6)
                        force.normalize()
                        force *= 12.0 * (1 + random() * 0.5)

                        d_hpr = Vec3(random(), random(),
                                     random()) * 360.0 * (3.0 + random())

                        shard_anim = Sequence(
                            Wait(0.22),
                            LerpFunc(self.animateShard,
                                     fromData=0,
                                     toData=2,
                                     duration=2.0,
                                     blendType='noBlend',
                                     extraArgs=[
                                         shard.get_pos(), d_hpr, shard, force
                                     ]),
                            LerpHprInterval(shard,
                                            1.0 + random(),
                                            d_hpr * 1.6,
                                            d_hpr,
                                            blendType='noBlend'),
                        )
                        shard_anim.start()

        elif checkresult == 0:
            #how lame... just a ..move..
            print("playing sound")
            self.moves += 1
            self.sounds.playSound("stonerotate.wav")
        print("moves:", self.moves, "  falls:", self.falls)
        #last but not least.. we start the animation .. did you know that the pc knows you'r failing before you actually do? .. scary..
        anim.start()
示例#2
0
    def rotateCube(self,direction):
        """
        rotates the cube according to the direction passed as first argument. accepted values are "up" "down" "left" "right"
        """
        #first check if we are allowed to move the block at all..
        if self.isAnimated() : 
            print "canceling rotation of cube"
            return
            
        #cleaning up from last rotation (we cant clean those up at the end of this function cause the interval needs the dummynode for rotation)
        self.cube.wrtReparentTo(render)
        try:
            dummy.remove_node()
        except:
            pass

        self.setAnimated(True)
        duration = 0.2
        x1,y1,x2,y2 = self.getCubeTiles()
        
        self.level.animateTile(x1,y1)
        self.level.animateTile(x2,y2)
        
        dummy = render.attachNewNode("dummy")
        dummy.reparentTo(self.cube)
        dummy.setZ(render,0)
        dummy.wrtReparentTo(render)
        dummy.setHpr(0,0,0)
        dest_hpr = Vec3(0)
            
        if self.cube.getZ(render) > .7:
            #case1 : cube is standing upright
            #ok... since we rotate relative there are rounding errors... since there !might! be some uebernoob playing the game
            #needing one gazillion rotations to reach the goal it might happen those rounding errors actually get visible
            #so let him enjoy and reset the cube every-time it's standing straight-up.
            
            self.cube.setZ(render,1) #how comes this is one?.. well.. i know.. because of my learnings..
            self.cube.setX(render, round(self.cube.getX(render),0)  )
            self.cube.setY(render, round(self.cube.getY(render),0)  )
            self.cube.setH(render, round(self.cube.getH(render),0)  )
            self.cube.setP(render, round(self.cube.getP(render),0)  )
            self.cube.setR(render, round(self.cube.getR(render),0)  )


            if direction == "right":
                dummy.setY(dummy,.5)
                self.cube.wrtReparentTo(dummy) 
                dest_hpr = Vec3(0, -90, 0)
                self.setCubeTiles( x1, y1+1 ,x1 , y1+2 )
                
            if direction == "left":
                dummy.setY(dummy,-.5)
                self.cube.wrtReparentTo(dummy)
                dest_hpr = Vec3(0, 90, 0)
                self.setCubeTiles( x1, y1-2 ,x1 , y1-1 )
               
            if direction == "up":
                dummy.setX(dummy,-.5)
                self.cube.wrtReparentTo(dummy) 
                dest_hpr = Vec3(0, 0, -90)
                self.setCubeTiles( x1-2, y1 ,x1-1 , y1 )
                            
            if direction == "down":
                dummy.setX(dummy,.5)
                self.cube.wrtReparentTo(dummy) 
                dest_hpr = Vec3(0, 0, 90)
                self.setCubeTiles( x1+1, y1 ,x1+2 , y1 )          
            
        elif x1 == x2 :  #if aligned to y-axis
            if direction == "right":
                dummy.setY(dummy,1)
                self.cube.wrtReparentTo(dummy)
                dest_hpr = Vec3(0, -90, 0)
                self.setCubeTiles( x1, y1+2  )

            if direction == "left":
                dummy.setY(dummy,-1)
                self.cube.wrtReparentTo(dummy) 
                dest_hpr = Vec3(0, 90, 0)
                self.setCubeTiles( x1, y1-1)

            if direction == "up":
                dummy.setX(dummy,-.5)
                self.cube.wrtReparentTo(dummy) 
                dest_hpr = Vec3(0, 0, -90)
                self.setCubeTiles( x1-1, y1, x2-1, y2 )
                
            if direction == "down":
                dummy.setX(dummy,.5)
                self.cube.wrtReparentTo(dummy) 
                dest_hpr = Vec3(0, 0, 90)
                self.setCubeTiles( x1+1, y1, x2+1, y2 )


        elif y1==y2 : #if it is alligned to x-axis..

            if direction == "right":
                dummy.setY(dummy,.5)
                self.cube.wrtReparentTo(dummy)
                dest_hpr = Vec3(0, -90, 0)
                self.setCubeTiles( x1, y1+1, x2, y2+1 )
                
            if direction == "left":
                dummy.setY(dummy,-.5)
                self.cube.wrtReparentTo(dummy) 
                dest_hpr = Vec3(0, 90, 0)
                self.setCubeTiles( x1, y1-1, x2, y2-1 )
            
            if direction == "up":
                dummy.setX(dummy,-1)
                self.cube.wrtReparentTo(dummy) 
                dest_hpr = Vec3(0, 0, -90)
                self.setCubeTiles( x1-1, y1  )
                
            if direction == "down":
                dummy.setX(dummy,1)
                self.cube.wrtReparentTo(dummy) 
                dest_hpr = Vec3(0, 0, 90)
                self.setCubeTiles( x1+2, y1  )

        else:
            print("Invalid move. Waiting ..")


        print("Rotating!")
        anim = self.animate( LerpHprInterval(dummy, duration, dest_hpr,(0,0,0) ) )

        
        #this sorta.. doesnt belong here.. but i dunno where to put it yet.
        x1,y1,x2,y2 = self.getCubeTiles()
        # self.level.tintTile(x1,y1) #this is fun to play with if your cube is invisible...
        # self.level.tintTile(x2,y2)
        self.level.stopAnimatedTile(x1,y1) #stops the tile-animation for the tiles below the block
        self.level.stopAnimatedTile(x2,y2)
        
        #cheking what consequences your move had... muhahaa... if you get a 1 .. i've got bad news for you
        checkresult = checkMove(self.level.levelNode,self.getCubeTiles(),self.sounds)
        if checkresult == 1:
            self.falls +=1
            self.moves +=1

            # Force to the corner when the cuboid falls down
            side_force = 1.7

            force = Vec3(0, 0, 0)
            if direction == "up":
                force = Vec3(-side_force, 0, 0)
            elif direction == "down":
                force = Vec3(side_force, 0, 0)
            elif direction == "left":
                force = Vec3(0, -side_force, 0)
            elif direction == "right":
                force = Vec3(0, side_force, 0)

            dummy.set_hpr(render, Vec3(0, 0, 0))

            del anim

            self.setAnimated(True)
            final_hpr = dest_hpr * 3.0 + Vec3(random(), random(), random()) * 360.0 * 0.0
            anim = LerpFunc(self.animateCube, fromData=0, toData=1, duration=1.3, blendType='noBlend', extraArgs=[dummy.get_pos(render), Vec3(0), final_hpr, dummy, force])
            taskMgr.doMethodLater( anim.getDuration(), self.resetCube , "resetTask")

        elif checkresult == 2:

            #ok.. once reached the goal, move the block down, fading it out. thenload the new level etc.
            anim.pop()
            anim.append( Func(lambda: self.level.fadeOutLevel() ) )

            Sequence(Wait(0.3), Func(lambda *args: self.cube.hide())).start()

            taskMgr.doMethodLater( anim.getDuration()+2 , self.levelUp , "lvlup")
            taskMgr.doMethodLater( anim.getDuration()+2 , lambda *args: self.cube.show(), "show cube")
            taskMgr.doMethodLater( anim.getDuration()+2 , lambda *args: self.shard_node.node().remove_all_children(), "clear shards")
            
            Sequence(Wait(0.2), Func(lambda *args: self.sounds.playSound("finish.wav"))).start()

            self.moves = 0
            self.falls = 0

            cube_min, cube_max = Vec3(-0.5, -0.5, -1), Vec3(0.5, 0.5, 1)
            self.shard_node.set_pos(dummy.get_pos(render) + Vec3(-0.5, 0, 0))
            shard_size = (cube_max - cube_min) / 5.0

            self.shard_node.hide()
            Sequence(Wait(0.22), Func(lambda *args: self.shard_node.show())).start()

            for i in range(5):
                for j in range(5):
                    for k in range(5):
                        shard = loader.loadModel("models/CubeShard.bam")
                        shard.reparent_to(self.shard_node)
                        shard.set_x(i * shard_size.x + 0.1)
                        shard.set_y(j * shard_size.y + 0.1)
                        shard.set_z(k * shard_size.z + 0.2)
                        shard.set_scale(0.8 + random())

                        force = Vec3(i-2 - 0.15, j-2 - 0.15, k-2 + 2.6)
                        force.normalize()
                        force *= 12.0 * (1 + random() * 0.5)

                        d_hpr = Vec3(random(), random(), random()) * 360.0 * (3.0 + random())

                        shard_anim = Sequence(
                            Wait(0.22),
                            LerpFunc(self.animateShard, fromData=0, toData=2, duration=2.0, blendType='noBlend', extraArgs=[shard.get_pos(), d_hpr, shard, force]),
                            LerpHprInterval(shard, 1.0 + random(), d_hpr * 1.6, d_hpr, blendType='noBlend'),
                        )
                        shard_anim.start()

        elif checkresult == 0:
            #how lame... just a ..move..
            print "playing sound"
            self.moves += 1
            self.sounds.playSound("stonerotate.wav")
        print "moves:",self.moves ,"  falls:",self.falls
        #last but not least.. we start the animation .. did you know that the pc knows you'r failing before you actually do? .. scary..
        anim.start()