-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCommandPattern.cs
224 lines (216 loc) · 7.15 KB
/
CommandPattern.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
namespace CommandPattern
{
public class InputHandler : MonoBehaviour
{
//Tuşlarla kontrol ettiğimiz kutu
public Transform boxTrans;
//İhtiyacımız olan diger butonlar
private Command buttonW, buttonS, buttonA, buttonD, buttonB, buttonZ, buttonR;
//tekrar ve geri almak icin Command List oluşturuyoruz.
public static List<Command> oldCommands = new List<Command>();
//Kutunun başlangıç konumu
private Vector3 boxStartPos;
//Coroutine resetlemek icin
private Coroutine replayCoroutine;
//Tekrarlamak istersek
public static bool shouldStartReplay;
/Oynarken butona basmamak icin
private bool isReplaying;
void Start()
{
//commandlari bind ediyoruz
buttonB = new DoNothing();
buttonW = new MoveForward();
buttonS = new MoveReverse();
buttonA = new MoveLeft();
buttonD = new MoveRight();
buttonZ = new UndoCommand();
buttonR = new ReplayCommand();
boxStartPos = boxTrans.position;
}
void Update()
{
if (!isReplaying)
{
HandleInput();
}
StartReplay();
}
//Hangi butona basarsak o butonu bind etmek icin if ile kontrol ediyoruz.
public void HandleInput()
{
if (Input.GetKeyDown(KeyCode.A))
{
buttonA.Execute(boxTrans, buttonA);
}
else if (Input.GetKeyDown(KeyCode.B))
{
buttonB.Execute(boxTrans, buttonB);
}
else if (Input.GetKeyDown(KeyCode.D))
{
buttonD.Execute(boxTrans, buttonD);
}
else if (Input.GetKeyDown(KeyCode.R))
{
buttonR.Execute(boxTrans, buttonZ);
}
else if (Input.GetKeyDown(KeyCode.S))
{
buttonS.Execute(boxTrans, buttonS);
}
else if (Input.GetKeyDown(KeyCode.W))
{
buttonW.Execute(boxTrans, buttonW);
}
else if (Input.GetKeyDown(KeyCode.Z))
{
buttonZ.Execute(boxTrans, buttonZ);
}
}
//Tekrar icin
void StartReplay()
{
if (shouldStartReplay && oldCommands.Count > 0)
{
shouldStartReplay = false;
//Stop the coroutine so it starts from the beginning
if (replayCoroutine != null)
{
StopCoroutine(replayCoroutine);
}
//Burda Tekrar basliyor
replayCoroutine = StartCoroutine(ReplayCommands(boxTrans));
}
}
//replay yani tekrar coroutini
IEnumerator ReplayCommands(Transform boxTrans)
{
//hareket ettirmiyoruz.
isReplaying = true;
//start pozisyonundan hareket ettiriyoruz
boxTrans.position = boxStartPos;
for (int i = 0; i < oldCommands.Count; i++)
{
//Kutuyu geçerli command ile hareket ettirmek icin.
oldCommands[i].Move(boxTrans);
yield return new WaitForSeconds(0.3f);
}
//tekrar hareket icin
isReplaying = false;
}
}
}
//namespace kullanarak
using UnityEngine;
using System.Collections;
using System.Collections.Generic;namespace CommandPattern
{
//parent class
public abstract class Command
{
//Butona bastığımızda kutu ne kadar uzaklıkta hareket ediyor.
protected float moveDistance = 1f; //Kayit etmek icin (command)
public abstract void Execute(Transform boxTrans, Command command);//eski commandi geri almak icin
public virtual void Undo(Transform boxTrans) { }
public virtual void Move(Transform boxTrans) { }
}
//
// Child sinif
//
public class MoveForward : Command
{
//Butona bastigimizda cagrilir
public override void Execute(Transform boxTrans, Command command)
{
Move(boxTrans);
//Commandi kaydediyoruz
InputHandler.oldCommands.Add(command);
}
//Eski commandi geri alıyoruz
public override void Undo(Transform boxTrans)
{
boxTrans.Translate(-boxTrans.forward * moveDistance);
}
public override void Move(Transform boxTrans)
{
boxTrans.Translate(boxTrans.forward * moveDistance);
}
}
public class MoveReverse : Command
{
public override void Execute(Transform boxTrans, Command command)
{
Move(boxTrans);
InputHandler.oldCommands.Add(command);
}
public override void Undo(Transform boxTrans)
{
boxTrans.Translate(boxTrans.forward * moveDistance);
}
public override void Move(Transform boxTrans)
{
boxTrans.Translate(-boxTrans.forward * moveDistance);
}
}
public class MoveLeft : Command
{
public override void Execute(Transform boxTrans, Command command)
{
Move(boxTrans);
InputHandler.oldCommands.Add(command);
}
public override void Undo(Transform boxTrans)
{
boxTrans.Translate(boxTrans.right * moveDistance);
}
public override void Move(Transform boxTrans)
{
boxTrans.Translate(-boxTrans.right * moveDistance);
}
}
public class MoveRight : Command
{
public override void Execute(Transform boxTrans, Command command)
{
Move(boxTrans);
InputHandler.oldCommands.Add(command);
}
public override void Undo(Transform boxTrans)
{
boxTrans.Translate(-boxTrans.right * moveDistance);
}
public override void Move(Transform boxTrans)
{
boxTrans.Translate(boxTrans.right * moveDistance);
}
}
public class DoNothing : Command
{
public override void Execute(Transform boxTrans, Command command){}
}
public class UndoCommand : Command
{
public override void Execute(Transform boxTrans, Command command)
{
List oldCommands = InputHandler.oldCommands;
if (oldCommands.Count &amp;amp;amp;amp;gt; 0)
{
Command latestCommand = oldCommands[oldCommands.Count - 1];//Command ile kutuyu hareket ettiriyoruz
latestCommand.Undo(boxTrans);//Listeden commandi siliyoruz
oldCommands.RemoveAt(oldCommands.Count - 1);
}
}
}
//Replay ediyoruz tum commandlari
public class ReplayCommand : Command
{
public override void Execute(Transform boxTrans, Command command)
{
InputHandler.shouldStartReplay = true;
}
}
}