-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPlane.cs
133 lines (124 loc) · 4.99 KB
/
Plane.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Text;
using System.Threading.Tasks;
namespace BombPlane
{
public class Plane
{
public Plane(Direction direction, int headX, int headY)
{
this.direction = direction;
HeadPoint = new Point(headX, headY);
}
public Plane(Direction direction, Point headPoint)
{
this.direction = direction;
HeadPoint = headPoint;
}
public bool Conflict(Plane plane) { return plane.Points.Overlaps(Points); }
public Direction direction;
public Point HeadPoint;
public int HeadX { get { return HeadPoint.X; } set { HeadPoint.X = value; } }
public int HeadY { get { return HeadPoint.Y; } set { HeadPoint.Y = value; } }
private HashSet<Point> _points;
public HashSet<Point> Points
{
get
{
if (_points == null)
{
Point[] points;
switch (direction)
{
// 方向决定了头节点的朝向;旋转是绕着头节点的
case Direction.Up:
points = new Point[10] {
new Point(HeadX, HeadY),
new Point(HeadX, HeadY + 1),
new Point(HeadX - 1, HeadY + 1),
new Point(HeadX + 1, HeadY + 1),
new Point(HeadX - 2, HeadY + 1),
new Point(HeadX + 2, HeadY + 1),
new Point(HeadX, HeadY + 2),
new Point(HeadX, HeadY + 3),
new Point(HeadX + 1, HeadY + 3),
new Point(HeadX - 1, HeadY + 3),
};
break;
case Direction.Down:
points = new Point[10] {
new Point(HeadX, HeadY),
new Point(HeadX, HeadY - 1),
new Point(HeadX - 1, HeadY - 1),
new Point(HeadX + 1, HeadY - 1),
new Point(HeadX - 2, HeadY - 1),
new Point(HeadX + 2, HeadY - 1),
new Point(HeadX, HeadY - 2),
new Point(HeadX, HeadY - 3),
new Point(HeadX + 1, HeadY - 3),
new Point(HeadX - 1, HeadY - 3),
};
break;
case Direction.Left:
points = new Point[10] {
new Point(HeadX, HeadY),
new Point(HeadX + 1, HeadY),
new Point(HeadX + 1, HeadY + 1),
new Point(HeadX + 1, HeadY - 1),
new Point(HeadX + 1, HeadY + 2),
new Point(HeadX + 1, HeadY - 2),
new Point(HeadX + 2, HeadY),
new Point(HeadX + 3, HeadY),
new Point(HeadX + 3, HeadY + 1),
new Point(HeadX + 3, HeadY - 1),
};
break;
case Direction.Right:
points = new Point[10] {
new Point(HeadX, HeadY),
new Point(HeadX - 1, HeadY),
new Point(HeadX - 1, HeadY + 1),
new Point(HeadX - 1, HeadY - 1),
new Point(HeadX - 1, HeadY + 2),
new Point(HeadX - 1, HeadY - 2),
new Point(HeadX - 2, HeadY),
new Point(HeadX - 3, HeadY),
new Point(HeadX - 3, HeadY + 1),
new Point(HeadX - 3, HeadY - 1),
};
break;
default:
throw new Exception("Unexpected");
}
_points = new HashSet<Point>(points);
}
return _points;
}
}
private HashSet<Point> _bodyPoints;
public HashSet<Point> BodyPoints
{
get
{
if (_bodyPoints == null)
{
_bodyPoints = new(Points);
_bodyPoints.Remove(HeadPoint);
}
return _bodyPoints;
}
}
public const int WingLength = 2;
public const int BodyLength = 4;
}
public enum Direction
{
Up = 0,
Down = 1,
Left = 2,
Right = 3
}
}