private void FunnelMethod(Cell currentCell, Cell destinationCell) { // prepare local variables // the previous path node is either the actual previous path node, or in the case of portalling where we might receive a path with only 1 point - the destination - then we use the group's center of gravity in place of the previous path node Vector3 cellPos = currentCell.position; Vector3 currentPathNode = _currentPath[_currentPathStep].position; Vector3 previousPathNode = _currentPathStep > 0 ? _currentPath[_currentPathStep - 1].position : this.group.centerOfGravity; // get all walkable cell neighbours GetWalkableCellNeighbours(currentCell, _tempWalkableNeighbours, !_allowDiagonals); int neighboursCount = _tempWalkableNeighbours.count; for (int i = 0; i < neighboursCount; i++) { var neighbour = _tempWalkableNeighbours[i]; Vector3 neighbourPos = neighbour.position; // check whether the neighbour cell we are visiting has unwalkable neighbours Vector3 directionVector = Vector3.zero; bool noUnwalkables = GetCellNeighboursAndUnwalkability(neighbour, _extraTempWalkableNeighbours, !_allowDiagonals); if (noUnwalkables && (neighbourPos - destinationCell.position).sqrMagnitude > _funnelWidthSqr) { // if cell has no unwalkable neighbours... // Sum up a vector comprised of 3 vectors: 1) fast marching direction (avoid obstacles), 2) goal direction (point to next node path), 3) path direction (point in path's direction) Vector3 fastMarchVector = neighbourPos.DirToXZ(cellPos).normalized; Vector3 goalVector = neighbourPos.DirToXZ(currentPathNode).normalized; Vector3 pathVector = previousPathNode.DirToXZ(currentPathNode).normalized; directionVector = (fastMarchVector + goalVector + pathVector) / 3f; } else { // if this cell has unwalkable neighbours, then just use fast marching direction and multiply the magnitude with the obstacle strength factor directionVector = neighbourPos.DirToXZ(cellPos).normalized *_obstacleStrengthFactor; } _fastMarchedCellsSet[neighbourPos] = new PlaneVector(directionVector); Vector3 closestPos = GetClosestPositionOnLine(previousPathNode, currentPathNode, neighbourPos); if ((closestPos - neighbourPos).sqrMagnitude > _funnelWidthSqr) { // only add neighbour cells within the defined funnel area around the path continue; } _openSet.Enqueue(neighbour); } // check whether we need to move backwards along the path (we start at the destination) // Basically, if we reach a cell that is less than one cell size away from our previous path node, then move on to the next path nodes (decrementally) if (_currentPathStep > 0 && (previousPathNode - cellPos).sqrMagnitude < (_grid.cellSize * _grid.cellSize)) { if (_currentPath[_currentPathStep - 1] is IPortalNode) { // never move past portal nodes, we never want to remove them because we want to force units to go through the portal return; } _currentPathStep -= 1; } }
void Start() { m_rigidobdy = GetComponent <Rigidbody>(); m_bezierTime = 0.0f; m_planeDamage = new List <GameObject>() { null, null, null, null }; transform.Find("NameTag").gameObject.GetComponent <TextMesh>().text = GetComponent <PhotonView>().owner.CustomProperties["RoomDisplayName"].ToString(); if (GetComponent <PhotonView>().isMine) { transform.Find("NameTag").gameObject.GetComponent <TextMesh>().text = ""; } m_planeBank = transform.Find("PlaneBank"); m_lastBezierPoint = transform.position; for (int i = 0; i < m_lastPositions.Length; i++) { m_lastPositions[i] = new PlaneVector(transform.position, transform.up); } for (int i = 0; i < m_photonPositions.Length; i++) { m_photonPositions[i] = transform.position; } string teamBomberPath = ""; if ((int)GetComponent <PhotonView>().owner.CustomProperties["Team"] == 1) { teamBomberPath = "Bomber01"; gameObject.layer = 8; transform.Find("NameTag").gameObject.GetComponent <TextMesh>().color = Color.green; } else { teamBomberPath = "Bomber02"; gameObject.layer = 9; transform.Find("NameTag").gameObject.GetComponent <TextMesh>().color = Color.red; } Transform graphicPivot = transform.Find("PlaneBank").Find("PlaneGraphic"); GameObject graphic = (GameObject)Instantiate((GameObject)Resources.Load(teamBomberPath), graphicPivot.position, graphicPivot.rotation); graphic.transform.parent = graphicPivot; graphic.transform.localPosition = Vector3.zero; graphic.transform.localRotation = Quaternion.identity; m_bulletSpawnPoint = graphic.transform.Find("BulletSpawn"); m_leftContrail = graphic.transform.Find("LeftSmokeTrail").GetComponent <ParticleSystem>(); m_rightContrail = graphic.transform.Find("RightSmokeTrail").GetComponent <ParticleSystem>(); m_gunCharge = transform.GetChild(0).GetChild(0).Find("GunCharge").gameObject; m_gunCharge.GetComponent <Animator>().speed = 1 / GameObject.Find("BrainCloudStats").GetComponent <BrainCloudStats>().m_multiShotDelay; }
private void SmoothFields() { var zero = PlaneVector.zero; // loop over entire grid int sizeX = _grid.sizeX, sizeZ = _grid.sizeZ; for (int x = 0; x < sizeX; x++) { for (int z = 0; z < sizeZ; z++) { PlaneVector direction = _fastMarchedCells[x, z]; if (direction.sqrMagnitude == 0f) { _cellDirs[x, z].direction = zero; _cellDirs[x, z].pathPortalIndex = -1; // if the given cell has not been fast marched, it should not be smoothed, and reset it to default continue; } // smooth the cell's vector, if and only if it has no blocked cell neighbours (or missing, if built-in containment is true) int smoothVectors = 1; PlaneVector smoothVector = direction; var cell = _grid.cellMatrix[x, z]; bool noUnwalkables = GetCellNeighboursAndUnwalkability(cell, _tempWalkableNeighbours, !_allowDiagonals); if (noUnwalkables) { int walkableNeighboursCount = _tempWalkableNeighbours.count; for (int i = 0; i < walkableNeighboursCount; i++) { var neighbour = _tempWalkableNeighbours[i]; PlaneVector dir = _fastMarchedCells[neighbour.matrixPosX, neighbour.matrixPosZ]; if (dir.sqrMagnitude == 0f) { // if the neighbour has not been fast marched, then ignore it continue; } smoothVectors++; smoothVector += dir; } // divide by the amount of vectors summed to get back to the original magnitude smoothVector /= smoothVectors; } // update the vector field cell _cellDirs[x, z].direction = smoothVector; _cellDirs[x, z].pathPortalIndex = -1; } /* end grid traversal z */ } /* end grid traversal x */ }
public float AngleTo(PlaneVector aVector) { float direction = 1; float angle = 0.0f; if ((m_direction.x * aVector.m_direction.y) - (m_direction.y * aVector.m_direction.x) < 0) { direction = -1; } angle = Vector3.Angle(m_direction, aVector.m_direction); if (m_direction == aVector.m_direction) angle = 0; return angle * direction; }
public float AngleTo(PlaneVector aVector) { float direction = 1; float angle = 0.0f; if ((m_direction.x * aVector.m_direction.y) - (m_direction.y * aVector.m_direction.x) < 0) { direction = -1; } angle = Vector3.Angle(m_direction, aVector.m_direction); if (m_direction == aVector.m_direction) { angle = 0; } return(angle * direction); }
private void SmoothFields() { // iterate over all fast marched cells - this uses an enumerator and thus allocates a small amount of memory foreach (var pair in _fastMarchedCellsSet) { Vector3 pos = pair.Key; var cell = GetCellAtPos(pos); int smoothVectors = 1; PlaneVector smoothVector = pair.Value; // get all cell neighbours, if there are no unwalkable (or missing neighbours, if built-in containment is true) bool noUnwalkables = GetCellNeighboursAndUnwalkability(cell, _tempWalkableNeighbours, !_allowDiagonals); if (noUnwalkables) { int walkableNeighboursCount = _tempWalkableNeighbours.count; for (int i = 0; i < walkableNeighboursCount; i++) { var neighbour = _tempWalkableNeighbours[i]; Vector3 neighbourPos = neighbour.position; PlaneVector dir = GetFastMarchedCellAtPos(neighbourPos); if (dir.sqrMagnitude == 0f) { // if the neighbour has not been fast marched, then ignore it continue; } smoothVectors++; smoothVector += dir; } smoothVector /= smoothVectors; } // set the vector field cell direction _cellDirsSet[pos] = new VectorFieldCell(smoothVector); } }
void Start() { m_bezierTime = 0.0f; m_planeDamage = new List<GameObject>() { null, null, null, null }; transform.FindChild("NameTag").gameObject.GetComponent<TextMesh>().text = GetComponent<PhotonView>().owner.customProperties["RoomDisplayName"].ToString(); if (GetComponent<PhotonView>().isMine) { transform.FindChild("NameTag").gameObject.GetComponent<TextMesh>().text = ""; } m_planeBank = transform.FindChild("PlaneBank"); m_lastBezierPoint = transform.position; for (int i = 0; i < m_lastPositions.Length; i++) { m_lastPositions[i] = new PlaneVector(transform.position, transform.up); } for (int i = 0; i < m_photonPositions.Length; i++) { m_photonPositions[i] = transform.position; } string teamBomberPath = ""; if ((int)GetComponent<PhotonView>().owner.customProperties["Team"] == 1) { teamBomberPath = "Bomber01"; gameObject.layer = 8; transform.FindChild("NameTag").gameObject.GetComponent<TextMesh>().color = Color.green; } else { teamBomberPath = "Bomber02"; gameObject.layer = 9; transform.FindChild("NameTag").gameObject.GetComponent<TextMesh>().color = Color.red; } Transform graphicPivot = transform.FindChild("PlaneBank").FindChild("PlaneGraphic"); GameObject graphic = (GameObject)Instantiate((GameObject)Resources.Load(teamBomberPath), graphicPivot.position, graphicPivot.rotation); graphic.transform.parent = graphicPivot; graphic.transform.localPosition = Vector3.zero; graphic.transform.localRotation = Quaternion.identity; m_bulletSpawnPoint = graphic.transform.FindChild("BulletSpawn"); m_leftContrail = graphic.transform.FindChild("LeftSmokeTrail").GetComponent<ParticleSystem>(); m_rightContrail = graphic.transform.FindChild("RightSmokeTrail").GetComponent<ParticleSystem>(); m_gunCharge = transform.GetChild(0).GetChild(0).FindChild("GunCharge").gameObject; m_gunCharge.GetComponent<Animator>().speed = 1 / GameObject.Find("BrainCloudStats").GetComponent<BrainCloudStats>().m_multiShotDelay; }
/// <summary> /// Initializes a new instance of the <see cref="VectorFieldCell"/> struct. /// </summary> /// <param name="portalIndex">Index of the portal.</param> /// The plane vector defaults to a PlaneVector.zero (0f, 0f) in this case. public VectorFieldCell(int portalIndex) { this.direction = PlaneVector.zero; this.pathPortalIndex = portalIndex; }
/// <summary> /// Initializes a new instance of the <see cref="VectorFieldCell"/> struct. /// </summary> /// <param name="direction">The direction.</param> /// The path portal index defaults to -1 in this case. public VectorFieldCell(PlaneVector direction) { this.direction = direction; this.pathPortalIndex = -1; }
/// <summary> /// Initializes a new instance of the <see cref="VectorFieldCell"/> struct. /// </summary> /// <param name="direction">The direction.</param> /// <param name="portalIndex">Index of the portal.</param> public VectorFieldCell(PlaneVector direction, int portalIndex) { this.direction = direction; this.pathPortalIndex = portalIndex; }
void Start() { m_lastPositions = new PlaneVector[1]; m_planeDamage = new List<GameObject>() { null, null, null, null }; Debug.Log(m_playerID); transform.FindChild("NameTag").gameObject.GetComponent<TextMesh>().text = BombersPlayerController.GetPlayer(m_playerID).m_displayName; if (BombersPlayerController.GetPlayer(m_playerID).isLocalPlayer) //isLocal { transform.FindChild("NameTag").gameObject.GetComponent<TextMesh>().text = ""; } m_planeBank = transform.FindChild("PlaneBank"); for (int i = 0; i < m_lastPositions.Length; i++) { m_lastPositions[i] = new PlaneVector(transform.position, transform.up); } string teamBomberPath = ""; if (BombersPlayerController.GetPlayer(m_playerID).m_team == 1) { teamBomberPath = "Bomber01"; gameObject.layer = 8; transform.FindChild("NameTag").gameObject.GetComponent<TextMesh>().color = Color.green; } else { teamBomberPath = "Bomber02"; gameObject.layer = 9; transform.FindChild("NameTag").gameObject.GetComponent<TextMesh>().color = Color.red; } Transform graphicPivot = transform.FindChild("PlaneBank").FindChild("PlaneGraphic"); GameObject graphic = (GameObject)Instantiate((GameObject)Resources.Load(teamBomberPath), graphicPivot.position, graphicPivot.rotation); graphic.transform.parent = graphicPivot; graphic.transform.localPosition = Vector3.zero; graphic.transform.localRotation = Quaternion.identity; m_bulletSpawnPoint = graphic.transform.FindChild("BulletSpawn"); m_leftContrail = graphic.transform.FindChild("LeftSmokeTrail").GetComponent<ParticleSystem>(); m_rightContrail = graphic.transform.FindChild("RightSmokeTrail").GetComponent<ParticleSystem>(); m_gunCharge = transform.GetChild(0).GetChild(0).FindChild("GunCharge").gameObject; m_gunCharge.GetComponent<Animator>().speed = 1 / GameObject.Find("BrainCloudStats").GetComponent<BrainCloudStats>().m_multiShotDelay; }
void FixedUpdate() { if (!isLocalPlayer) { return; } Vector3 direction = GetComponent<Rigidbody>().velocity; if (direction == Vector3.zero) { direction = transform.up; } else { direction.Normalize(); } direction = transform.up; float angle = m_lastPositions[0].AngleTo(new PlaneVector(transform.position, direction)) * 5; if (angle > 90) { angle = 90; } else if (angle < -90) { angle = -90; } if (angle < 0) { m_isBankingRight = true; m_isBankingLeft = false; } else if (angle > 0) { m_isBankingRight = false; m_isBankingLeft = true; } else { m_isBankingRight = false; m_isBankingLeft = false; } float targetAngle = 0; if (m_bankTime < 0) { targetAngle = -90 * m_bankCurve.Evaluate(m_bankTime / -m_timeToFullBank); } else { targetAngle = 90 * m_bankCurve.Evaluate(m_bankTime / m_timeToFullBank); } m_bankAngle = targetAngle; Vector3 eulerAngles = m_planeBank.localEulerAngles; eulerAngles.y = m_bankAngle; m_planeBank.localEulerAngles = eulerAngles; m_lastPositions[0] = new PlaneVector(transform.position, direction); }