Exemplo n.º 1
0
        public static void Test()
        {
            var grid = new BlockGrid();

            grid.LoadRow(9, "####################");
            grid.LoadRow(8, "#                  #");
            grid.LoadRow(7, "#          #   #####");
            grid.LoadRow(6, "#  ####### #####   #");
            grid.LoadRow(5, "# #######          #");
            grid.LoadRow(4, "#  ##  ##      #   #");
            grid.LoadRow(3, "#* ##  ###     #   #");
            grid.LoadRow(2, "#  ##  #       #   #");
            grid.LoadRow(1, "#      #####       #");
            grid.LoadRow(0, "####################");

            for (int i = 0; i < 100; i++)
            {
                grid.Render();
                Process(grid);
                System.Threading.Thread.Sleep(2000);
            }
        }
Exemplo n.º 2
0
        //Apply thruster force.
        public void FixedUpdate()
        {
            //If the thrust is forced, power = 1, otherwise check driver inputs if there is one.
            if (ThrustForced)
            {
                ThrustPower = 1.00f;
            }
            else if (Driver)
            {
                Vector3 up   = transform.up;
                Vector3 move = Driver.Move;
                Axis    axis = LargestComponent(up);

                ThrustPower = (axis == Axis.X ? (move.x == 0 ? false : (up.x > 0 == move.x > 0))
                             : axis == Axis.Y ? (move.y == 0 ? false : (up.y > 0 == move.y > 0))
                             : axis == Axis.Z ? (move.z == 0 ? false : (up.z > 0 == move.z > 0))
                             : false) ? 1.00f : 0.00f;
            }
            else
            {
                ThrustPower = 0.00f;
            }

            BlockGrid grid = Location.Grid;

            if (grid && IsThrusting)
            {
                var force = FORCE * ThrustPower * Time.deltaTime;
                grid.Rigidbody.AddForceAtPosition(transform.up * -force, transform.position);
            }

            VisualEffect effect = Effect;

            if (effect)
            {
                effect.SetBool("Thrusting", IsThrusting);
            }
        }
        public void Update()
        {
            BlockPreview preview = Preview;

            if (preview != null)
            {
                //Find which GameObject is being targeted.
                Transform camera    = Camera.main.transform;
                Vector3   direction = camera.forward;
                if (Physics.Raycast(camera.position, direction, out look))
                {
                    // <<something was hit>>

                    //Apply 'hitError' to the raycast hit.
                    look.point -= direction * hitError;

                    BlockGrid grid = Grid = look.transform.gameObject.GetComponent <BlockGrid>();
                    if (grid)
                    {
                        //There is a grid, so align the block to the grid.
                        preview.transform.position = grid.WorldPoint_to_WorldGrid(look.point);
                        preview.transform.rotation = grid.transform.rotation * Rotation;
                    }
                    else
                    {
                        //There isn't any grid, so just put the preview at the raycast hit.
                        preview.transform.position = look.point;
                    }
                }
                else
                {
                    //Nothing is being targeted so just put the preview at the max distance.
                    Grid       = null;
                    look.point = camera.position + (direction * distance);
                    preview.transform.position = look.point;
                }
            }
        }
Exemplo n.º 4
0
    // Start is called before the first frame update
    void Start()
    {
        grid = FindObjectOfType <BlockGrid>();
        myId = grid.makeKey(transform.position);

        // Find out if there is already a block in that space.
        if (grid.exists(myId))
        {
            Destroy(this.gameObject);
            return;
        }

        //Put the block into the grid
        grid.addToGrid(myId, this);

        //The next part does not need to be done if the block is part of the walls
        if (myColor != bColor.WALL)
        {
            adjacentSpaces[0] = transform.position + Vector3.up;
            adjacentSpaces[1] = transform.position + Vector3.right;
            adjacentSpaces[2] = transform.position - Vector3.up;
            adjacentSpaces[3] = transform.position - Vector3.right;

            UpdateBlock();
            foreach (Block block in adjacentBlocks)
            {
                if (block != null)
                {
                    block.UpdateBlock();
                }
            }
            if (!grid.IsBlockRooted(transform.position))
            {
                List <string> bList = new List <string>();
                BackToBubble(bList);
            }
        }
    }
