Lines (Mayan Magic)
This is a version of the game Lines with a mayan theme.
PLAY GAMENote: You need the Unity Web Plugin to play this example.
Main Game Logic
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
using System; using System.Linq; using System.Collections.Generic; using UnityEngine; using Gamelogic; using Gamelogic.Grids; public class Lines : GLMonoBehaviour { #region Inspector public LinesCell cellPrefab; public int width = 5; public int height = 5; public int lineLength = 4; public int colorCount = 3; public int cellsPerTurn = 5; public GameObject root; #endregion #region Private Fields private Vector2 hexDimensions = new Vector2(74, 84);// private PointyHexGrid grid; private IMap3D map; private bool isHoldingCell; private LinesCell cellThatIsBeingHeld; private Rect screenRect; private int moveCount; #endregion #region Callbacks public void Start() { moveCount = 0; BuildGrid(); AddNewCells(); } public void Update() { if(Input.GetMouseButtonDown(0)) { Vector3 worldPosition = ExampleUtils.ScreenToWorld_NGUI(root, Input.mousePosition); PointyHexPoint hexPoint = map[worldPosition]; if(grid.Contains(hexPoint)) { ClickPoint (hexPoint); } } } #endregion private void ClickPoint(PointyHexPoint hexPoint) { LinesCell clickedCell = grid [hexPoint]; if (isHoldingCell) { if (clickedCell.IsEmpty) { MoveCell(clickedCell); if (!ClearLinesAroundPoint(hexPoint)) { moveCount++; AddNewCells(); }//otherwise, give the player a "free" turn. } else if (clickedCell == cellThatIsBeingHeld) { DropCell(clickedCell); } } else { if (!clickedCell.IsEmpty) { PickUpCell(clickedCell); } } } private void PickUpCell(LinesCell clickedCell) { cellThatIsBeingHeld = clickedCell; isHoldingCell = true; clickedCell.HighlightOn = true; } private void DropCell (LinesCell clickedCell) { cellThatIsBeingHeld.HighlightOn = false; cellThatIsBeingHeld = null; isHoldingCell = false; } private void MoveCell(LinesCell clickedCell) { cellThatIsBeingHeld.HighlightOn = false; SwapCells (cellThatIsBeingHeld, clickedCell); cellThatIsBeingHeld = null; isHoldingCell = false; } private void BuildGrid() { screenRect = new Rect(-Screen.width/2, -Screen.height / 2, Screen.width, Screen.height); grid = PointyHexGrid .BeginShape() .UpTriangle(7) .Translate(-1, 2) .Union() .DownTriangle(7) .EndShape(); map = new PointyHexMap(hexDimensions) .WithWindow(screenRect) .AlignMiddelCenter(grid) .AnchorCellMiddelCenter() .To3DXY(); foreach (PointyHexPoint point in grid) { LinesCell cell = Instantiate (cellPrefab); Vector3 worldPoint = map[point]; cell.transform.parent = root.transform; cell.transform.localPosition = worldPoint; cell.transform.localScale = Vector3.one; cell.SetState (true, -1); grid[point] = cell; } } private void AddNewCells() { var emptyCells = GetEmptyCells(); List cellsToPlace; cellsToPlace = emptyCells.SampleRandom(cellsPerTurn); foreach(PointyHexPoint point in cellsToPlace) { SetCellToRandom(grid[point]); ClearLinesAroundPoint(point); } emptyCells = GetEmptyCells(); if(!emptyCells.Any()) { Debug.Log ("Game Ends!"); } } private IEnumerable GetEmptyCells () { return from point in grid where grid [point].IsEmpty select point; } private void SetCellToRandom(LinesCell cell) { int newColor = (UnityEngine.Random.Range(0, colorCount)); cell.SetState(false, newColor); } private static void SwapCells(LinesCell cell1, LinesCell cell2) { int tempColor = cell1.Color; bool tempIsEmpty = cell1.IsEmpty; cell1.SetState(cell2.IsEmpty, cell2.Color); cell2.SetState(tempIsEmpty, tempColor); } private bool ClearLinesAroundPoint(PointyHexPoint point) { bool wasLinesRemoved = false; IEnumerable lines = from line in Algorithms.GetConnectedLines(grid, point, (p1, p2) => grid[p1].Color == grid[p2].Color) where line.Count() >= lineLength select line; foreach (IEnumerable line in lines) { foreach(PointyHexPoint linePoint in line) { grid[linePoint].SetState(true, -1); } wasLinesRemoved = true; } return wasLinesRemoved; } } |