-
Notifications
You must be signed in to change notification settings - Fork 0
/
Projectile.cs
192 lines (182 loc) · 5.98 KB
/
Projectile.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
using System;
using System.Diagnostics;
namespace Blone
{
public abstract class Projectile
{
/// <summary>
/// Sets up x and y coordinate, also sets what direction for the projectile to continue moving in.
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
/// <param name="direction"></param>
public Projectile(int x, int y, string direction)
{
Direction = direction;
switch (direction)
{
case DevHelper.Up:
X = x;
Y = y - 1;
break;
case DevHelper.Down:
X = x;
Y = y + 1;
break;
case DevHelper.Left:
X = x - 1;
Y = y;
break;
case DevHelper.Right:
X = x + 1;
Y = y;
break;
}
CheckCollision();
}
public int X;
public int Y;
public Stopwatch MoveStopwatch = new Stopwatch();
public int Damage;
public int Speed;
public int MaxDistance;
public bool Removed;
public string Type;
public string Direction;
public ConsoleColor FormerBackgroundColor;
public int DistanceTraveled = 0;
/// <summary>
/// Checks collision for the projectile.
/// </summary>
/// <returns>A string describing what type of object it collided with.</returns>
public string CheckCollision()
{
var possibleX = X;
var possibleY = Y;
switch (Direction)
{
case DevHelper.Up:
possibleY -= 1;
break;
case DevHelper.Down:
possibleY += 1;
break;
case DevHelper.Right:
possibleX += 1;
break;
case DevHelper.Left:
possibleX -= 1;
break;
}
for (int i = 0; i < GameContainer.EnemyList.Count; i++)
{
if (GameContainer.EnemyList[i].X == X && GameContainer.EnemyList[i].Y == Y)
{
Erase();
Remove();
GameContainer.EnemyList.RemoveAt(i);
GameContainer.Score += DevHelper.ScorePerKill;
GameContainer.UserInterface.UpdateScore();
return DevHelper.EnemyCollision;
}
}
for (int i = 0; i < GameContainer.WallList.Length; i++)
{
if (GameContainer.WallList[i].X == possibleX && GameContainer.WallList[i].Y == possibleY)
{
Erase();
Remove();
return DevHelper.WallCollision;
}
}
return DevHelper.NoCollision;
}
/// <summary>
/// Draws the object at it's current x and y coordinates.
/// </summary>
public void Draw()
{
Console.SetCursorPosition(X, Y);
switch (Type)
{
case DevHelper.Bullet:
Console.ForegroundColor = ConsoleColor.Yellow;
Console.BackgroundColor = ConsoleColor.Magenta;
break;
case DevHelper.RifleAmmo:
Console.ForegroundColor = ConsoleColor.Blue;
Console.BackgroundColor = ConsoleColor.Magenta;
break;
case DevHelper.ShotgunShell:
Console.ForegroundColor = ConsoleColor.Red;
Console.BackgroundColor = ConsoleColor.Magenta;
break;
}
Console.Write("o");
}
/// <summary>
/// Moves the projectile in the current direction.
/// </summary>
public void Move()
{
if (DistanceTraveled < MaxDistance)
{
switch (Direction)
{
case DevHelper.Up:
Erase();
Y = Y - 1;
Draw();
break;
case DevHelper.Down:
Erase();
Y = Y + 1;
Draw();
break;
case DevHelper.Right:
Erase();
X = X + 1;
Draw();
break;
case DevHelper.Left:
Erase();
X = X - 1;
Draw();
break;
}
DistanceTraveled++;
MoveStopwatch.Restart();
}
else if (Removed == false)
{
Erase();
Remove();
}
}
/// <summary>
/// Erases the object console.
/// </summary>
public void Erase()
{
Console.SetCursorPosition(X, Y);
Console.BackgroundColor = ConsoleColor.Black;
Console.Write(" ");
}
/// <summary>
/// Removes the object from the projectile list in GameContainer.
/// </summary>
public void Remove()
{
for (int i = 0; i < GameContainer.ProjectileList.Count; i++)
{
if (GameContainer.ProjectileList[i].X == X
&& GameContainer.ProjectileList[i].Y == Y
&& GameContainer.ProjectileList[i].Type == Type)
{
Removed = true;
GameContainer.ProjectileList.RemoveAt(i);
}
}
}
}
}