Skip to content

Commit

Permalink
#1.2.0-rc2
Browse files Browse the repository at this point in the history
  • Loading branch information
BelicusBr committed Dec 31, 2023
1 parent 5baeb52 commit 141edee
Show file tree
Hide file tree
Showing 34 changed files with 1,160 additions and 1,164 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export_presets.cfg
# Mono-specific ignores
.mono/
.vscode/
*.sln
data_*/
mono_crash.*.json
*.code-workspace
80 changes: 40 additions & 40 deletions GodotEngine.Utility/Coroutine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,54 +2,54 @@
using System.Threading;
using System.Collections;

namespace Cobilas.GodotEngine.Utility {
public sealed class Coroutine : IEnumerable, IDisposable {
private bool disposedValue;
private IEnumerator enumerator;
private CancellationTokenSource source;

public string ID { get; private set; }
public bool IsRunning { get; private set; }
public bool IsCancellationRequested => source.IsCancellationRequested;

public Coroutine(IEnumerator enumerator, string iD) {
ID = iD;
this.enumerator = enumerator;
this.source = new CancellationTokenSource();
}

~Coroutine()
=> Dispose(disposing: false);
namespace Cobilas.GodotEngine.Utility;

public sealed class Coroutine : IEnumerable, IDisposable {
private bool disposedValue;
private IEnumerator enumerator;
private CancellationTokenSource source = new();

public void Cancel()
=> this.source.Cancel();
public string ID { get; private set; }
public bool IsRunning { get; private set; }
public bool IsCancellationRequested => source!.IsCancellationRequested;

public void CancelAfter(TimeSpan delay)
=> this.source.CancelAfter(delay);
public Coroutine(IEnumerator enumerator, string iD) {
this.enumerator = enumerator;
ID = iD;
}

public void CancelAfter(int millisecondsDelay)
=> this.source.CancelAfter(millisecondsDelay);
~Coroutine()
=> Dispose(disposing: false);

public void Dispose() {
Dispose(disposing: true);
GC.SuppressFinalize(this);
}
public void Cancel()
=> this.source.Cancel();

public void CancelAfter(TimeSpan delay)
=> this.source.CancelAfter(delay);

public void CancelAfter(int millisecondsDelay)
=> this.source.CancelAfter(millisecondsDelay);

public void Dispose() {
Dispose(disposing: true);
GC.SuppressFinalize(this);
}

internal void SetStatus(bool status)
=> IsRunning = status;
internal void SetStatus(bool status)
=> IsRunning = status;

IEnumerator IEnumerable.GetEnumerator()
=> this.enumerator;
IEnumerator IEnumerable.GetEnumerator()
=> this.enumerator;

private void Dispose(bool disposing) {
if (!disposedValue) {
if (disposing) {
enumerator = null;
ID = null;
source.Dispose();
}
disposedValue = true;
private void Dispose(bool disposing) {
if (!disposedValue) {
if (disposing) {
enumerator = null!;
ID = null!;
source.Dispose();
source = null!;
}
disposedValue = true;
}
}
}
160 changes: 79 additions & 81 deletions GodotEngine.Utility/CoroutineManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,105 +5,103 @@
using Cobilas.Collections;
using Cobilas.GodotEngine.Utility.Runtime;

namespace Cobilas.GodotEngine.Utility {
[RunTimeInitializationClass(nameof(CoroutineManager))]
public class CoroutineManager : Node {
private CoroutineItem[] waits;
namespace Cobilas.GodotEngine.Utility;

private static CoroutineManager _Coroutine;
private static readonly char[] chars = { 'a', 'b', 'c', 'd', 'e', 'f', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
[RunTimeInitializationClass(nameof(CoroutineManager))]
public class CoroutineManager : Node {
private CoroutineItem[] waits = System.Array.Empty<CoroutineItem>();

public override void _Ready() {
if (_Coroutine == null) {
_Coroutine = this;
}
}
private static CoroutineManager? _Coroutine = null;
private static readonly char[] chars = { 'a', 'b', 'c', 'd', 'e', 'f', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };

public override void _Process(float delta) {
for (int I = 0; I < ArrayManipulation.ArrayLength(waits); I++)
if (!waits[I].IsPhysicsProcess)
if (!waits[I].Run()) {
ArrayManipulation.Remove(I, ref waits);
--I;
}
}
public override void _Ready() {
_Coroutine ??= this;
}

public override void _PhysicsProcess(float delta) {
for (int I = 0; I < ArrayManipulation.ArrayLength(waits); I++)
if (waits[I].IsPhysicsProcess)
if (!waits[I].Run()) {
ArrayManipulation.Remove(I, ref waits);
--I;
}
}
public override void _Process(float delta) {
for (int I = 0; I < ArrayManipulation.ArrayLength(waits); I++)
if (!waits[I].IsPhysicsProcess)
if (!waits[I].Run()) {
ArrayManipulation.Remove(I, ref waits);
--I;
}
}

public static Coroutine StartCoroutine(IEnumerator enumerator) {
Coroutine Coroutine = new Coroutine(enumerator, GenID());
public override void _PhysicsProcess(float delta) {
for (int I = 0; I < ArrayManipulation.ArrayLength(waits); I++)
if (waits[I].IsPhysicsProcess)
if (!waits[I].Run()) {
ArrayManipulation.Remove(I, ref waits);
--I;
}
}

ArrayManipulation.Add(new CoroutineItem(Coroutine), ref _Coroutine.waits);
public static Coroutine StartCoroutine(IEnumerator enumerator) {
Coroutine Coroutine = new Coroutine(enumerator, GenID());

return Coroutine;
}
ArrayManipulation.Add(new CoroutineItem(Coroutine), ref _Coroutine!.waits);

public static void StopCoroutine(Coroutine Coroutine) {
foreach (var item in _Coroutine.waits)
if (item.ID == Coroutine.ID) {
item.Cancel();
break;
}
}
return Coroutine;
}

public static void StopAllCoroutines() {
foreach (var item in _Coroutine.waits)
public static void StopCoroutine(Coroutine Coroutine) {
foreach (var item in _Coroutine!.waits)
if (item.ID == Coroutine.ID) {
item.Cancel();
}
break;
}
}

public static string GenID() {
StringBuilder builder = new StringBuilder();
Random random = new Random();
for (int I = 0; I < 64; I++)
builder.Append(random.Next(0, 50) > 25 ? char.ToUpper(chars[random.Next(0, 15)]) : chars[random.Next(0, 15)]);
return builder.ToString();
}
public static void StopAllCoroutines() {
foreach (var item in _Coroutine!.waits)
item.Cancel();
}

public static string GenID() {
StringBuilder builder = new StringBuilder();
Random random = new Random();
for (int I = 0; I < 64; I++)
builder.Append(random.Next(0, 50) > 25 ? char.ToUpper(chars[random.Next(0, 15)]) : chars[random.Next(0, 15)]);
return builder.ToString();
}

private sealed class CoroutineItem {
private bool init;
private DateTime time;
private Coroutine coroutine;
private sealed class CoroutineItem {
private bool init;
private DateTime time;
private Coroutine coroutine;

public string ID => coroutine.ID;
public bool IsPhysicsProcess {
get {
object obj = (coroutine as IEnumerable).GetEnumerator().Current;
return obj is IYieldFixedUpdate || (obj is IYieldVolatile @volatile && @volatile.IsPhysicsProcess);
}
public string ID => coroutine.ID;
public bool IsPhysicsProcess {
get {
object obj = (coroutine as IEnumerable).GetEnumerator().Current;
return obj is IYieldFixedUpdate || (obj is IYieldVolatile @volatile && @volatile.IsPhysicsProcess);
}
}

public CoroutineItem(Coroutine coroutine) {
this.coroutine = coroutine;
time = DateTime.Now;
}
public CoroutineItem(Coroutine coroutine) {
this.coroutine = coroutine;
time = DateTime.Now;
}

public void Cancel()
=> coroutine.Cancel();
public void Cancel()
=> coroutine.Cancel();

public bool Run() {
if (coroutine.IsCancellationRequested) {
coroutine.SetStatus(false);
return coroutine.IsRunning;
}
bool res = true;
IEnumerator enumerator = (coroutine as IEnumerable).GetEnumerator();
TimeSpan delay = !(enumerator.Current is IYieldCoroutine wait) ? TimeSpan.Zero : wait.Delay;
if (!init) {
res = enumerator.MoveNext();
init = true;
} else if (DateTime.Now > time + delay)
if (res = enumerator.MoveNext())
time = DateTime.Now;
coroutine.SetStatus(res);
return res;
public bool Run() {
if (coroutine.IsCancellationRequested) {
coroutine.SetStatus(false);
return coroutine.IsRunning;
}
bool res = true;
IEnumerator enumerator = (coroutine as IEnumerable).GetEnumerator();
TimeSpan delay = !(enumerator.Current is IYieldCoroutine wait) ? TimeSpan.Zero : wait.Delay;
if (!init) {
res = enumerator.MoveNext();
init = true;
} else if (DateTime.Now > time + delay)
if (res = enumerator.MoveNext())
time = DateTime.Now;
coroutine.SetStatus(res);
return res;
}
}
}
34 changes: 17 additions & 17 deletions GodotEngine.Utility/Extension/Camera2D_GD_CB_Extension.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
using Cobilas.GodotEngine.Utility;

namespace Godot {
public static class Camera2D_GD_CB_Extension {
public static Vector2 ScreenToWorldPoint(this Camera2D C, Vector2 mousePosition) {
Rect2 rect;
Vector2 size = Screen.CurrentResolution * C.Zoom;
mousePosition *= C.Zoom;
if (C.AnchorMode == Camera2D.AnchorModeEnum.DragCenter)
rect = new Rect2(C.Position - size * .5f, size);
else rect = new Rect2(C.Position, size);
return mousePosition + Vector2.Right * rect.Left() + Vector2.Down * rect.Top();
}
namespace Godot;

public static Vector2 WorldToScreenPoint(this Camera2D C, Vector2 position) {
Vector2 rect = C.Position;
if (C.AnchorMode == Camera2D.AnchorModeEnum.DragCenter)
rect = C.Position - (Screen.CurrentResolution * C.Zoom) * .5f;
return position - rect;
}
public static class Camera2D_GD_CB_Extension {
public static Vector2 ScreenToWorldPoint(this Camera2D C, Vector2 mousePosition) {
Rect2 rect;
Vector2 size = Screen.CurrentResolution * C.Zoom;
mousePosition *= C.Zoom;
if (C.AnchorMode == Camera2D.AnchorModeEnum.DragCenter)
rect = new Rect2(C.Position - size * .5f, size);
else rect = new Rect2(C.Position, size);
return mousePosition + Vector2.Right * rect.Left() + Vector2.Down * rect.Top();
}

public static Vector2 WorldToScreenPoint(this Camera2D C, Vector2 position) {
Vector2 rect = C.Position;
if (C.AnchorMode == Camera2D.AnchorModeEnum.DragCenter)
rect = C.Position - (Screen.CurrentResolution * C.Zoom) * .5f;
return position - rect;
}
}
Loading

0 comments on commit 141edee

Please sign in to comment.