Exemplo n.º 5
0
        public void RemoveMultipleLinesTest()
        {
            List <Vector2> expectedPoints;
            BlockGrid      grid = RemoveLinesInitialize(out expectedPoints);

            //Get indexes of lines to remove
            List <int> lineIndexes = grid.GetCompletedLines();

            //Remove lines and shift above blocks down
            grid.RemoveLines(lineIndexes);

            //check that blocks removed and remaining above blocks shifted down
            List <Vector2> points = grid.GetOccupiedCoordinates();

            foreach (Vector2 p in expectedPoints)
            {
                //check if p in expected points
                if (!points.Contains(p))
                {
                    Assert.Fail("Block at point: {0}, {1} not found on grid", p.X, p.Y);
                }
            }
        }
Exemplo n.º 6
0
        private void processInput(BlockGrid grid, GameShape activeShape)
        {
            InputAction curInput = inputReader.GetLastAction();

            switch (curInput)
            {
            case InputAction.Pause:
                togglePause();
                break;

            case InputAction.Null:
                break;

            default:
                if (!isPaused)
                {
                    Console.WriteLine($"Applying {curInput} to shape: {activeShape}");
                    MovementManager.ApplyAction(curInput, grid, activeShape);
                }

                break;
            }
        }
Exemplo n.º 7
0
        // Author: DeAngelo Wilson
        public BlockGrid RemoveLinesInitialize(out List <Vector2> expectedPoints)
        {
            //points: (0,3) (0,4) (1,3) (1,4) -- (0,1) (0,2) (1,1) (1,2) -- (2,3) (2,4) (3,3) (3,4)
            expectedPoints = new List <Vector2>()
            {
                new Vector2(0, 1), new Vector2(0, 2), new Vector2(1, 1), new Vector2(1, 2), new Vector2(2, 2), new Vector2(2, 1),
                new Vector2(3, 2), new Vector2(3, 1)
            };



            BlockGrid grid = new BlockGrid(6, 6);

            GameShape shape1 = new SquareShape(new Block(0, 3), defaultOri);

            grid.PlaceShape(shape1);

            GameShape shape2 = new SquareShape(new Block(2, 1), defaultOri);

            grid.PlaceShape(shape2);

            GameShape shape3 = new SquareShape(new Block(0, 1), defaultOri);

            grid.PlaceShape(shape3);

            //complete line init
            GameShape shape4 = new SquareShape(new Block(4, 1), defaultOri);

            grid.PlaceShape(shape4);

            //2nd line needs 1 more square
            GameShape shape5 = new SquareShape(new Block(2, 3), defaultOri);

            grid.PlaceShape(shape5);

            return(grid);
        }
    public void RenderBlockParts()
    {
        Block     block = _blockRenderer.block;
        BlockGrid grid  = BlockGridController.Singleton.grid;

        Block frontBlock = grid.GetBlock(block.coordinates.GetFront(grid));
        Block rearBlock  = grid.GetBlock(block.coordinates.GetRear(grid));
        Block leftBlock  = grid.GetBlock(block.coordinates.GetRight(grid));
        Block rightBlock = grid.GetBlock(block.coordinates.GetLeft(grid));

        /*
         * Debug.Log("RenderBlockParts() -> " + block.coordinates + " (" + block.type + ")"
         + "\n\t front: \t" + frontBlock.coordinates + " (" + frontBlock.type + ")"
         + "\n\t rear: \t" + rearBlock.coordinates + " (" + rearBlock.type + ")"
         + "\n\t left: \t" + leftBlock.coordinates + " (" + leftBlock.type + ")"
         + "\n\t right: \t" + rightBlock.coordinates + " (" + rightBlock.type + ")"
         +  );
         */

        front.SetActive(frontBlock != null && frontBlock.type == BlockType.Ground);
        rear.SetActive(rearBlock != null && rearBlock.type == BlockType.Ground);
        left.SetActive(leftBlock != null && leftBlock.type == BlockType.Ground);
        right.SetActive(rightBlock != null && rightBlock.type == BlockType.Ground);
    }
