-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameField.cs
265 lines (244 loc) · 9.86 KB
/
GameField.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
using System;
using System.Collections.Generic;
using Android.Graphics;
namespace Segmentus
{
class GameField : TouchablePart
{
enum FieldState { Free, OneDown, OneSelected, OneSelectedOneDown,
OneStretched, OneStretchedOneAimed };
static Rect bounds;
FieldData fieldData;
FieldState state = FieldState.Free;
GamePoint[] points;
GameSegment dottedSegment;
List<GameSegment> segments = new List<GameSegment>();
SortedSet<int> availableSegments = new SortedSet<int>();
SortedSet<int> pointsAlive = new SortedSet<int>();
SortedSet<int> pointsTargeted = new SortedSet<int>();
int pointA, pointB;
public event Action<int> PlayerMoved;
static GameField()
{
int w = (int)(720 * GameView.scaleFactor);
int h = (int)(820 * GameView.scaleFactor);
bounds = new Rect(-w / 2, -h / 2, w / 2, h / 2);
}
public GameField(FieldData fieldData, Pivot parentPivot, float x, float y)
: base(bounds, parentPivot, x, y)
{
this.fieldData = fieldData;
points = new GamePoint[fieldData.pointsCnt];
for (int i = 0; i < fieldData.pointsCnt; ++i) {
int xc = (int)(fieldData.points[i].x * GameView.scaleFactor);
int yc = (int)(fieldData.points[i].y * GameView.scaleFactor);
points[i] = new GamePoint(pivot, xc, yc);
pointsAlive.Add(i);
}
for (int i = 0; i < fieldData.segmentsCnt; ++i)
availableSegments.Add(i);
}
public void AnimateAppearance()
{
foreach (GamePoint p in points)
p.AnimateAppearance();
}
public void ClearEvents()
{
PlayerMoved = null;
}
public void OnCompetitorsMove(int segID)
{
int point1 = fieldData.pointAbySegment[segID];
int point2 = fieldData.pointBbySegment[segID];
points[point1].State = GamePoint.PointState.UsedByCompetitor;
points[point2].State = GamePoint.PointState.UsedByCompetitor;
AddSegment(point1, point2, ColorBank.Blue);
}
void AddSegment(int point1, int point2, int colorID)
{
pointsAlive.Remove(point1);
pointsAlive.Remove(point2);
GamePoint a = points[point1], b = points[point2];
GameSegment cur = new GameSegment(colorID,
(int)(b.pivot.X - a.pivot.X), (int)(b.pivot.Y - a.pivot.Y),
pivot, (int)a.pivot.X, (int)a.pivot.Y);
cur.AnimateAppearance();
segments.Add(cur);
int segID = fieldData.segmentID[point1, point2];
foreach (int segProhibitID in fieldData.intersectedWith[segID])
availableSegments.Remove(segProhibitID);
}
int FindAlivePointID(int x, int y)
{
foreach (int pointID in pointsAlive)
if (points[pointID].bounds.Contains(x, y))
return pointID;
return -1;
}
void ClearTargets()
{
foreach (int pointID in pointsTargeted)
points[pointID].State = GamePoint.PointState.Normal;
pointsTargeted.Clear();
}
void MakeTargets(int pointID)
{
for (int i = 0; i < points.Length; ++i)
{
int segID = fieldData.segmentID[pointID, i];
if (availableSegments.Contains(segID))
{
points[i].State = GamePoint.PointState.Highlighted;
pointsTargeted.Add(i);
}
}
}
public override void OnTouchDown(int absX, int absY)
{
int x = (int)(absX - pivot.AbsX);
int y = (int)(absY - pivot.AbsY);
int curID = FindAlivePointID(x, y);
switch (state)
{
case FieldState.Free:
if (curID == -1)
break;
pointA = curID;
points[pointA].State = GamePoint.PointState.Selected;
MakeTargets(pointA);
state = FieldState.OneDown;
break;
case FieldState.OneSelected:
bool targeted = pointsTargeted.Contains(curID);
ClearTargets();
if (curID == -1 || curID == pointA)
{
points[pointA].State = GamePoint.PointState.Normal;
state = FieldState.Free;
break;
}
if (targeted)
{
pointB = curID;
points[pointB].State = GamePoint.PointState.Selected;
state = FieldState.OneSelectedOneDown;
break;
}
points[pointA].State = GamePoint.PointState.Normal;
pointA = curID;
points[pointA].State = GamePoint.PointState.Selected;
MakeTargets(pointA);
state = FieldState.OneDown;
break;
}
}
public override void OnTouchMove(int absX, int absY)
{
int x = (int)(absX - pivot.AbsX);
int y = (int)(absY - pivot.AbsY);
int curID = FindAlivePointID(x, y);
switch (state)
{
case FieldState.OneDown:
if (curID == pointA)
break;
dottedSegment = new GameSegment(ColorBank.Yellow, 0, 0, pivot,
(int)points[pointA].pivot.X, (int)points[pointA].pivot.Y);
dottedSegment.Style = GameSegment.SegmentStyle.Dotted;
state = FieldState.OneStretched;
break;
case FieldState.OneSelectedOneDown:
if (curID == pointB)
break;
points[pointA].State = GamePoint.PointState.Normal;
pointA = pointB;
points[pointA].State = GamePoint.PointState.Selected;
MakeTargets(pointA);
dottedSegment = new GameSegment(ColorBank.Yellow, 0, 0, pivot,
(int)points[pointA].pivot.X, (int)points[pointA].pivot.Y);
dottedSegment.Style = GameSegment.SegmentStyle.Dotted;
state = FieldState.OneStretched;
break;
case FieldState.OneStretchedOneAimed:
if (curID == pointB)
break;
points[pointB].State = GamePoint.PointState.Highlighted;
state = FieldState.OneStretched;
break;
}
if (state == FieldState.OneStretched)
{
if (curID == -1 || curID == pointA || !pointsTargeted.Contains(curID))
{
dottedSegment.HeadX = (int)(x - points[pointA].pivot.X);
dottedSegment.HeadY = (int)(y - points[pointA].pivot.Y);
return;
}
pointB = curID;
points[pointB].State = GamePoint.PointState.Selected;
dottedSegment.HeadX = (int)(points[pointB].pivot.X - points[pointA].pivot.X);
dottedSegment.HeadY = (int)(points[pointB].pivot.Y - points[pointA].pivot.Y);
state = FieldState.OneStretchedOneAimed;
}
}
void PerformSegment()
{
points[pointA].State = GamePoint.PointState.UsedByPlayer;
points[pointB].State = GamePoint.PointState.UsedByPlayer;
state = FieldState.Free;
AddSegment(pointA, pointB, ColorBank.Yellow);
PlayerMoved?.Invoke(fieldData.segmentID[pointA, pointB]);
}
public override void OnTouchUp(int x, int y)
{
switch (state)
{
case FieldState.OneDown:
state = FieldState.OneSelected;
break;
case FieldState.OneSelectedOneDown:
PerformSegment();
break;
case FieldState.OneStretched:
ClearTargets();
points[pointA].State = GamePoint.PointState.Normal;
dottedSegment = null;
state = FieldState.Free;
break;
case FieldState.OneStretchedOneAimed:
ClearTargets();
dottedSegment = null;
PerformSegment();
break;
}
}
void GoToFreeStateFromAnyTouchedState()
{
if (state == FieldState.Free || state == FieldState.OneSelected)
return;
ClearTargets();
points[pointA].State = GamePoint.PointState.Normal;
dottedSegment = null;
state = FieldState.Free;
if (state == FieldState.OneDown || state == FieldState.OneStretched)
return;
points[pointB].State = GamePoint.PointState.Normal;
}
public override void OnTouchOutside(int x, int y) => GoToFreeStateFromAnyTouchedState();
public override void OnTouchCancel(int x, int y) => GoToFreeStateFromAnyTouchedState();
public override void Deactivate()
{
base.Deactivate();
GoToFreeStateFromAnyTouchedState();
}
protected override void Draw(Canvas canvas)
{
foreach (GameSegment s in segments)
s.OnDraw(canvas);
dottedSegment?.OnDraw(canvas);
foreach (GamePoint p in points)
p.OnDraw(canvas);
}
}
}