-
Notifications
You must be signed in to change notification settings - Fork 0
/
CollisionManager.vhd
333 lines (326 loc) · 10.6 KB
/
CollisionManager.vhd
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
----------------------------------------------------------------------------------
-- Company:
-- Engineer:
--
-- Create Date: 17:52:39 04/09/2020
-- Design Name:
-- Module Name: CollisionManager - CollisionManagerArch
-- Project Name:
-- Target Devices:
-- Tool versions:
-- Description:
--
-- Dependencies:
--
-- Revision:
-- Revision 0.01 - File Created
-- Additional Comments:
--
----------------------------------------------------------------------------------
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.NUMERIC_STD.ALL;
entity CollisionManager is
port(Clk : in STD_LOGIC;
Reset : in STD_LOGIC;
BallVector : in STD_LOGIC_VECTOR(1 downto 0);
BallSize : in UNSIGNED(11 downto 0);
BallSpeed : in UNSIGNED(11 downto 0);
BallPositionY : in UNSIGNED(11 downto 0);
BallPositionX : in UNSIGNED(11 downto 0);
PlayerLeftPosition : in UNSIGNED(11 downto 0);
PlayerLeftSize : in UNSIGNED(11 downto 0);
PlayerRightPosition : in UNSIGNED(11 downto 0);
PlayerRightSize : in UNSIGNED(11 downto 0);
PowerUpActive : in STD_LOGIC;
PowerUpType : in STD_LOGIC_VECTOR(2 downto 0);
PowerUpPositionY : in UNSIGNED(11 downto 0);
PowerUpPositionX : in UNSIGNED(11 downto 0);
SetBallVector : out STD_LOGIC;
NewBallVector : out STD_LOGIC_VECTOR(1 downto 0);
BallSpeedUp : out STD_LOGIC;
BallSpeedDown : out STD_LOGIC;
BallSizeUp : out STD_LOGIC;
BallSizeDown : out STD_LOGIC;
PlayerLeftScore : out STD_LOGIC;
PlayerLeftSpeedUp : out STD_LOGIC;
PlayerLeftSpeedDown : out STD_LOGIC;
PlayerLeftSizeUp : out STD_LOGIC;
PlayerLeftSizeDown : out STD_LOGIC;
PlayerRightScore : out STD_LOGIC;
PlayerRightSpeedUp : out STD_LOGIC;
PlayerRightSpeedDown : out STD_LOGIC;
PlayerRightSizeUp : out STD_LOGIC;
PlayerRightSizeDown : out STD_LOGIC;
PowerUpTurnOff : out STD_LOGIC);
end CollisionManager;
architecture CollisionManagerArch of CollisionManager is
constant max_y : UNSIGNED(11 downto 0) := X"1DF";
constant max_x : UNSIGNED(11 downto 0) := X"27F";
constant player_minimal_size : UNSIGNED(11 downto 0) := X"003"; -- Means total 7 length (3 on each side)
signal ball_vector : STD_LOGIC_VECTOR(1 downto 0);
begin
NewBallVector <= ball_vector;
process(Clk, Reset, BallVector, BallPositionY, BallPositionX, PlayerLeftPosition, PlayerRightPosition)
variable player_margin_up : UNSIGNED(11 downto 0) := X"000";
variable player_margin_down : UNSIGNED(11 downto 0) := X"000";
variable ball_margin_up : UNSIGNED(11 downto 0) := X"000";
variable ball_margin_down : UNSIGNED(11 downto 0) := X"000";
begin
if rising_edge(Clk) then
PlayerLeftScore <= '0';
PlayerRightScore <= '0';
if Reset = '1' then
ball_vector <= not BallVector;
SetBallVector <= '1';
else
SetBallVector <= '0';
ball_vector <= BallVector;
ball_margin_up := BallPositionY + BallSize;
ball_margin_down := BallPositionY - BallSize;
if BallVector(0) = '1' then
-- Check if hits right wall (point)
if BallPositionX >= max_x - BallSpeed - BallSize then
player_margin_up := PlayerRightPosition + PlayerRightSize + player_minimal_size;
player_margin_down := PlayerRightPosition - PlayerRightSize - player_minimal_size;
-- Bounce from right player (if on vector)
if (ball_margin_down <= player_margin_up and ball_margin_down >= player_margin_down) or -- Player bigger than ball
(ball_margin_up <= player_margin_up and ball_margin_up >= player_margin_down) or --/
(player_margin_down <= ball_margin_up and player_margin_down >= ball_margin_down) or -- Ball bigger than player
(player_margin_up <= ball_margin_up and player_margin_up >= ball_margin_down) then --/
SetBallVector <= '1';
ball_vector(0) <= '0';
else
PlayerLeftScore <= '1';
end if;
end if;
else
-- Check if hits left wall (point)
if BallPositionX <= BallSpeed + BallSize then
player_margin_up := PlayerLeftPosition + PlayerLeftSize + player_minimal_size;
player_margin_down := PlayerLeftPosition - PlayerLeftSize - player_minimal_size;
-- Bounce from left player (if on vector)
if (ball_margin_down <= player_margin_up and ball_margin_down >= player_margin_down) or -- Player bigger than ball
(ball_margin_up <= player_margin_up and ball_margin_up >= player_margin_down) or --/
(player_margin_down <= ball_margin_up and player_margin_down >= ball_margin_down) or -- Ball bigger than player
(player_margin_up <= ball_margin_up and player_margin_up >= ball_margin_down) then --/
SetBallVector <= '1';
ball_vector(0) <= '1';
else
PlayerRightScore <= '1';
end if;
end if;
end if;
if BallVector(1) = '1' then
-- Check if bounces from top wall
if BallPositionY >= max_y - BallSpeed - BallSize then
SetBallVector <= '1';
ball_vector(1) <= '0';
end if;
else
-- Check if bounces from down wall
if BallPositionY <= BallSpeed + BallSize then
SetBallVector <= '1';
ball_vector(1) <= '1';
end if;
end if;
end if;
end if;
end process;
process(Clk, ball_vector, BallPositionY, BallPositionX, PowerUpActive)
variable ball_next_y : UNSIGNED(11 downto 0) := X"000";
variable ball_next_x : UNSIGNED(11 downto 0) := X"000";
begin
if rising_edge(Clk) then
BallSpeedUp <= '0';
BallSpeedDown <= '0';
BallSizeUp <= '0';
BallSizeDown <= '0';
PlayerLeftSpeedUp <= '0';
PlayerLeftSpeedDown <= '0';
PlayerLeftSizeUp <= '0';
PlayerLeftSizeDown <= '0';
PlayerRightSpeedUp <= '0';
PlayerRightSpeedDown <= '0';
PlayerRightSizeUp <= '0';
PlayerRightSizeDown <= '0';
PowerUpTurnOff <= '0';
-- Check power up
if PowerUpActive = '1' then
if ball_vector(0) = '1' then
ball_next_x := BallPositionX + BallSpeed;
else
ball_next_x := BallPositionX - BallSpeed;
end if;
if ball_vector(1) = '1' then
ball_next_y := BallPositionY + BallSpeed;
else
ball_next_y := BallPositionY - BallSpeed;
end if;
case ball_vector is
when "00" => -- Down left
if PowerUpPositionX >= ball_next_x and PowerUpPositionX <= BallPositionX and
PowerUpPositionY >= ball_next_y and PowerUpPositionY <= BallPositionY then
PowerUpTurnOff <= '1';
case PowerUpType is
when "000" =>
BallSpeedUp <= '1';
when "001" =>
BallSpeedDown <= '1';
when "010" =>
BallSizeUp <= '1';
when "011" =>
BallSizeDown <= '1';
when "100" =>
if ball_vector(0) = '1' then
PlayerLeftSpeedUp <= '1';
else
PlayerRightSpeedUp <= '1';
end if;
when "101" =>
if ball_vector(0) = '1' then
PlayerLeftSpeedDown <= '1';
else
PlayerRightSpeedDown <= '1';
end if;
when "110" =>
if ball_vector(0) = '1' then
PlayerLeftSizeUp <= '1';
else
PlayerRightSizeUp <= '1';
end if;
when "111" =>
if ball_vector(0) = '1' then
PlayerLeftSizeDown <= '1';
else
PlayerRightSizeDown <= '1';
end if;
when others =>
end case;
end if;
when "10" => -- Up left
if PowerUpPositionX >= ball_next_x and PowerUpPositionX <= BallPositionX and
PowerUpPositionY >= BallPositionY and PowerUpPositionY <= ball_next_y then
PowerUpTurnOff <= '1';
case PowerUpType is
when "000" =>
BallSpeedUp <= '1';
when "001" =>
BallSpeedDown <= '1';
when "010" =>
BallSizeUp <= '1';
when "011" =>
BallSizeDown <= '1';
when "100" =>
if ball_vector(0) = '1' then
PlayerLeftSpeedUp <= '1';
else
PlayerRightSpeedUp <= '1';
end if;
when "101" =>
if ball_vector(0) = '1' then
PlayerLeftSpeedDown <= '1';
else
PlayerRightSpeedDown <= '1';
end if;
when "110" =>
if ball_vector(0) = '1' then
PlayerLeftSizeUp <= '1';
else
PlayerRightSizeUp <= '1';
end if;
when "111" =>
if ball_vector(0) = '1' then
PlayerLeftSizeDown <= '1';
else
PlayerRightSizeDown <= '1';
end if;
when others =>
end case;
end if;
when "01" => -- Down right
if PowerUpPositionX >= BallPositionX and PowerUpPositionX <= ball_next_x and
PowerUpPositionY >= ball_next_y and PowerUpPositionY <= BallPositionY then
PowerUpTurnOff <= '1';
case PowerUpType is
when "000" =>
BallSpeedUp <= '1';
when "001" =>
BallSpeedDown <= '1';
when "010" =>
BallSizeUp <= '1';
when "011" =>
BallSizeDown <= '1';
when "100" =>
if ball_vector(0) = '1' then
PlayerLeftSpeedUp <= '1';
else
PlayerRightSpeedUp <= '1';
end if;
when "101" =>
if ball_vector(0) = '1' then
PlayerLeftSpeedDown <= '1';
else
PlayerRightSpeedDown <= '1';
end if;
when "110" =>
if ball_vector(0) = '1' then
PlayerLeftSizeUp <= '1';
else
PlayerRightSizeUp <= '1';
end if;
when "111" =>
if ball_vector(0) = '1' then
PlayerLeftSizeDown <= '1';
else
PlayerRightSizeDown <= '1';
end if;
when others =>
end case;
end if;
when "11" => -- Up right
if PowerUpPositionX >= BallPositionX and PowerUpPositionX <= ball_next_x and
PowerUpPositionY >= BallPositionY and PowerUpPositionY <= ball_next_y then
PowerUpTurnOff <= '1';
case PowerUpType is
when "000" =>
BallSpeedUp <= '1';
when "001" =>
BallSpeedDown <= '1';
when "010" =>
BallSizeUp <= '1';
when "011" =>
BallSizeDown <= '1';
when "100" =>
if ball_vector(0) = '1' then
PlayerLeftSpeedUp <= '1';
else
PlayerRightSpeedUp <= '1';
end if;
when "101" =>
if ball_vector(0) = '1' then
PlayerLeftSpeedDown <= '1';
else
PlayerRightSpeedDown <= '1';
end if;
when "110" =>
if ball_vector(0) = '1' then
PlayerLeftSizeUp <= '1';
else
PlayerRightSizeUp <= '1';
end if;
when "111" =>
if ball_vector(0) = '1' then
PlayerLeftSizeDown <= '1';
else
PlayerRightSizeDown <= '1';
end if;
when others =>
end case;
end if;
when others =>
end case;
end if;
end if;
end process;
end CollisionManagerArch;