public IEnumerator doGlobalStructureBoundsNavRemove(Transform root, Bounds startBounds) { if (BoltNetwork.isClient) { yield break; } if (this.globalNavStructures.Contains(root.gameObject)) { yield break; } this.globalNavStructures.Add(root.gameObject); if (this.doingGlobalNavUpdate) { yield break; } this.doingGlobalNavUpdate = true; yield return(new WaitForSeconds(0.5f)); while (!this.waitForLoadSequence) { yield return(null); } if (!AstarPath.active) { yield break; } if (!root) { yield break; } int globalNavStructureCount = this.globalNavStructures.Count; for (int i = 0; i < globalNavStructureCount; i++) { Transform transform = this.globalNavStructures[i].transform; globalNavId globalNavId = transform.GetComponent <globalNavId>(); if (!globalNavId) { globalNavId = transform.gameObject.AddComponent <globalNavId>(); globalNavId.navId = i; for (int j = i + 1; j < globalNavStructureCount; j++) { globalNavId globalNavId2 = this.globalNavStructures[j].GetComponent <globalNavId>(); if (!globalNavId2 && (transform.position - this.globalNavStructures[j].transform.position).sqrMagnitude < 10000f) { globalNavId2 = this.globalNavStructures[j].AddComponent <globalNavId>(); globalNavId2.navId = i; } } } } int countUpdates = 0; List <GameObject> processedStructures = new List <GameObject>(); for (int k = 0; k < globalNavStructureCount; k++) { globalNavId component = this.globalNavStructures[k].GetComponent <globalNavId>(); int navId = component.navId; if (!processedStructures.Contains(this.globalNavStructures[k])) { List <Collider> list = new List <Collider>(); for (int l = 0; l < globalNavStructureCount; l++) { globalNavId component2 = this.globalNavStructures[l].GetComponent <globalNavId>(); if (component2.navId == navId) { processedStructures.Add(this.globalNavStructures[l]); Collider[] componentsInChildren = this.globalNavStructures[l].GetComponentsInChildren <Collider>(); for (int m = 0; m < componentsInChildren.Length; m++) { gridObjectBlocker component3 = componentsInChildren[m].transform.GetComponent <gridObjectBlocker>(); if (component3) { list.Add(componentsInChildren[m]); } } Collider component4 = this.globalNavStructures[l].GetComponent <Collider>(); if (component4 && component4.GetComponent <gridObjectBlocker>()) { list.Add(component4); } } } Bounds bounds = list[0].bounds; for (int n = 1; n < list.Count; n++) { bounds.Encapsulate(list[n].bounds); } countUpdates++; GraphUpdateObject graphUpdateObject = new GraphUpdateObject(bounds); this.graphsBeingUpdated = true; AstarPath.active.astarData.recastGraph.rasterizeColliders = false; int graphIndex = (int)AstarPath.active.astarData.recastGraph.graphIndex; graphUpdateObject.nnConstraint.graphMask = 1 << graphIndex; AstarPath.active.UpdateGraphs(graphUpdateObject, 0f); } } float startNavTime = Time.realtimeSinceStartup; this.astarGuo.GetComponent <TileHandlerHelper>().ForceUpdate(); AstarPath.active.FlushWorkItems(); this.doingGlobalNavUpdate = false; foreach (GameObject gameObject in this.globalNavStructures) { if (gameObject) { globalNavId component5 = gameObject.GetComponent <globalNavId>(); if (component5) { UnityEngine.Object.Destroy(component5); } } } this.globalNavStructures.Clear(); Scene.MutantControler.calculateMainNavArea(); Debug.Log("startup nav cut structures time = " + (Time.realtimeSinceStartup - startNavTime)); yield break; }
// Use this for initialization void Start() { //------------------------------------------------------------------------------------------------------------- //Redoing texture to look nicer this._OriginalTexture = (Texture2D)this.renderer.material.mainTexture; Texture2D NewTexture = new Texture2D(this._mapSize, this._mapSize, TextureFormat.RGB24, true); int[,] mapTiles = new int[this._mapSize, this._mapSize]; Color[] NewMainTexPixels = new Color[this._mapSize * this._mapSize]; for (int i = 0; i < this._mapSize; i++) { for (int j = 0; j < this._mapSize; j++) { Color PixelColor = this._OriginalTexture.GetPixel(i, j); int smallTextureX = i % 32; int smallTextureY = j % 32; int pixelPosition = this._mapSize * j + i; //Get the AI path node closest to this pixel //THIS IS SO AWESOME IT'S F*****G SWEET float vectorX = (i / 25.6f) - 10.0f; float vectorZ = (j / 25.6f) - 10.0f; Vector3 ClosestVector = new Vector3(vectorX, 0.0f, vectorZ); Node PathNode = AstarPath.active.GetNearest(ClosestVector).node; //Debug.Log (PixelColor.r + "," + PixelColor.g + "," + PixelColor.b); if (PixelColor == Color.green) { mapTiles [i, j] = GRASS; NewMainTexPixels[pixelPosition] = this.GrassTexture.GetPixel(smallTextureX, smallTextureY); PathNode.penalty = this._PENALTY_GRASS; PathNode.tags = GRASS; } else if (PixelColor == Color.blue) { mapTiles [i, j] = WATER; NewMainTexPixels[pixelPosition] = this.WaterTexture.GetPixel(smallTextureX, smallTextureY); PathNode.walkable = false; PathNode.tags = WATER; } else if (PixelColor.r == (128.0f / 255.0f) && PixelColor.g == (64.0f / 255.0f)) //Mountain brown { mapTiles [i, j] = MOUNTAIN; NewMainTexPixels[pixelPosition] = this.MountainTexture.GetPixel(smallTextureX, smallTextureY); PathNode.penalty = this._PENALTY_MOUNTAIN; PathNode.tags = MOUNTAIN; } else if (PixelColor.g == (128.0f / 255.0f)) //Forest green { mapTiles [i, j] = FOREST; NewMainTexPixels[pixelPosition] = this.ForestTexture.GetPixel(smallTextureX, smallTextureY); PathNode.penalty = this._PENALTY_FOREST; PathNode.tags = FOREST; } else if (PixelColor.r == (127.0f / 255.0f) && PixelColor.g == (127.0f / 255.0f) && PixelColor.b == (127.0f / 255.0f)) { mapTiles [i, j] = ROAD; NewMainTexPixels[pixelPosition] = this.RoadTexture.GetPixel(smallTextureX, smallTextureY); PathNode.penalty = this._PENALTY_ROAD; PathNode.tags = ROAD; } //Updating the actual node with its changes in the active grid graph? GraphUpdateObject GUO = new GraphUpdateObject(); GUO.Apply(PathNode); } } NewTexture.SetPixels(NewMainTexPixels); NewTexture.Apply(); this.renderer.material.mainTexture = NewTexture; }
public static void GetGUO(this GraphUpdateScene gus, ref GraphUpdateObject guo) { if (gus.points == null || gus.points.Length == 0) { var polygonCollider = gus.GetComponent <PolygonCollider2D>(); if (polygonCollider != null) { var points2D = polygonCollider.points; Vector3[] pts = new Vector3[points2D.Length]; for (int i = 0; i < pts.Length; i++) { var p = points2D[i] + polygonCollider.offset; pts[i] = new Vector3(p.x, 0, p.y); } var mat = gus.transform.localToWorldMatrix * Matrix4x4.TRS(Vector3.zero, Quaternion.Euler(-90, 0, 0), Vector3.one); var shape = new GraphUpdateShape(gus.points, gus.convex, mat, gus.minBoundsHeight); if (guo == null) { guo = new GraphUpdateObject(); } guo.bounds = shape.GetBounds(); guo.shape = shape; } else { var bounds = gus.GetBounds(); if (bounds.center == Vector3.zero && bounds.size == Vector3.zero) { Debug.LogError("Cannot apply GraphUpdateScene, no points defined and no renderer or collider attached", gus); } if (guo == null) { guo = new GraphUpdateObject(bounds); } else { guo.bounds = bounds; } guo.shape = null; } } else { GraphUpdateShape shape; // Used for compatibility with older versions var worldPoints = new Vector3[gus.points.Length]; for (int i = 0; i < gus.points.Length; i++) { worldPoints[i] = gus.transform.TransformPoint(gus.points[i]); } shape = new GraphUpdateShape(worldPoints, gus.convex, Matrix4x4.identity, gus.minBoundsHeight); if (guo == null) { guo = new GraphUpdateObject(); } guo.bounds = shape.GetBounds(); guo.shape = shape; } guo.nnConstraint = NNConstraint.None; guo.modifyWalkability = gus.modifyWalkability; guo.setWalkability = gus.setWalkability; guo.addPenalty = gus.penaltyDelta; guo.updatePhysics = gus.updatePhysics; guo.updateErosion = gus.updateErosion; guo.resetPenaltyOnPhysics = gus.resetPenaltyOnPhysics; guo.modifyTag = gus.modifyTag; guo.setTag = gus.setTag; }
public IEnumerator doStructureBoundsNavRemove(Transform root, Bounds startBounds, float delay) { if (BoltNetwork.isClient) { yield break; } if (this.currentNavStructures.Contains(root.gameObject)) { yield break; } this.currentNavStructures.Add(root.gameObject); if (!AstarPath.active) { yield break; } if (this.doingNavUpdate) { yield break; } this.doingNavUpdate = true; yield return(new WaitForSeconds(delay)); if (!root) { yield break; } Bounds combinedBounds = startBounds; List <Collider> allCol = new List <Collider>(); for (int i = 0; i < this.currentNavStructures.Count; i++) { Collider[] componentsInChildren = this.currentNavStructures[i].GetComponentsInChildren <Collider>(); for (int j = 0; j < componentsInChildren.Length; j++) { gridObjectBlocker component = componentsInChildren[j].transform.GetComponent <gridObjectBlocker>(); if (component) { allCol.Add(componentsInChildren[j]); } } } Collider rootCol = root.GetComponent <Collider>(); if (rootCol) { allCol.Add(rootCol); } for (int k = 0; k < allCol.Count; k++) { combinedBounds.Encapsulate(allCol[k].bounds); } GraphUpdateObject guo = new GraphUpdateObject(combinedBounds); while (this.graphsBeingUpdated) { yield return(null); } this.graphsBeingUpdated = true; AstarPath.active.astarData.recastGraph.rasterizeColliders = false; int indexOfGraph = (int)AstarPath.active.astarData.recastGraph.graphIndex; guo.nnConstraint.graphMask = 1 << indexOfGraph; AstarPath.active.UpdateGraphs(guo, 0f); this.doingNavUpdate = false; this.currentNavStructures.Clear(); base.StartCoroutine(this.validateClimbingWalls(true)); while (this.graphsBeingUpdated) { AstarPath.active.astarData.recastGraph.rasterizeColliders = false; yield return(null); } Scene.MutantControler.calculateMainNavArea(); yield break; }
private void FixedUpdate() { //sets the area just a bit bigger than the collider to default tag GraphUpdateObject bigguo = new GraphUpdateObject(new Bounds(GetComponent <Collider2D>().bounds.center, GetComponent <Collider2D>().bounds.extents *1.1f)); bigguo.modifyTag = true; bigguo.setTag = 0; AstarPath.active.UpdateGraphs(bigguo); //sets the area in the collider to P1 tag GraphUpdateObject guo = new GraphUpdateObject(new Bounds(GetComponent <Collider2D>().bounds.center, GetComponent <Collider2D>().bounds.extents)); guo.modifyTag = true; guo.setTag = 1; AstarPath.active.UpdateGraphs(guo); if (TankControls) { //gives the speed speed += inputVector.y * (acceleration * Time.deltaTime) * temp_speed; speed = Mathf.Clamp(speed, maxSpeed * -1, maxSpeed); transform.Translate(Vector2.up * speed, Space.Self); //gives it the drag speed = speed * Drag * temp_drag; if (Mathf.Abs(speed) <= minSpeed && inputVector.y == 0) { speed = 0; } turnSpeed += inputVector.x * turnAcceleration * Time.deltaTime * temp_speed; transform.Rotate(Vector3.back * turnSpeed); turnSpeed *= Drag * temp_drag; if (speed < 0 && turnSpeed < 0) { turnSpeed = Mathf.Abs(turnSpeed) * -1; } else if (speed < 0 && turnSpeed > 0) { turnSpeed = Mathf.Abs(turnSpeed); } if (Mathf.Abs(speed) <= minSpeed && inputVector.y == 0) { speed = 0; } } else if (!ReverseControls) { //gives the speed newspeed += inputVector * (acceleration * Time.deltaTime) * temp_speed; Vector2.ClampMagnitude(newspeed, maxSpeed); target_degree = (Mathf.Atan2(-newspeed.x, newspeed.y) * Mathf.Rad2Deg + 360) % 360; //if (currkey) bool fob = !(Mathf.DeltaAngle(transform.eulerAngles.z, target_degree) > 180 - reverse_allowance); Debug.Log(Mathf.DeltaAngle(transform.eulerAngles.z + 180, target_degree)); Debug.Log(fob); if (fob) { transform.Translate(Vector2.up * newspeed.magnitude, Space.Self); } else { transform.Translate(Vector2.down * newspeed.magnitude, Space.Self); } //gives it the drag newspeed = newspeed * Drag * temp_drag; if (Mathf.Abs(speed) <= minSpeed && inputVector.y == 0) { speed = 0; } float angle; newturnspeed += inputVector.magnitude * turnAcceleration * Time.deltaTime * temp_speed; if (fob) { angle = Mathf.LerpAngle(transform.eulerAngles.z, target_degree, newturnspeed); } else { angle = Mathf.LerpAngle(transform.eulerAngles.z, target_degree + 180, newturnspeed); } transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward); newturnspeed *= Drag * temp_drag; } else { //gives the speed newspeed += inputVector * (acceleration * Time.deltaTime) * temp_speed; Vector2.ClampMagnitude(newspeed, maxSpeed); target_degree = (Mathf.Atan2(-newspeed.x, newspeed.y) * Mathf.Rad2Deg + 360) % 360; Debug.Log(Mathf.DeltaAngle(transform.eulerAngles.z + 180, target_degree)); if (reverse == 0) { transform.Translate(Vector2.up * newspeed.magnitude, Space.Self); } else { transform.Translate(Vector2.down * newspeed.magnitude, Space.Self); } //gives it the drag newspeed = newspeed * Drag * temp_drag; if (Mathf.Abs(speed) <= minSpeed && inputVector.y == 0) { speed = 0; } float angle; newturnspeed += inputVector.magnitude * turnAcceleration * Time.deltaTime * temp_speed; angle = Mathf.LerpAngle(transform.eulerAngles.z, target_degree, newturnspeed); if (inputVector != Vector2.zero) { transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward); } newturnspeed *= Drag * temp_drag; } }
public void UpdateGraphs(GraphUpdateObject ob, float t) { base.StartCoroutine(this.UpdateGraphsInteral(ob, t)); }
public override bool Trigger(object sender, object arg) { if (!this.CanTrigger) { return(false); } switch (_mode) { case RecalculateMode.All: { if (_delay > 0f) { this.InvokeGuaranteed(() => AstarPath.active.ScanAsync(), _delay); } else { AstarPath.active.ScanAsync(); } return(true); } case RecalculateMode.Region: { var bounds = new Bounds(this.transform.position, this.transform.localScale); var guo = new GraphUpdateObject(bounds) { updatePhysics = true }; if (_delay > 0f) { this.InvokeGuaranteed(() => AstarPath.active.UpdateGraphs(guo), _delay); } else { AstarPath.active.UpdateGraphs(guo); } } break; case RecalculateMode.BoundsOfCollider: { if (_objectRef == null || !(_objectRef is Collider)) { return(false); } var bounds = (_objectRef as Collider).bounds; var guo = new GraphUpdateObject(bounds) { updatePhysics = true }; if (_delay > 0f) { this.InvokeGuaranteed(() => AstarPath.active.UpdateGraphs(guo), _delay); } else { AstarPath.active.UpdateGraphs(guo); } } break; } return(false); }
private void UpdatePenaltyArea() { updateGraphFrameCounter++; if (WaitedEnoughFrames(ref updateGraphFrameCounter, updateGraphFrameCount)) { Vector3 penaltyPos = transform.position; Bounds resetBounds; GraphUpdateObject resetGuo; resetBounds = new Bounds(penaltyPos, boundsSize * 10f); resetGuo = new GraphUpdateObject(resetBounds); resetGuo.resetPenaltyOnPhysics = true; resetGuo.requiresFloodFill = false; resetGuo.addPenalty = 0; AstarPath.active.UpdateGraphs(resetGuo); if (SwarmManager.singleton.playerIsHunted || SwarmManager.singleton.playerMadeEnemysFlee) { Bounds penaltyBounds; GraphUpdateObject guo; if (SwarmManager.singleton.playerIsHunted) { penaltyPos = transform.position + (-transform.up * 0.5f); penaltyBounds = new Bounds(penaltyPos, boundsSize); guo = new GraphUpdateObject(penaltyBounds); guo.resetPenaltyOnPhysics = true; guo.requiresFloodFill = false; guo.addPenalty = 1000; AstarPath.active.UpdateGraphs(guo); return; } if (SwarmManager.singleton.playerMadeEnemysFlee) { penaltyPos = transform.position + (transform.up * 1.5f); penaltyBounds = new Bounds(penaltyPos, boundsSize); penaltyBounds.Expand(2.75f); guo = new GraphUpdateObject(penaltyBounds); guo.resetPenaltyOnPhysics = false; guo.requiresFloodFill = false; guo.addPenalty = 25000; AstarPath.active.UpdateGraphs(guo); penaltyPos = transform.position;; Bounds penaltyBoundsAtPlayer = new Bounds(penaltyPos, boundsSize); penaltyBoundsAtPlayer.Expand(1.2f); GraphUpdateObject guoPlayer = new GraphUpdateObject(penaltyBoundsAtPlayer); guoPlayer.resetPenaltyOnPhysics = false; guoPlayer.requiresFloodFill = false; guoPlayer.addPenalty = 50000; AstarPath.active.UpdateGraphs(guoPlayer); } } } }
IEnumerator UpdateGraphsInternal(GraphUpdateObject ob, float delay) { yield return(new WaitForSeconds(delay)); UpdateGraphs(ob); }
// Update used for setting destinations and performing actions void Update() { //enemy attempts to use powerups as soon as it holds them if (inventory.heldPU != Power_Ups.Empty) { UsePowerups(); } //sets the area just a bit bigger than the seeker to default tag GraphUpdateObject bigguo = new GraphUpdateObject(new Bounds(transform.position, new Vector3(ai.radius, ai.radius) * 1.1f)); bigguo.modifyTag = true; bigguo.setTag = 0; AstarPath.active.UpdateGraphs(bigguo); //sets the area in the seeker to P1 tag GraphUpdateObject guo = new GraphUpdateObject(new Bounds(transform.position, new Vector3(ai.radius, ai.radius))); guo.modifyTag = true; guo.setTag = pathtag; //updates node tags AstarPath.active.UpdateGraphs(guo); //grab/buying actions inventory.grab = 0; if (inventory.atRegister) { inventory.buying = true; } else { inventory.buying = false; } if (ai.hasPath) { //rotation /*if (ai.remainingDistance < 1.5) //depricated bug fix, but not deleted in case bug comes up again * transform.rotation = new Quaternion(0, 0, target.target.rotation.z - 180, 0);//prvents it sticking on the side of the shelf * else*/ transform.up = ai.desiredVelocity;//sets rotation right if (ai.reachedEndOfPath)//when arrived at a shelf { Debug.Log("ended path"); if (DestinationSetter.target.tag == "Shelf") //if at shelf, grab { inventory.grab = 1; } SetDestination(); } } else//basically only happens at the beginning of round, or if something jank happens { SetDestination(); } }
public void UpdateGraphs(GraphUpdateObject ob, float delay) { StartCoroutine(UpdateGraphsInternal(ob, delay)); }
public static void generate(Level level, int width, int height) { GameObject sapien = (GameObject)Resources.Load("mob_homo_sapien", typeof(GameObject)); GameObject flores = (GameObject)Resources.Load("mob_homo_floresiensis", typeof(GameObject)); level.tiles = new Tile[width, height]; level.block = new Block[width, height]; for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { float perlinX, perlinY; perlinX = i * 0.7f; perlinY = j * 0.7f; GameObject go = (GameObject)GameObject.Instantiate(Tile.allTileModels[Tile.Type.STONE], new Vector3(i * 2, 0, j * 2), Quaternion.Euler(new Vector3(0, 0, 0))); Bounds btile = go.collider.bounds; //Pathfinding.Console.Write ("// Placing Object\n"); GraphUpdateObject guo = new GraphUpdateObject(btile); AstarPath.active.UpdateGraphs(guo); level.tiles[i, j] = go.GetComponent <Tile>(); Block.Type blockType; //GENERATE END GAME if (j == height - 1 && i == width / 2) { for (int x = -width / 2; x < width / 2; x++) { for (int y = 0; y < 14; y++) { GameObject tile = (GameObject)GameObject.Instantiate(Tile.allTileModels[Tile.Type.STONE], new Vector3((x + i) * 2, 0, (y + j) * 2), Quaternion.Euler(new Vector3(0, 0, 0))); } } continue; } if (j > 0 && j < 4 && i > width / 2 - 2 && i < width / 2 + 2) { continue; } if (i == 0 || i == width - 1 || j == 0 || j == height - 1) { blockType = Block.Type.BORDER; } else { blockType = Block.Type.STONE; } currentSeed = defaultSeed; if (blockType == Block.Type.BORDER || perlinNoise(perlinX, perlinY, 0.6f) > -0.2f) { GameObject block = (GameObject)GameObject.Instantiate(Block.allBlockModels[blockType], new Vector3(i * 2, 0, j * 2), Quaternion.Euler(new Vector3(0, 0, 0))); Bounds b = block.collider.bounds; //Pathfinding.Console.Write ("// Placing Object\n"); GraphUpdateObject guo1 = new GraphUpdateObject(b); AstarPath.active.UpdateGraphs(guo1); level.block[i, j] = block.GetComponent <Block>(); level.block[i, j].Init(blockType, level, i, j); } else { if (UnityEngine.Random.Range(0, 1f) < 0.04f) { currentSeed = defaultSeed + 123; if (perlinNoise(perlinX, perlinY, 0.2f) > 0.0f) { GameObject npc = (GameObject)GameObject.Instantiate(sapien, new Vector3(i * 2, 0, j * 2), Quaternion.identity); Sapien mob = npc.GetComponent <Sapien>(); mob.type = 0; mob.level = level; level.allMobs.Add(mob); } else { GameObject npc = (GameObject)GameObject.Instantiate(flores, new Vector3(i * 2, 0, j * 2), Quaternion.identity); Sapien mob = npc.GetComponent <Sapien>(); mob.type = 1; mob.level = level; level.allMobs.Add(mob); } } } } } OnGenerateMap(level, width, height); }
private IEnumerator UpdateGraphsInteral(GraphUpdateObject ob, float t) { yield return new WaitForSeconds(t); this.UpdateGraphs(ob); yield break; }
void FixedUpdate() { if (!isDead) { movement.x = Input.GetAxisRaw("Horizontal"); movement.y = Input.GetAxisRaw("Vertical"); if (playerPossession.isPossessing || playerPossession.isDepossessing) { movement.x = 0; movement.y = 0; } if (playerPossession.isPossessed && playerPossession.itemSpeed == 0) { movement.x = 0; movement.y = 0; } //if A/D pressed, save time it was pressed if (movement.x != 0) { if (timeDownX == 0.0f) //if we don't have a stored time for it { timeDownX = Time.time; } } else { timeDownX = 0.0f; //reset time if no button is being pressed } //if W/S pressed, save time it was pressed if (movement.y != 0) { if (timeDownY == 0.0f) //if we don't have a stored time for it { timeDownY = Time.time; } } else { timeDownY = 0.0f; //reset time if no button is being pressed } //check which button was hit last to determine direction if (timeDownX > timeDownY) { movement = Vector2.right * movement.x; } else { movement = Vector2.up * movement.y; } movementSinceLastGraphUpdate += Math.Abs(movement.x) + Math.Abs(movement.y); if (movementSinceLastGraphUpdate > 0.4) { Bounds bounds = GetComponents <BoxCollider2D>()[1].bounds; bounds.Expand(0.5f); var guo = new GraphUpdateObject(bounds); guo.updatePhysics = true; AstarPath.active.UpdateGraphs(guo); movementSinceLastGraphUpdate = 0; } UpdateAnimationAndMove(); } }
void updateMesh() { var myBounds = new GraphUpdateObject(GetComponent <Collider> ().bounds); AstarPath.active.UpdateGraphs(myBounds); }