protected override JobHandle OnUpdate(JobHandle inputDeps) { if (Input.GetKeyDown(KeyCode.H)) { inputDeps = new InstantiateJob { CmdBuffer = m_entityCommandBufferSystem.CreateCommandBuffer().ToConcurrent() } }
void OnState() { if (state == UIState.CREATE) { int buildingWidth = 5; int buildingHeight = 5; Vector2Int mouseCoord = World.nodeCoordFromWorldPos((Vector2)Camera.main.ScreenToWorldPoint(Input.mousePosition) - new Vector2((buildingWidth - 1) / 2f * 1.044f, 0)); PlaceableReturn placeableReturn = Placeable(mouseCoord.x, mouseCoord.y, buildingWidth, buildingHeight); RemoveMarkers(); if (Input.GetMouseButtonDown(0) && placeableReturn.placeable) { Building building = new Building(Tower); List <BuildingNode> newNodes = new List <BuildingNode>(); foreach (PlanetNode n in placeableReturn.placeableNodes) { BuildingNode newNode = new BuildingNode(n.Q, n.R, n.worldPos, building); newNodes.Add(newNode); World.nodes[new Vector2Int(n.Q, n.R)] = newNode; World.planetNodes.TryRemove(new Vector2Int(n.Q, n.R), out _); } building.buildingNodes = newNodes; HashSet <PlanetNode> workerNodes = new HashSet <PlanetNode>(); foreach (PlanetNode n in placeableReturn.placeableNodes) { foreach (PlanetNode neighbour in n.getNeighbours()) { if (!workerNodes.Contains(neighbour)) { workerNodes.Add(neighbour); } } } building.workerNodes = workerNodes; InstantiateJob j = new InstantiateJob(building); JobManager.instance.QueueJob(j); _workerNodes = workerNodes; ExitState(UIState.CREATE); state = UIState.IDLE; EnterState(state); } else { SetMarkers(placeableReturn); } } }
/* * * if I have a job * 1) walk to one of the jobs workingnodes * 2) add completion to the job * 3) if job complete: repeat 0) * */ void Update() { if (Time.time < sleepUntil) { return; } if (job == null || job.IsComplete()) { job = JobManager.instance.TryGetJob(); if (job == null) { sleepUntil = Time.time + .2f; return; } else { if (job == previousJob) { sleepUntil = Time.time + sleepTimeAfterSameJobAssigned; JobManager.instance.QueueJob(job); job = null; return; } InstantiateJob instantiateJob = job as InstantiateJob; if (instantiateJob != null) { PathRequestManager.RequestPath(new PathRequest( World.nodes[World.nodeCoordFromWorldPos(transform.position)], instantiateJob.building.workerNodes.ToList(), OnPathFound)); } } } InstantiateJob instantiateJob2 = job as InstantiateJob; if (instantiateJob2 != null) { if (instantiateJob2.building.workerNodes.Contains(occupiedNode as PlanetNode)) { job.AddProcess(Time.deltaTime * .3f); } } }
IEnumerator FollowPath() { Vector3 a, b, c = transform.position; if (path.Length == 0) { yield break; } else if (path.Length == 1) { yield return(LookAt(path[0].worldPos)); float ti = Time.deltaTime * moveSpeed; for (; ti < 1f; ti += Time.deltaTime * moveSpeed * 2) { transform.localPosition = Vector3.Lerp(c, path[0].worldPos, ti); yield return(null); } transform.localPosition = path[0].worldPos; yield break; } Node currentNode; World.nodes.TryGetValue(new Vector2Int(path[1].Q, path[1].R), out currentNode); if (!World.planetNodes.ContainsKey(new Vector2Int(path[1].Q, path[1].R))) { InstantiateJob instantiateJob = job as InstantiateJob; if (instantiateJob != null) { PathRequestManager.RequestPath(new PathRequest( World.nodes[World.nodeCoordFromWorldPos(transform.position)], instantiateJob.building.workerNodes.ToList(), OnPathFound)); } yield break; } occupyNode(currentNode); yield return(LookAt(path[1].worldPos)); float t = Time.deltaTime * moveSpeed; for (; t < 1f; t += Time.deltaTime * moveSpeed * 2) { transform.localPosition = Vector3.Lerp(c, (path[1].worldPos + c) / 2, t); //Bezier.GetPoint(a, b, c, t); yield return(null); } t -= 1f; c = transform.position; for (int i = 2; i < path.Length; i++) { World.nodes.TryGetValue(new Vector2Int(path[i].Q, path[i].R), out currentNode); if (!World.planetNodes.ContainsKey(new Vector2Int(path[i].Q, path[i].R))) { InstantiateJob instantiateJob = job as InstantiateJob; if (instantiateJob != null) { PathRequestManager.RequestPath(new PathRequest( World.nodes[World.nodeCoordFromWorldPos(transform.position)], instantiateJob.building.workerNodes.ToList(), OnPathFound)); } for (; t < 1f; t += Time.deltaTime * moveSpeed) { transform.localPosition = c + (path[i - 1].worldPos - c) * t; //we just go forward to the centre so we don't need to change the rotation here yield return(null); } yield break; } occupyNode(currentNode); a = c; b = path[i - 1].worldPos; c = (b + path[i].worldPos) * 0.5f; for (; t < 1f; t += Time.deltaTime * moveSpeed) { transform.localPosition = Bezier.GetPoint(a, b, c, t); Vector3 d = Bezier.GetDerivative(a, b, c, t); float angle = Mathf.Atan2(d.y, d.x) * Mathf.Rad2Deg; transform.localRotation = Quaternion.AngleAxis(angle - 90, Vector3.forward); yield return(null); } t -= 1f; } a = c; b = path[path.Length - 1].worldPos; c = b; for (; t < 1f; t += Time.deltaTime * moveSpeed) { transform.localPosition = Bezier.GetPoint(a, b, c, t); Vector3 d = Bezier.GetDerivative(a, b, c, t); float angle = Mathf.Atan2(d.y, d.x) * Mathf.Rad2Deg; transform.localRotation = Quaternion.AngleAxis(angle - 90, Vector3.forward); yield return(null); } transform.localPosition = path[path.Length - 1].worldPos; path = null; }