Exemplo n.º 9
0
 public void OnEnable()
 {
     script = (BlockGrid)target;
 }
Exemplo n.º 10
0
 /// <summary>Creates a new <see cref="GridLocation"/> with an offset.</summary>
 public GridLocation(BlockGrid grid, Vector3Int vector, Vector3Int offset)
 {
     this.grid   = grid;
     this.vector = vector;
     this.offset = offset;
 }
Exemplo n.º 11
0
 /// <summary>Creates a new <see cref="GridLocation"/> with no offset.</summary>
 public GridLocation(BlockGrid grid, Vector3Int vector)
 {
     this.grid   = grid;
     this.vector = vector;
     offset      = Vector3Int.zero;
 }
Exemplo n.º 12
0
    public GridCoordinates GetLeft(BlockGrid grid)
    {
        GridCoordinates coords = new GridCoordinates(x - 1, y);

        return(coords.OutOfBounds(grid) ? null : coords);
    }
Exemplo n.º 13
0
 void Awake()
 {
     blockGrid = target as BlockGrid;
 }
Exemplo n.º 14
0
 private void Start()
 {
     _grid       = GameObject.FindGameObjectWithTag("BlockGrid").GetComponent <BlockGrid>();
     _blockPanel = GetComponentInParent <BlockPanel>();
 }
Exemplo n.º 15
0
 private void Awake()
 {
     Instance            = this;
     DeathScreen.OnQuit += Save;
     gridArray           = new Block[width, height, 2];
 }
Exemplo n.º 16
0
 /// <summary>Hides the terminal of the given <see cref="BlockGrid"/>.</summary>
 public void HideTerminal(BlockGrid grid)
 {
     throw new NotImplementedException("UIManager.HideTerminal()");
 }
Exemplo n.º 17
0
    public GridCoordinates GetRearRight(BlockGrid grid)
    {
        GridCoordinates coords = new GridCoordinates(x + 1, y - 1);

        return(coords.OutOfBounds(grid) ? null : coords);
    }
Exemplo n.º 18
0
    public GridCoordinates GetFront(BlockGrid grid)
    {
        GridCoordinates coords = new GridCoordinates(x, y + 1);

        return(coords.OutOfBounds(grid) ? null : coords);
    }
Exemplo n.º 19
0
 public bool OnGridEdge(BlockGrid grid)
 {
     return(OutOfBounds(grid) || x == 0 || y == 0 || x == grid.Width - 1 || y == grid.Length - 1);
 }
Exemplo n.º 20
0
    /*
     * public List<GridCoordinates> GetAdjacentCoords(BlockGrid grid)
     * {
     *  List<GridCoordinates> resultList = new List<GridCoordinates>();
     *  GridCoordinates front = GetFront(grid);
     *  GridCoordinates right = GetRight(grid);
     *  GridCoordinates rear = GetRear(grid);
     *  GridCoordinates left = GetLeft(grid);
     *
     *  if (front != null)
     *  {
     *      resultList.Add(front);
     *  }
     *  if (right != null)
     *  {
     *      resultList.Add(right);
     *  }
     *  if (rear != null)
     *  {
     *      resultList.Add(rear);
     *  }
     *  if (left != null)
     *  {
     *      resultList.Add(left);
     *  }
     *
     *  return resultList;
     * }
     */

    public bool OutOfBounds(BlockGrid grid)
    {
        return(x < 0 || y < 0 || x >= grid.Width || y >= grid.Length);
    }
