protected IEnumerator remoteCall() { changinglevels = true; WWW content = new WWW(@"http://wiki-412.appspot.com/json/alpha2.json"); while(!content.isDone) { Debug.Log("fetching"); yield return null; } string hold = content.text; currentlevel = JsonMapper.ToObject<Level>(hold); currentlevel.fixLimits(); currentlevel.constructMatrix(3); generateLevel(); changinglevels = false; }
//breadth first mapping of level, that goes one extra block out. Naive implementation. Need to get character away from cubes to work public Level mapLevel(string name) { //Create array of directions to check for mapping Vector3[] directions = new Vector3[6]; //declare array to go through, maybe find more elegant way directions[0] = Vector3.right; directions[1] = Vector3.left; directions[2] = Vector3.up; directions[3] = Vector3.down; directions[4] = Vector3.forward; directions[5] = Vector3.back; List<Point> reached = new List<Point>(); List<Point> toreach = new List<Point>(); toreach.Add(new Point(GameObject.FindGameObjectWithTag("block").transform.position,true)); Vector3 position; bool occupied; Level level = new Level(); Entity template; RaycastHit hitblock; while(toreach.Count!=0) { position = toreach[0].position; occupied = toreach[0].occupied; template = new Entity(); //new Entity for(int i = 0;i<6;i++) //check all 6 sides for blocks and unchecked spaces { if(Physics.Raycast(position,directions[i],out hitblock,1f))//if object in direction and it hasn't been reached or will be reached, add it to scan que { if(hitblock.collider.tag=="block") { Point temp = new Point(position+directions[i],true); if(!reached.Contains(temp)&&!toreach.Contains(temp)) toreach.Add(temp); } else if(hitblock.collider.tag=="character") { Entity temp = new Entity(); temp.setData(Mathf.RoundToInt(position.x+directions[i].x),Mathf.RoundToInt(position.y+directions[i].y),Mathf.RoundToInt(position.z+directions[i].z),states.character,(int) charactertypes.basic); if(!level.Objects.Contains(temp)) level.addObject(temp); } } else if(!Physics.Raycast(position,directions[i],1f)&&occupied == true)//if no object in front and coming from occupied block, and not in either list, then add { Point temp = new Point(position+directions[i],false); if(!reached.Contains(temp)&&!toreach.Contains(temp)) toreach.Add(temp); } } //if block then add if(occupied==true) { template.setData(Mathf.RoundToInt(position.x),Mathf.RoundToInt(position.y),Mathf.RoundToInt(position.z),states.block,(int) blocktypes.basic);//add entity for occupied spaces level.addObject(template); } //add to reached points, remove from points to scan reached.Add(toreach[0]); toreach.Remove(toreach[0]); } level.name = name; level.fixLimits(); return level; }
//Use stream reader and external json library to read in a saved level //after loading in make sure to fix limits to the maximums so that padding will be consistent when added //then make the matrix of right size, including padding protected void read(int padding) { Debug.Log ("reading level"); if(!Application.isWebPlayer) { using(StreamReader file = new StreamReader(Application.dataPath+"/Levels/"+currentlevel.name+".json")) { string hold = file.ReadToEnd(); currentlevel = JsonMapper.ToObject<Level>(hold); } currentlevel.fixLimits(); currentlevel.constructMatrix(padding); generateLevel(); } else if(Application.isWebPlayer) { StartCoroutine(remoteCall()); } }