-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbullet.sv
187 lines (138 loc) · 5.04 KB
/
bullet.sv
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
module bullet ( input Reset, frame_clk,hit,
input isWallBottom,isWallTop,isWallRight,isWallLeft,create,
input[1:0] game_end,
input [9:0] tankX,tankY,
input [7:0] sin, cos,
output is_bullet_active,
input [31:0] keycode,
output [9:0] BulletX, BulletY, BulletS,BulletXStep,BulletYStep,
output [15:0] bulletTimer);
logic [9:0] Bullet_X_Pos, Bullet_Y_Pos, Bullet_Size;
logic [9:0] Bullet_X_Motion, Bullet_Y_Motion, Bullet_X_Motion1, Bullet_Y_Motion1;
logic [5:0] Angle_Motion,Angle_new;
logic creation_flag;
logic [15:0]timer,timer2; //timer is to dissapear bullet, timer2 is to make the bullet active after some time
logic [7:0] key;
logic [15:0] Bullet_X_Comp, Bullet_Y_Comp;
logic signX, signY;
logic [7:0] sinBullet;
logic [7:0] cosBullet;
parameter [9:0] Bullet_X_Center=0; // Center position on the X axis
parameter [9:0] Bullet_Y_Center=0; // Center position on the Y axis
parameter [9:0] Bullet_X_Min=0; // Leftmost point on the X axis
parameter [9:0] Bullet_X_Max=639; // Rightmost point on the X axis
parameter [9:0] Bullet_Y_Min=0; // Topmost point on the Y axis
parameter [9:0] Bullet_Y_Max=479; // Bottommost point on the Y axis
parameter [7:0] Bullet_X_Step=8'b0010_0000;
parameter [7:0] Bullet_Y_Step=8'b0010_0000;
//angle counter clockwise step 1 corresponds to 4 degrees. 22 is 360 set to 0
assign Bullet_Size = 1; // assigns the value 4 as a 10-digit binary number, ie "0000000100"
always_ff @ (posedge Reset or posedge frame_clk )
begin: Move_Bullet
if (Reset) // Asynchronous Reset
begin
Bullet_Y_Motion <= 10'd0; //Ball_Y_Step;
Bullet_X_Motion <= 10'd0; //Ball_X_Step;
Bullet_X_Pos <= Bullet_Y_Center;
Bullet_Y_Pos <= Bullet_X_Center;
is_bullet_active<=0;
timer<=0;
timer2<=0;
end
else if(game_end>0)
begin
Bullet_Y_Motion <= 10'd0; //Ball_Y_Step;
Bullet_X_Motion <= 10'd0; //Ball_X_Step;
Bullet_X_Pos <= Bullet_Y_Center;
Bullet_Y_Pos <= Bullet_X_Center;
is_bullet_active<=0;
timer<=0;
timer2<=0;
end
else
begin
//The flag drives the logic below
if(create)
begin
creation_flag<=1;
end
else
begin
creation_flag<=0;
end
//Create bullet,save the shooting angle
if (creation_flag && !is_bullet_active)
begin
is_bullet_active<=1'b1;
//save the sin and cos at the moment of creation and calc direction
sinBullet<=sin;
cosBullet<=cos;
signX = Bullet_X_Step[7] ^ cos[7];
signY = Bullet_Y_Step[7] ^ sin[7];
Bullet_X_Comp[15:0] = Bullet_X_Step[6:0]*cos[6:0];
Bullet_Y_Comp[15:0] = Bullet_Y_Step[6:0]*sin[6:0];
//The steps to take according to angle
Bullet_Y_Motion[9:0] = {{6{~signY}},~Bullet_Y_Comp[10:7]}+1;
Bullet_X_Motion[9:0] = {{6{signX}},Bullet_X_Comp[10:7]};
Bullet_X_Motion1[9:0] <= Bullet_X_Motion[9:0];
Bullet_Y_Motion1[9:0] <= Bullet_Y_Motion[9:0];
Bullet_X_Pos<=tankX+Bullet_X_Motion[9:0] + Bullet_X_Motion[9:0] + Bullet_X_Motion[9:0] + Bullet_X_Motion[9:0] + Bullet_X_Motion[9:0];//add offset based on angle needs start out of the tanks body for collisions to work???
//Multiply by a different step size in the beginning
Bullet_Y_Pos<=tankY+Bullet_Y_Motion[9:0] + Bullet_Y_Motion[9:0] + Bullet_Y_Motion[9:0] + Bullet_Y_Motion[9:0] + Bullet_Y_Motion[9:0];
// timer2<=timer2+1'b1;
// if(timer2>=16'd5)
// is_bullet_active<=1'b1;
// else
// is_bullet_active=is_bullet_active;
end
else if (is_bullet_active)
begin
//Collision logic
if(isWallBottom ==1'b1)
begin
Bullet_Y_Motion = ~Bullet_Y_Motion+1'b1;
Bullet_X_Motion = Bullet_X_Motion;
end
else if(isWallTop == 1'b1)
begin
Bullet_Y_Motion = ~Bullet_Y_Motion+1'b1;
Bullet_X_Motion = Bullet_X_Motion;
end
else if (isWallRight==1'b1)
begin
Bullet_Y_Motion = Bullet_Y_Motion;
Bullet_X_Motion = ~Bullet_X_Motion+1'b1;
end
else if (isWallLeft==1'b1)
begin
Bullet_Y_Motion = Bullet_Y_Motion;
Bullet_X_Motion = ~Bullet_X_Motion+1'b1;
end
else
begin
Bullet_Y_Motion = Bullet_Y_Motion;
Bullet_X_Motion = Bullet_X_Motion;
end
//Counter to make the bullet dissapear after some time
timer<=timer+1'b1;
Bullet_X_Pos<=Bullet_X_Pos+Bullet_X_Motion;
Bullet_Y_Pos<=Bullet_Y_Pos+Bullet_Y_Motion;
if(timer>=16'd1000)
begin
is_bullet_active=1'b0;
timer<=16'd0;
end
else
begin
is_bullet_active=is_bullet_active;
end
end
end
end
assign BulletX = Bullet_X_Pos;
assign BulletY = Bullet_Y_Pos;
assign BulletS = Bullet_Size;
assign bulletTimer = timer;
assign BulletXStep = Bullet_X_Motion;
assign BulletYStep = Bullet_Y_Motion;
endmodule