private void OnPositionChange(ModelPositionElement ghost) { // save level SaveProgress(); // check for intersection if (Field.IntersectBlocks(ghost)) { // game over if (ghost.Position.y + ghost.Height >= Height) { Debug.Log(ghost.Position.y); State = GameState.Complete; PlayerPrefs.DeleteKey(Settings.ApplicationName); return; } Field.SplitFallBlock(Falling.Element); View.RemoveFallBlock(); View.RedrawField(Field); Falling.Dispose(); State = GameState.Next; return; } if (!Field.IntersectWalls(ghost)) { Falling.Element = ghost; } View.RedrawFallElement(Falling.Element); }
public bool IntersectBlocks(ModelPositionElement element) { if (IntersectBottom(element)) { return(true); } bool intersect = false; element.EachElement((int i, int j, bool filled) => { if (filled) { var position = element.Position + new Vector2Int(i, j); var index = position.y * Width + position.x; if (index >= 0) { if (index < Blocks.Length) { if (Blocks[index]) { intersect = true; } } } } }); return(intersect); }
public void SplitFallBlock(ModelPositionElement element) { // fill element element.EachElement((int i, int j, bool filled) => { if (filled) { var position = element.Position + new Vector2Int(i, j); int index = position.y * Width + position.x; Blocks[index] = true; } }); // check rows var cloneBblocks = new bool[Height * Width]; int rowIndex = 0; for (int j = 0; j < Height; ++j) { var complete = 0; for (int i = 0; i < Width; ++i) { if (Blocks[j * Width + i]) { complete++; } } if (complete < Width) { GameUtils.CopyRowUnsafe(Blocks, j * Width, cloneBblocks, Width * rowIndex++, Width); } } Blocks = cloneBblocks; }
public void HorizontalMove(int delta) { ModelPositionElement ghost = Element.Clone; ghost.Position += new Vector2Int(delta, 0); Events.Invoke(ghost); }
public void VerticalMove() { ModelPositionElement ghost = Element.Clone; ghost.Position += new Vector2Int(0, -1); Events.Invoke(ghost); }
public void CreateFallBlock(ModelPositionElement element) { FallenBlock = new GameObject("FallenBlock"); FallenBlock.transform.parent = FallArea.transform; var pieces = new List <GameObject>(); element.EachElement((int i, int j, bool filled) => { if (filled) { var piece = Instantiate(GameSettings.Instance.BlockPrefab, FallenBlock.transform); piece.transform.localPosition = new Vector3(i * BlockSize, j * BlockSize, 0); var render = piece.GetComponent <SpriteRenderer>(); render.color = element.Color; pieces.Add(piece); } }); // save pieces for rotation m_Pieces = pieces.ToArray(); RedrawFallElement(element); }
public void Rotate(bool clockwise) { ModelPositionElement ghost = Element.Clone; ghost.Blocks = GameUtils.Rotate(clockwise, Element.Blocks, Element.Width, Element.Height); ghost.Width = Element.Height; ghost.Height = Element.Width; Events.Invoke(ghost); }
public bool IntersectWalls(ModelPositionElement element) { if (element.Position.x >= 0) { if (element.Position.x + element.Width <= Width) { return(false); } } return(true); }
public void RedrawFallElement(ModelPositionElement element) { FallenBlock.transform.localPosition = new Vector3( element.Position.x * BlockSize, element.Position.y * BlockSize ); int index = 0; element.EachElement((int i, int j, bool filled) => { if (filled) { var piece = m_Pieces[index++]; piece.transform.localPosition = new Vector3(i * BlockSize, j * BlockSize, 0); } }); }
private bool IntersectBottom(ModelPositionElement element) { return(element.Position.y < 0); }
public void Dispose() { Element = null; }
public void CreateBlock(ModelElement val) { Element = new ModelPositionElement(val); }