-
Notifications
You must be signed in to change notification settings - Fork 0
/
Gun.cs
93 lines (86 loc) · 3.34 KB
/
Gun.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
using System;
using System.Collections;
using System.Diagnostics;
using System.Security.Cryptography.X509Certificates;
using System.Threading.Tasks;
namespace Blone
{
public abstract class Gun
{
public string Type;
public int MagazineSize;
public int AmmunitionInMagazine;
public int RoundsPerKSeconds;
public int ReloadMilliseconds;
public string AmmoType;
public bool Reloading = false;
public Stopwatch ShootTimer = new Stopwatch();
public Stopwatch ReloadTimer = new Stopwatch();
/// <summary>
/// Shoots a projectile object.
/// </summary>
/// <param name="x">What x coordinate it should spawn in.</param>
/// <param name="y">What y coordinate it should spawn in</param>
/// <param name="direction">What direction it should have.</param>
public void Shoot(int x, int y, string direction)
{
if (Reloading == false && AmmunitionInMagazine > 0 && ShootTimer.ElapsedMilliseconds > RoundsPerKSeconds)
{
var possibleX = x;
var possibleY = y;
var notInsideWall = true;
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.WallList.Length; i++)
{
if (GameContainer.WallList[i].X == possibleX && GameContainer.WallList[i].Y == possibleY)
notInsideWall = false;
}
// If statement inside if statement to limit WallList looping, to minimize performance loss.
if (notInsideWall)
{
switch (AmmoType)
{
case DevHelper.Bullet:
GameContainer.ProjectileList.Add(new Bullet(x, y, direction));
break;
case DevHelper.RifleAmmo:
GameContainer.ProjectileList.Add(new RifleAmmo(x, y, direction));
break;
case DevHelper.ShotgunShell:
GameContainer.ProjectileList.Add(new ShotgunShell(x, y, direction));
break;
}
ShootTimer.Restart();
AmmunitionInMagazine--;
GameContainer.UserInterface.UpdateAmmo();
}
}
}
/// <summary>
/// Reloads the weapon, hero cannot shoot during reload.
/// </summary>
public async void Reload()
{
ReloadTimer.Start();
Reloading = true;
await Task.Delay(TimeSpan.FromMilliseconds(ReloadMilliseconds));
AmmunitionInMagazine = MagazineSize;
GameContainer.UserInterface.UpdateAmmo();
Reloading = false;
}
}
}