from sprite2d import *
from panda3d.core import PandaNode, NodePath
from globals import *
from direct.task.Task import Task


class Platform2D():

    def __init__(self, nodepath, name, level, scale = 2):

        self.rootnp = nodepath

        self.geom = loader.loadModel("models/level"+str(level)+"/"+name)
        self.geom.reparentTo(self.rootnp)

        self.sprite = Sprite2d("models/level"+str(level)+"/"+ name + ".png", rows=1, cols=1, scale=scale, twoSided=True, repeatX=1, anchorX="Center", anchorY="Center")
        self.sprite.node.reparentTo(self.rootnp)



class Star():

    def __init__(self, nodepath, name, level):

        if level == '1':
            cols = 4
            scale = 2
        elif level == '2':
            cols = 1
            scale = 4
        elif level == '5':
            cols = 1
            scale = 2

        self.rootnp = nodepath
        if level == '1':
            self.rootnp.setScale(0.5)
        self.geom = loader.loadModel("models/level"+str(level)+"/"+name)
        self.geom.reparentTo(self.rootnp)
    
        for coll in self.geom.findAllMatches("**/star_trigger*"):
            coll.node().setIntoCollideMask(TRIGGER_MASK)

        if level != '3':
            self.sprite = Sprite2d("models/level"+str(level)+"/"+ name + ".png", rows=1, cols=cols, scale=scale, twoSided=True, repeatX=1, anchorX="Center", anchorY="Center")
            self.sprite.node.reparentTo(self.rootnp)

        if level == '1':
            self.animateTask = taskMgr.add(self.animate, "animateTask")
            self.animateTask.fps = 10


    def animate(self, task):
        currentFrame = int(task.time * task.fps) 
        self.sprite.setFrame(currentFrame % 4)
        return Task.cont



