public void SetData(AntData data) { Debug.ClearDeveloperConsole(); gameObject.SetActive(false); myGuid = Guid.Parse(data.MyGuid); gameObject.transform.parent = GameObject.Find("Ants").transform; baseSpeed = data.BaseSpeed; currentSpeed = data.CurrentSpeed; damage = data.Damage; health = data.Health; Prefab = data.Prefab; minds = data.Minds; unitGroupID = Guid.Parse(data.UnitGroupID); if (data.ClosestEnemy != string.Empty) { closestEnemy = GameWorld.Instance.FindAnt(Guid.Parse(data.ClosestEnemy)); } isAtBase = data.IsAtBase; TeamID = data.TeamID; gameObject.SetActive(true); gameObject.transform.localEulerAngles = new Vector3(data.RotationX, data.RotationY, data.RotationZ); transform.localScale = new Vector3(data.ScaleX, data.ScaleY, data.ScaleZ); GetComponent <NavMeshAgent>().enabled = true; for (int i = 0; i < minds.Count; i++) { minds[i].SetData(data.MindData[i]); } }
private IEnumerator animateAllAnts(List <AntData> ants, List <List <int> > paths) { while (numAntsFinished < ants.Count) { for (int i = 0; i < ants.Count; ++i) { AntData tmp = ants[i]; animateAnt(ref tmp, paths[i]); } yield return(null); } antsFinished = true; //Debug.Log("animateAllAnts finished"); }
public AntContext(IState initial, AntData data) : this(initial) { Data = data; CurrentState.OnEnter(this); }
public override void OnEnter(IContext context) { Data = ((AntContext)context).Data; base.OnEnter(context); }
public IEnumerator animateIterCtx(IterationContext itCtx) { lengthOfAnimation = 5; timeLeft = lengthOfAnimation; antsFinished = false; numAntsFinished = 0; if (itCtx.currIter == 0) { pheromons = null; } pathControler.color = pathControler.color; double min, max; findMinMax(itCtx.pheromoneMatrix, out min, out max); double a, b; findLinarScale(min, max, 0.01, 1, out a, out b); applyLinearScale(a, b, itCtx.pheromoneMatrix); List <AntData> ants = new List <AntData>(); AntData antData; for (int i = 0; i < itCtx.antsRoutes.Count; ++i) { antData = new AntData(); antData.currentDest = 0; antData.timePerPath = lengthOfAnimation / itCtx.antsRoutes[i].Count; antData.timeLeftOnPath = 0; antData.antGameObject = GameObject.Instantiate(antPefab, Vector3.zero, Quaternion.identity); antData.finished = false; ants.Add(antData); } int tmp, tmp1; for (int i = 0; i + 1 < itCtx.iterShortestPath.Count; ++i) { tmp = cityControler.activeCitysIndex[itCtx.iterShortestPath[i]]; tmp1 = cityControler.activeCitysIndex[itCtx.iterShortestPath[i + 1]]; if (pathControler.pathMatrix[tmp, tmp1]) { pathControler.pathMatrix[tmp, tmp1].startColor = new Color(1, 1, 1, 1); pathControler.pathMatrix[tmp, tmp1].endColor = new Color(1, 1, 1, 1); } } StartCoroutine(animateAllAnts(ants, itCtx.antsRoutes)); while (timeLeft > 0) { timeLeft -= Time.deltaTime; animatePaths(itCtx); //animateAllAnts(ants, itCtx.antsRoutes); yield return(null); } pheromons = itCtx.pheromoneMatrix; while (!antsFinished) { yield return(null); } for (int i = 0; i < itCtx.antsRoutes.Count; ++i) { GameObject.Destroy(ants[i].antGameObject); } if (itCtx.currIter < itCtx.numOfIters - 1 && !acoControler.stoped) { StartCoroutine(acoControler.waitForIterCtx()); } else { acoControler.stopAlgorithm(); reset(); if (!acoControler.stoped) { for (int i = 0; i + 1 < itCtx.iterShortestPath.Count; ++i) { tmp = cityControler.activeCitysIndex[itCtx.iterShortestPath[i]]; tmp1 = cityControler.activeCitysIndex[itCtx.iterShortestPath[i + 1]]; if (pathControler.pathMatrix[tmp, tmp1]) { pathControler.pathMatrix[tmp, tmp1].startColor = new Color(1, 1, 1, 1); pathControler.pathMatrix[tmp, tmp1].startWidth = 0.7f; pathControler.pathMatrix[tmp, tmp1].endColor = new Color(1, 1, 1, 1); pathControler.pathMatrix[tmp, tmp1].endWidth = 0.7f; } } } } // Debug.Log("animateItCtxt finished"); yield break; }
private void animateAnt(ref AntData ant, List <int> path) { if (ant.timeLeftOnPath > 0) { ant.timeLeftOnPath -= Time.deltaTime; GameObject cityFrom = cityControler.citys[cityControler.activeCitysIndex[path[ant.currentDest - 1]]]; GameObject cityTo = cityControler.citys[cityControler.activeCitysIndex[path[ant.currentDest]]]; Transform child1 = PathControler.findDeepChild(cityFrom.transform, "Outter"); Transform child2 = PathControler.findDeepChild(cityTo.transform, "Outter"); if (!child1 || !child2) { return; } float xRadius = (child2.position - child1.position).magnitude / 2; float yRadius = xRadius / 2; float minAngle = -90; float maxAngle = 90; float angle = Mathf.LerpAngle(minAngle, maxAngle, (ant.timePerPath - ant.timeLeftOnPath) / ant.timePerPath); var test = ant.antGameObject.transform.InverseTransformPoint(child2.position); float x = Mathf.Sin(Mathf.Deg2Rad * angle) * test.magnitude; float z = Mathf.Cos(Mathf.Deg2Rad * angle) * test.magnitude / 2; GameObject child = ant.antGameObject.transform.GetChild(0).gameObject; var move = new Vector3(x, z, 0); child.transform.localPosition = move; //update pos /* GameObject cityFrom = cityControler.citys[cityControler.activeCitysIndex[path[ant.currentDest-1]]]; * GameObject cityTo= cityControler.citys[cityControler.activeCitysIndex[path[ant.currentDest]]]; * if(!cityFrom || !cityTo) * { * return ; * }*/ } else { if (ant.currentDest < path.Count - 1) { ++ant.currentDest; int i, j; i = cityControler.activeCitysIndex[path[ant.currentDest - 1]]; j = cityControler.activeCitysIndex[path[ant.currentDest]]; ant.timePerPath = distanceControler.distanceMatrix[i, j] / acoControler.antSpeed; ant.timeLeftOnPath = distanceControler.distanceMatrix[i, j] / acoControler.antSpeed; GameObject cityFrom = cityControler.citys[cityControler.activeCitysIndex[path[ant.currentDest - 1]]]; GameObject cityTo = cityControler.citys[cityControler.activeCitysIndex[path[ant.currentDest]]]; Transform child1 = PathControler.findDeepChild(cityFrom.transform, "Outter"); Transform child2 = PathControler.findDeepChild(cityTo.transform, "Outter"); if (!child1 || !child2) { return; } Vector3 pos = child1.position + (child2.position - child1.position) / 2; ant.antGameObject.transform.position = pos; var rot = Quaternion.FromToRotation(Vector3.right, child2.position - child1.position); ant.antGameObject.transform.rotation = rot; //ant.antGameObject.transform.position = cityFrom.transform.position; } else if (!ant.finished) { ++numAntsFinished; ant.finished = true; } } }
private void OnEnable() { _antData = Instantiate(_antData); _antData.Inventory = new List <string>(); AntContext = new AntContext(new StackFindLeafState(), _antData); }
private IEnumerator LoadEnumerator(string name) { if (File.Exists(GetSavePath() + $"/{name}.txt")) { string saveString = File.ReadAllText(Application.dataPath + $"/Saves/{name}.txt"); SaveObject saveObject = JsonUtility.FromJson <SaveObject>(saveString); if (SceneManager.GetActiveScene().name != saveObject.LevelName) { AsyncOperation asyncLoadLevel = SceneManager.LoadSceneAsync(saveObject.LevelName, LoadSceneMode.Single); while (!asyncLoadLevel.isDone) { Debug.Log("Loading the Scene"); yield return(null); } } if (name == "QuickSave") { UiController.UpdateEventText("QuickLoading..."); } else { UiController.UpdateEventText("Loading..."); } for (int i = 0; i < ResourceList.Count;) { ResourceList[i].Destroy(); } for (int i = 0; i < saveObject.Resources.Count; i++) { ResourceNodeData data = saveObject.ResourceData[i]; GameObject newNode = (GameObject)GameObject.Instantiate(Resources.Load($"Prefabs/Resources/{data.Prefab}"), new Vector3(data.PositionX, data.PositionY, data.PositionZ), Quaternion.identity); newNode.GetComponent <ResourceNode>().SetData(data); } foreach (UnitController controller in UnitControllerList) { controller.SetData(saveObject.TeamMindGroupData.FirstOrDefault(data => data.TeamId == controller.TeamId).MindGroupDataList); } for (int i = 0; i < AntList.Count;) { AntList[i].Destroy(); } for (int i = 0; i < saveObject.Ants.Count; i++) { AntData data = saveObject.AntData[i]; GameObject newAnt = (GameObject)GameObject.Instantiate(Resources.Load($"Prefabs/{data.Prefab}"), new Vector3(data.PositionX, data.PositionY, data.PositionZ), Quaternion.identity); newAnt.GetComponent <Ant>().SetData(data); } for (int i = 0; i < BaseControllerList.Count; i++) { BaseControllerList[i].SetData(saveObject.BaseControllerData[i]); } if (name == "QuickSave") { UiController.UpdateEventText("QuickLoad Complete!"); } else { UiController.UpdateEventText("Load Complete!"); } } else { UiController.UpdateEventText("Save file not found!", Color.red); } }