-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bullet.cs
177 lines (135 loc) · 5.19 KB
/
Bullet.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
/************************************************************************************
SOURCE FILE: Bullet.cs
PROGRAM: server
FUNCTIONS: Bullet (int id, byte type, Player player)
Update()
IsColliding(float x, float z, float r)
DATE: Mar. 14, 2018
REVISIONS:
DESIGNER: Benny Wang
PROGRAMMER: Benny Wang, Li-Yan Tong
NOTES:
This is that class that the server uses to represent bullets in game.
This class stores all the information related to bullets and has the
ability to check if a player is colliding with it.
**********************************************************************************/
using System;
public class Bullet
{
public int BulletId { get; set; }
public byte PlayerId { get; set; }
public byte Damage { get; set; }
public byte Type { get; set; }
public float Size { get; set; }
private float speed { get; set; }
private DateTime deathTime;
public float X { get; set; }
public float Z { get; set; }
private float deltaX;
private float deltaZ;
public byte Event { get; set; }
/************************************************************************************
FUNCTION: Bullet
DATE: Mar. 14, 2018
REVISIONS:
DESIGNER: Benny Wang
PROGRAMMER: Benny Wang, Li-Yan Tong
INTERFACE: Bullet(int id, byte type, Player player)
int id: The id of the bullet.
byte type: The type of the bullet.
Player: The player that shot the bullet.
NOTES:
Creates a bullet using the position and rotation of the player. The behaviour of the
bullet is determined by what type is passed in.
**********************************************************************************/
public Bullet (int id, byte type, Player player)
{
this.BulletId = id;
this.PlayerId = player.id;
this.Type = type;
this.X = player.x;
this.Z = player.z;
this.Event = R.Game.Bullet.IGNORE;
DateTime currentTime = DateTime.Now;
switch (type)
{
case R.Type.KNIFE:
this.Damage = 70;
this.Size = 0.5f;
this.speed = 0.4f;
this.deathTime = currentTime.AddSeconds(0.03);
break;
case R.Type.PISTOL:
this.Damage = 10;
this.Size = 0.1f;
this.speed = 0.4f;
this.deathTime = currentTime.AddSeconds(1.0);
break;
case R.Type.SHOTGUN:
this.Damage = 13;
this.Size = 0.25f;
this.speed = 0.3f;
this.deathTime = currentTime.AddSeconds(0.4);
break;
case R.Type.RIFLE:
this.Damage = 20;
this.Size = 0.1f;
this.speed = 0.6f;
this.deathTime = currentTime.AddSeconds(2.0);
break;
default:
this.Damage = 0;
this.Size = 0;
this.speed = 0;
this.deathTime = currentTime.AddSeconds(0);
break;
}
// Changed from sin to cos for both
this.deltaX = (float)(this.speed * Math.Sin(player.r * (Math.PI / 180)));
this.deltaZ = (float)(this.speed * Math.Cos(player.r * (Math.PI / 180)));
}
/************************************************************************************
FUNCTION: Update
DATE: Mar. 14, 2018
REVISIONS:
DESIGNER: Benny Wang
PROGRAMMER: Benny Wang
INTERFACE: bool Update()
RETURN: True if the bullet position was updated, false if the bullet's lifetime expired.
NOTES:
Updates the position of the bullet. If the bullet's lifetime expires during this call
the position is not updated and false is returned. Otherwise, true is returned.
**********************************************************************************/
public bool Update()
{
if (DateTime.Now > this.deathTime)
{
// Console.WriteLine("bullet {0} expired at {1}", this.BulletId, DateTime.Now);
return false;
}
this.X += this.deltaX;
this.Z += this.deltaZ;
return true;
}
/************************************************************************************
FUNCTION: IsColliding
DATE: Mar. 14, 2018
REVISIONS:
DESIGNER: Benny Wang
PROGRAMMER: Benny Wang
INTERFACE: bool IsColliding(float x, float z, float r)
float x: The x position.
float z: The z position.
float r: The radius.
RETURN: True if the given circle is colliding with the bullet, false otherwise.
NOTES:
Checks whether the given circle specificed by the given x, z, radius, collides with
the bullet or not.
**********************************************************************************/
public bool IsColliding(float x, float z, float r)
{
double distance = Math.Sqrt((this.X - x) * (this.X - x) + (this.Z - z) * (this.Z - z));
double radiusSum = this.Size + r;
return (distance < radiusSum);
}
}