Exemplo n.º 21
0
        private Cluster GetCluster(Vector2Int startPoint, ICollection <Block> vacantBlocks, BlockGrid blockGrid)
        {
            var cluster        = new Cluster();
            var visitedGridPos = new HashSet <Vector2Int>();

            void FloodFill(int x, int y)
            {
                var gridPos = new Vector2Int(x, y);

                if (visitedGridPos.Contains(gridPos))
                {
                    return;
                }

                visitedGridPos.Add(gridPos);

                if (blockGrid.TryGetMember(gridPos, out var member))
                {
                    if (vacantBlocks.Contains(member))
                    {
                        vacantBlocks.Remove(member);
                        cluster.Add(member);

                        if (member.GetIsController())
                        {
                            if (cluster.containsController)
                            {
                                Debug.LogWarning($"Cluster {cluster} contains multiple controllers, this shouldn't happen");
                            }

                            cluster.containsController = true;
                        }
                    }

                    FloodFill(x - 1, y);
                    FloodFill(x + 1, y);
                    FloodFill(x, y - 1);
                    FloodFill(x, y + 1);
                }
            }

            FloodFill(startPoint.x, startPoint.y);

            return(cluster);
        }
Exemplo n.º 22
0
 public MapData(string name, BlockGrid blockGrid)
 {
     this.name      = name;
     this.blockGrid = blockGrid;
 }
Exemplo n.º 23
0
        // Author: Stahl Samuel, Yuetao Zhu
        public override void Update()
        {
            AudioEngine.Update();
            if (music.Volume > 0.30f)
            {
                vol_delta = -0.002f;
            }
            else if (music.Volume < 0.00f)
            {
                vol_delta = 0.002f;
            }
            music.Volume += vol_delta;

            // Intentionally disabling sounds for now
            music.Volume = 0.000f;

            inputReader.GetInputs();
            if (!isPaused)
            {
                //--------------------------------------------------------
                // Rotate Sprite -- shows if game paused or not...
                //--------------------------------------------------------
                pRedBird.angle = pRedBird.angle + 0.01f;
                pRedBird.Update();


                BlockGrid grid        = state.getGrid();
                GameShape activeShape = state.getActiveShape();

                // Things we need to check on every update:
                // 1. activeShape is placed => trigger GameState.activateNext() and set active shape to new active shape
                // 2. if timer is expired => reset timer and MovementManager.ApplyAction(InputAction.MoveDown,grid,shape);
                // 3. if timer is not expired => processInput();

                if (activeShape.isPlaced)
                {
                    //Update activeShape, lines, score, level, check game-over
                    UpdateGameState(grid);
                }
                //if shape was placed, timer was just reset (mutually exclusive)
                else if (lineCycleTimer.IsExpired())
                {
                    Console.WriteLine($"Moving shape down: {activeShape}");
                    MovementManager.ApplyAction(InputAction.MoveDown, grid, activeShape);
                    lineCycleTimer.ResetTimer();
                    inputTimer.ResetTimer();
                }
                else
                {
                    if (inputTimer.IsExpired())
                    {
                        processInput(grid, activeShape);
                        inputTimer.ResetTimer();
                    }
                }
            }
            else
            {
                if (inputTimer.IsExpired())
                {
                    // Paused
                    processInput(null, null);
                    inputTimer.ResetTimer();
                }
            }
        }
