Skip to content

Commit

Permalink
Remove some more uses of IntervalTimer
Browse files Browse the repository at this point in the history
  • Loading branch information
12joan committed Sep 17, 2024
1 parent b31e6b2 commit 0ac8e9c
Show file tree
Hide file tree
Showing 12 changed files with 142 additions and 201 deletions.
41 changes: 17 additions & 24 deletions Assets/src/DeathAnimation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using UnityEngine;

public class DeathAnimation : MonoBehaviour {

public float FadeOutDelay;
public float FadeOutDuration;
public SpriteRenderer SpriteRenderer;
Expand All @@ -13,43 +12,37 @@ public class DeathAnimation : MonoBehaviour {
public List<ParticleSystem> ParticleSystems;
public SpawnFlyingRingPull SpawnFlyingRingPull;

private IntervalTimer Timer;
private Stopwatch Stopwatch = null;
private Color InitialColor;

void Awake() {
Timer = new IntervalTimer() {
Interval = FadeOutDuration
};
}

public void Play() {
InitialColor = SpriteRenderer.color;

ParticleSystems.ForEach(x => x.Play());
Invoke("StartFadeOut", FadeOutDelay);
Invoke("DestroyGameObject", DestroyTime);
AsyncTimer.BaseTime.SetTimeout(StartFadeOut, FadeOutDelay);
AsyncTimer.BaseTime.SetTimeout(DestroyGameObject, DestroyTime);

if (SpawnFlyingRingPull != null)
SpawnFlyingRingPull.Instantiate();
}

void StartFadeOut() {
Timer.Reset();
}

void Update() {
if (Timer.Started) {
SpriteRenderer.color = new Color(
InitialColor.r,
InitialColor.g,
InitialColor.b,
Mathf.Clamp(1f - Timer.Progress(), 0, InitialColor.a)
);
}
private void StartFadeOut() {
Stopwatch = new Stopwatch.BaseTime();
}

void DestroyGameObject() {
private void DestroyGameObject() {
Destroy(GameObject);
}

void Update() {
if (Stopwatch == null)
return;

SpriteRenderer.color = new Color(
InitialColor.r,
InitialColor.g,
InitialColor.b,
Mathf.Clamp(1f - Stopwatch.Progress(FadeOutDuration), 0, InitialColor.a)
);
}
}
19 changes: 9 additions & 10 deletions Assets/src/DelayPhase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,12 @@ public class DelayPhase : APhase {
);
public UnityEvent OnBegin, OnFinish;

private IntervalTimer Timer;
private Stopwatch Stopwatch;

public override void LocalBegin() {
Timer = new IntervalTimer() {
TimeClass = UsePlayingTime ? "PlayingTime" : "Time",
Interval = Delay
};
Stopwatch = UsePlayingTime
? new Stopwatch.PlayingTime()
: new Stopwatch.BaseTime();

#if UNITY_EDITOR
Debug.Assert(!(UsePlayingTime && PauseGameplay), "DelayPhase cannot use playing time AND pause gameplay");
Expand All @@ -29,23 +28,23 @@ public override void LocalBegin() {
if (PauseGameplay)
StateManager.AddState(State.NotPlaying);

Timer.Reset();

DialogueMusicFadeBehaviour.ApplyEnterBehaviour();
OnBegin.Invoke();
}

public override void WhilePhaseRunning() {
float progress = Stopwatch.Progress(Delay);

if (HUDBar != null)
HUDBar.Progress = Timer.Progress();
HUDBar.Progress = progress;

Timer.IfElapsed(() => {
if (progress >= 1f) {
if (PauseGameplay)
StateManager.RemoveState(State.NotPlaying);

DialogueMusicFadeBehaviour.ApplyExitBehaviour();
OnFinish.Invoke();
PhaseFinished();
});
};
}
}
19 changes: 9 additions & 10 deletions Assets/src/DialogueManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,19 @@ public class DialogueManager : MonoBehaviour {
private bool Open = false;
private Action OnFinish;
private ButtonPressHelper ButtonPressHelper = new MultipleButtonPressHelper();
private IntervalTimer ContinuePromptTimer = new IntervalTimer();
private AsyncTimer.EnqueuedEvent ContinuePromptTimer;

void Awake() {
if (SingletonInstance)
Current = this;

ContinuePromptTimer = new IntervalTimer() {
Interval = ContinuePromptDelay
};

DialogueBox.Hide();

DialogueBox.OnFinished.AddListener(() => ContinuePromptTimer.Reset());
DialogueBox.OnFinished.AddListener(() => {
ContinuePromptTimer = AsyncTimer.BaseTime.SetTimeout(() => {
DialogueBox.SetContinuePromptVisible(true);
}, ContinuePromptDelay);
});
}

public void Play(DialogueSequence dialogueSequence, Action onFinish) {
Expand Down Expand Up @@ -64,13 +64,14 @@ void Update() {
if (Debug.isDebugBuild && ButtonPressHelper.GetButtonPress("cancel")) {
Exit();
}

DialogueBox.SetContinuePromptVisible(ContinuePromptTimer.Elapsed());
}

void NextPage() {
CurrentPageIndex += 1;

DialogueBox.SetContinuePromptVisible(false);
AsyncTimer.BaseTime.ClearTimeout(ContinuePromptTimer);

if (CurrentPageIndex >= DialogueSequence.PageCount) {
Exit();
} else {
Expand Down Expand Up @@ -100,8 +101,6 @@ void ShowCurrentPage() {
}
}, CurrentPage.AutoAdvanceDelay);
}

ContinuePromptTimer.Stop();
}

void Exit() {
Expand Down
26 changes: 6 additions & 20 deletions Assets/src/Explosion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,14 @@
using UnityEngine.UI;

public class Explosion : MonoBehaviour {

public Image Image;
public float Duration;
public GameObject ExplosionGameObject;

IntervalTimer Timer;
private Stopwatch Stopwatch;

void Start() {
Timer = new IntervalTimer() {
Interval = Duration
};

Timer.Reset();
Stopwatch = new Stopwatch.BaseTime();
}

void Update() {
Expand All @@ -26,20 +21,11 @@ void Update() {
colour.a = Alpha();
Image.color = colour;

if (Timer.Elapsed())
if (Progress() >= 1f)
Destroy(ExplosionGameObject);
}

float Scale() {
return Mathf.Lerp(0.2f, 1f, Progress());
}

float Alpha() {
return Mathf.Lerp(1f, 0.75f, Progress());
}

float Progress() {
return Timer.TimeSinceElapsed() / Duration;
}

private float Scale() => Mathf.Lerp(0.2f, 1f, Progress());
private float Alpha() => Mathf.Lerp(1f, 0.75f, Progress());
private float Progress() => Stopwatch.Progress(Duration);
}
19 changes: 10 additions & 9 deletions Assets/src/FadeImage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,26 @@ public class FadeImage : MonoBehaviour {
public float Duration;
public AnimationCurve AnimationCurve;

private IntervalTimer Timer;
private Stopwatch Stopwatch;

void Start() {
Timer = new IntervalTimer() {
Interval = Duration
};

if (PerformOnStart)
Perform();
}

public void Perform() {
Timer.Reset();
Stopwatch = new Stopwatch.BaseTime();
}

void Update() {
if (Timer.Started) {
Image.color = new Color(1, 1, 1, AnimationCurve.Evaluate(Timer.Progress()));
Timer.IfElapsed(() => Timer.Stop());
if (Stopwatch == null)
return;

float progress = Stopwatch.Progress(Duration);
Image.color = new Color(1, 1, 1, AnimationCurve.Evaluate(progress));

if (progress >= 1f) {
Stopwatch = null;
}
}
}
94 changes: 45 additions & 49 deletions Assets/src/GrenadeExplodesAfterTime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using UnityEngine;

public class GrenadeExplodesAfterTime : MonoBehaviour {

public BulletData BulletData;
public Rigidbody2D Rigidbody;
public GrenadeWaitingBeforeThrow GrenadeWaitingBeforeThrow;
Expand All @@ -18,72 +17,69 @@ public class GrenadeExplodesAfterTime : MonoBehaviour {
public GameObject Explosion;
public GameObject ExplosionSound;

private IntervalTimer RadiusIndicatorTimer;
private IntervalTimer ExplodeTimer;
private Stopwatch ExplodeStopwatch;
private Stopwatch RadiusIndicatorStopwatch = null;

void Awake() {
RadiusIndicatorTimer = new IntervalTimer() {
TimeClass = "PlayingTime",
Interval = RadiusIndicatorTime
};

ExplodeTimer = new IntervalTimer() {
TimeClass = "PlayingTime",
Interval = ExplodeTime
};

ExplodeTimer.Reset();
ExplodeStopwatch = new Stopwatch.PlayingTime();
}

void Update() {
if (!StateManager.Playing)
return;

if (!RadiusIndicatorTimer.Started && !WaitingToThrow() && ExplodeTimer.Progress() >= 0.50) {
RadiusIndicatorTimer.Reset();
if (RadiusIndicatorStopwatch == null) {
if (!WaitingToThrow() && ExplodeProgress() > 0.5f) {
RadiusIndicatorStopwatch = new Stopwatch.PlayingTime();
}
} else {
SetRadiusIndicatorRadius(Radius * RadiusIndicatorProgress());
}

if (RadiusIndicatorTimer.Started) {
SetRadiusIndicatorRadius(Radius * RadiusIndicatorTimer.Progress());
if (ExplodeProgress() >= 1f) {
Explode();
}
}

ExplodeTimer.IfElapsed(() => {
bool isCounterAttack = DamageHitPointsInRadius.Invoke(
damage: WaitingToThrow() ? DamageExplodingInHand : BulletData.Damage,
origin: transform.position,
radius: Radius,
canBeCounterAttacked: true,
damageCurve: DamageCurve,
shouldDamage: (go) => {
if (!DamagesPlayer && go.tag == "Player")
return false;
if (!DamagesEnemies && go.tag == "Enemy")
return false;
return true;
}
);
private void Explode() {
bool isCounterAttack = DamageHitPointsInRadius.Invoke(
damage: WaitingToThrow() ? DamageExplodingInHand : BulletData.Damage,
origin: transform.position,
radius: Radius,
canBeCounterAttacked: true,
damageCurve: DamageCurve,
shouldDamage: (go) => {
if (!DamagesPlayer && go.tag == "Player")
return false;
if (!DamagesEnemies && go.tag == "Enemy")
return false;
return true;
}
);

// Send grenade back to originator on counterattack
if (isCounterAttack && BulletData.Originator && BulletData.Originator != PlayerGameObject.Current) {
Vector3 toOriginator = (BulletData.Originator.transform.position - transform.position);
Rigidbody.velocity = toOriginator * VelocityCoefficient;
DamagesEnemies = true;
ExplodeStopwatch.Reset();
return;
}

if (isCounterAttack && BulletData.Originator && BulletData.Originator != PlayerGameObject.Current) {
Vector3 toOriginator = (BulletData.Originator.transform.position - transform.position);
Rigidbody.velocity = toOriginator * VelocityCoefficient;
DamagesEnemies = true;
ExplodeTimer.Reset();
} else {
GameObject ExplosionGameObject = Instantiate(Explosion, transform.position, Quaternion.identity);
ExplosionGameObject.transform.localScale = new Vector3(2 * Radius, 2 * Radius, 2 * Radius);
GameObject ExplosionGameObject = Instantiate(Explosion, transform.position, Quaternion.identity);
ExplosionGameObject.transform.localScale = new Vector3(2 * Radius, 2 * Radius, 2 * Radius);

if (ExplosionSound != null)
Instantiate(ExplosionSound, transform.position, Quaternion.identity);
if (ExplosionSound != null)
Instantiate(ExplosionSound, transform.position, Quaternion.identity);

Destroy(gameObject);
}
});
Destroy(gameObject);
}

bool WaitingToThrow() => (GrenadeWaitingBeforeThrow != null) && GrenadeWaitingBeforeThrow.Waiting;
private float ExplodeProgress() => ExplodeStopwatch.Progress(ExplodeTime);
private float RadiusIndicatorProgress() => RadiusIndicatorStopwatch.Progress(RadiusIndicatorTime);
private bool WaitingToThrow() => (GrenadeWaitingBeforeThrow != null) && GrenadeWaitingBeforeThrow.Waiting;

void SetRadiusIndicatorRadius(float radius) {
private void SetRadiusIndicatorRadius(float radius) {
RadiusIndicator.localScale = new Vector3(2 * radius, 2 * radius, 2 * radius);
}

}
Loading

0 comments on commit 0ac8e9c

Please sign in to comment.