-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUnit.cs
213 lines (199 loc) · 5.81 KB
/
Unit.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media;
namespace Bulkes
{
class Unit
{
protected float x;
protected float y;
protected float baseX;
protected float baseY;
protected float baseRadius;
protected float radius;
protected float animationRadius;
protected bool isDeleted;
protected Color color;
protected Unit target;
public virtual float getSpeedX()
{
return speedX;
}
public virtual float getSpeedY()
{
return speedY;
}
protected float speedX;
protected float speedY;
//constructors
public Unit():this(0f, 0f, 0f)
{ }
public Unit(float _x, float _y, float _radius) : this(_x, _y, _radius, Colors.Gray)
{ }
public Unit(float _x, float _y, float _radius, Color _color)
{
x = _x;
y = _y;
radius = _radius * Settings.UserScale;
baseRadius = _radius;
color = _color;
isDeleted = false;
animationRadius = 0f;
}
//setters
public void setX(float x)
{
this.x = x;
}
public void setY(float y)
{
this.y = y;
}
public void setRadius(float radius)
{
this.baseRadius = radius;
this.radius = radius * Settings.UserScale;
}
//overrided in Bulk
public virtual void updatePosition(Unit unit)//update location + radius
{
radius = baseRadius * Settings.UserScale;
// x = unit.x + ((baseX - unit.x) * Settings.UserScale);
// y = unit.y + ((baseY - unit.y) * Settings.UserScale);
if (!isOnMainScreen())
animationRadius = radius;
}
public void setSpeed(float _speedX, float _speedY)
{
speedX = _speedX;
speedY = _speedY;
}
public void setColor(Color color)
{
this.color = color;
}
public void setPosition(float _x, float _y)
{
x = _x;
y = _y;
}
public void setIsDeleted(bool isDeleted)
{
this.isDeleted = isDeleted;
}
public float getX()
{
return x;
}
public float getY()
{
return y;
}
public float getRadius()
{
return radius;
}
public virtual float getFeed()//this method must override in all class
{
return 0f;
}
public bool getIsDeleted()
{
return isDeleted;
}
public Color getColor()
{
return color;
}
//public methods
public void move(float dx, float dy)
{
x += dx;
y += dy;
}
public bool isOverlapped(Unit unit)
{
if (unit.getX() < x - (radius + unit.radius)
|| unit.getX() > x + (radius + unit.radius)
|| unit.getY() < y - (radius + unit.radius)
|| unit.getY() > y + (radius + unit.radius))
return false;
Indicator pointOut = new Indicator();//point on radius external circle
pointOut.getParameters(unit.getX(), unit.getY(), unit.getRadius(), x, y);//(x;y) - center current unit
float dx;
float dy;
dx = pointOut.getX() - x;
dy = pointOut.getY() - y;
return (dx * dx + dy * dy) < (radius * radius);
}
public bool isEaten(Unit unit)
{
if (unit.radius > radius)
return unit.isEaten(this);
float dx;
float dy;
dx = unit.getX() - x;
dy = unit.getY() - y;
return (dx * dx + dy * dy) < (radius * radius);
}
//protected
public bool isOnMainScreen()
{
if (x + radius < -50f || x - radius > Settings.ScreenWidthDefault + 50)
return false;
if (y + radius < -50f || y - radius > Settings.ScreenHeightDefault + 50)
return false;
return true;
}
protected bool moveToTarget()
{
float dx;
float dy;
float newX;
float newY;
dx = target.getX() - x;
dy = target.getY() - y;
if (dx * dx + dy * dy < (target.getRadius() - radius) * (target.getRadius() - radius))
return true;
//catchTarget();
if (Math.Abs(dx) > Math.Abs(dy))
{
if (dx > 0)
newX = x + getSpeedX();
else
newX = x - getSpeedX();
setPosition(newX, solveY(newX));
}
else
{
if (dy > 0)
newY = y + getSpeedY();
else
newY = y - getSpeedY();
setPosition(solveX(newY), newY);
}
return false;
}
//private
private float solveY(float _x)//x - increment; y = f(x)
{
float k;
k = (target.getY() - getY()) / (target.getX() - getX());
return k * _x - k * getX() + getY();
}
private float solveX(float _y)//y - increment; x = f(y)
{
float k;
if (Math.Abs(target.getX() - getX()) < 0.001f)
return getX();
else
{
k = (target.getY() - getY()) / (target.getX() - getX());
return (_y - getY()) / k + getX();
}
}
}
}