-
Notifications
You must be signed in to change notification settings - Fork 2
/
Person.cs
169 lines (159 loc) · 5.41 KB
/
Person.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
using System;
using System.Drawing;
using System.Collections.Generic;
using System.Text;
namespace Zombie_Sim
{
class Person : Sentient
{
public const int MOVE_DISTANCE_MAX = 10;
//public const int SPOT_DISTANCE = 25;
protected enum MentalState { Calm, Panicked, Aggressive, OnFire, Hunter };
protected static Color[] ModeColors = new Color[]{Color.Green, Color.Yellow, Color.Blue, Color.Orange, Color.Violet};
protected const int WalkDistance = 5;
protected const int RunDistance = WalkDistance * 3;
private MentalState DefaultMode;
private MentalState Mode;
private int Strength;
private int Courage;
public Person(int x, int y, int h, int s, int c, Bitmap da)
{
ID = IDC++;
attackers = new Queue<Sentient>();
location = new Rectangle(x, y, DrawWidth, DrawWidth);
Health = h;
MaxHealth = Health;
Strength = s;
Courage = c;
Mode = MentalState.Calm;
DrawColor = ModeColors[(int)Mode];
DrawArea = da;
Fighting = false;
MoveDistance = MOVE_DISTANCE_MAX;
if (rnd == null)
rnd = new Random();
}
public Person(Rectangle r, int h, int s, int c, Bitmap da)
{
ID = IDC++;
attackers = new Queue<Sentient>();
location = r;
Health = h;
MaxHealth = Health;
Strength = s;
Courage = c;
DefaultMode = MentalState.Calm;
Mode = DefaultMode;
DrawColor = ModeColors[(int)Mode];
DrawArea = da;
Fighting = false;
MoveDistance = WalkDistance;
if (rnd == null)
rnd = new Random();
}
~Person()
{
removeReference(this);
}
public override void Move()
{
if (!Fighting)
{
Heal();
if (Mode == MentalState.Calm)
{
MoveDistance = WalkDistance;
Wander();
}
else if (Mode == MentalState.Aggressive)
{
MoveDistance = RunDistance;
GoTowardsTarget();
}
else if (Mode == MentalState.Panicked)
{
MoveDistance = RunDistance;
GoAwayFromTarget();
}
}
}
public override void Update()
{
Mode = DefaultMode;
if (!Fighting)
{
int closest = SpotDistance;
LinkedListNode<Sentient> sn = Sentients.First;
while (sn != null && attackers.Count == 0)
{
if (sn.Value is Zombie)
{
if (location.IntersectsWith(sn.Value.getLocation()))
{
attackers.Enqueue(sn.Value);
Mode = MentalState.Aggressive;
sn.Value.addToAttackers(this);
}
else
{
int dx = location.Left - sn.Value.getLocation().Left;
int dy = location.Top - sn.Value.getLocation().Top;
int d = (int)Math.Ceiling(Math.Sqrt(dx * dx + dy * dy));
if (d < closest)
{
closest = d;
target = sn.Value;
}
}
}
sn = sn.Next;
}
if (attackers.Count > 0)
{
target = attackers.Dequeue();
Fighting = true;
Mode = MentalState.Aggressive;
}
if (Mode == MentalState.Calm && closest < SpotDistance)
{
if (rnd.Next(1, 10) > Courage)
Mode = MentalState.Panicked;
else
Mode = MentalState.Aggressive;
}
}
else
{
if (target == null)
{
Fighting = false;
Update();
return;
}
target.attack((int)Math.Floor(rnd.NextDouble() * Strength));
}
DrawColor = ModeColors[(int)Mode];
}
public override void attack(int damage)
{
Health -= damage;
if (Health <= 0)
{
removeReference(this);
LinkedListNode<Sentient> sn = Sentients.Find(this);
LinkedListNode<Sentient> zn = new LinkedListNode<Sentient>(new Zombie(location, rnd.Next(3, 11), DrawArea));
if (sn == null)
{
Sentients.AddLast(zn);
Sentients.Remove(this);
}
else
{
Sentients.AddAfter(sn, zn);
Sentients.Remove(this);
}
Draw(true);
}
}
}
}