private void InstantiateLevelGrid(LevelComponent[,] levelGrid, Vector2 levelHead, WorldChunkID chunkID) { GameObject chunk = new GameObject(); chunk.name = "LevelChunk" + chunkID; for (int i = 0; i < levelGrid.GetLength(0); i++) { for (int j = 0; j < levelGrid.GetLength(1); j++) { if (levelGrid[i, j] != null) { Transform atom = null; if (levelGrid[i, j].Atom != LevelAtom.None) { atom = Instantiate(GetTransform(levelGrid[i, j].Atom)); atom.position += new Vector3((i + levelHead.x - 1) * 1.5f, j + levelHead.y, -1); atom.parent = chunk.transform; } if (levelGrid[i, j].Coin) { if (atom == null) { Debug.LogWarning("Attempted to add coin to null level atom"); } else { if (levelGrid[i, j].Atom != LevelAtom.Hook) { Debug.LogWarning("Attempted to add a coin to a non-hook. Coins only go on hooks!"); } else { Transform t = Instantiate(coinPrefab); t.GetComponent <PickupBehaviour>().SetHook(atom); float coinOffsetY = -0.4f; Vector2 wobbleOffset = new Vector2(0.1f, 0.1f); t.position += new Vector3((i + levelHead.x - 1) * 1.5f + wobbleOffset.x, j + levelHead.y + coinOffsetY + wobbleOffset.y, -2); t.parent = atom; } } } } } } LevelAtom[] highAtoms = new LevelAtom[2]; highAtoms[0] = LevelAtom.Ground; highAtoms[1] = LevelAtom.Waterfall; GameObject bg = AddBackgroundGround(GetHighestArray(levelGrid, highAtoms)); bg.transform.parent = chunk.transform; }
public static bool HasAtom(int x, LevelAtom atom, LevelComponent[,] grid) { for (int j = 0; j < grid.GetLength(1); j++) { if (grid[x, j] != null) { if (grid[x, j].Atom == atom) { return(true); } } } return(false); }
public static int GetHighestAtom(int x, LevelAtom atom, LevelComponent[,] grid) { int highest = -1; for (int j = 0; j < grid.GetLength(1); j++) { if (grid[x, j] != null) { if (grid[x, j].Atom == atom) { highest = j; } } } return(highest); }
private static void AddGround(int chunkID) { for (int i = 0; i < levelGrid.GetLength(0); i++) { LevelAtom[] excluded = new LevelAtom[1]; excluded[0] = LevelAtom.Floor; int highest = GetHighestAtomExcluding(i, excluded, levelGrid); bool ground = HasAtom(i, LevelAtom.Ground, levelGrid); if ((!ground) && (highest > -1) && (highest + 2 < height)) { if ((Random.value > 0.25f) || (chunkID == 0)) { levelGrid[i, highest + 2] = new LevelComponent(LevelAtom.Ground, Direction.Down, false); } else { levelGrid[i, highest + 2] = new LevelComponent(LevelAtom.Waterfall, Direction.Down, false); } } } }
public Transform GetTransform(LevelAtom a) { switch (a) { case LevelAtom.Hook: return(hookPrefab); case LevelAtom.Wall: return(wallPrefab); case LevelAtom.Floor: return(floorPrefab); case LevelAtom.Blade: return(bladePrefab); case LevelAtom.Ground: return(groundPrefab); case LevelAtom.Waterfall: return(waterfallPrefab); } return(null); }
private static bool Step() { LevelAtom oldAtom = levelGrid[headX, headY].Atom; Direction oldDirection = levelGrid[headX, headY].Direction; Move(); LevelAtom atom = RandomLevelAtom(); Direction direction = new Direction(); if (oldDirection == Direction.DownRight) { atom = LevelAtom.Floor; direction = Direction.UpRight; } else { if (headX < 2) { direction = Direction.Right; } else if (headY < 2) { direction = Direction.Up; } else if (headY > height - 3) { direction = Direction.Down; } else if (headX == width) { return(false); } else { direction = RandomDirection(); } if ((oldDirection == Direction.Up) && (direction == Direction.Down)) { direction = Direction.Right; } else if ((oldDirection == Direction.Down) && (direction == Direction.Up)) { direction = Direction.Right; } else if ((oldDirection == Direction.Up) && (direction == Direction.DownRight)) { direction = Direction.Right; } } if (atom == LevelAtom.Ground) { if (HasAtom(headX, LevelAtom.Ground, levelGrid)) { atom = LevelAtom.Hook; } } bool coin = false; if (Random.value > 0.9) { coin = true; } if ((headX < width) && (headY < height) && (headX >= 0) && (headY >= 0)) { levelGrid[headX, headY] = new LevelComponent(atom, direction, coin); } else { return(false); } return(true); }
public LevelComponent(LevelAtom atom, Direction dir, bool coin) { this.atom = atom; this.dir = dir; this.coin = coin; }