public override void OnStateStay(float deltaTime) { FlockingGroup group = CharacterMng.Instance.GetGroup(m_targetCharacter.GetFlockingAgent.GroupHandle); if (m_targetCharacter.Ally == EAllyType.Friend) { if (group.GetOwner.State != ECharacterState.Move) { m_targetCharacter.State = ECharacterState.Idle; return; } Vector3 dir; m_targetCharacter.GetFlockingAgent.GetAxis(group.GetOwner.transform, out dir); m_targetCharacter.transform.position += dir * m_targetCharacter.GetStat.MoveSpeed * deltaTime; } else { if (m_targetCharacter.Target != null) { m_targetCharacter.State = ECharacterState.Chase; return; } Vector3 dir; m_targetCharacter.GetFlockingAgent.GetAxis(group.GetOwner.transform, out dir); m_targetCharacter.transform.position += dir * m_targetCharacter.GetStat.MoveSpeed * deltaTime; } }
void instantiatOneGroup() { Vector3 playerPos = player.transform.position; Vector3 groupSpawnCenter = playerPos + new Vector3(Random.Range(-this.tankSize, this.tankSize), Random.Range(1.0f, 5.0f), Random.Range(-this.tankSize, this.tankSize)); GameObject spawnCenterObj = (GameObject)Instantiate(goalPrefab, groupSpawnCenter, Quaternion.identity); int fishKindId = Random.Range(0, this.allFishKinds.Length); GameObject flockPrefab = allFishKinds [fishKindId]; FlockingGroup flockGroup = FlockingGroupFactory.createGroup(fishKindId, numInstance[fishKindId], spawnCenterObj, spawnRad); Vector3[] flockSpawnPos = flockGroup.getGroupSpawnPoints(); GameObject[] flocks = new GameObject[numInstance[fishKindId]]; for (int i = 0; i < numInstance[fishKindId]; i++) { GameObject flockGo = (GameObject)Instantiate(flockPrefab, flockSpawnPos[i], Quaternion.identity); Flock flock = flockGo.GetComponent <Flock> (); flock.setFlockGroup(flockGroup); flocks [i] = flockGo; } flockGroup.setFlock(flocks); allFishGroups.Add(flockGroup); }
/// <summary> /// Sets the unit map for referencing later on. This is required because the map is created /// after the director so it cannot be included in the constructor. /// </summary> internal void SetMap(ref Map.Map map) { mUnitMap = new UnitMap((int)map.GetMeasurements().X, (int)map.GetMeasurements().Y); mMap = map; mSelectedGroup = new FlockingGroup(ref mDirector, ref mMap); mGroups.Add(mSelectedGroup); }
public void EnsureIncluded(FlockingGroup group) { if (!mGroups.Contains(group)) { mGroups.Add(group); } }
public FlockingGroup GetNewFlock() { var group = new FlockingGroup(ref mDirector, ref mMap); mGroups.Add(group); return(group); }
void deleteGroupContent(FlockingGroup flockingGroup) { foreach (GameObject go in flockingGroup.getFlocksInGroup()) { Destroy(go); } Destroy(flockingGroup.currGoal()); }
void Update() { int[] toBeDeletedIdx = new int[allFishGroups.Count]; for (int groupIdx = 0; groupIdx < allFishGroups.Count; groupIdx++) { FlockingGroup flockGroup = ((FlockingGroup)allFishGroups [groupIdx]); Vector3 groupCenter = flockGroup.getFlockCenter(); float dist = Vector3.Distance(groupCenter, player.transform.position); if (dist > tankSize) { deleteGroupContent(flockGroup); toBeDeletedIdx [groupIdx] = 1; } if (flockGroup.getScored() && dist > flockGroup.getDeleteDist()) { deleteGroupContent(flockGroup); toBeDeletedIdx [groupIdx] = 1; EventManager.TriggerEvent <LoseScoreEvent, Vector3, string> (player.transform.position, "Player"); ScoreManager.score -= flockGroup.groupScore() / 2; Debug.Log("LOST SCORE!!!!!!!!"); } if (flockGroup.checkTriggerBehaviour(player.transform)) { flockGroup.triggerBehaviour(); } } ArrayList newAllFishGroups = new ArrayList(); for (int i = 0; i < toBeDeletedIdx.Length; i++) { if (toBeDeletedIdx [i] != 1) { newAllFishGroups.Add(allFishGroups[i]); } } allFishGroups = newAllFishGroups; int currGroupsSize = allFishGroups.Count; for (; currGroupsSize < maxGroup; currGroupsSize++) { instantiatOneGroup(); } }
public bool GetAxis(Transform destination, out Vector3 direction) { direction = Vector3.zero; FlockingGroup group = CharacterMng.Instance.GetGroup(m_groupHandle); List <Transform> context = new List <Transform>(); Vector3 characterPos = m_character.transform.position; for (int i = 0; i < group.Members.Count; ++i) { Vector3 targetPos = group.Members[i].transform.position; float distance = Vector3.Distance(characterPos, targetPos); if (distance <= m_radius) { if (group.Members[i] == m_character) { continue; } // 겹쳐있다면 if (distance == 0) { goto DestinationCalcul; } // 만약 너무 가까운 그룹원이 있다면 멀어진다. else if (distance < m_agentDensity + group.Members[i].GetFlockingAgent.m_agentDensity) { direction += (characterPos - group.Members[i].transform.position).normalized * 3; context.Add(group.Members[i].transform); } } } DestinationCalcul: // 목적지와 방향백터를 연산한다. if (!context.Contains(destination)) { direction += (destination.position - characterPos).normalized; context.Add(destination); } // 방향벡터 합산 direction /= context.Count; return(true); }
public override void OnStateStay(float deltaTime) { if (m_targetCharacter.Target == null) { m_targetCharacter.State = ECharacterState.Idle; return; } if (Vector2.Distance(m_targetCharacter.transform.position, m_targetCharacter.Target.transform.position) <= m_targetCharacter.GetStat.AttackRange) { m_targetCharacter.State = ECharacterState.Attack; return; } Vector3 dirVector = m_targetCharacter.Target.transform.position - m_targetCharacter.transform.position; dirVector.Normalize(); m_targetCharacter.MoveAxis = dirVector; m_targetCharacter.transform.position += dirVector * m_targetCharacter.GetStat.MoveSpeed * deltaTime; if (m_targetCharacter.Ally == EAllyType.Enermy) { FlockingGroup group = CharacterMng.Instance.GetGroup(m_targetCharacter.GetFlockingAgent.GroupHandle); group.SetOwner = m_targetCharacter; for (int i = 0; i < group.Members.Count; ++i) { if (group.Members[i] == group.GetOwner) { continue; } if (group.Members[i].State != ECharacterState.Idle && group.Members[i].State != ECharacterState.Move) { continue; } group.Members[i].MoveAxis = m_targetCharacter.MoveAxis; group.Members[i].State = ECharacterState.Move; } } }
public void ReloadSetMap(ref Map.Map map) { mMap = map; mUnitMap = new UnitMap((int)map.GetMeasurements().X, (int)map.GetMeasurements().Y); mSelectedGroup = new FlockingGroup(ref mDirector, ref mMap); foreach (var funit in mFriendlyMilitary) { mUnitMap.AddUnit(funit); } //This list includes every platform foreach (var platform in mReconstructionList) { mUnitMap.AddUnit(platform); } foreach (var hunit in mHostileMilitary) { mUnitMap.AddUnit(hunit); } }
public override void OnStateStay(float deltaTime) { if (m_targetCharacter.GetAStarAgent.State == AStarAgent.EAgentState.Standby) { m_targetCharacter.State = ECharacterState.Idle; return; } FlockingGroup group = CharacterMng.Instance.GetGroup(m_targetCharacter.GetFlockingAgent.GroupHandle); for (int i = 0; i < group.Members.Count; ++i) { if (group.Members[i] == group.GetOwner) { continue; } group.Members[i].MoveAxis = m_targetCharacter.MoveAxis; group.Members[i].State = ECharacterState.Move; } Vector3 lookDirection = m_targetCharacter.transform.localScale; lookDirection.x = -1 * Mathf.Sign(m_targetCharacter.GetAStarAgent.Velocity.x); m_targetCharacter.transform.localScale = lookDirection; }
public void setFlockGroup(FlockingGroup flockGroup) { this.flockGroup = flockGroup; }
public void RemoveGroup(FlockingGroup group) { m_group.Remove(group.GroupHandle); }
public bool Kill(FlockingGroup group) { return(mGroups.Remove(group)); }
public void Update(GameTime gametime) { mUnitMap?.Update(gametime); #region Check targets for friendly units foreach (var unit in mFriendlyMilitary) { // iterate through each friendly unit, if there's a target nearby, shoot the closest one. // get all the adjacent units var adjacentUnits = mUnitMap?.GetAdjacentUnits(unit.AbsolutePosition); // remember the closest adjacent unit. ICollider closestAdjacent = null; var closestAdjacentDistance = 1500f; // a separate variable is used so that it can be initalized with a very big value. if (adjacentUnits == null) { continue; } // iterate through all adjacent units to find the closest adjacent unit. foreach (var adjacentUnit in adjacentUnits) { // only calculate the distance to the adjacent unit if the unit is not friendly. if (!adjacentUnit.Friendly) { // calculate the distance var dist = Geometry.GetQuickDistance(unit.AbsolutePosition, adjacentUnit.AbsolutePosition); // check if it's within range if (dist < unit.Range) { // if yes, check if it's closer than the previous closest if (dist < closestAdjacentDistance) { closestAdjacentDistance = dist; closestAdjacent = adjacentUnit; } } } } // if there is something close enough, shoot it. Else, set the target to null. if (closestAdjacent != null) { unit.SetShootingTarget(closestAdjacent); } else { unit.SetShootingTarget(null); } } #endregion #region Check targets for friendly turrets foreach (var turret in mFriendlyDefensePlatforms) { // iterate through each friendly turret, if there's a target nearby, shoot it. // get all the adjacent units var adjacentUnits = mUnitMap?.GetAdjacentUnits(turret.AbsolutePosition); // remember the closest adjacent unit. ICollider closestAdjacent = null; var closestAdjacentDistance = 1500f; // a separate variable is used so that it can be initalized with a very big value. if (adjacentUnits == null) { continue; } // iterate through all adjacent units to find the closest adjacent unit. foreach (var adjacentUnit in adjacentUnits) { // only calculate the distance to the adjacent unit if the unit is not friendly. if (!adjacentUnit.Friendly) { // calculate the distance var dist = Geometry.GetQuickDistance(turret.AbsolutePosition, adjacentUnit.AbsolutePosition); // check if it's within range if (dist < turret.Range) { // if yes, check if it's closer than the previous closest if (dist < closestAdjacentDistance) { closestAdjacentDistance = dist; closestAdjacent = adjacentUnit; } } } } // if there is something close enough, shoot it. Else, set the target to null. if (closestAdjacent != null) { turret.SetShootingTarget(closestAdjacent); } else { turret.SetShootingTarget(null); } } #endregion #region Check targets for hostile units foreach (var unit in mHostileMilitary) { // iterate through each hostile unit, if there's a target nearby, shoot it. // get all the adjacent units var adjacentUnits = mUnitMap?.GetAdjacentUnits(unit.AbsolutePosition); // remember the closest adjacent unit. ICollider closestAdjacent = null; var closestAdjacentDistance = 1500f; // a separate variable is used so that it can be initalized with a very big value. if (adjacentUnits == null) { continue; } // iterate through all adjacent units to find the closest adjacent unit. foreach (var adjacentUnit in adjacentUnits) { // only calculate the distance to the adjacent unit if the unit is friendly. if (adjacentUnit.Friendly) { // calculate the distance var dist = Geometry.GetQuickDistance(unit.AbsolutePosition, adjacentUnit.AbsolutePosition); // check if it's within range if (dist < unit.Range) { // if yes, check if it's closer than the previous closest if (dist < closestAdjacentDistance) { closestAdjacentDistance = dist; closestAdjacent = adjacentUnit; } } } } // if there is something close enough, shoot it. Else, set the target to null. if (closestAdjacent != null) { var platform = closestAdjacent as PlatformBlank; if (platform != null && !platform.GetBluePrintStatus()) { unit.SetShootingTarget(closestAdjacent); } else if (platform == null) { unit.SetShootingTarget(closestAdjacent); } } else { unit.SetShootingTarget(null); } } #endregion #region Check targets for hostile turrets foreach (var turret in mHostileDefensePlatforms) { // iterate through each friendly turret, if there's a target nearby, shoot it. // get all the adjacent units var adjacentUnits = mUnitMap?.GetAdjacentUnits(turret.AbsolutePosition); // remember the closest adjacent unit. ICollider closestAdjacent = null; var closestAdjacentDistance = 1500f; // a separate variable is used so that it can be initalized with a very big value. if (adjacentUnits == null) { continue; } // iterate through all adjacent units to find the closest adjacent unit. foreach (var adjacentUnit in adjacentUnits) { // only calculate the distance to the adjacent unit if the unit is friendly. if (adjacentUnit.Friendly) { // calculate the distance var dist = Geometry.GetQuickDistance(turret.AbsolutePosition, adjacentUnit.AbsolutePosition); // check if it's within range if (dist < turret.Range) { // if yes, check if it's closer than the previous closest if (dist < closestAdjacentDistance) { closestAdjacentDistance = dist; closestAdjacent = adjacentUnit; } } } } // if there is something close enough, shoot it. Else, set the target to null. if (closestAdjacent != null) { turret.SetShootingTarget(closestAdjacent); } else { turret.SetShootingTarget(null); } } #endregion #region Flocking adding if (mSelected.Count > 0) { mIsSelected = true; mSelectedGroup.Reset(); mSelected.ForEach(u => mSelectedGroup.AssignUnit(u)); mGroups.Add(mSelectedGroup); } else if (mIsSelected) { mIsSelected = false; mSelectedGroup = new FlockingGroup(ref mDirector, ref mMap); } mSelected = new List <IFlocking>(); mGroups.RemoveAll(g => g.Die()); mGroups.ForEach(g => g.Update(gametime)); #endregion }