public void SetShape(miniTF_ShapeDesc shape, Player player, miniTF grid, Vector2 _pos)
    {
        this.player   = player;
        this.shape    = shape;
        this.grid     = grid;
        sprite        = GetComponent <SpriteRenderer>();
        sprite.sprite = shape.sprite;
        sprite.color  = Color.Lerp(PlayerManager.PlayerColors[player.id], Color.white, 0.5f);

        String[] lines = this.shape.shape.Split('\n');
        for (int y = 0; y < lines.Length; y++)
        {
            for (int x = 0; x < lines[y].Length; x++)
            {
                if (lines[y][x] == 'A')
                {
                    coveredSquares.Add(new Vector2(x - 1, 1 - y));
                }
            }
        }

        this.x = (int)_pos.x;
        this.y = (int)_pos.y;

        SetPos();
    }
Example #2
0
 void ActivateTiles(int _x, int _y, miniTF_ShapeDesc shape)
 {
     String[] lines = shape.shape.Split('\n');
     for (int y = 0; y < lines.Length; y++)
     {
         for (int x = 0; x < lines[y].Length; x++)
         {
             if (lines[y][x] == 'A')
             {
                 miniTF_Tile tile = GetTile(_x + x, _y + 2 - y);
                 if (tile != null)
                 {
                     tile.SetTileActive(true);
                     tile.ToggleShowing();
                 }
             }
         }
     }
 }
Example #3
0
    bool CheckForClashes(int _x, int _y, miniTF_ShapeDesc shape)
    {
        String[] lines = shape.shape.Split('\n');
        for (int y = 0; y < lines.Length; y++)
        {
            for (int x = 0; x < lines[y].Length; x++)
            {
                if (lines[y][x] == 'A')
                {
                    miniTF_Tile tile = GetTile(_x + x, _y + 2 - y);
                    if (tile == null || tile.TileIsActive)
                    {
                        return(true);
                    }
                }
            }
        }

        return(false);
    }
Example #4
0
    void Update()
    {
        if (stillPlacingShapes)
        {
            nextShapeTimer += Time.deltaTime;
            while (nextShapeTimer >= NextShapeTime && !placementFailed && ActiveTileCount() < ActiveTileTarget)
            {
                nextShapeTimer -= NextShapeTime;

                // Randomise the list of shapes.
                List <miniTF_ShapeDesc> randomShapeList = new List <miniTF_ShapeDesc>(Shapes);
                bool successfulPlacement = false;
                while (!successfulPlacement && randomShapeList.Count > 0)
                {
                    int shapeIndex = UnityEngine.Random.Range(0, randomShapeList.Count);
                    miniTF_ShapeDesc nextRandomShape = randomShapeList[shapeIndex];
                    randomShapeList.RemoveAt(shapeIndex);

                    Debug.Log("trying a random shape");

                    List <miniTF_Tile> inactiveNeighbouringActive = GetInactiveNeighbouringActive();
                    if (DebugMarkersEnabled)
                    {
                        for (int i = 0; i < inactiveNeighbouringActive.Count; i++)
                        {
                            GameObject marker = GameObject.CreatePrimitive(PrimitiveType.Sphere);
                            marker.transform.localScale = Vector3.one * 0.5f;
                            marker.transform.position   = GetPosition(inactiveNeighbouringActive[i].x, inactiveNeighbouringActive[i].y);
                        }
                    }

                    while (!successfulPlacement && inactiveNeighbouringActive.Count > 0)
                    {
                        int index = UnityEngine.Random.Range(0, inactiveNeighbouringActive.Count);

                        // Now pick one of the random inactive points.
                        miniTF_Tile nextTile = inactiveNeighbouringActive[index];
                        inactiveNeighbouringActive.RemoveAt(index);

                        String[] lines = nextRandomShape.shape.Split('\n');
                        for (int y = 0; y < lines.Length; y++)
                        {
                            for (int x = 0; x < lines[y].Length; x++)
                            {
                                // As y increases, we're going 'down' the shape that was described.
                                // e.g.
                                //y x>0 1 2
                                //v
                                //0   0 A A
                                //1   0 A 0
                                //2   0 A 0

                                if (!successfulPlacement && lines[y][x] == 'A')
                                {
                                    // Then, this is a potential starting point,
                                    // because it contains an active square.

                                    Debug.Log(x + ", " + y + ": potential starting point");

                                    if (!CheckForClashes(nextTile.x - x, nextTile.y - 2 + y, nextRandomShape))
                                    {
                                        ActivateTiles(nextTile.x - x, nextTile.y - 2 + y, nextRandomShape);
                                        successfulPlacement = true;

                                        if (DebugMarkersEnabled)
                                        {
                                            GameObject marker = GameObject.CreatePrimitive(PrimitiveType.Sphere);
                                            marker.transform.position = GetPosition(nextTile.x, nextTile.y);
                                            successfulPlacement       = true;
                                        }
                                    }
                                }

                                if (!successfulPlacement && lines[y][x] == 'A')
                                {
                                    // Then this is a potential starting point. See if it would cause any clashes.
                                    if (false && !CheckForClashes(nextTile.x + x, nextTile.y - y, nextRandomShape))
                                    {
                                        Debug.Log("no clashes");
                                        ActivateTiles(nextTile.x + (x - 1), nextTile.y - y, nextRandomShape);
                                        if (DebugMarkersEnabled)
                                        {
                                            GameObject marker = GameObject.CreatePrimitive(PrimitiveType.Sphere);
                                            marker.transform.position = GetPosition(nextTile.x, nextTile.y);
                                            successfulPlacement       = true;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }

                if (!successfulPlacement)
                {
                    placementFailed = true;
                }

                if (placementFailed || ActiveTileCount() >= ActiveTileTarget)
                {
                    stillPlacingShapes  = false;
                    stillFlippingShapes = true;

                    // Create the players.
                    spawnedShapes = new List <miniTF_Shape>();
                    for (int i = 0; i < PlayerManager.GetPlayers().Count; i++)
                    {
                        GameObject   player = GameObject.Instantiate(PlayerObject);
                        miniTF_Shape s      = player.GetComponent <miniTF_Shape>();
                        s.SetShape(randomShapes[i], PlayerManager.GetPlayer(i), this, PlayerPositions[i]);
                        spawnedShapes.Add(s);
                    }
                }
            }
        }

        if (stillFlippingShapes)
        {
            flipShapeTimer += Time.deltaTime;

            while (flipShapeTimer >= NextFlipTime)
            {
                flipShapeTimer -= NextFlipTime;
                int shapeIndex = UnityEngine.Random.Range(0, spawnedShapes.Count);
                spawnedShapes[shapeIndex].ToggleRandom();

                flipCount++;
                if (flipCount >= FlipNumber)
                {
                    stillFlippingShapes = false;
                }
            }
        }
    }