-
Notifications
You must be signed in to change notification settings - Fork 1
/
bullets.pas
114 lines (96 loc) · 1.84 KB
/
bullets.pas
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
unit bullets;
interface
uses
Classes;
const
BULLET_RADIUS=3;
BULLET_SPEED=29;
BULLET_HIT_HP=5;
type
TBullet=class
private
x,y,alfa:extended;
ownerTank:TObject;
public
isCollided:boolean;
constructor Create(x,y,alfa:extended; ownerTank:TObject);
function getX:Extended;
function getY:Extended;
function getAlfa:Extended;
procedure doMove;
end;
TBulletList=class(TList)
public
function at(i:integer):TBullet;
end;
implementation
uses
globals,
tanks,
world;
{ TBullet }
constructor TBullet.Create(x, y, alfa: extended; ownerTank:TObject);
begin
Self.ownerTank:=ownerTank;
Self.x:=x;
Self.y:=y;
Self.alfa:=alfa;
end;
procedure TBullet.doMove;
var
dr,
dx,dy,
xnew,ynew:extended;
i:integer;
tank:TTank;
begin
isCollided:=false;
dr:=BULLET_SPEED/5;
xnew:=x+dr*cos(alfa);
ynew:=y+dr*sin(alfa);
x:=xnew;
y:=ynew;
if (x<0) or (y<0) or
(x>getWorld.width) or (y>getWorld.height) then
isCollided:=true;
for i:=0 to getWorld.tanks.count-1 do
begin
tank:=getWorld.tanks.at(i);
dx:=(tank.getX-xnew);
dy:=(tank.getY-ynew);
if tank<>ownerTank then
if dx*dx+dy*dy<sqr(BULLET_RADIUS+TANK_RADIUS) then
begin
tank.decHP(BULLET_HIT_HP);
isCollided:=true;
try
if (ownerTank as TTank).isAssigned then
begin
(ownerTank as TTank).incHits();
if tank.getHP<=0 then
(ownerTank as TTank).incFrags();
end;
except
end;
break
end;
end;
end;
function TBullet.getAlfa: Extended;
begin
Result:=alfa;
end;
function TBullet.getX: Extended;
begin
Result:=x
end;
function TBullet.getY: Extended;
begin
Result:=y
end;
{ TBulletList }
function TBulletList.at(i: integer): TBullet;
begin
Result:=TBullet(Items[i]);
end;
end.