from pandac.PandaModules import *

from globals import *
from sprite2d import *
from objects import *
from enemy import *
import controls
from direct.actor.Actor import Actor



class Level():


    def __init__(self, model,player):

        self.rootnp = NodePath("Level Root")
        self.player = player
        self.player.limit = 300.

        self.geom = loader.loadModel("models/" + model)
        self.geom.reparentTo(self.rootnp)

        self.geom.setCollideMask(ALL_OFF)

        coll = self.geom.find("**/floor_collide")
        if not coll.isEmpty():
            coll.node().setIntoCollideMask(FLOOR_MASK)
        for element in self.geom.findAllMatches("**/*"):
            #print element
            coll = element.find("**/floor_collide*")
            if not coll.isEmpty():
                coll.node().setIntoCollideMask(FLOOR_MASK)
            coll = element.find("**/wall_collide*")
            if not coll.isEmpty():
                coll.node().setIntoCollideMask(WALL_MASK)

        #--- initialisation ----
        
        enemyList = []


class Level1(Level):

    def __init__(self, model,player):

        controls.options =   {'look' : False,
                            'fixe y' : True,
                            'fixe z' : True}
        Level.__init__(self, model,player)
        player.star = 45
        self.player.limit = 236

        coll = self.geom.findAllMatches("**/*Box*")
        if not coll.isEmpty():
            for e in coll:
                box = Platform2D(e, name = 'box', level = '1')

        coll = self.geom.findAllMatches("**/*Barrel*")
        if not coll.isEmpty():
            for e in coll:
                box = Platform2D(e, name = 'barrel', level = '1')

        coll = self.geom.findAllMatches("**/*Chariot1*")
        if not coll.isEmpty():
            for e in coll:
                box = Platform2D(e, name = 'chariot1', level = '1')

        coll = self.geom.findAllMatches("**/*Chariot2*")
        if not coll.isEmpty():
            for e in coll:
                box = Platform2D(e, name = 'chariot2', level = '1')


        coll = self.geom.findAllMatches("**/*Saloon*")
        if not coll.isEmpty():
            for e in coll:
                box = Platform2D(e, name = 'saloon', level = '1')

        coll = self.geom.findAllMatches("**/*Manger*")
        if not coll.isEmpty():
            for e in coll:
                box = Platform2D(e, name = 'manger', level = '1')

        coll = self.geom.findAllMatches("**/*Fence*")
        if not coll.isEmpty():
            for e in coll:
                box = Platform2D(e, name = 'fence', level = '1')

        coll = self.geom.findAllMatches("**/*House1*")
        if not coll.isEmpty():
            for e in coll:
                box = Platform2D(e, name = 'house1', level = '1')

        coll = self.geom.findAllMatches("**/*House2*")
        if not coll.isEmpty():
            for e in coll:
                box = Platform2D(e, name = 'house2', level = '1')

        coll = self.geom.findAllMatches("**/*Star*")
        if not coll.isEmpty():
            for e in coll:
                box = Star(e, name = 'star', level = '1')
                box.rootnp.setY(-0.1)
                


        coll = self.geom.findAllMatches("**/*Snake*")
        if not coll.isEmpty():
            for e in coll:
                snake = Snake(e)

        coll = self.geom.findAllMatches("**/*BottleSpawn*")
        if not coll.isEmpty():
            for e in coll:
                bottleSpawn = BottleSpawn(e,self.player)

        self.shootBottle = taskMgr.add(shootBottle, "shoot bottle")


        self.logo = Sprite2d("models/level1/logo.png", rows=1, cols=1, scale=2, twoSided=True, repeatX=1, anchorX="Center", anchorY="Center")
        self.logo.node.reparentTo(self.rootnp)
        self.logo.node.setX(7)
        self.logo.node.setZ(5)

        self.ground = Sprite2d("models/tex/level1_ground.png", rows=1, cols=1, scale=2, twoSided=True, repeatX=20, anchorX="Left", anchorY="Top")
        self.ground.node.reparentTo(self.rootnp)
        self.ground.node.setX(-2.0)
        self.ground.node.setZ(self.ground.node.getZ()+0.2)
        self.ground.node.setY(self.ground.node.getY()+1.0)


        self.back1 = Sprite2d("models/tex/level1_back_low.png", rows=1, cols=1, scale=2, twoSided=True, repeatX=20, anchorX="Left", anchorY="Top")
        self.back1.node.reparentTo(self.rootnp)
        self.back1.node.setX(-2.0)
        self.back1.node.setZ(self.back1.node.getZ()+6.7/2.0)
        self.back1.node.setY(self.back1.node.getY()+2.0)


        self.back2 = Sprite2d("models/tex/level1_back_high.png", rows=1, cols=1, scale=2, twoSided=True, repeatX=20, anchorX="Left", anchorY="Top")
        self.back2.node.reparentTo(self.rootnp)
        self.back2.node.setX(-2.0)
        self.back2.node.setZ(self.back2.node.getZ()+6.7/2.0 + 10.2/2.0)
        self.back2.node.setY(self.back2.node.getY()+3.0)