Exemplo n.º 24
0
        private static void Process(BlockGrid grid)
        {
            //var pres = grid.CalcPressure();
            //pres.Render();

            var water = grid.FindWater();

            water.AddRange(grid.Find(BlockType.WaterSource));
            var moveTo = new SortedList <int, Position>(new DuplicateKeyComparer <int>());

            foreach (var w in water)
            {
                if (grid [w.X + 1, w.Y] == BlockType.Air)
                {
                    moveTo.Add(w.Y, new Position(w.X + 1, w.Y));
                }
                if (grid [w.X, w.Y + 1] == BlockType.Air)
                {
                    moveTo.Add(w.Y + 1, new Position(w.X, w.Y + 1));
                }
                if (grid [w.X - 1, w.Y] == BlockType.Air)
                {
                    moveTo.Add(w.Y, new Position(w.X - 1, w.Y));
                }
                if (grid [w.X, w.Y - 1] == BlockType.Air)
                {
                    moveTo.Add(w.Y - 1, new Position(w.X, w.Y - 1));
                }
            }

            var       newBlock     = moveTo.Values[0];
            BlockType newBlockType = BlockType.Water;

            if (grid[newBlock.X, newBlock.Y - 1] == BlockType.Air)
            {
                newBlockType = BlockType.FallingWater;
            }
            grid.SetBlock(newBlock, newBlockType);

            // Convert falling water to water
            foreach (var w in grid.Find(BlockType.FallingWater))
            {
                if (grid[w.X + 1, w.Y] == BlockType.Water || grid[w.X - 1, w.Y] == BlockType.Water)
                {
                    grid[w.X, w.Y] = BlockType.Water;
                }
            }

            /*
             * foreach (var s in grid.Find(BlockType.WaterSource))
             * {
             *  var stream = grid.FindStream(s);
             *  var p = new List<Coord>();
             *  foreach(var c in stream)
             *  {
             *      if (pres[c.X, c.Y] == 0)
             *      {
             *          // Falling
             *          if (grid[c.X, c.Y - 1] == BlockType.Air)
             *              p.Add(new Coord(c.X, c.Y - 1));
             *          else if (grid[c.X + 1, c.Y - 1] == BlockType.Air)
             *              p.Add(new Coord(c.X + 1, c.Y - 1));
             *          else if (grid[c.X - 1, c.Y - 1] == BlockType.Air)
             *              p.Add(new Coord(c.X - 1, c.Y - 1));
             *      }
             *      else if (pres[c.X,c.Y] < 0)
             *      {
             *          // Up
             *          if (grid[c.X, c.Y + 1] == BlockType.Air)
             *              p.Add(new Coord(c.X, c.Y + 1));
             *          else if (grid[c.X + 1, c.Y + 1] == BlockType.Air)
             *              p.Add(new Coord(c.X + 1, c.Y + 1));
             *          else if (grid[c.X - 1, c.Y + 1] == BlockType.Air)
             *              p.Add(new Coord(c.X - 1, c.Y + 1));
             *      }
             *      else
             *      {
             *          // Sideways
             *          if (grid[c.X + 1, c.Y] == BlockType.Air)
             *              p.Add(new Coord(c.X + 1, c.Y));
             *          else if (grid[c.X - 1, c.Y] == BlockType.Air)
             *              p.Add(new Coord(c.X - 1, c.Y));
             *      }
             *  }
             *  grid.SetBlock(p[0], BlockType.Water);
             * }
             */


            //var water = grid.FindNeighbours(BlockType.Water);
            //water.AddRange(grid.FindNeighbours(BlockType.FallingWater));
            //foreach (var s in water)
            //{
            //    if (grid[s.X,s.Y-1] == BlockType.Dirt)
            //    {
            //        grid[s.X, s.Y] = BlockType.Water;
            //    }
            //    if (grid[s.X, s.Y - 1] == BlockType.Air)
            //    {
            //        grid[s.X, s.Y-1] = BlockType.FallingWater;
            //    }
            //}

            //foreach (var s in grid.Find(BlockType.Water))
            //{
            //    if (grid[s.X,s.Y-1] == BlockType.Air)
            //    {
            //        grid[s.X, s.Y] = BlockType.FallingWater;
            //    }
            //    else if (grid[s.X+1,s.Y-1] == BlockType.Air)
            //    {
            //        grid[s.X, s.Y] = BlockType.Air;
            //        grid[s.X + 1, s.Y - 1] = BlockType.Water;
            //    }
            //    else if (grid[s.X-1,s.Y-1] == BlockType.Air)
            //    {
            //        grid[s.X, s.Y] = BlockType.Air;
            //        grid[s.X - 1, s.Y - 1] = BlockType.Water;
            //    }
            //}

            //foreach (var s in grid.Find(BlockType.Water))
            //{
            //    if (grid[s.X, s.Y - 1] == BlockType.Air)
            //    {
            //        grid[s.X, s.Y - 1] = BlockType.FallingWater;
            //        grid[s.X, s.Y] = BlockType.Air;
            //    }
            //}
        }
