/// <summary>
    /// Spawns new figure on scene
    /// </summary>
    /// <param name="controller">Chess piece controller</param>
    /// <param name="startPosition">Piece starting position</param>
    private void SpawnNewFigure(FigureController controller, Vector3 startPosition)
    {
        Material standardMaterial = Resources.Load <Material>("Light");

        controller.GetComponent <MeshRenderer>().sharedMaterial = standardMaterial;

        string     prefabName = figuresNames.Find(x => controller.name.Contains(x));
        GameObject newFigure  = Instantiate(Resources.Load <GameObject>(prefabName));

        newFigure.name = prefabName + figures.Count.ToString();
        newFigure.transform.position = startPosition;
        newFigure.transform.SetParent(this.gameObject.transform);

        figures.Add(newFigure.GetComponent <FigureController>());
    }
    /// <summary>
    /// Invoked after clicking o chess piece detection, highlights clicked piece
    /// </summary>
    /// <param name="objectName">Clicked object</param>
    public void OnClickDetected(string objectName)
    {
        Material standardMaterial = Resources.Load <Material>("Light");

        figures.ForEach((x) =>
        {
            if (!x.IsMoving)
            {
                x.GetComponent <MeshRenderer>().sharedMaterial = standardMaterial;
            }
            ;
        });

        if (figures.Exists(x => x.name == objectName) && !figures.Exists(x => x.IsMoving))
        {
            figureToPlace = figures.Find(x => x.name == objectName);
            figureToPlace.GetComponent <MeshRenderer>().sharedMaterial = Resources.Load <Material>("HightlightLight");
        }
    }
Esempio n. 3
0
    void SpawnFigure(FigureController figureController, int figureIndex, float rotation)
    {
        figureController.transform.localRotation = Quaternion.identity;

        int colorIndex = Random.Range(0, 6);

        int[,] figure = Figures1010.Figures[figureIndex];
        for (int i = 0; i < figure.GetLength(0); i++)
        {
            for (int j = 0; j < figure.GetLength(1); j++)
            {
                if (figure[figure.GetLength(0) - i - 1, j] == 0)
                {
                    continue;
                }

                NumberedBrick brick = Instantiate(brickPrefab, figureController.transform);
                figureController.bricks.Add(brick);

                RectTransform brickRectTransform = brick.GetComponent <RectTransform>();

                brickRectTransform.anchorMin = new Vector2(0.5f, 0.5f);
                brickRectTransform.anchorMax = new Vector2(0.5f, 0.5f);

                Rect    rect      = figureController.GetComponent <RectTransform>().rect;
                Vector2 brickSize = new Vector2
                {
                    x = rect.width / 4,
                    y = rect.height / 4
                };

                Vector2 coords        = new Vector2(j - figure.GetLength(1) / 2f, i - figure.GetLength(0) / 2f);
                Vector2 brickPosition = Vector2.Scale(coords, brickSize);
                brickPosition += Vector2.Scale(brickSize, brickRectTransform.pivot);
                brick.GetComponent <RectTransform>().anchoredPosition = brickPosition;

                brick.ColorIndex = colorIndex;
            }
        }

        figureController.transform.localRotation = Quaternion.Euler(0f, 0f, rotation);
    }