void FixedUpdate() { // Spawn a new tile if (distanceLatestTileTravelled >= tileLength) { distanceLatestTileTravelled = 0; SpawnTile(0); } else { distanceLatestTileTravelled += Time.deltaTime * Block_Speed; } // Update current tiles for (int i = tileList.Count - 1; i >= 0; i--) { // Remove thiles that reached maximum distance if (!tileList[i].MoveTile()) { MovingTile tile = tileList[i]; tileList.Remove(tileList[i]); Destroy(tile.gameObject); } } }
void PatrolPointLogic() { Vector3 pos = GetMousePosInGrid(); movingTile.transform.position = pos; if (Input.GetKey(KeyCode.A)) { if (table.Contains(pos)) { return; } if (!currentTile.GetComponent <MovingTile>().IsLegalPatrolPoint(pos)) { return; } currentTile.GetComponent <MovingTile>().patrolPoints.Add(pos); MovingTile patrolLoc = Instantiate(movingTile, pos, Quaternion.identity) as MovingTile; patrolLoc.HalfAlpha(); table.Add(pos, patrolLoc.gameObject); } if (Input.GetKey(KeyCode.D)) { if (table.Contains(pos)) { GameObject patrolPoint = table[pos] as GameObject; //do not delete start tile if (patrolPoint.name.Contains("MovingTile")) { Destroy(patrolPoint); table.Remove(pos); currentTile.GetComponent <MovingTile>().patrolPoints.Remove(pos); } } } }
private void getNextTile() { int color = serieCode[index]; int corridor = serieCode[index + 1]; int foot = serieCode[index + 2]; tile = gameObject.AddComponent <MovingTile>(); tile.Init(color, corridor, foot); //this does not retrieve the good tile //tile.Init(1, 0,0); // this works index += +3; }
private void SpawnTile(float offset) { GameObject go = Instantiate(Tile_Prefab) as GameObject; MovingTile movingTilee = go.transform.GetComponent <MovingTile>(); movingTilee.setParameters(Block_Speed, Tile_Max_Distance, movingDirection); go.transform.SetParent(transform); go.transform.localPosition = movingDirection * offset; movingTilee.setDistanceTravelled(offset); go.transform.localScale = tileScale; tileList.Add(movingTilee); }
void PlaceTile() { Vector3 pos = GetMousePosInGrid(); if (table.Contains(pos)) { return; } currentTile = guiController.GetCurrentObject(); currentTile = Instantiate(currentTile, pos, Quaternion.identity) as GameObject; currentTile.transform.parent = mapParent; table.Add(currentTile.transform.position, currentTile); if (currentTile.name.Contains("MovingTile")) { movingTile = currentTile.GetComponent <MovingTile>(); movingTile = Instantiate(movingTile) as MovingTile; buildMode = buildMode_t.PATROL_POINTS; } }
public void ImportMap(string path) { foreach (Transform child in mapParent) { table.Remove(child.position); child.gameObject.active = false; Destroy(child.gameObject); } mapParent.DetachChildren(); table.Clear(); bool result = MapIO.ImportMap(path, mapParent, propParent); //if map was successfuly imported, do any post-initialization here if (result == true) { foreach (Transform child in mapParent) { if (child.name.Contains("Tile")) { table.Add(child.position, child.gameObject); } if (child.name.Contains("MovingTile")) { MovingTile importedMovingTile = child.GetComponent <MovingTile>(); foreach (Vector3 patrolPoint in importedMovingTile.patrolPoints) { if (!table.Contains(patrolPoint)) { MovingTile patrolLoc = Instantiate(importedMovingTile, patrolPoint, Quaternion.identity) as MovingTile; patrolLoc.HalfAlpha(); table.Add(patrolPoint, patrolLoc.gameObject); } } } //If this is a wind hazard if (child.name.Contains("Wind")) { Vector3 posInMap = child.position; posInMap.y = 0.0f; Tile windTile = (table[posInMap] as GameObject).GetComponent <Tile>(); if (windTile != null) { windTile.PlaceObject(child.gameObject); } } } //mark prop containing tiles foreach (Transform prop in propParent) { Vector3 posInMap = prop.position; posInMap.y = 0.0f; Tile adjacentPropTile = null; //check if this prop takes 2 tiles if (posInMap.z == (int)posInMap.z) { Vector3 adjacentPosInMap = posInMap; posInMap.z -= 0.5f; adjacentPosInMap.z += 0.5f; adjacentPropTile = (table[adjacentPosInMap] as GameObject).GetComponent <Tile>(); } else if (posInMap.x == (int)posInMap.x) { Vector3 adjacentPosInMap = posInMap; posInMap.x -= 0.5f; adjacentPosInMap.x += 0.5f; adjacentPropTile = (table[adjacentPosInMap] as GameObject).GetComponent <Tile>(); } Tile propTile = (table[posInMap] as GameObject).GetComponent <Tile>(); if (propTile != null) { propTile.PlaceObject(prop.gameObject); } //if this prop takes up more than 2 spaces, add a modifiable component if (adjacentPropTile != null) { adjacentPropTile.PlaceObject(prop.gameObject); prop.gameObject.AddComponent <ModifiableProp>().SetTiles(propTile, adjacentPropTile); } } tileController.OrganizeTiles(); FixPropTextures(); } }
void PlaceTile() { Vector3 pos = GetMousePosInGrid(); if (table.Contains(pos)) return; currentTile = guiController.GetCurrentObject(); currentTile = Instantiate(currentTile,pos,Quaternion.identity) as GameObject; currentTile.transform.parent = mapParent; table.Add(currentTile.transform.position,currentTile); if (currentTile.name.Contains("MovingTile")){ movingTile = currentTile.GetComponent<MovingTile>(); movingTile = Instantiate(movingTile) as MovingTile; buildMode = buildMode_t.PATROL_POINTS; } }
public void AddToMovingItemList(MovingTile movingObject) { tileList.Add(movingObject); }
/// <summary> /// Goes through all the entities and finds all of the ones that are Static, or physics(Dynamic maybe in the future) /// Creates and returns them using the data found in the xml file /// </summary> /// <param name="environment">Environment that these items exist in</param> /// <returns>A list of objects that are in the game</returns> public List<GameObject> GetObjects(ref PhysicsEnvironment environment) { List<GameObject> objects = new List<GameObject>(); foreach (EntityInfo entity in mEntities) { //If the object is static, make a static object if (entity.mType == XmlKeys.STATIC_OBJECT) { Tile tile = new Tile(mContent, .8f, entity); objects.Add(tile); } //If the object is physics, make a physics object if (entity.mType == XmlKeys.PHYSICS_OBJECT) { if (entity.mProperties.ContainsKey("Rail") && entity.mProperties.ContainsKey("Length")) mRails.Add(entity); bool isSquare = entity.mProperties.ContainsKey("Shape") && entity.mProperties["Shape"] == "Square"; float mass = 1; if (entity.mProperties.ContainsKey("Mass")) { mass = (float)Convert.ToDouble(entity.mProperties["Mass"]); } if (entity.mProperties.ContainsKey(XmlKeys.REVERSE)) { ReverseTile rTile = new ReverseTile(mContent, ref environment, 0.8f, entity); rTile.Mass = mass; objects.Add(rTile); } else { MovingTile mTile = new MovingTile(mContent, ref environment, 0.8f, entity); mTile.Mass = mass; objects.Add(mTile); } } } return objects; }
public void SpawnMap(int mapID, int playerCount) { Texture2D bitmap = MapLibrary.bitmaps[mapID]; width = bitmap.width; height = bitmap.height; int playerSpawn = 0; List <GameObject> tpTiles = new List <GameObject>(); List <GameObject> movingTiles = new List <GameObject>(); for (int y = 0; y < bitmap.height; y++) { for (int x = 0; x < bitmap.width; x++) { int k; Color32 c = bitmap.GetPixel(x, y);// *255; k = MapLibrary.ColorToTile(c); if (k % 2 == 0 && k > 0) { if (playerSpawn < 4) { playerSpawnPoints[playerSpawn].transform.position = transform.position + new Vector3(x * scaling, 0, -y * scaling); playerSpawn++; } k /= 2; } else if (k > 2) { k = (k + 1) / 2; } if (k > 0) { GameObject tile = Instantiate(tilePrefabs[k]); tile.transform.GetComponent <Tile>().tileID = (y * width) + x; tile.transform.SetParent(tilesHolder.transform); tile.transform.position = transform.position + new Vector3(x * scaling, 0, -y * scaling); if (k == 5) { tpTiles.Add(tile); } else if (k == 6) { movingTiles.Add(tile); tile.transform.position += new Vector3(0, 1.5f, 0); } } } } //print(bitmap.GetPixel(j, i) * 255); /* * for (int i = 0; i < dim; i++) * { * for (int j = 0; j < dim; j++) * { * int k; * if (i > dim / 2) * k = 0; * else * k = 1; * k = 0; * k = MapLibrary.maps25x25[mapID, i, j]; * if (k % 2 == 0 && k > 0) * { * if (playerSpawn < 4) * { * playerSpawnPoints[playerSpawn].transform.position = transform.position + new Vector3(j * 1.5f, 0, i * 1.5f); * playerSpawn++; * } * k /= 2; * } * else if (k > 2) * { * k = (k + 1) / 2; * } * GameObject tile = Instantiate(tilePrefabs[k]); * tile.transform.GetComponent<Tile>().tileID = (i * dim) + j; * tile.transform.SetParent(tilesHolder.transform); * tile.transform.position = transform.position + new Vector3(j * 1.5f, 0, i * 1.5f); * if (k == 5) * { * tpTiles.Add(tile); * } * } * }*/ List <int> tpTileMap = (List <int>)MapLibrary.teleportTiles[mapID]; if (tpTileMap != null) { for (int i = 0; i < tpTiles.Count; i++) { TeleportTile t = tpTiles[i].transform.GetComponent <TeleportTile>(); int rcver = tpTileMap[i]; if (rcver >= 0) { t.receiver = tpTiles[rcver]; tpTiles[rcver].transform.GetComponent <TeleportTile>().senders.Add(t.gameObject); } } } List <List <float> > destinations = (List <List <float> >)MapLibrary.movingTiles[mapID]; List <Vector3> sizes = (List <Vector3>)MapLibrary.movingTilesSizes[mapID]; if (destinations != null) { for (int i = 0; i < destinations.Count; i++) { MovingTile m = movingTiles[i].transform.GetComponent <MovingTile>(); m.destinations = new List <Vector3>(); m.transform.localScale = new Vector3(scaling * sizes[i].x, sizes[i].y, scaling * sizes[i].z); m.originalPosition = m.transform.position; List <float> coords = destinations[i]; for (int j = 0; j < coords.Count; j += 3) { float xScale = sizes[i].x, zScale = sizes[i].z, yScale = sizes[i].y; xScale--; yScale--; zScale--; Vector3 dest = m.originalPosition + new Vector3(coords[j], coords[j + 1], coords[j + 2]) * scaling + new Vector3(xScale, yScale, -zScale) * scaling / 2;; m.destinations.Add(dest); } } } transform.name = "Map " + (mapID + 1); for (int i = 0; i < 4; i++) { GameObject.Find("Player " + (i + 1)).transform.position = new Vector3(playerSpawnPoints [i].transform.position.x, 2, playerSpawnPoints [i].transform.position.z); } }
/// <summary> /// Exports the map with the given map name and mapHolder and propHolder parents - returns the file path exported to /// </summary> /// <returns> /// The map. /// </returns> /// <param name='mapName'> /// Map name. /// </param> /// <param name='mapHolder'> /// Map holder. /// </param> /// <param name='propHolder'> /// Prop holder. /// </param> public static string ExportMap(string mapName, Transform mapHolder, Transform propHolder) { //Get map folder path and create folder if it does not exist string mapFolderPath = Application.dataPath + "/MyMaps/"; if (!Directory.Exists(mapFolderPath)) { Directory.CreateDirectory(mapFolderPath); } if (mapName.Length < 1) { return(null); } string mapExportPath = mapFolderPath + mapName + ".txt"; if (mapExportPath.Length != 0) { TextWriter f = new StreamWriter(mapExportPath); //go through tiles and hazards foreach (Transform child in mapHolder) { if (child.name.Contains("(Clone)")) { child.name = child.name.Replace("(Clone)", ""); } //Go through the tile properties of the game object(if it has any) and export them if necessary Tile tile = child.GetComponent <Tile>(); if (tile != null) { //export tile hint if any if (tile.ContainsHint()) { child.tag = "HintTile"; //add tag appropriately } } //if child has a tag(e.g. StartTile or EndTile - write it at the end) if (!child.tag.Equals("Untagged")) { f.WriteLine(child.position.x + "\t" + child.position.y + "\t" + child.position.z + "\t" + child.name + "\t" + child.tag); //if a hint tile, write out hint as well if (tile.ContainsHint()) { f.WriteLine(tile.hint); } } else { f.WriteLine(child.position.x + "\t" + child.position.y + "\t" + child.position.z + "\t" + child.name); } if (child.name.Equals("MovingTile")) { MovingTile movingTile = child.gameObject.GetComponent <MovingTile>(); //if moving tile has any patrol points - write them to a new line if (movingTile.patrolPoints.Count > 1) { for (int i = 0; i < movingTile.patrolPoints.Count; i++) { f.Write(movingTile.patrolPoints[i].x + "," + movingTile.patrolPoints[i].y + "," + movingTile.patrolPoints[i].z); if (i != movingTile.patrolPoints.Count - 1) { f.Write("\t"); } } } //else if it hasn't any set - just put it's original place as the one point else { f.Write(child.position.x + "," + child.position.y + "," + child.position.z); } f.WriteLine(); } else if (child.name.Equals("Wind")) { Wind wind = child.gameObject.GetComponent <Wind>(); f.Write(wind.range + "," + wind.direction + "\n"); } } f.WriteLine("PROPS"); //indicate that we are switching to props //go through props foreach (Transform prop in propHolder) { if (prop.name.Contains("(Clone)")) { prop.name = prop.name.Replace("(Clone)", ""); } //for each prop write position, rotation and scale f.WriteLine(prop.position.x + "\t" + prop.position.y + "\t" + prop.position.z + "\t" + prop.rotation.eulerAngles.x + "\t" + prop.rotation.eulerAngles.y + "\t" + prop.rotation.eulerAngles.z + "\t" + prop.localScale.x + "\t" + prop.localScale.y + "\t" + prop.localScale.z + "\t" + prop.name); } f.Close(); f = null; Debug.Log("Map successfuly exported as: " + mapExportPath); return(mapExportPath); } return(null); }
/// <summary> /// Imports the map from the given map path and populates the mapHolder and propHolder parents - returns success value /// </summary> /// <returns> /// The map. /// </returns> /// <param name='mapPath'> /// Where to stream the map from /// </param> /// <param name='mapHolder'> /// MapHolder transform to put the tiles into /// </param> /// <param name='propHolder'> /// PropHolder transform to put the props into /// </param> public static bool ImportMap(string mapPath, Transform mapHolder, Transform propHolder) { TextReader r = new StreamReader(mapPath); if (r == null) { return(false); } bool tileMode = true; //indicates what mode we are on - tiles or props while (true) { string line = r.ReadLine(); if (line != null) { if (line.Equals("PROPS")) { tileMode = false; } //tile mode else if (tileMode) { string[] text = line.Split(new char[] { '\t' }); //GET TILE POSITION AND ADD TO PARENT Vector3 position = new Vector3(float.Parse(text[0]), float.Parse(text[1]), float.Parse(text[2])); string objectType = text[3]; string objectTag = null; if (text.Length > 4) { objectTag = text[4]; } GameObject gameObject; if (objectType.Contains("Tile")) { gameObject = Resources.Load(tilePath + objectType) as GameObject; } else { gameObject = Resources.Load(hazardPath + objectType) as GameObject; } gameObject = MonoBehaviour.Instantiate(gameObject, position, Quaternion.identity) as GameObject; gameObject.transform.parent = mapHolder; if (objectTag != null) { gameObject.tag = objectTag; } //IF Object HAS ANY SPECIFIC PROPERTIES - ADD THEM HERE //if hint tile - add hint to it if (gameObject.tag.Equals("HintTile")) { Tile tile = gameObject.GetComponent <Tile>(); tile.hintEnabled = true; tile.hint = r.ReadLine(); } //if moving tile - add patrol coordinates to it - they are on the next line if (objectType.Equals("MovingTile")) { MovingTile newMovingTile = gameObject.GetComponent <MovingTile>(); line = r.ReadLine(); text = line.Split(new char[] { '\t' }); for (int i = 0; i < text.Length; i++) { string vector = text[i]; string[] xyz = vector.Split(new char[] { ',' }); Vector3 patrolP = new Vector3(float.Parse(xyz[0]), float.Parse(xyz[1]), float.Parse(xyz[2])); newMovingTile.patrolPoints.Add(patrolP); } } //If this is a wind hazard else if (objectType.Equals("Wind")) { Wind wind = gameObject.GetComponent <Wind>(); line = r.ReadLine(); text = line.Split(new char[] { ',' }); int range; if (!int.TryParse(text[0], out range)) { Debug.Log("Error parsing wind hazard range info"); } direction_t dir = (direction_t)System.Enum.Parse(typeof(direction_t), text[1]); wind.SetRange(range); wind.SetDirection(dir); } } //prop mode else { string[] text = line.Split(new char[] { '\t' }); //GET PROP POSITION,ROTAION AND SCALE AND ADD TO PARENT Vector3 position = new Vector3(float.Parse(text[0]), float.Parse(text[1]), float.Parse(text[2])); Vector3 rotation = new Vector3(float.Parse(text[3]), float.Parse(text[4]), float.Parse(text[5])); Vector3 scale = new Vector3(float.Parse(text[6]), float.Parse(text[7]), float.Parse(text[8])); string propType = text[9]; GameObject propObject; Quaternion rot = Quaternion.identity; rot.eulerAngles = rotation; propObject = Resources.Load("2DAsset") as GameObject; propObject = MonoBehaviour.Instantiate(propObject, position, rot) as GameObject; propObject.transform.parent = propHolder; propObject.transform.localScale = scale; propObject.name = propType; } } else { break; } } r.Close(); r = null; Debug.Log("Map successfuly imported"); return(true); }