Ejemplo n.º 1
0
class PieceLayer(cocos.layer.Layer):
    def __init__(self, piece, width, height):
        super(PieceLayer, self).__init__()
        
        self._width = width
        self._height = height
        self._piece = piece
        #logging.debug('New piece layer %d x %d, image name %s' % (width, height, piece.imageName()))

        # Pieces with the 'color' property get a background.
        if hasattr(piece, 'color'):
            #logging.debug('color ' + str(piece.color))
            c = colors[piece.color]
            bg = ColorLayer(c[0], c[1], c[2], 192, width=width, height=height)
            self.add(bg)
            self._background = bg

        pieceSprite = Sprite(piece.imageName())
        pieceSprite.image_anchor_x = 0
        pieceSprite.image_anchor_y = 0

        # Scale the sprite to the correct size.
        rect = pieceSprite.get_rect()
        scale = min(float(width) / rect.width,
                    float(height) / rect.height)
        pieceSprite.scale = scale
        self._pieceSprite = pieceSprite

        self.add(pieceSprite)
        self._opacity = 255

        self._turnIndicator = None
        self._updateTurnIndicator()
        self._chargeIndicator = None
        self._updateChargeIndicator()

    @property
    def opacity(self):
        return self._opacity

    @opacity.setter
    def opacity(self, op):
        # This is a cheap version of transparency: just set the children
        # to be transparent.
        self._background.opacity = op
        self._pieceSprite.opacity = op
        self._opacity = op

    @property
    def width(self):
        return self._width

    @property
    def height(self):
        return self._height

    def _updateTurnIndicator(self):
        """Displays text indicating how many turns are left before attacking."""

        if hasattr(self._piece, 'turn'):
            if self._turnIndicator is None:
                self._turnIndicator = TurnIndicator(36)
                self.add(self._turnIndicator)

            self._turnIndicator.text = str(self._piece.turn)
            self._turnIndicator.position = (self.width / 2, self.height / 2)
        else:
            if self._turnIndicator is not None:
                self.remove(self._turnIndicator)
                self._turnIndicator = None

    def _updateChargeIndicator(self):
        """Displays a bar indicating how charged the unit is."""

        if hasattr(self._piece, 'chargeAtTurn') and hasattr(self._piece, 'turn'):
            maxValue = self._piece.chargeAtTurn(0)
            value = self._piece.toughness
            if self._chargeIndicator is None:
                bgColor = (255, 255, 255, 0)
                emptyColor = (255, 0, 0, 255)
                fullColor = (0, 255, 0, 255)
                self._chargeIndicator = MeterLayer(8, self.height, maxValue, bgColor, emptyColor, fullColor, horizontal=False)
                self.add(self._chargeIndicator)

            self._chargeIndicator.setValueAndMax(value, maxValue)
            self._chargeIndicator.position = (self.width-8, 0)
        else:
            if self._chargeIndicator is not None:
                self.remove(self._chargeIndicator)
                self._chargeIndicator = None

    def refresh(self):
        self._updateTurnIndicator()
        self._updateChargeIndicator()