File tree
Expand file treeCollapse file tree7 files changed
+33
-42
lines changed Assets/Patterns/14. Command Queue (Event Queue)
Expand file treeCollapse file tree7 files changed
+33
-42
lines changed This file was deleted.
Original file line number | Diff line number | Diff line change |
---|
@@ -5,21 +5,21 @@ namespace CommandQueuePattern
|
5 | 5 | public class CommandQueue
|
6 | 6 | {
|
7 | 7 | // queue of commands
|
8 |
| -private readonly Queue<CommandBase> _queue; |
| 8 | +private readonly Queue<ICommand> _queue; |
9 | 9 |
|
10 | 10 | // it's true when a command is running
|
11 | 11 | private bool _isPending;
|
12 | 12 |
|
13 | 13 | public CommandQueue()
|
14 | 14 | {
|
15 | 15 | // create a queue
|
16 |
| -_queue = new Queue<CommandBase>(); |
| 16 | +_queue = new Queue<ICommand>(); |
17 | 17 |
|
18 | 18 | // no command is running
|
19 | 19 | _isPending = false;
|
20 | 20 | }
|
21 | 21 |
|
22 |
| -public void Enqueue(CommandBase cmd) |
| 22 | +public void Enqueue(ICommand cmd) |
23 | 23 | {
|
24 | 24 | // add a command
|
25 | 25 | _queue.Enqueue(cmd);
|
|
Original file line number | Diff line number | Diff line change |
---|
|
1 | 1 | using System;
|
2 |
| -using System.Collections; |
3 |
| -using System.Collections.Generic; |
4 |
| -using UnityEngine; |
5 | 2 |
|
6 | 3 | namespace CommandQueuePattern
|
7 | 4 | {
|
8 |
| -public class FirstCmd : CommandBase |
| 5 | +public class FirstCmd : ICommand |
9 | 6 | {
|
10 | 7 | private readonly GameController _owner;
|
11 | 8 |
|
12 |
| -public override bool IsFinished { get; protected set; } = false; |
| 9 | +public Action OnFinished { get; set; } |
13 | 10 |
|
14 | 11 | public FirstCmd(GameController owner)
|
15 | 12 | {
|
16 | 13 | _owner = owner;
|
17 | 14 | }
|
18 | 15 |
|
19 |
| -public override void Execute() |
| 16 | +public void Execute() |
20 | 17 | {
|
21 | 18 | // activate gameobject
|
22 | 19 | _owner.firstPopUp.gameObject.SetActive(true);
|
@@ -33,8 +30,7 @@ private void OnClose()
|
33 | 30 | _owner.firstPopUp.gameObject.SetActive(false);
|
34 | 31 |
|
35 | 32 | // rise the OnFinished event to say we're done with this command
|
36 |
| -CallOnFinished(); |
37 |
| -IsFinished = true; |
| 33 | +OnFinished?.Invoke(); |
38 | 34 | }
|
39 | 35 | }
|
40 | 36 | }
|
Original file line number | Diff line number | Diff line change |
---|
|
1 |
| -namespace CommandQueuePattern |
| 1 | +using System; |
| 2 | + |
| 3 | +namespace CommandQueuePattern |
2 | 4 | {
|
3 |
| -public class SecondCmd : CommandBase |
| 5 | +public class SecondCmd : ICommand |
4 | 6 | {
|
5 | 7 | private readonly GameController _owner;
|
6 | 8 |
|
7 |
| -public override bool IsFinished { get; protected set; } = false; |
8 |
| - |
9 | 9 | public SecondCmd(GameController owner)
|
10 | 10 | {
|
11 | 11 | _owner = owner;
|
12 | 12 | }
|
13 | 13 |
|
14 |
| -public override void Execute() |
| 14 | +public Action OnFinished { get; set; } |
| 15 | + |
| 16 | +public void Execute() |
15 | 17 | {
|
16 | 18 | // activate gameobject
|
17 | 19 | _owner.secondPopup.gameObject.SetActive(true);
|
@@ -28,8 +30,7 @@ private void OnClose()
|
28 | 30 | _owner.secondPopup.gameObject.SetActive(false);
|
29 | 31 |
|
30 | 32 | // rise the OnFinished event to say we're done with this command
|
31 |
| -CallOnFinished(); |
32 |
| -IsFinished = true; |
| 33 | +OnFinished?.Invoke(); |
33 | 34 | }
|
34 | 35 | }
|
35 | 36 | }
|
Original file line number | Diff line number | Diff line change |
---|
|
1 |
| -namespace CommandQueuePattern |
| 1 | +using System; |
| 2 | + |
| 3 | +namespace CommandQueuePattern |
2 | 4 | {
|
3 |
| -public class ThirdCmd : CommandBase |
| 5 | +public class ThirdCmd : ICommand |
4 | 6 | {
|
5 | 7 | private readonly GameController _owner;
|
6 | 8 |
|
7 |
| -public override bool IsFinished { get; protected set; } = false; |
| 9 | +public Action OnFinished { get; set; } |
8 | 10 |
|
9 | 11 | public ThirdCmd(GameController owner)
|
10 | 12 | {
|
11 | 13 | _owner = owner;
|
12 | 14 | }
|
13 | 15 |
|
14 |
| -public override void Execute() |
| 16 | +public void Execute() |
15 | 17 | {
|
16 | 18 | // activate gameobject
|
17 | 19 | _owner.thirdPopup.gameObject.SetActive(true);
|
@@ -28,8 +30,7 @@ private void OnClose()
|
28 | 30 | _owner.thirdPopup.gameObject.SetActive(false);
|
29 | 31 |
|
30 | 32 | // rise the OnFinished event to say we're done with this command
|
31 |
| -CallOnFinished(); |
32 |
| -IsFinished = true; |
| 33 | +OnFinished?.Invoke(); |
33 | 34 | }
|
34 | 35 | }
|
35 | 36 | }
|
Original file line number | Diff line number | Diff line change |
---|
|
| 1 | +using System; |
| 2 | + |
| 3 | +namespace CommandQueuePattern |
| 4 | +{ |
| 5 | +public interface ICommand |
| 6 | +{ |
| 7 | +Action OnFinished { get; set; } |
| 8 | + |
| 9 | +void Execute(); |
| 10 | +} |
| 11 | +} |
File renamed without changes.
You can’t perform that action at this time.
0 commit comments