Skip to content

Instantly share code, notes, and snippets.

@docky
Created April 25, 2012 14:42
Show Gist options
  • Save docky/2490250 to your computer and use it in GitHub Desktop.
Save docky/2490250 to your computer and use it in GitHub Desktop.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Grid2D : MonoBehaviour
{
// prefab tiles
public GameObject[] prefabs;
// current game state
public GameState gameState;
// singleton
public static Grid2D instance;
public enum TileType
{
Empty = 0, // Drop
Start = 1, // Normal tile (player start)
Normal = 2, // Normal tile
Ice = 3, // Icy
Crumble = 4, // Tile disappears after hop
CrumbleIce = 5, // Tile disappears after hop
Block = 10, // Hard tile that stops player
}
public Vector3 offset;
[System.Serializable]
public class Tile
{
public int x = -1;
public int y = -1;
public TileType type;
public Tile(int newX, int newY)
{
x = newX;
y = newY;
}
public Tile (TileType newType)
{
type = newType;
}
// convert tile to different tiletype
void HopOnTile()
{
switch (type)
{
case TileType.Normal:
type = TileType.Ice;
break;
case TileType.Crumble:
type = TileType.Empty;
break;
case TileType.Start:
type = TileType.Ice;
break;
default:
break;
}
}
}
/* Game State stores all the information about the map.
* This will be used to help solve the map, and also allow
* player to rewind the game.
*/
public class GameState
{
public int hops = 0;
public int playerPositionX = -1;
public int playerPositionY = -1;
public Tile[,] tileGrid;
public Vector3 offset;
public GameState(int width, int height)
{
tileGrid = new Tile[width, height];
for (int y = 0; y < tileGrid.GetUpperBound(1); y++)
for (int x = 0; x < tileGrid.GetUpperBound(0); x++)
{
tileGrid[x,y] = new Grid2D.Tile(x,y);
tileGrid[x,y].type = (TileType) Random.Range(0, 6);
tileGrid[x,y].type = ( Random.Range(0, 3) == 0 ? TileType.Empty : tileGrid[x,y].type );
}
}
public GameState(int[,] newMap )
{
//GameState( newMap.GetUpperBound(0), newMap.GetUpperBound(1) );
int width = newMap.GetUpperBound(1), height = newMap.GetUpperBound(0);
tileGrid = new Tile[width, height];
Debug.Log("grid size = " + tileGrid.GetUpperBound(0) + " x " + tileGrid.GetUpperBound(1) );
for (int y = 0; y < tileGrid.GetUpperBound(1) +1; y++)
for (int x = 0; x < tileGrid.GetUpperBound(0) +1; x++)
{
tileGrid[x,y] = new Grid2D.Tile( (TileType) newMap[y,x] );
}
}
public Tile CheckTile( int newX, int newY)
{
// if checking beyond the map, return an empty tile
if (newX >= tileGrid.GetUpperBound(0)
|| newX < 0
|| newY >= tileGrid.GetUpperBound(1)
|| newY < 0)
return new Tile (TileType.Empty);
return tileGrid[newX, newY];
}
// lists all non-Empty tiles
public Tile[] AllTiles()
{
return AllTiles(TileType.Empty, false);
}
// check TileType specifically
public Tile[] AllTiles( TileType setType, bool active )
{
List <Tile> allTiles = new List<Tile>();
for (int y = 0; y < tileGrid.GetUpperBound(1); y++)
for (int x = 0; x < tileGrid.GetUpperBound(0); x++)
{
// if tile matches the condition, add to List
// (alternate for active = false)
if (active == (tileGrid[x,y].type == setType) )
allTiles.Add( tileGrid[x,y] );
}
return allTiles.ToArray();
}
}
void Awake()
{
instance = this;
// gameState = new GameState(10,8);
int[,] mapLayout = new int[,]
{
{2, 2, 2, 2, 2, 2, 2, 2, 4, 4},
{0, 0, 2, 2, 2, 2, 2, 4, 3, 4},
{0, 0, 2, 3, 4, 3, 4, 2, 3, 1},
{2, 2, 2, 0, 5, 0, 5, 2, 3, 2},
{3, 4, 2, 5, 5, 5, 5, 5, 5, 1},
{4, 3, 2, 2, 2, 2, 2, 2, 3, 2},
{4, 4, 4, 4, 2, 4, 2, 2, 3, 4}
};
gameState = new GameState(mapLayout);
}
void Start()
{
BuildTiles();
Tile[] allTiles = gameState.AllTiles(TileType.Normal, true);
Debug.Log("alltiles len " + allTiles.Length) ;
int randomTile = Random.Range(0, allTiles.Length);
Debug.Log("all tiles = " + randomTile);
Tile pickedTile = allTiles[randomTile];
Hopski.instance.ResetPosition(pickedTile.x, pickedTile.y, offset );
}
void BuildTiles()
{
Tile[,] grid = gameState.tileGrid;
// destroy current children
Transform[] children = GetComponentsInChildren <Transform> ();
foreach (Transform item in children)
{
if (item.gameObject != gameObject)
Destroy(item.gameObject);
}
Debug.Log("grid size = " + grid.GetUpperBound(0) + " x " + grid.GetUpperBound(1) );
offset = -0.5f * new Vector3(grid.GetUpperBound(0) - 1, 0, -(grid.GetUpperBound(1) + 1));
//offset = Vector3.zero;
for (int y = 0; y < grid.GetUpperBound(1); y++)
{
for (int x = 0; x < grid.GetUpperBound(0); x++)
{
if (grid[x,y].type != TileType.Empty)
{
int tileID = (int) grid[x,y].type;
// Debug.Log(" " + x + "," + y + " : " + grid[x,y].type.ToString() + "(" + tileID + ")" );
GameObject newTile = (GameObject) Instantiate( prefabs[tileID] );
newTile.transform.parent = transform;
newTile.transform.localPosition = new Vector3(x, 0, -y) + offset;
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment