Skip to content

Commit 44ffa13

Browse files
committed
feat: Improve adjust timer
1 parent 386e961 commit 44ffa13

File tree

9 files changed

+114
-81
lines changed

9 files changed

+114
-81
lines changed

Assets/JCSUnity/Scripts/Actions/2D/AI/AIMoveAction/JCS_2DFlyAction.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -91,21 +91,21 @@ public enum StatusY
9191
[Tooltip("Time to do one Fly.")]
9292
[SerializeField]
9393
[Range(0.0f, 10.0f)]
94-
private float mTimeZone = 2.0f;
94+
private float mTime = 2.0f;
9595

96-
[Tooltip("Time that will randomly affect the Time Zone.")]
96+
[Tooltip("Time that will randomly affect the time.")]
9797
[SerializeField]
9898
[Range(0.0f, 3.0f)]
99-
private float mAdjustTimeZone = 1.5f;
99+
private float mAdjustTime = 1.5f;
100100

101101
// time to record down the real time to do one fly action after we
102102
// calculate the real time.
103-
private float mRealTimeZone = 0.0f;
103+
private float mRealTime = 0.0f;
104104

105105
// timer to do fly.
106106
private float mTimer = 0.0f;
107107

108-
// check to see if we can reset our time zone.
108+
// check to see if we can reset our time.
109109
private bool mFlyed = false;
110110

111111
[Tooltip("Type of the delta time.")]
@@ -455,7 +455,7 @@ private void DoFly()
455455

456456
mTimer += JCS_Time.ItTime(mTimeType);
457457

458-
if (mTimer < mRealTimeZone)
458+
if (mTimer < mRealTime)
459459
return;
460460