Exemplo n.º 25
0
    public void handleMovement()
    {
        //Debug.Log("HANDLE MOVEMENT");
        // Move Left
        if (Input.GetAxis("Horizontal") < 0 &&
            Time.time - lastMove >= moveTime || Input.GetKeyDown(KeyCode.LeftArrow) || Input.GetKeyDown(KeyCode.A))
        {
            //Debug.Log("LEFT RECV");
            // Modify position
            transform.position += new Vector3(-1, 0, 0);

            // See if valid
            if (isValidGridPos())
            {
                // It's valid. Update grid.
                updateGrid();
                player.transform.position += new Vector3(-1, 0, 0);
                updateSight();
            }
            else
            {
                // It's not valid. revert.
                transform.position += new Vector3(1, 0, 0);
            }
            //Debug.Log("LEFT FAIL");
            lastMove = Time.time;
        }

        // Move Right
        else if (Input.GetAxis("Horizontal") > 0 &&
                 Time.time - lastMove >= moveTime || Input.GetKeyDown(KeyCode.RightArrow) || Input.GetKeyDown(KeyCode.D))
        {
            // Modify position
            transform.position += new Vector3(1, 0, 0);


            // See if valid
            if (isValidGridPos())
            {
                // It's valid. Update grid.
                updateGrid();
                player.transform.position += new Vector3(1, 0, 0);
                updateSight();
            }
            else
            {
                // It's not valid. revert.
                transform.position += new Vector3(-1, 0, 0);
            }
            lastMove = Time.time;
        }

        // Rotate
        if (Input.GetKeyDown(KeyCode.UpArrow) || Input.GetKeyDown(KeyCode.W))
        {
            //Debug.Log(transform.position.ToString());
            transform.Rotate(0, 0, -90);

            // See if valid
            if (isValidGridPos())
            {
                // It's valid. Update grid.
                updateGrid();
            }
            else
            {
                // It's not valid. revert.
                transform.Rotate(0, 0, 90);
                //Debug.Log("ROT FAIL");
            }
        }

        // Move Downwards and Fall
        if (Input.GetKeyDown(KeyCode.DownArrow) ||
            Time.time - lastFall >= dropTime || Input.GetKeyDown(KeyCode.S))
        {
            // Modify position
            transform.position += new Vector3(0, -1, 0);

            // See if valid
            if (isValidGridPos())
            {
                // It's valid. Update grid.
                updateGrid();
            }
            else
            {
                // It's not valid. revert.
                transform.position += new Vector3(0, 1, 0);

                // Clear filled horizontal lines
                //BlockGrid.deleteFullRows();
                float x            = BlockGrid.roundVec2(transform.position).x;
                int   sectionStart = Mathf.RoundToInt(x / secLength) * secLength;
                BlockGrid.deleteFullSections(sectionStart, secLength, maxBlockOffset);

                // Spawn next Group
                player.spawnNext();
            }

            lastFall = Time.time;
        }
    }
Exemplo n.º 26
0
 // Start is called before the first frame update
 void Start()
 {
     grid = FindObjectOfType <BlockGrid>();
 }