class Level2(Level):

    def __init__(self, model,player):

        Level.__init__(self, model,player)
        player.star = 27
        self.player.limit = 161

        controls.options =   {'look' : False,
                            'fixe y' : True,
                            'fixe z' : False}

        coll = self.geom.findAllMatches("**/*Map*")
        if not coll.isEmpty():
            for e in coll:
                print "Map"
                box = Platform2D(e, name = 'map', level = '2', scale = 4)
                box.rootnp.setX(box.rootnp, -1.5)
                box.rootnp.setZ(box.rootnp, -0.75)

        coll = self.geom.findAllMatches("**/*Star*")
        if not coll.isEmpty():
            for e in coll:
                box = Star(e, name = 'star', level = '2')
                box.rootnp.setY(-0.1)
                print "STAR"
                #box.rootnp.setX(box.rootnp, 1.5)
                #box.rootnp.setZ(box.rootnp, 0.75)

        coll = self.geom.findAllMatches("**/*Centipede*")
        if not coll.isEmpty():
            for e in coll:
                centi = Centipede(e)
                #centi.rootnp.setZ(centi.rootnp, 1.5)

        coll = self.geom.findAllMatches("**/*Spider*")
        if not coll.isEmpty():
            for e in coll:
                spider = Spider(e)


        self.background = Sprite2d("models/level2/background.png", rows=1, cols=1, scale=4, twoSided=True, repeatX=2, anchorX="Left", anchorY="Bottom")
        self.background.node.setX(-base.cam.getX()-1.0)
        self.background.node.setZ(-base.cam.getZ()-1.0)

        self.background.node.setY(self.background.node.getY()+60.0)
        self.background.node.reparentTo(base.cam)

class Level3(Level):

    def __init__(self, model,player):

        Level.__init__(self, model,player)
        self.player.limit_z = -1.5
        self.player.planar = False
        self.limit = 143.

        myFog = Fog("Fog Name")
        myFog.setColor(0,0,0)
        myFog.setExpDensity(0.03)
        self.rootnp.setFog(myFog)

        coll = self.geom.findAllMatches("**/*Star*")
        if not coll.isEmpty():
            for e in coll:
                box = Star(e, name = 'star', level = '3')
                box.rootnp.setScale(0.5)
                #box.rootnp.setY(-0.1)

        self.logo = Sprite2d("models/level3/logo.png", rows=1, cols=1, scale=6, twoSided=True, repeatX=1, anchorX="Center", anchorY="Center")
        self.logo.node.reparentTo(self.rootnp)
        self.logo.node.setX(7)
        self.logo.node.setZ(7)


class Level4(Level):

    def __init__(self, model,player):

        Level.__init__(self, model,player)
        self.player.limit_z = -1.5
        self.player.planar = False
        self.player.limit = 130



        self.geom.setX(50)
        self.geom.setY(-1.0)

        myFog = Fog("Fog Name")
        myFog.setColor(0,0,0)
        myFog.setExpDensity(0.025)
        self.rootnp.setFog(myFog)

        #self.boss = loader.loadModel("models/level4/boss/P1_Stance")

        self.boss = Actor("models/level4/boss/P1_Stance", {'Stance':'P1_Stance', 'Laugh':'models/level4/boss/P1_Laugh'})
        self.boss.setX(70)
        self.boss.setScale(0.2)
        self.boss.reparentTo(self.rootnp)
        self.boss.loop("Laugh")
        self.boss.setPlayRate(0.6, 'Laugh')

        coll = self.geom.findAllMatches("**/*Star*")
        if not coll.isEmpty():
            for e in coll:
                box = Star(e, name = 'star', level = '3')
                box.rootnp.setScale(0.5)
                #box.rootnp.setY(-0.1)

        self.logo = Sprite2d("models/level4/logo.png", rows=1, cols=1, scale=6, twoSided=True, repeatX=1, anchorX="Center", anchorY="Center")
        self.logo.node.reparentTo(self.rootnp)
        self.logo.node.setX(7)
        self.logo.node.setZ(7)

class Level5(Level):

    def __init__(self, model,player):

        Level.__init__(self, model,player)
        player.star = 8
        self.player.limit = 155.

        coll = self.geom.findAllMatches("**/*Map*")
        if not coll.isEmpty():
            for e in coll:
                print "Map"
                box = Platform2D(e, name = 'map', level = '5', scale = 2)

        coll = self.geom.findAllMatches("**/*Star*")
        if not coll.isEmpty():
            for e in coll:
                box = Star(e, name = 'star', level = '5')
                box.rootnp.setY(-0.1)

        coll = self.geom.findAllMatches("**/*Pirate*")
        if not coll.isEmpty():
            for e in coll:
                pirate = Pirate(e)
                #centi.rootnp.setZ(centi.rootnp, 1.5)

        coll = self.geom.findAllMatches("**/*Meatboy*")
        if not coll.isEmpty():
            for e in coll:
                meatboy = MeatBoy(e)