461461
FlyByPossiblity();
@@ -466,8 +466,8 @@ private void DoFly()
466466
/// </summary>
467467
private void ResetTimeZone()
468468
{
469-
float adjustTime = JCS_Random.Range(-mAdjustTimeZone, mAdjustTimeZone);
470-
mRealTimeZone = mTimeZone + adjustTime;
469+
float adjustTime = JCS_Random.Range(-mAdjustTime, mAdjustTime);
470+
mRealTime = mTime + adjustTime;
471471

472472
mFlyed = false;
473473
mTimer = 0;

Assets/JCSUnity/Scripts/Actions/2D/AI/AIMoveAction/JCS_2DJumpAction.cs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,21 +39,21 @@ public class JCS_2DJumpAction : JCS_AIAction
3939
[Tooltip("Time to do one jump.")]
4040
[SerializeField]
4141
[Range(0.0f, 10.0f)]
42-
private float mTimeZone = 2.0f;
42+
private float mTime = 2.0f;
4343

44-
[Tooltip("Time that will randomly affect the time zone.")]
44+
[Tooltip("Time that will randomly affect the time.")]
4545
[SerializeField]
4646
[Range(0.0f, 3.0f)]
47-
private float mAdjustTimeZone = 1.5f;
47+
private float mAdjustTime = 1.5f;
4848

4949
// time to record down the real time to do one jump after we calculate
5050
// the real time.
51-
private float mRealTimeZone = 0;
51+
private float mRealTime = 0;
5252

5353
// timer to do jump.
5454
private float mTimer = 0;
5555

56-
// check to see if we can reset our time zone.
56+
// check to see if we can reset our time.
5757
private bool mJumped = false;
5858

5959
[Tooltip("Type of the delta time.")]
@@ -100,8 +100,8 @@ public class JCS_2DJumpAction : JCS_AIAction
100100

101101
// Action Variables
102102
public float JumpForce { get { return this.mJumpForce; } set { this.mJumpForce = value; } }
103-
public float AdjustTimeZone { get { return this.mAdjustTimeZone; } set { this.mAdjustTimeZone = value; } }
104-
public float TimeZone { get { return this.mTimeZone; } set { this.mTimeZone = value; } }
103+
public float AdjustTime { get { return this.mAdjustTime; } set { this.mAdjustTime = value; } }
104+
public float Time { get { return this.mTime; } set { this.mTime = value; } }
105105
public float Possibility { get { return this.mPossibility; } set { this.mPossibility = value; } }
106106
public JCS_TimeType DeltaTimeType { get { return this.mTimeType; } set { this.mTimeType = value; } }
107107

@@ -175,7 +175,7 @@ public void Jump(float force)
175175
// do animation
176176
mLiveObjectAnimator.DoAnimation(JCS_LiveObjectState.JUMP);
177177

178-
// next frame re-calculate the time zone.
178+
// next frame re-calculate the time.
179179
mJumped = true;
180180

181181
// start check if grounded and end the jump animation.
@@ -193,7 +193,7 @@ private void DoJump()
193193

194194
mTimer += JCS_Time.ItTime(mTimeType);
195195

196-
if (mTimer < mRealTimeZone)
196+
if (mTimer < mRealTime)
197197
return;
198198

199199
JumpByPossibility();
@@ -204,8 +204,8 @@ private void DoJump()
204204
/// </summary>
205205
private void ResetTimeZone()
206206
{
207-
float adjustTime = JCS_Random.Range(-mAdjustTimeZone, mAdjustTimeZone);
208-
mRealTimeZone = mTimeZone + adjustTime;
207+
float adjustTime = JCS_Random.Range(-mAdjustTime, mAdjustTime);
208+
mRealTime = mTime + adjustTime;
209209

210210
mJumped = false;
211211
mTimer = 0;

Assets/JCSUnity/Scripts/Actions/2D/AI/AIMoveAction/JCS_2DWalkAction.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,21 +63,21 @@ public enum Status
6363
[Tooltip("Time to do one walk.")]
6464
[SerializeField]
6565
[Range(0.0f, 10.0f)]
66-
private float mTimeZone = 2.0f;
66+
private float mTime = 2.0f;
6767

68-
[Tooltip("Time that will randomly affect the Time Zone.")]
68+
[Tooltip("Time that will randomly affect the time.")]
6969
[SerializeField]
7070
[Range(0.0f, 3.0f)]
71-
private float mAdjustTimeZone = 1.5f;
71+
private float mAdjustTime = 1.5f;
7272

7373
// time to record down the real time to do one walk
7474
// action after we calculate the real time.
75-
private float mRealTimeZone = 0;
75+
private float mRealTime = 0;
7676

7777
// timer to do walk.
7878
private float mTimer = 0;
7979

80-
// check to see if we can reset our time zone.
80+
// check to see if we can reset our time.
8181
private bool mWalked = false;
8282

8383
[Tooltip("Type of the delta time.")]
@@ -335,7 +335,7 @@ private void DoWalk()
335335

336336
mTimer += JCS_Time.ItTime(mTimeType);
337337

338-
if (mTimer < mRealTimeZone)
338+
if (mTimer < mRealTime)
339339
return;
340340

341341
WalkByPossiblity();
@@ -346,8 +346,8 @@ private void DoWalk()
346346
/// </summary>
347347
private void ResetTimeZone()
348348
{
349-
float adjustTime = JCS_Random.Range(-mAdjustTimeZone, mAdjustTimeZone);
350-
mRealTimeZone = mTimeZone + adjustTime;
349+
float adjustTime = JCS_Random.Range(-mAdjustTime, mAdjustTime);
350+
mRealTime = mTime + adjustTime;
351351

352352
mWalked = false;
353353
mTimer = 0;

Assets/JCSUnity/Scripts/Actions/2D/Shooting/JCS_ShootAllAngle.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,10 @@ public class JCS_ShootAllAngle : MonoBehaviour, JCS_IAction
4848
[Range(0.01f, 15.0f)]
4949
private float mDelayTime = 1.0f;
5050

51-
[Tooltip("Time that will randomly affect the time zone.")]
51+
[Tooltip("Time that will randomly affect the time.")]
5252
[SerializeField]
5353
[Range(0.0f, 3.0f)]
54-
private float mAdjustTimeZone = 1.5f;
54+
private float mAdjustTime = 1.5f;
5555

5656
[Tooltip("Axis the bullet shoots.")]
5757
[SerializeField]
@@ -61,7 +61,7 @@ public class JCS_ShootAllAngle : MonoBehaviour, JCS_IAction
6161

6262
private bool mShooted = false;
6363

64-
private float mRealTimeZone = 0.0f;
64+
private float mRealTime = 0.0f;
6565

6666
private float mCount = 0.0f;
6767

@@ -76,7 +76,7 @@ public class JCS_ShootAllAngle : MonoBehaviour, JCS_IAction
7676
public bool AutoShootByOrder { get { return this.mAutoShootByOrder; } set { this.mAutoShootByOrder = value; } }
7777
public float DegreePerShoot { get { return this.mDegreePerShoot; } set { this.mDegreePerShoot = value; } }
7878
public float DelayTime { get { return this.mDelayTime; } set { this.mDelayTime = value; } }
79-
public float AdjustTimeZone { get { return this.mAdjustTimeZone; } set { this.mAdjustTimeZone = value; } }
79+
public float AdjustTime { get { return this.mAdjustTime; } set { this.mAdjustTime = value; } }
8080
public JCS_Axis ShootAxis { get { return this.mShootAxis; } set { this.mShootAxis = value; } }
8181
public JCS_TimeType DeltaTimeType { get { return this.mTimeType; } set { this.mTimeType = value; } }
8282

@@ -182,7 +182,7 @@ private void AutoShootActionByFrame()
182182

183183
mDelayTimer += JCS_Time.ItTime(mTimeType);
184184

185-
if (mRealTimeZone < mDelayTimer)
185+
if (mRealTime < mDelayTimer)
186186
{
187187
ShootAllAngleByFrame();
188188

@@ -204,7 +204,7 @@ private void AutoShootActionByOrder()
204204

205205
mDelayTimer += JCS_Time.ItTime(mTimeType);
206206

207-
if (mRealTimeZone < mDelayTimer)
207+
if (mRealTime < mDelayTimer)
208208
{
209209
ShootAllAngleByOrder();
210210

@@ -218,8 +218,8 @@ private void AutoShootActionByOrder()
218218
/// </summary>
219219
private void ResetTimeZone()
220220
{
221-
float adjustTime = JCS_Random.Range(-mAdjustTimeZone, mAdjustTimeZone);
222-
mRealTimeZone = mDelayTime + adjustTime;
221+
float adjustTime = JCS_Random.Range(-mAdjustTime, mAdjustTime);
222+
mRealTime = mDelayTime + adjustTime;
223223

224224
mShooted = false;
225225
mDelayTimer = 0;

Assets/JCSUnity/Scripts/Actions/JCS_AdjustTimeTrigger.cs

Lines changed: 45 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public class JCS_AdjustTimeTrigger : MonoBehaviour
2020
{
2121
/* Variables */
2222

23-
// action to trigger if the time is reached.
23+
// Action to trigger if the time is reached.
2424
public Action onAction = null;
2525

2626
[Separator("Check Variables (JCS_AdjustTimeTrigger)")]
@@ -29,9 +29,9 @@ public class JCS_AdjustTimeTrigger : MonoBehaviour
2929
we calculate the real time.")]
3030
[SerializeField]
3131
[ReadOnly]
32-
private float mRealTimeZone = 0.0f;
32+
private float mRealTime = 0.0f;
3333

34-
[Tooltip("Timer to check if reach the real time zone.")]
34+
[Tooltip("Timer to check if reach the real time.")]
3535
[SerializeField]
3636
[ReadOnly]
3737
private float mTimer = 0.0f;
@@ -43,9 +43,9 @@ public class JCS_AdjustTimeTrigger : MonoBehaviour
4343

4444
[Separator("Initialize Variables (JCS_AdjustTimeTrigger)")]
4545

46-
[Tooltip("Run immediately on start.")]
46+
[Tooltip("Run immediately on the first frame.")]
4747
[SerializeField]
48-
private bool mRunImmediate = true;
48+
private bool mInvokeOnStart = true;
4949

5050
[Separator("Runtime Variables (JCS_AdjustTimeTrigger)")]
5151

@@ -56,12 +56,12 @@ public class JCS_AdjustTimeTrigger : MonoBehaviour
5656
[Tooltip("Time to trigger the event.")]
5757
[SerializeField]
5858
[Range(0.0f, 30.0f)]
59-
private float mTimeZone = 2.0f;
59+
private float mTime = 2.0f;
6060

6161
[Tooltip("Time that will randomly affect the time.")]
6262
[SerializeField]
6363
[Range(0.0f, 20.0f)]
64-
private float mAdjustTimeZone = 1.5f;
64+
private float mAdjustTime = 1.5f;
6565

6666
[Tooltip("Type of the delta time.")]
6767
[SerializeField]
@@ -73,10 +73,10 @@ public class JCS_AdjustTimeTrigger : MonoBehaviour
7373

7474
/* Setter & Getter */
7575

76-
public bool RunImmediate { get { return this.mRunImmediate; } set { this.mRunImmediate = value; } }
76+
public bool InvokeOnStart { get { return this.mInvokeOnStart; } set { this.mInvokeOnStart = value; } }
7777
public bool Active { get { return this.mActive; } set { this.mActive = value; } }
78-
public float TimeZone { get { return this.mTimeZone; } set { this.mTimeZone = value; } }
79-
public float AdjustTimeZone { get { return this.mAdjustTimeZone; } set { this.mAdjustTimeZone = value; } }
78+
public float Time { get { return this.mTime; } set { this.mTime = value; } }
79+
public float AdjustTime { get { return this.mAdjustTime; } set { this.mAdjustTime = value; } }
8080
public JCS_TimeType DeltaTimeType { get { return this.mTimeType; } set { this.mTimeType = value; } }
8181
public UnityEvent OnAction { get { return this.mOnAction; } set { this.mOnAction = value; } }
8282

@@ -85,7 +85,7 @@ public class JCS_AdjustTimeTrigger : MonoBehaviour
8585
private void Start()
8686
{
8787
// Run immediately on the first frame.
88-
if (mRunImmediate)
88+
if (mInvokeOnStart)
8989
Invoke(nameof(ExecuteAction), JCS_Constants.FIRST_FRAME_INVOKE_TIME);
9090
}
9191

@@ -98,13 +98,39 @@ private void Update()
9898
}
9999

100100
/// <summary>
101-
/// Calculate the time to do event once.
101+
/// Reset time and timer, then run the action once.
102102
/// </summary>
103-
public void ResetTimeZone()
103+
public void ResetAndRun()
104104
{
105-
float adjustTime = JCS_Random.Range(-mAdjustTimeZone, mAdjustTimeZone);
106-
mRealTimeZone = mTimeZone + adjustTime;
105+
RecalculateTimeAndResetTimer();
107106

107+
ExecuteAction();
108+
}
109+
110+
/// <summary>
111+
/// Recalculate the time and reset the timer.
112+
/// </summary>
113+
public void RecalculateTimeAndResetTimer()
114+
{
115+
RecalculateTime();
116+
117+
ResetTimer();
118+
}
119+
120+
/// <summary>
121+
/// Recalculate the real time.
122+
/// </summary>
123+
public void RecalculateTime()
124+
{
125+
float adjustTime = JCS_Random.Range(-mAdjustTime, mAdjustTime);
126+
mRealTime = mTime + adjustTime;
127+
}
128+
129+
/// <summary>
130+
/// Reset the timer.
131+
/// </summary>
132+
public void ResetTimer()
133+
{
108134
mDidAction = false;
109135
mTimer = 0.0f;
110136
}
@@ -116,11 +142,13 @@ public void ResetTimeZone()
116142
private void DoAction()
117143
{
118144
if (mDidAction)
119-
ResetTimeZone();
145+
{
146+
RecalculateTimeAndResetTimer();
147+
}
120148

121149
mTimer += JCS_Time.ItTime(mTimeType);
122150

123-
if (mRealTimeZone > mTimer)
151+
if (mRealTime > mTimer)
124152
return;
125153

126154
ExecuteAction();

Assets/JCSUnity/Scripts/Actions/Spawning/JCS_BasicWaveSpawner.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -233,16 +233,16 @@ public void SpawnATransform()
233233
/// </summary>
234234
private void DoSpawnRandomTransform()
235235
{
236-
// reset time zone everytime if spawned
236+
// reset time everytime if spawned
237237
if (mSpawned)
238-
ResetTimeZone();
238+
RecalculateTimeAndResetTimer();
239239

240240
mSpawnTimer += JCS_Time.ItTime(mTimeType);
241241

242242
if (mSpawnTimer < mRealSpawnTime)
243243
return;
244244

245-
// turn on the trigger so it will reset time zone.
245+
// turn on the trigger so it will reset time.
246246
mSpawned = true;
247247

248248
SpawnATransform();
@@ -262,9 +262,9 @@ private void ResizeSpawnList()
262262
}
263263

264264
/// <summary>
265-
/// Re-calculate the real time zone.
265+
/// Recalculate the real time.
266266
/// </summary>
267-
private void ResetTimeZone()
267+
private void RecalculateTimeAndResetTimer()
268268
{
269269
float adjustTime = JCS_Random.Range(-mRandomizeSpawnTime, mRandomizeSpawnTime);
270270
mRealSpawnTime = mSpawnTime + adjustTime;

0 commit comments

Comments
 (0)