public void Animate(GridPosition_script position, GridPosition_script destination) { if (this._animator != null) { // DOWN if (position.Row < destination.Row) { this._animator.SetInteger("Horizontal", 0); this._animator.SetInteger("Vertical", 1); // UP } else if (position.Row > destination.Row) { this._animator.SetInteger("Horizontal", 0); this._animator.SetInteger("Vertical", -1); // RIGHT } else if (position.Column < destination.Column) { this._animator.SetInteger("Horizontal", 1); this._animator.SetInteger("Vertical", 0); // LEFT } else if (position.Column > destination.Column) { this._animator.SetInteger("Horizontal", -1); this._animator.SetInteger("Vertical", 0); } } }
private void LoadTiles() { Tile_script lastTile; Vector2 lastTilePosition; int[][] mapData = this.LoadFromFile("Levels/level_001_resource"); Vector3 offset = Camera.main.ScreenToWorldPoint(new Vector3(0, Screen.height, 0)); this._mapSize = new GridPosition_script(mapData.Length, mapData[0].Length); for (int row = 0; row < this._mapSize.Row; row++) { for (int column = 0; column < this._mapSize.Column; column++) { this.PlaceTile(mapData[row][column], row, column, offset); } } lastTile = this.Tiles[new GridPosition_script((this._mapSize.Row - 1), (this._mapSize.Column - 1))]; lastTilePosition = lastTile.transform.position; this._cameraObject.SetLimit(new Vector3(lastTilePosition.x + this.TileSize.x, lastTilePosition.y - this.TileSize.y, 0)); this.SpawnPortals(); }
private void Release() { this._health = 100; this.IsActive = false; this.Position = TileMap_script.Instance.PortalStartSpawnPos; Game_script.Instance.ObjectPool.Release(this.gameObject); Game_script.Instance.RemoveMonster(this); }
public void Initialize(GridPosition_script position, Vector3 worldPosition, Transform parent, int type) { this.IsAvailable = (type == 0); this.IsWalkable = (type > 0); this.Position = position; this.transform.position = worldPosition; this.transform.SetParent(parent); TileMap_script.Instance.Tiles.Add(new GridPosition_script(this.Position.Row, this.Position.Column), this); }
private void SetPath(Stack <Node_script> path) { if (path != null) { this._path = path; this.Animate(this.Position, this._path.Peek().Position); this.Position = this._path.Peek().Position; this._destination = this._path.Pop().WorldPosition; } }
private void Move() { if (this.IsActive) { this.transform.position = Vector3.MoveTowards(this.transform.position, this._destination, (this._speed * Time.deltaTime)); if ((this.transform.position == this._destination) && (this._path != null) && (this._path.Count > 0)) { this.Animate(this.Position, this._path.Peek().Position); this.Position = this._path.Peek().Position; this._destination = this._path.Pop().WorldPosition; } } }
private static bool IsMoveAllowed(Node_script currentNode, Node_script neighbor) { GridPosition_script direction = (neighbor.Position - currentNode.Position); GridPosition_script first = new GridPosition_script(currentNode.Position.Row, (currentNode.Position.Column + direction.Column)); GridPosition_script second = new GridPosition_script((currentNode.Position.Row + direction.Row), currentNode.Position.Column); if (TileMap_script.Instance.IsWithinBounds(first) && !TileMap_script.Instance.Tiles[first].IsWalkable) { return(false); } if (TileMap_script.Instance.IsWithinBounds(second) && !TileMap_script.Instance.Tiles[second].IsWalkable) { return(false); } return(true); }
private void SpawnPortals() { this._portalStartSpawnPos = new GridPosition_script(0, 0); this._portalEndSpawnPos = new GridPosition_script(6, 16); GameObject portalStart = Instantiate( this._portalStartPrefab, this.Tiles[this._portalStartSpawnPos].GetComponent <Tile_script>().WorldPosition, Quaternion.identity ); this.PortalStart = portalStart.GetComponent <Portal_script>(); this.PortalStart.name = "portal_002_prefab"; GameObject portalEnd = Instantiate( this._portalEndPrefab, this.Tiles[this._portalEndSpawnPos].GetComponent <Tile_script>().WorldPosition, Quaternion.identity ); portalStart.transform.SetParent(GameObject.Find("Portals_object").transform); portalEnd.transform.SetParent(GameObject.Find("Portals_object").transform); }
public static Stack <Node_script> GetPath(GridPosition_script startPosition, GridPosition_script goalPosition) { if (_nodes == null) { CreateNodes(); } int gCost; Node_script neighbor; GridPosition_script neighborPosition; Node_script currentNode = _nodes[startPosition]; Node_script goalNode = _nodes[goalPosition]; HashSet <Node_script> openList = new HashSet <Node_script>(); HashSet <Node_script> closedList = new HashSet <Node_script>(); Stack <Node_script> path = new Stack <Node_script>(); openList.Add(currentNode); while (openList.Count > 0) { // GET THE NODE WITH THE LOWEST F COST currentNode = openList.OrderBy(n => n.F).First(); // GOAL REACHED if (currentNode == goalNode) { // RECONSTRUCT PATH BY BACKTRACING PARENTS path.Push(currentNode); while (currentNode.Parent != null) { currentNode = currentNode.Parent; path.Push(currentNode); } break; } // COSE CURRENT openList.Remove(currentNode); closedList.Add(currentNode); // OPEN NEIGHBORS for (int row = -1; row <= 1; row++) { for (int column = -1; column <= 1; column++) { neighborPosition = new GridPosition_script(currentNode.Position.Row - row, currentNode.Position.Column - column); // SKIP OUT-OF-BOUNDS, UNWALKABLES AND CURRENT NODE if (TileMap_script.Instance.IsWithinBounds(neighborPosition) && (TileMap_script.Instance.Tiles[neighborPosition].IsWalkable) && (neighborPosition != currentNode.Position)) { neighbor = _nodes[neighborPosition]; // SKIP CLOSED NODE if (closedList.Contains(neighbor)) { continue; } // HORIZONTAL/VERTICAL if (Mathf.Abs(column - row) == 1) { gCost = 10; // DIAGONAL } else { if (!IsMoveAllowed(currentNode, neighbor)) { continue; } gCost = 14; } // NEW/UNVISITED if (!openList.Contains(neighbor)) { openList.Add(neighbor); // SKIP IF NOT BETTER } else if ((currentNode.G + gCost) >= neighbor.G) { continue; } // UPDATE COST neighbor.CalculateCost(currentNode, goalNode, gCost); } } } } _nodes.Clear(); _nodes = null; //// VIZUALIZE OPEN LIST //GameObject.Find("AstarTest_object").GetComponent<AstarTest_script>().Visualize(openList, Color.yellow); //GameObject.Find("AstarTest_object").GetComponent<AstarTest_script>().Visualize(closedList, Color.cyan); //GameObject.Find("AstarTest_object").GetComponent<AstarTest_script>().Visualize(path, Color.blue); return(path); }
public bool IsWithinBounds(GridPosition_script position) { return((position.Column >= 0) && (position.Row >= 0) && (position.Column < this._mapSize.Column) && (position.Row < this._mapSize.Row)); }