Skip to content

Commit

Permalink
Fixed issue where units could not despawn at the end of a patrol
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelday008 committed Oct 10, 2019
1 parent a945c9e commit 180a7ab
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Characters/AI/AIPatrol.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public Vector3 GetDestination(bool destinationReached) {
}

public bool PatrolComplete() {
//Debug.Log("AIPatrol.PatrolComplete()");
Debug.Log("AIPatrol.PatrolComplete(): loopDestination: " + loopDestinations + "; destinationReachedCount: " + destinationReachedCount + "; maxDestinations: " + maxDestinations + "; destinationListCount: " + destinationList.Count);

if (randomDestinations && maxDestinations == 0) {
//Debug.Log("AIPatrol.PatrolComplete() randomDestinations && maxDestinations == 0; return false");
Expand Down
6 changes: 3 additions & 3 deletions Characters/AI/States/PatrolState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,18 @@ public void Update() {
}

if (Vector3.Distance(aiController.MyBaseCharacter.MyCharacterUnit.transform.position, currentDestination) <= aiController.MyBaseCharacter.MyCharacterUnit.MyAgent.stoppingDistance + aiController.MyBaseCharacter.MyCharacterUnit.MyCharacterMotor.MyNavMeshDistancePadding) {
//Debug.Log(aiController.gameObject.name + ".PatrolState.Update(): Destination Reached!");
Debug.Log(aiController.gameObject.name + ".PatrolState.Update(): Destination Reached!");
currentDestination = aiController.MyAiPatrol.GetDestination(true);

// destination reached
if (aiController.MyAiPatrol.PatrolComplete()) {
if (aiController.MyAiPatrol.MyDespawnOnCompletion) {
aiController.MyBaseCharacter.MyCharacterUnit.Despawn();
aiController.MyBaseCharacter.MyCharacterUnit.Despawn(0, false, true);
} else {
aiController.ChangeState(new IdleState());
}
} else {
//Debug.Log(aiController.gameObject.name + ".PatrolState.Update(): Destination Reached and patrol not complete yet!");
Debug.Log(aiController.gameObject.name + ".PatrolState.Update(): Destination Reached and patrol not complete yet!");
coroutine = (aiController as MonoBehaviour).StartCoroutine(PauseForNextDestination(currentDestination));
this.aiController.MyBaseCharacter.MyCharacterUnit.MyCharacterMotor.MyMovementSpeed = this.aiController.MyMovementSpeed;
}
Expand Down
18 changes: 11 additions & 7 deletions Characters/BaseClasses/CharacterUnit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -259,27 +259,31 @@ public override bool SetMiniMapText(Text text) {
return true;
}

public void Despawn(float despawnDelay = 0f) {
//Debug.Log(gameObject.name + ".CharacterUnit.Despawn(" + despawnDelay + ")");
public void Despawn(float despawnDelay = 0f, bool addSystemDefaultTime = true, bool forceDespawn = false) {
Debug.Log(gameObject.name + ".CharacterUnit.Despawn(" + despawnDelay + ", " + addSystemDefaultTime + ", " + forceDespawn + ")");
//gameObject.SetActive(false);
// TEST ADDING A MANDATORY DELAY
if (despawnCoroutine == null) {
StartCoroutine(PerformDespawnDelay(despawnDelay));
StartCoroutine(PerformDespawnDelay(despawnDelay, addSystemDefaultTime, forceDespawn));
}
// we are going to send this ondespawn call now to allow another unit to respawn from a spawn node without a long wait during events that require rapid mob spawning
OnDespawn(gameObject);
}

public IEnumerator PerformDespawnDelay(float despawnDelay) {
public IEnumerator PerformDespawnDelay(float despawnDelay, bool addSystemDefaultTime = true, bool forceDespawn = false) {
// add all possible delays together
float totalDelay = despawnDelay + this.despawnDelay + SystemConfigurationManager.MyInstance.MyDefaultDespawnTimer;
float extraTime = 0f;
if (addSystemDefaultTime) {
extraTime = SystemConfigurationManager.MyInstance.MyDefaultDespawnTimer;
}
float totalDelay = despawnDelay + this.despawnDelay + extraTime;
while (totalDelay > 0f) {
totalDelay -= Time.deltaTime;
yield return null;
}

if (baseCharacter.MyCharacterStats.IsAlive == false) {
// this character could have been ressed while waiting to despawn. don't let it despawn if that happened
if (baseCharacter.MyCharacterStats.IsAlive == false || forceDespawn == true) {
// this character could have been ressed while waiting to despawn. don't let it despawn if that happened unless forceDesapwn is true (such as at the end of a patrol)
Destroy(gameObject);
}
}
Expand Down

0 comments on commit 180a7ab

Please sign in to comment.