-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEnemy.cs
89 lines (82 loc) · 2.8 KB
/
Enemy.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media;
using System.Windows.Shapes;
namespace Bulkes
{
class Enemy : Bulk
{
int stepToTarget;
public Enemy(float _x, float _y, float _radius) : this(_x, _y, _radius, Settings.EnemyDefaultColor)
{
}
public Enemy(float _x, float _y, float _radius, Color _color) : base(_x, _y, _radius, _color)
{
isMoved = true;
setSpeed(Settings.EnemyStepValue, Settings.EnemyStepValue);
stepToTarget = 0;
}
public void setTarget(Unit unit)
{
target = unit;
}
private bool isVisibleUnit(Unit unit)
{
if (Math.Abs(x - unit.getX()) > Settings.EnemyFindOffset + radius)
return false;
if (Math.Abs(y - unit.getY()) > Settings.EnemyFindOffset + radius)
return false;
return true;
}
private void findNewTarget(GameMap gameMap, SectorHolder sectors)
{
stepToTarget = 0;
target = null;
sectors.solveSectorToMove(this);
float attraction;
float maxAttraction = int.MinValue;
int currentPriority;
foreach(Unit point in gameMap.getMap())
{
if (point != this && !point.getIsDeleted())
{
if (isVisibleUnit(point) && radius > point.getRadius())
{
currentPriority = sectors.getPriorityForUnit(point);
float distance = Math.Abs(x - point.getX()) + Math.Abs(y - point.getY());//not real distance use only for choice
float feedByDistance = point.getFeed() / distance;
attraction = feedByDistance - currentPriority * Settings.PriorityValue;
if (attraction > maxAttraction)
{
maxAttraction = attraction;
target = point;
}
}
}
}
}
public void updateState(GameMap gameMap, SectorHolder sectors)
{
if (stepToTarget == Settings.EnemyMaxStepToTarget || target == null) //update null
{
findNewTarget(gameMap, sectors);
}
if (target == null)
target = gameMap.getAnyUnit();
if (moveToTarget())
{
findNewTarget(gameMap, sectors);
stepToTarget = 0;
}
else
stepToTarget++;
}
public bool isTarget(Unit unit)
{
return target == unit;
}
}
}