-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add feature to run during physics process
- Added a new feature that allows the Update() method of a coroutine to be run in physics frames instead of regular process frames - Refactored the code of the CoroutineManager (DRY)
- Loading branch information
Showing
5 changed files
with
141 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
namespace HCoroutines; | ||
|
||
/// <summary> | ||
/// The ProcessMode determines when the Update() method of a coroutine is called. | ||
/// </summary> | ||
public enum CoProcessMode { | ||
/// <summary> | ||
/// The ProcessMode is inherited from the parent coroutine. If there is no parent, then the Normal mode is used. | ||
/// </summary> | ||
Inherit, | ||
|
||
/// <summary> | ||
/// The Update() method is called during each process frame (like the _Process() method). | ||
/// </summary> | ||
Normal, | ||
|
||
/// <summary> | ||
/// The Update() method is called during each physics frame (like the _PhysicsProcess() method). | ||
/// </summary> | ||
Physics | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
using System.Collections.Generic; | ||
|
||
namespace HCoroutines.Util; | ||
|
||
/// <summary> | ||
/// HashSet implementation that defers Add() and Remove() calls during iteration. | ||
/// </summary> | ||
public class DeferredHashSet<T> | ||
{ | ||
public HashSet<T> Items = new(); | ||
|
||
private bool isIterating = false; | ||
private HashSet<T> itemsToAdd = new(); | ||
private HashSet<T> itemsToRemove = new(); | ||
|
||
public void Add(T item) | ||
{ | ||
if (isIterating) | ||
{ | ||
itemsToAdd.Add(item); | ||
itemsToRemove.Remove(item); | ||
} | ||
else | ||
{ | ||
Items.Add(item); | ||
} | ||
} | ||
|
||
public void Remove(T item) | ||
{ | ||
if (isIterating) | ||
{ | ||
itemsToRemove.Add(item); | ||
itemsToAdd.Remove(item); | ||
} | ||
else | ||
{ | ||
Items.Remove(item); | ||
} | ||
} | ||
|
||
public void Lock() | ||
{ | ||
isIterating = true; | ||
} | ||
|
||
public void Unlock() | ||
{ | ||
isIterating = false; | ||
ExecutePendingAddRemoves(); | ||
} | ||
|
||
private void ExecutePendingAddRemoves() | ||
{ | ||
foreach (T item in itemsToAdd) | ||
{ | ||
Items.Add(item); | ||
} | ||
itemsToAdd.Clear(); | ||
|
||
foreach (T item in itemsToRemove) | ||
{ | ||
Items.Remove(item); | ||
} | ||
itemsToRemove.Clear(); | ||
} | ||
} |