void GenerateMapVisual() { Transform MapObject = new GameObject("MapObj").transform; for (int x = 0; x < mapSizeX; x++) { for (int y = 0; y < mapSizeY; y++) { if (map[x, y] == -1) { TrapTile tt = trapTiles[savedMap[x, y]]; GameObject go = (GameObject)Instantiate(tt.hoverVisual, new Vector3(x, 0, y), Quaternion.identity); go.transform.SetParent(MapObject); } else if (map[x, y] == -2) { TrapTile tt = trapTiles[savedMap[x, y]]; GameObject go = (GameObject)Instantiate(tt.noHover, new Vector3(x, 0, y), Quaternion.identity); go.transform.SetParent(MapObject); } else { TrapTile tt = trapTiles[savedMap[x, y]]; GameObject go = (GameObject)Instantiate(tt.tileVisualPrefab, new Vector3(x, 0, y), Quaternion.identity); go.transform.SetParent(MapObject); } } } }
private void GenerateGridFromCSV(string filepath, string filename) { Tile[] obstacleTiles = { new Tile() { tile = (GameObject)Instantiate(Resources.Load("tileset-481")), type = "ObstacleTile" }, new Tile() { tile = (GameObject)Instantiate(Resources.Load("tileset-484")), type = "ObstacleTile" }, new Tile() { tile = (GameObject)Instantiate(Resources.Load("tileset-487")), type = "ObstacleTile" }, new Tile() { tile = (GameObject)Instantiate(Resources.Load("tileset-489")), type = "ObstacleTile" }, new Tile() { tile = (GameObject)Instantiate(Resources.Load("tileset-554")), type = "ObstacleTile" } }; Tile[] normalTiles = { new Tile() { tile = (GameObject)Instantiate(Resources.Load("tileset-105")), type = "NormalTile" }, // uncracked new Tile() { tile = (GameObject)Instantiate(Resources.Load("tileset-81")), type = "NormalTile" }, new Tile() { tile = (GameObject)Instantiate(Resources.Load("tileset-82")), type = "NormalTile" }, new Tile() { tile = (GameObject)Instantiate(Resources.Load("tileset-83")), type = "NormalTile" }, new Tile() { tile = (GameObject)Instantiate(Resources.Load("tileset-103")), type = "NormalTile" }, new Tile() { tile = (GameObject)Instantiate(Resources.Load("tileset-104")), type = "NormalTile" }, new Tile() { tile = (GameObject)Instantiate(Resources.Load("tileset-292")), type = "NormalTile" }, new Tile() { tile = (GameObject)Instantiate(Resources.Load("tileset-293")), type = "NormalTile" }, new Tile() { tile = (GameObject)Instantiate(Resources.Load("tileset-294")), type = "NormalTile" } }; Tile start = new Tile() { tile = (GameObject)Instantiate(Resources.Load("StartTile")), type = "StartTile" }; Tile end = new Tile() { tile = (GameObject)Instantiate(Resources.Load("EndTile")), type = "EndTile" }; Tile trap = new Tile() { tile = (GameObject)Instantiate(Resources.Load("SpikeTile")), type = "TrapTile" }; Tile ice = new Tile() { tile = (GameObject)Instantiate(Resources.Load("IceTile")), type = "IceTile" }; string[,] GridCSV = CsvUtil.readData(filepath, filename); rows = GridCSV.GetLength(0); cols = GridCSV.GetLength(1); float centerOffsetX = -cols * tileSize / 2; // center float centerOffsetY = rows * tileSize / 2; for (int i = 0; i < GridCSV.GetLength(0); i++) { List <Tile> tileRow = new List <Tile>(); for (int j = 0; j < GridCSV.GetLength(1); j++) { GameObject tile; if (GridCSV[i, j] == "S") { tile = (GameObject)Instantiate(start.tile, transform); StartTile startTile = new StartTile() { tile = tile, type = "StartTile" }; tileRow.Add(startTile); } else if (GridCSV[i, j] == "E") { tile = (GameObject)Instantiate(end.tile, transform); EndTile endTile = new EndTile() { tile = tile, type = "EndTile" }; tileRow.Add(endTile); } else if (GridCSV[i, j] == "W") //W = wallobstacle { int randomChoice = (int)Random.Range(0, obstacleTiles.Length - 0.001f); tile = (GameObject)Instantiate(obstacleTiles[randomChoice].tile, transform); ObstacleTile obstacleTile = new ObstacleTile() { tile = tile, type = "ObstacleTile" }; tileRow.Add(obstacleTile); } else if (GridCSV[i, j] == "T") { tile = (GameObject)Instantiate(trap.tile, transform); TrapTile trapTile = new TrapTile() { tile = tile, type = "TrapTile" }; tileRow.Add(trapTile); } else if (GridCSV[i, j] == "I") { tile = (GameObject)Instantiate(ice.tile, transform); IceTile iceTile = new IceTile() { tile = tile, type = "IceTile" }; tileRow.Add(iceTile); } else { int randomChoice = (int)Random.Range(0, normalTiles.Length - 0.001f); tile = (GameObject)Instantiate(normalTiles[randomChoice].tile, transform); NormalTile normalTile = new NormalTile() { tile = tile, type = "NormalTile" }; tileRow.Add(normalTile); } float posX = j * tileSize + centerOffsetX; float posY = i * -tileSize + centerOffsetY; if (GridCSV[i, j] == "S") { StartPos = new Vector2(posX, posY); } tile.transform.position = new Vector2(posX, posY); } Tiles.Add(tileRow); } foreach (Tile obstacleTile in obstacleTiles) { Destroy(obstacleTile.tile); } foreach (Tile normalTile in normalTiles) { Destroy(normalTile.tile); } Destroy(start.tile); Destroy(end.tile); Destroy(trap.tile); Destroy(ice.tile); //bool x = await Task.FromResult(false); }
// Update is called once per frame void Update() { movement.x = swipeBehavior.movement.x + Input.GetAxis("Horizontal"); movement.y = swipeBehavior.movement.y + Input.GetAxis("Vertical"); // store variables used for movement float tileSize = grid.GetTileSize(); float bottomLimit = -1 * grid.GetRows() * tileSize / 2; float topLimit = grid.GetRows() * tileSize / 2; float leftLimit = -1 * grid.GetCols() * tileSize / 2; float rightLimit = grid.GetCols() * tileSize / 2; PlayerAnimationUpdate(); if (!PlayerOnNextPosition()) { return; } // if on a coin if (/*GridManager*/ grid.GetConsumable(rb.position.x, rb.position.y).type == "CoinTile") { if (playerStats.Consumables.ContainsKey("Coin")) { playerStats.Consumables["Coin"]++; } else { playerStats.Consumables["Coin"] = 1; } print("Coins: " + playerStats.Consumables["Coin"]); Destroy(grid.GetConsumable(rb.position.x, rb.position.y).consumable); grid.GetConsumable(rb.position.x, rb.position.y).type = "null"; return; } // if on a stepAdder if (grid.GetConsumable(nextPosition.x, nextPosition.y).type == "StepAdderTile") { stepConstraint += 5; print("constraint: " + stepConstraint); Destroy(grid.GetConsumable(nextPosition.x, nextPosition.y).consumable); grid.GetConsumable(nextPosition.x, nextPosition.y).type = "null"; return; } // if player on ice if (/*GridManager*/ grid.GetTile(nextPosition.x, nextPosition.y).type == "IceTile") { if (CurrDir == "down") { // if in bound if (nextPosition.y - tileSize > bottomLimit) { Tile nextTile = grid.GetTile(nextPosition.x, nextPosition.y - tileSize); if (nextTile.IsPassable()) { nextPosition.y -= tileSize; return; } } } else if (CurrDir == "up") { // if in bound if (nextPosition.y + tileSize <= topLimit) { Tile nextTile = grid.GetTile(nextPosition.x, nextPosition.y + tileSize); if (nextTile.IsPassable()) { nextPosition.y += tileSize; return; } } } else if (CurrDir == "left") { // if in bound if (nextPosition.x - tileSize >= leftLimit) { Tile nextTile = grid.GetTile(nextPosition.x - tileSize, nextPosition.y); if (nextTile.IsPassable()) { nextPosition.x -= tileSize; return; } } } else { // if in bound if (nextPosition.x + tileSize < rightLimit) { Tile nextTile = grid.GetTile(nextPosition.x + tileSize, nextPosition.y); if (nextTile.IsPassable()) { nextPosition.x += tileSize; return; } } } } // if player on trap, compare if player moved since last frame if (grid.GetTile(nextPosition.x, nextPosition.y).type == "TrapTile") { if (nextPosition.x != currFrameX || nextPosition.y != currFrameY) { TrapTile currTrapTile = (TrapTile)(grid.GetTile(nextPosition.x, nextPosition.y)); if (currTrapTile.IsVulnerable) { IsGameOver = true; } else { ((TrapTile)(grid.GetTile(nextPosition.x, nextPosition.y))).IsVulnerable = true; } } } //if the player reach the step limit if (stepConstraint - counter <= 0) { print(" is game over, stepConstraint: " + stepConstraint + ", counter: " + counter); IsGameOver = true; } // check whether the player reaches the end if (grid.GetTile(nextPosition.x, nextPosition.y).type == "EndTile") { IsWin = true; } if (IsWin) { // if(dialogBoxFlag && dialogBoxCounter > 30) // { // dialogBoxFlag = false; // dialogBoxCounter = 0; // if(EditorUtility.DisplayDialog("You win","Choose your action", "Back to level selection", "Ok" )) // { // print("Pressed back to level selection."); // SceneManager.LoadScene("LevelScene",LoadSceneMode.Additive); // // SceneManager.SetActiveScene(SceneManager.GetSceneByName("LevelScene")); // } // else // { // print("Pressed OK."); // } // } if (!winFlag) { star = 1; if (playerStats.Consumables["Coin"] > 2) { print("coin if"); star++; } if (stepConstraint - counter > 3) { print("step if"); star++; } gameMessage = "You Win!!! Rating: You Got " + star + "/3 Stars!!"; CoinManager.numOfCoins += playerStats.Consumables["Coin"]; SendWinAnalytics(); winFlag = true; dialogBoxFlag = true; dialogBoxCounter++; } // keep counting frames to wait for enough time to show the dialog box dialogBoxCounter++; return; } if (IsGameOver) { gameMessage = "You Lose!!! Need More Steps? Visit the Shop!"; // if(dialogBoxFlag && dialogBoxCounter > 30) // { // dialogBoxFlag = false; // dialogBoxCounter = 0; // if(EditorUtility.DisplayDialog("You lose","Choose your action", "Back to level selection", "Ok" )) // { // print("Pressed back to level selection."); // SceneManager.LoadScene("LevelScene",LoadSceneMode.Additive); // // SceneManager.SetActiveScene(SceneManager.GetSceneByName("LevelScene")); // } // else // { // print("Pressed OK."); // } // } if (!sentLoseAnalytics) { TrialNum.numOfTrial++; SendLoseAnalytics(); // CoinManager.numOfCoins -= 5; if (CoinManager.numOfCoins < 0) { CoinManager.numOfCoins = 0; } sentLoseAnalytics = true; dialogBoxFlag = true; dialogBoxCounter++; } // keep counting frames to wait for enough time to show the dialog box dialogBoxCounter++; return; } currFrameX = nextPosition.x; currFrameY = nextPosition.y; if ((Math.Abs(movement.x) > epsilon || Math.Abs(movement.y) > epsilon) && PlayerOnNextPosition()) { if (Math.Abs(movement.x) < Math.Abs(movement.y)) { movement.x = 0; if (movement.y < 0) { CurrDir = "down"; // check bound if (nextPosition.y - tileSize > bottomLimit) { if (grid.GetTile(nextPosition.x, nextPosition.y - tileSize).IsPassable()) { // move down print("next tile: " + grid.GetTile(nextPosition.x, nextPosition.y - tileSize).type); nextPosition.y -= tileSize; IncrementCounter(); } if (grid.GetConsumable(rb.position.x, rb.position.y).type == "SpikeTile") { Destroy(grid.GetConsumable(rb.position.x, rb.position.y).consumable); grid.GetConsumable(rb.position.x, rb.position.y).type = "null"; } } } else { // move up CurrDir = "up"; // check bound if (nextPosition.y + tileSize <= topLimit) { if (grid.GetTile(nextPosition.x, nextPosition.y + tileSize).IsPassable()) { print("next tile: " + grid.GetTile(nextPosition.x, nextPosition.y + tileSize).type); nextPosition.y += tileSize; IncrementCounter(); } if (grid.GetConsumable(rb.position.x, rb.position.y).type == "SpikeTile") { Destroy(grid.GetConsumable(rb.position.x, rb.position.y).consumable); grid.GetConsumable(rb.position.x, rb.position.y).type = "null"; } } } } else { movement.y = 0; if (movement.x < 0) { CurrDir = "left"; // move left if (nextPosition.x - tileSize >= leftLimit) { if (grid.GetTile(nextPosition.x - tileSize, nextPosition.y).IsPassable()) { print("next tile: " + grid.GetTile(nextPosition.x - tileSize, nextPosition.y).type); nextPosition.x -= tileSize; IncrementCounter(); } if (grid.GetConsumable(rb.position.x, rb.position.y).type == "SpikeTile") { Destroy(grid.GetConsumable(rb.position.x, rb.position.y).consumable); grid.GetConsumable(rb.position.x, rb.position.y).type = "null"; } } } else { // move right CurrDir = "right"; if (nextPosition.x + tileSize < rightLimit) { if (grid.GetTile(nextPosition.x + tileSize, nextPosition.y).IsPassable()) { print("next tile: " + grid.GetTile(nextPosition.x + tileSize, nextPosition.y).type); nextPosition.x += tileSize; IncrementCounter(); } if (grid.GetConsumable(rb.position.x, rb.position.y).type == "SpikeTile") { Destroy(grid.GetConsumable(rb.position.x, rb.position.y).consumable); grid.GetConsumable(rb.position.x, rb.position.y).type = "null"; } } } } print("next pos: " + nextPosition.x + ", " + nextPosition.y); } }