public UndoData(int row, int column, FootholdType foothold, Direction direction) { this.row = row; this.column = column; this.foothold = foothold; this.direction = direction; }
void JumpCallback() { // Get foothold type FootholdType type = _types[_curRow, _curColumn]; if (type.IsRedirect()) { SetDirection(type.GetDirection().Reverse()); } if (_solution.Count == 0) { ResetMap(); if (_solutions.Count > 0) { // Next solution _solution = _solutions.Dequeue(); // Start to jump Invoke("Jump", jumpDuration); } } else { // Continue to jump Jump(); } }
public static bool IsRedirect(this FootholdType type) { return(type == FootholdType.RedirectLeft || type == FootholdType.RedirectUp || type == FootholdType.RedirectRight || type == FootholdType.RedirectDown); }
void SetFoothold(int row, int column, FootholdType type) { // if (_types[row, column] != type) { _types[row, column] = type; UpdateFoothold(row, column); } }
public int Resolve(MapData mapData) { int[,] footholds = mapData.footholds; _row = footholds.GetRow(); _column = footholds.GetColumn(); // Create array of foothold types _types = new FootholdType[_row, _column]; // Reset total _total = 0; // Set footholds for (int i = 0; i < _row; i++) { int row = _row - 1 - i; for (int j = 0; j < _column; j++) { FootholdType type = footholds[i, j].ToFootholdType(); _types[row, j] = type; if (type != FootholdType.None) { if (type == FootholdType.Double) { _total += 2; } else { _total++; } } } } _curRow = _row - 1 - mapData.startRow; _curColumn = mapData.startColumn; _curDirection = mapData.direction; // Reset count _count = 0; // Reset counter _counter = 0; Try(); return(_counter); }
void UpdateFoothold(int row, int column) { FootholdType type = _types[row, column]; if (type == FootholdType.None) { _footholds[row, column].enabled = false; } else { _footholds[row, column].enabled = true; if (type == FootholdType.Normal) { _footholds[row, column].sprite = normalFoothold; } else if (type == FootholdType.Double) { _footholds[row, column].sprite = doubleFoothold; } else if (type == FootholdType.Time) { _footholds[row, column].sprite = timeFoothold; } else if (type == FootholdType.RedirectLeft) { _footholds[row, column].sprite = redirectLeftFoothold; } else if (type == FootholdType.RedirectUp) { _footholds[row, column].sprite = redirectUpFoothold; } else if (type == FootholdType.RedirectRight) { _footholds[row, column].sprite = redirectRightFoothold; } else if (type == FootholdType.RedirectDown) { _footholds[row, column].sprite = redirectDownFoothold; } } }
public GameObject GetFootholdPrefab(FootholdType type) { if (type == FootholdType.Normal) { return(footholdPrefab); } if (type == FootholdType.Double) { return(doubleFootholdPrefab); } //TODO // if (type == FootholdType.Redirect) // { // return redirectFootholdPrefab; // } return(null); }
void Jump() { Direction direction = _solution.Dequeue(); int nextRow = -1; int nextColumn = -1; if (NextCell(direction, ref nextRow, ref nextColumn)) { // Set direction SetDirection(direction); // Get foothold type FootholdType type = _types[_curRow, _curColumn]; // Update foothold if (type == FootholdType.Double) { // Set foothold SetFoothold(_curRow, _curColumn, FootholdType.Normal); } else if (type != FootholdType.None) { // Set foothold SetFoothold(_curRow, _curColumn, FootholdType.None); } // Set next cell _curRow = nextRow; _curColumn = nextColumn; // Jump var jump = MoveAction.MoveTo(GetPosition(nextRow, nextColumn), jumpDuration * 0.5f); var delay = DelayAction.Create(jumpDuration * 0.5f); var callFunc = CallFuncAction.Create(JumpCallback); var action = SequenceAction.Create(jump, delay, callFunc); _frog.gameObject.Play(action); } }
public static string ToStringExt(this FootholdType type) { if (type == FootholdType.Normal) { return("O"); } if (type == FootholdType.Double) { return("2"); } if (type == FootholdType.Time) { return("T"); } if (type == FootholdType.RedirectLeft) { return("L"); } if (type == FootholdType.RedirectUp) { return("U"); } if (type == FootholdType.RedirectRight) { return("R"); } if (type == FootholdType.RedirectDown) { return("D"); } return("X"); }
public GameObject GetFootholdPrefab(FootholdType type) { if (type == FootholdType.Normal) { return(footholdPrefab); } if (type == FootholdType.Double) { return(doubleFootholdPrefab); } if (type == FootholdType.Time) { return(timeFootholdPrefab); } if (type == FootholdType.RedirectLeft) { return(redirectLeftFootholdPrefab); } if (type == FootholdType.RedirectUp) { return(redirectUpFootholdPrefab); } if (type == FootholdType.RedirectRight) { return(redirectRightFootholdPrefab); } if (type == FootholdType.RedirectDown) { return(redirectDownFootholdPrefab); } return(null); }
public static Direction GetDirection(this FootholdType type) { if (type == FootholdType.RedirectLeft) { return(Direction.Left); } if (type == FootholdType.RedirectUp) { return(Direction.Up); } if (type == FootholdType.RedirectRight) { return(Direction.Right); } if (type == FootholdType.RedirectDown) { return(Direction.Down); } return(Direction.None); }
void ResetMap() { if (_mapData == null) { return; } // Get footholds int[,] footholds = _mapData.footholds; // Reset footholds for (int i = 0; i < _row; i++) { int row = _row - 1 - i; for (int j = 0; j < _column; j++) { FootholdType type = footholds[i, j].ToFootholdType(); if (type != FootholdType.None) { SetFoothold(row, j, type); } } } // Reset start cell _curRow = _row - 1 - _mapData.startRow; _curColumn = _mapData.startColumn; // Reset direction _curDirection = _mapData.direction; // Update frog UpdateFrog(); }
void Try() { int nextRow = -1; int nextColumn = -1; for (int i = 0; i < 4; i++) { Direction dir = Directions[i]; if (!dir.IsOpposite(_curDirection)) { if (NextCell(dir, ref nextRow, ref nextColumn)) { int row = _curRow; int column = _curColumn; Direction direction = _curDirection; FootholdType type1 = _types[_curRow, _curColumn]; FootholdType type2 = _types[nextRow, nextColumn]; // Set direction _curDirection = dir; // Update current cell if (type1 == FootholdType.Double) { _types[_curRow, _curColumn] = FootholdType.Normal; // Increase counter _count++; } else if (type1 != FootholdType.None) { _types[_curRow, _curColumn] = FootholdType.None; // Increase counter _count++; } if (type2.IsRedirect()) { _curDirection = type2.GetDirection(); } // Set current cell to next one _curRow = nextRow; _curColumn = nextColumn; // Check if finished if (_count == _total - 1) { _counter++; } else { Try(); } // Restore cell _curRow = row; _curColumn = column; // Restore type if (type1 != FootholdType.None) { _types[row, column] = type1; // Decrease counter _count--; } // Restore direction _curDirection = direction; } } } }
void SetMapData(MapData mapData) { // Set map data _mapData = mapData; // Remove all children transform.DestroyImmediateChildren(); // Get footholds int[,] footholds = mapData.footholds; // Get number of rows _row = footholds.GetRow(); // Get number of columns _column = footholds.GetColumn(); // Create array of foothold types _types = new FootholdType[_row, _column]; // Create array of footholds _footholds = new SpriteRenderer[_row, _column]; // Reset total _total = 0; // Reset count _count = 0; // Set footholds for (int i = 0; i < _row; i++) { int row = _row - 1 - i; for (int j = 0; j < _column; j++) { FootholdType type = footholds[i, j].ToFootholdType(); // Set type _types[row, j] = type; if (type != FootholdType.None) { GameObject foothold = new GameObject("Foothold"); foothold.transform.position = GetPosition(row, j); foothold.transform.SetParent(transform); _footholds[row, j] = foothold.AddComponent <SpriteRenderer>(); // Update foothold UpdateFoothold(row, j); if (type == FootholdType.Double) { _total += 2; } else if (type != FootholdType.None) { _total++; } } } } // Set start cell _curRow = _row - 1 - mapData.startRow; _curColumn = mapData.startColumn; // Set start direction _curDirection = mapData.direction; // Frog GameObject frog = new GameObject("Frog"); frog.transform.SetParent(transform); _frog = frog.AddComponent <SpriteRenderer>(); _frog.sortingOrder = 1; // Update frog UpdateFrog(); }
void Try2() { int nextRow = -1; int nextColumn = -1; for (int i = 0; i < 4; i++) { Direction dir = Directions[i]; if (!dir.IsOpposite(_curDirection)) { if (NextCell(dir, ref nextRow, ref nextColumn)) { // Push _dirs.Push(dir); // Save cell int row = _curRow; int column = _curColumn; // Save direction Direction direction = _curDirection; FootholdType type1 = _types[_curRow, _curColumn]; FootholdType type2 = _types[nextRow, nextColumn]; // Set direction _curDirection = dir; // Update current cell if (type1 == FootholdType.Double) { // Set foothold _types[_curRow, _curColumn] = FootholdType.Normal; // Increase counter _count++; } else if (type1 != FootholdType.None) { // Set foothold _types[_curRow, _curColumn] = FootholdType.None; // Increase counter _count++; } if (type2.IsRedirect()) { _curDirection = type2.GetDirection(); } // Set current cell to next one _curRow = nextRow; _curColumn = nextColumn; // Check if finished if (_count == _total - 1) { Direction[] dirs = _dirs.ToArray(); int count = dirs.Length; Queue <Direction> solution = new Queue <Direction>(count); for (int idx = 0; idx < count; idx++) { solution.Enqueue(dirs[count - 1 - idx]); } // Add to solutions list _solutions.Enqueue(solution); } else { Try2(); } // Restore cell _curRow = row; _curColumn = column; // Restore type if (type1 != FootholdType.None) { // Set foothold _types[_curRow, _curColumn] = type1; // Decrease counter _count--; } // Restore direction _curDirection = direction; // Pop _dirs.Pop(); } } } }
public static bool IsNone(this FootholdType type) { return(type == FootholdType.None); }
// Load from file public bool Load(string fileName) { MapData mapData = null; Helper.Load <MapData>(fileName, ref mapData); if (mapData == null) { Debug.Log("Map not found!"); return(false); } // mapData.Log(); // Clear items Clear(); // Get footholds int[,] footholds = mapData.footholds; // Set rows rows = footholds.GetRow(); // Set columns columns = footholds.GetColumn(); // Create map items _items = new MapItem[rows, columns]; for (int i = 0; i < rows; i++) { int row = rows - 1 - i; for (int j = 0; j < columns; j++) { FootholdType footholdType = footholds[i, j].ToFootholdType(); if (footholdType != FootholdType.None) { ItemType itemType = footholdType.ToItemType(); AddItem(itemType, GetItemPrefab(itemType), row, j); } } } // Set start cell _startRow = rows - 1 - mapData.startRow; _startColumn = mapData.startColumn; // Set direction _direction = mapData.direction; // Set time foothold duration mapData.DeserializeTimeFootholds((row, column, duration) => { MapItem item = _items[rows - 1 - row, column]; if (item != null) { TimeScript time = item.item.GetComponent <TimeScript>(); if (time != null) { time.duration = duration; } else { //Log.Debug("TimeScript required!"); } } else { //Log.Debug("TimeFoothold is null!"); } }); // Get foothold GameObject foothold = _items[_startRow, _startColumn].item; if (foothold != null) { // Create frog GameObject frog = Instantiate(GetItemPrefab(ItemTypeHelper.GetFrog(_direction))) as GameObject; frog.name = "Frog"; frog.transform.SetParent(foothold.transform); frog.transform.localPosition = Vector3.zero; } return(true); }
IEnumerator Try() { int nextRow = -1; int nextColumn = -1; for (int i = 0; i < 4; i++) { Direction dir = Directions[i]; if (!dir.IsOpposite(_curDirection)) { if (NextCell(dir, ref nextRow, ref nextColumn)) { // Push _cells.Push(new Cell(_curRow, _curColumn)); // Save cell int row = _curRow; int column = _curColumn; // Save direction Direction direction = _curDirection; FootholdType type1 = _types[_curRow, _curColumn]; FootholdType type2 = _types[nextRow, nextColumn]; // Set direction SetDirection(dir); // Update current cell if (type1 == FootholdType.Double) { // Set foothold SetFoothold(_curRow, _curColumn, FootholdType.Normal); // Increase counter _count++; } else if (type1 != FootholdType.None) { // Set foothold SetFoothold(_curRow, _curColumn, FootholdType.None); // Increase counter _count++; } // Jump var jump = MoveAction.MoveTo(GetPosition(nextRow, nextColumn), jumpDuration * 0.5f); if (type2.IsRedirect()) { Direction newDirection = type2.GetDirection(); _frog.gameObject.Play(SequenceAction.Create(jump, CallFuncAction.Create(() => { SetDirection(newDirection); }))); } else { _frog.gameObject.Play(jump); } // Set current cell to next one _curRow = nextRow; _curColumn = nextColumn; yield return(new WaitForSeconds(jumpDuration)); // Check if finished if (_count == _total - 1) { Show(); } else { yield return(StartCoroutine(Try())); } // Restore cell _curRow = row; _curColumn = column; // Restore position _frog.transform.position = GetPosition(_curRow, _curColumn); // Restore type if (type1 != FootholdType.None) { // Set foothold SetFoothold(_curRow, _curColumn, type1); // Decrease counter _count--; } // Restore direction SetDirection(direction); // Pop _cells.Pop(); yield return(new WaitForSeconds(unjumpDuration)); } } } }
public static bool IsNormal(this FootholdType type) { return(type == FootholdType.Normal); }
public static ItemType ToItemType(this FootholdType type) { return((ItemType)type); }
public static bool IsDouble(this FootholdType type) { return(type == FootholdType.Double); }
public static int ToInt(this FootholdType type) { return((int)type); }
public static bool IsTime(this FootholdType type) { return(type == FootholdType.Time); }