//transfer position to world position public static Position world2position(VecTime worldPosition, Position center, float fullLat, float fullLon) { float lontitute = (((-worldPosition.worldPosition.x) * fullLon) / 10.0f) + center.lontitute; float latitute = (((-worldPosition.worldPosition.z) * fullLat) / 10.0f) + center.latitute; Position position = new Position(latitute, lontitute, new PTime(worldPosition.time.totalTime)); return(position); }
/////////////////////////static function///////////////////////////// //transfer position to world position public static VecTime position2world(Position position, Position center, float fullLat, float fullLon, GameObject objPrefab) { float radius = objPrefab.transform.localScale.x / 2; float x = (position.lontitute - center.lontitute) * 10 / fullLon; float z = (position.latitute - center.latitute) * 10 / fullLat; VecTime temp = new VecTime(new Vector3(-x, radius, -z), new PTime(position.time.totalTime)); return(temp); }
//whole tracks do filling between firstposition and lastposition public static void wholeTracksFilling(VecTime WfirstPostion, VecTime WlastPosition, List <Track> tracks) { foreach (Track track in tracks) { //old version, filling whole first and last position, //seem it is not working for dotween //track.trackFilling(WfirstPostion, WlastPosition); track.trackFilling(track.WfirstPosition, track.WlastPosition); } }
//save the world position public void getWorldPosition(Position center, float fullLat, float fullLon, GameObject objPrefab) { foreach (Position position in positions) { worldPositions.Add(position2world(position, center, fullLat, fullLon, objPrefab)); } worldPositions.Sort(); WfirstPosition = worldPositions[0]; WlastPosition = worldPositions[worldPositions.Count - 1]; }
//release the track's resources public void clearSelf() { name = null; firstPosition = null; lastPosition = null; positions.Clear(); positions = null; WfirstPosition = null; WlastPosition = null; worldPositions.Clear(); worldPositions = null; }
//track do filling between firstposition and lastposition public void trackFilling(VecTime WfirstPosition, VecTime WlastPosition) { //figure out if track have first or last position if (this.WfirstPosition.time.totalTime != WfirstPosition.time.totalTime) { VecTime temp = new VecTime(this.WfirstPosition.worldPosition, WfirstPosition.time); worldPositions.Add(temp); //release temp = null; } if (this.WlastPosition.time.totalTime != WlastPosition.time.totalTime) { VecTime temp = new VecTime(this.WlastPosition.worldPosition, WlastPosition.time); worldPositions.Add(temp); //release temp = null; } VecTime.filling(worldPositions); }
// Use this for initialization public Drawer(GameObject objPrefab, Track track, float duration) { obj = objPrefab; //send obj to its first place obj.transform.position = track.WfirstPosition.worldPosition; myPosition = obj.transform.position; this.duration = duration; WfirstPosition = track.WfirstPosition; WlastPosition = track.WlastPosition; List <Vector3> positions = new List <Vector3>(); List <float> durations = new List <float>(); //avoid first duration time durations.Add(0.01f); for (int i = 0; i < track.worldPositions.Count; i++) { positions.Add(track.worldPositions[i].worldPosition); if (i != 0) { durations.Add(getDuration(track.worldPositions[i - 1].time.totalTime, track.worldPositions[i].time.totalTime)); } } tweener = DOTween.ToArray(() => myPosition, x => myPosition = x, positions.ToArray(), durations.ToArray()); tweener.SetAutoKill(false).SetEase(Ease.Linear).Pause(); //release positions.Clear(); positions = null; durations.Clear(); durations = null; }
// Use this for initialization public Drawer(GameObject objPrefab, Track track, float duration, bool isCompanion) { obj = objPrefab; //send obj to its first place obj.transform.position = track.WfirstPosition.worldPosition; if (isCompanion) { obj.transform.position += companionHeight; } myPosition = obj.transform.position; //obj.GetComponent<Renderer>().enabled = false; //this.track = track; this.duration = duration; WfirstPosition = track.WfirstPosition; if (isCompanion) { //WfirstPosition.worldPosition += companionHeight; } WlastPosition = track.WlastPosition; if (isCompanion) { // WlastPosition.worldPosition += companionHeight; } List <Vector3> positions = new List <Vector3>(); List <float> durations = new List <float>(); //avoid first duration time durations.Add(0.01f); for (int i = 0; i < track.worldPositions.Count; i++) { if (isCompanion) { positions.Add(track.worldPositions[i].worldPosition + companionHeight); } else { positions.Add(track.worldPositions[i].worldPosition); } if (i != 0) { durations.Add(getDuration(track.worldPositions[i - 1].time.totalTime, track.worldPositions[i].time.totalTime)); //Debug.Log(track.name + ",firstPosition " + track.worldPositions[i - 1].worldPosition + "," + // track.worldPositions[i - 1].time + ",lastPosition " + // track.worldPositions[i].worldPosition + "," + track.worldPositions[i].time+ // ",true time is "+ getDuration(track.worldPositions[i - 1].time.totalTime, track.worldPositions[i].time.totalTime)); } } tweener = DOTween.ToArray(() => myPosition, x => myPosition = x, positions.ToArray(), durations.ToArray()); //tweener = obj.transform.DOPath(positions.ToArray(), duration, PathType.CatmullRom, PathMode.Full3D, 5, null); tweener.SetAutoKill(false).SetEase(Ease.Linear); if (isCompanion) { obj.GetComponent <MeshRenderer>().material.SetColor("_Color", new Color(1, 0, 0, 0)); obj.GetComponent <MeshRenderer>().material.SetColor("_OutlineColor", new Color(3f / 255f, 225f / 255f, 115f / 255f, 0)); } //release positions.Clear(); positions = null; durations.Clear(); durations = null; //this.track.positions.Clear(); //this.track.positions = null; //this.track.worldPositions.Clear(); //this.track.worldPositions = null; }
// Update is called once per frame void Update() { switch (flow) { ////////////////////////about load file and prepare/////////////////////// case 0: switch (fileFlow) { //simple init site case 0: testText = GameObject.Find("TestText"); backImage = GameObject.Find("BackImage"); companionLines = GameObject.Find("CompanionLines"); drawers = new List <Drawer>(); drawTracks = new GameObject("drawTracks"); index = new Dictionary <string, List <int> >(); fileFlow = 2; break; //get index case 2: index = Track.LoadIndex("files", "fixed_index"); fileFlow = 3; break; //get companion line index case 3: companionLinesIndex = Track.LoadIndexForCompanionLine("files", "fixed_index"); fileFlow = 4; break; //create the companion lines object case 4: int biggest = Map.getTheObjNumber(companionLinesIndex); Map.getCompanionLineObj(companionLines, companionLinePrefab, biggest); fileFlow = 5; break; //create planes case 5: map = GameObject.Find("Directional light").GetComponent <Map>(); map.getPlanes(4, 4); fileFlow = 6; flow = 1; break; default: break; } break; /////////////////get the center, firstposition and lastposition////////////// //some maintest work can put here case 1: tracks = Track.LoadFile("files", "new_data"); Position[] result = Track.calculTracks(tracks); center = result[0]; firstPosition = result[1]; lastPosition = result[2]; //release Array.Clear(result, 0, result.Length); result = null; flow = 2; break; ////////////////////////////about map////////////////////////////////////// case 2: switch (mapFlow) { //refresh map case 0: map.Refresh(center, 4, 4); mapFlow = 1; break; //get image case 1: StartCoroutine(map._Refresh(map.planes[number], map.points[number])); number++; if (number >= map.planes.Length) { number = 0; mapFlow = 2; flow = 3; } break; default: break; } break; ////////////////////////about drawer/////////////////////////////////////// case 3: switch (drawerFlow) { //generate the world position for each track case 0: Track.generateWorldPosition(tracks, center, map.fullLat, map.fullLon, objPrefab); drawerFlow = 1; break; //create first and last wolrd position case 1: WfirstPosition = Track.position2world(firstPosition, center, map.fullLat, map.fullLon, objPrefab); WlastPosition = Track.position2world(lastPosition, center, map.fullLat, map.fullLon, objPrefab); drawerFlow = 2; break; //create the time bar value and its drawer case 2: duration = Drawer.getDuration(WfirstPosition.time.totalTime, WlastPosition.time.totalTime); hSliderValue = WfirstPosition.time.totalTime; wholeTime = DOTween.To(x => hSliderValue = x, WfirstPosition.time.totalTime, WlastPosition.time.totalTime, duration); wholeTime.SetAutoKill(false).SetEase(Ease.Linear).Pause(); drawerFlow = 3; break; //generate the objects and their drawer case 3: Track getTrack = tracks[number]; if (getTrack.positions.Count > 0) { GameObject obj = Instantiate(objPrefab); obj.SetActive(false); obj.name = getTrack.name; obj.transform.FindChild("board").transform.FindChild("ID").GetComponent <TextMesh>().text = obj.name; Drawer drawer = new Drawer(obj, getTrack, Drawer.getDuration(getTrack.WfirstPosition.time.totalTime, getTrack.WlastPosition.time.totalTime)); drawers.Add(drawer); drawer.obj.transform.parent = drawTracks.transform; } number++; //release getTrack.clearSelf(); getTrack = null; if (number >= tracks.Count) { //release tracks.Clear(); tracks = null; //back number number = 0; GC.Collect(); drawerFlow = 4; } break; //for companions //check their companion situation case 4: if (index.ContainsKey(drawers[number].obj.name)) { drawers[number].getCompanionTimes(index[drawers[number].obj.name]); drawers[number].isCompanion = true; } number++; if (number >= drawers.Count) { number = 0; drawerFlow = 5; } break; //create empty child gameobject for objects to create lines later case 5: int lineNumber = drawers[number].getObjectNumber(); for (int i = 0; i < lineNumber; i++) { GameObject lineObj = Instantiate(linePrefab); lineObj.name = "line" + i; lineObj.transform.parent = drawers[number].obj.transform; drawers[number].lineObjects.Add(lineObj); } number++; if (number >= drawers.Count) { number = 0; drawerFlow = 6; flow = 4; } break; default: break; } break; /////////////////////wait plane finished and done text///////////////////// case 4: switch (waitFlow) { //wait for plane finished case 0: planeWaitTime -= Time.deltaTime; if (planeWaitTime < 0) { //to delay some time planeWaitTime = 1; for (int i = 0; i < map.planes.Length; i++) { GameObject plane = GameObject.Find("plane" + i); if (plane.GetComponent <Renderer>().material.mainTexture != map.planePrefab.GetComponent <Renderer>().sharedMaterial.mainTexture) { waitFlow = 1; } else { waitFlow = 0; StartCoroutine(map._Refresh(map.planes[i], map.points[i])); break; } } } break; //for done text case 1: loadingTweener = testText.GetComponent <Text>().DOText("DONE", 2, true).SetAutoKill(false).SetEase(Ease.Linear); waitFlow = 2; break; //for enjoy text case 2: if (loadingTweener.IsComplete()) { loadingTweener.Kill(); loadingTweener = testText.GetComponent <Text>().DOText("THEN ENJOY", 2, true).SetAutoKill(false).SetEase(Ease.Linear); waitFlow = 3; } break; //for loading image disappear case 3: if (loadingTweener.IsComplete()) { loadingTweener.Kill(); loadingTweener = DOTween.To(x => alphaValue = x, 1, 0, 2).SetAutoKill(false).SetEase(Ease.Linear); waitFlow = 4; } break; //for back image disappear case 4: if (loadingTweener.IsComplete()) { loadingTweener.Kill(); loadingTweener = DOTween.To(x => alphaValue = x, 1, 0, 2).SetAutoKill(false).SetEase(Ease.Linear); testText.GetComponent <Text>().DOText("", 2); waitFlow = 5; } else { loadingImage.GetComponent <Image>().color = new Color(1, 1, 1, alphaValue); } break; //prepare to go to the true scene case 5: if (loadingTweener.IsComplete()) { isLoading = false; loadingTweener.Kill(); anim.Clear(); anim = null; Destroy(loadingImage); Destroy(backImage); //release map map.clearSelf(); map = null; GC.Collect(); waitFlow = 6; flow = 5; } else { backImage.GetComponent <RawImage>().color = new Color(1, 1, 1, alphaValue); } break; default: break; } break; //play ground case 5: if (isPlaying) { wholeTime.PlayForward(); //for companion lines dealWithCompanionLines((int)hSliderValue / 60); //for drawers dealWithDrawers(false); } break; default: break; } }
// Update is called once per frame void Update() { switch (button) { //load file and prepare case 0: //get location tracks = Track.LoadFile(Application.streamingAssetsPath, "new_data.txt"); //get the center, firstposition and lastposition Position[] result = Track.calculTracks(tracks); center = result[0]; firstPosition = result[1]; lastPosition = result[2]; //clean Array.Clear(result, 0, result.Length); result = null; //get map map = GameObject.Find("Directional light").GetComponent <Map>(); map.Refresh(center); for (int i = 0; i < map.planes.Length; i++) { StartCoroutine(map._Refresh(map.planes[i], map.points[i])); } //generate the world position for each track Track.generateWorldPosition(tracks, center, map.fullLat, map.fullLon, objPrefab); //transfer first and last position to world position WfirstPosition = Track.position2world(firstPosition, center, map.fullLat, map.fullLon, objPrefab); WlastPosition = Track.position2world(lastPosition, center, map.fullLat, map.fullLon, objPrefab); //create the time bar value duration = Drawer.getDuration(WfirstPosition.time.totalTime, WlastPosition.time.totalTime); hSliderValue = WfirstPosition.time.totalTime; wholeTime = DOTween.To(x => hSliderValue = x, WfirstPosition.time.totalTime, WlastPosition.time.totalTime, duration); wholeTime.SetAutoKill(false).SetEase(Ease.Linear); button = 2; break; //not used part cause filling cost too much resources //also we can use only dotween now //filling /* * case 1: * //here like the locate ball, filling each ball one update * Track fillingTemp = tracks[number]; * if (fillingTemp.positions.Count > 0) * { * //do filling * //new version, cause dotween face problem * fillingTemp.trackFilling(fillingTemp.WfirstPosition, fillingTemp.WlastPosition); * } * number++; * if (number == tracks.Count) * { * button = 2; * number = 0; * * } * //release * fillingTemp = null; * GC.Collect(); * break; */ case 2: Track getTrack = tracks[number]; if (getTrack.positions.Count > 0) { //Debug.Log(getTrack.name + ",firstPosition " + getTrack.firstPosition.latitute + "," + // getTrack.firstPosition.lontitute + "," + getTrack.firstPosition.time + ",lastPosition " + // getTrack.lastPosition.latitute + "," + // getTrack.lastPosition.lontitute + "," + getTrack.lastPosition.time); GameObject obj = Instantiate(objPrefab); obj.name = getTrack.name; Drawer drawer = new Drawer(obj, getTrack, Drawer.getDuration(getTrack.WfirstPosition.time.totalTime, getTrack.WlastPosition.time.totalTime), false); drawers.Add(drawer); drawer.obj.transform.parent = drawTracks.transform; //for companions //old version, for 3d show /* * if (getTrack.name.Contains("6602") || getTrack.name.Contains("9789") || getTrack.name.Contains("14914")) * { * GameObject objC = Instantiate(objPrefab); * objC.name = getTrack.name; * companions[0].Add(new Drawer(objC, getTrack, Drawer.getDuration(getTrack.WfirstPosition.time.totalTime, getTrack.WlastPosition.time.totalTime), true)); * } * else if (getTrack.name.Contains("7459") || getTrack.name.Contains("7585")) * { * GameObject objC = Instantiate(objPrefab); * objC.name = getTrack.name; * companions[1].Add(new Drawer(objC, getTrack, Drawer.getDuration(getTrack.WfirstPosition.time.totalTime, getTrack.WlastPosition.time.totalTime), true)); * } * else if (getTrack.name.Contains("13423") || getTrack.name.Contains("13426")) * { * GameObject objC = Instantiate(objPrefab); * objC.name = getTrack.name; * companions[2].Add(new Drawer(objC, getTrack, Drawer.getDuration(getTrack.WfirstPosition.time.totalTime, getTrack.WlastPosition.time.totalTime), true)); * } */ //new version if (drawer.obj.name.Contains("6602") || drawer.obj.name.Contains("9789") || drawer.obj.name.Contains("14914")) { drawer.isCompanion = true; companions[0].Add(drawer); } else if (drawer.obj.name.Contains("7459") || drawer.obj.name.Contains("7585")) { drawer.isCompanion = true; companions[1].Add(drawer); } else if (drawer.obj.name.Contains("13423") || drawer.obj.name.Contains("13426")) { drawer.isCompanion = true; companions[2].Add(drawer); } //release drawer = null; } number++; if (number == tracks.Count) { //release tracks.Clear(); tracks = null; button = 3; number = 0; GC.Collect(); } //release getTrack = null; break; //create plane case 3: for (int i = 0; i < map.planes.Length; i++) { GameObject plane = GameObject.Find("plane" + i); if (plane.GetComponent <Renderer>().material.mainTexture != null) { button = 4; } else { button = 3; StartCoroutine(map._Refresh(map.planes[i], map.points[i])); break; } } if (button == 4) { //clean loading isLoading = false; Array.Clear(anim, 0, anim.Length); anim = null; Destroy(loadingPlane); //release map map = null; GC.Collect(); } break; case 4: if (isPlaying) { wholeTime.PlayForward(); foreach (Drawer drawer in drawers) { if (hSliderValue >= drawer.WfirstPosition.time.totalTime) { drawer.tweener.PlayForward(); drawer.drawLine(isPlaying); if (drawer.isCompanion) { drawer.obj.GetComponent <MeshRenderer>().material.SetColor("_Color", new Color(0, 234f / 255f, 1, 1)); drawer.obj.GetComponent <LineRenderer>().material.SetColor("_OutlineColor", new Color(0, 234f / 255f, 1, 1)); } } if (hSliderValue >= drawer.WlastPosition.time.totalTime) { if (drawer.isCompanion) { drawer.obj.GetComponent <MeshRenderer>().material.SetColor("_Color", new Color(1, 0, 0, 1)); } } } if (companionPrepared) { foreach (Drawer drawer in mouseControllor.targetCompanion[mouseControllor.companionNumber]) { if (hSliderValue >= drawer.WfirstPosition.time.totalTime) { drawer.tweener.PlayForward(); drawer.drawLine(isPlaying); if (drawer.isCompanion) { drawer.obj.GetComponent <MeshRenderer>().material.SetColor("_Color", new Color(0, 234f / 255f, 1, 1)); drawer.obj.GetComponent <LineRenderer>().material.SetColor("_OutlineColor", new Color(0, 234f / 255f, 1, 1)); } } } } } if (isPause) { wholeTime.Pause(); foreach (Drawer drawer in drawers) { if (hSliderValue >= drawer.WfirstPosition.time.totalTime && hSliderValue < drawer.WlastPosition.time.totalTime) { drawer.tweener.Pause(); /* * if (drawer.isCompanion) * { * drawer.obj.GetComponent<MeshRenderer>().material.SetColor("_Color", new Color(0, 234f / 255f, 1, 1)); * drawer.obj.GetComponent<LineRenderer>().material.SetColor("_OutlineColor", new Color(0, 234f / 255f, 1, 1)); * } */ } } if (companionPrepared) { foreach (Drawer drawer in mouseControllor.targetCompanion[mouseControllor.companionNumber]) { if (hSliderValue >= drawer.WfirstPosition.time.totalTime) { drawer.tweener.Pause(); } } } } break; //not used part //create ballsS /* * case 99: * Track track = tracks[number]; * if (track.positions.Count > 0) * { * Locator lb = new Locator(center, map.fullLat, map.fullLon, track.name); * //List<Position> pos = track.positions; * //lb.locateObject(objPrefab, pos); * lb.worldLocate(objPrefab, track.worldPositions); * } * number++; * if (number == tracks.Count) * { * button = 4; * } * break; */ default: break; } }