Untitled

mail@pastecode.io avatar
unknown
javascript
2 years ago
1.6 kB
2
Indexable
Never
    createCards(count)
    {
        this.cards = [];
        
        this.rowContainer = new PIXI.Container();
        this.container.addChild(this.rowContainer);

        for(let i = 0; i < count; i++)
        {
            const card = this.addCard();

            if(i == 2)
                card.width = 200;
//            card.x = i * card.width;
            
            this.rowContainer.addChild(card);
        }

        this.positionCardsInRow(this.rowContainer);
    }

    positionCardsInRow(rowContainer)
    {
        const fullWidth = config.logicalWidth * 0.9;
        rowContainer.x = config.logicalWidth * 0.05;
        const cardsWidth = this.getCardsWidthInRow(rowContainer);

        const whiteSpace = fullWidth - cardsWidth;
        console.log(whiteSpace);
        const spacePerItem = (whiteSpace <= 0) ? 5 : whiteSpace/rowContainer.children.length;

        
        let xPos = spacePerItem/2;

        rowContainer.children.forEach(card => {
            card.x = xPos;
            xPos += card.width;
            xPos += spacePerItem;
        });

        if(rowContainer.width > fullWidth)
        {
            rowContainer.scale.x = fullWidth/rowContainer.width;
        } else
        {
            rowContainer.scale.set(1);
        }
    }

    getCardsWidthInRow(row)
    {
        let width = 0;

        row.children.forEach(card => {
            width  += card.width;
        });

        return width;
    }



    addCard()
    {
        const card = new PIXI.Sprite(Globals.resources.card.texture);

        return card;
    }


}