-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathPlayerControl.v
More file actions
122 lines (114 loc) · 4.68 KB
/
PlayerControl.v
File metadata and controls
122 lines (114 loc) · 4.68 KB
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
`include "define.v"
module PlayerControl #(
speed = 20
)
(
input clk,
input reset,
input w, input a, input s, input d,
input [$clog2(`WIDTH) - 1:0] x,
input [$clog2(`HEIGHT) - 1:0] y,
output reg [$clog2(`WIDTH) - 1:0] next_x,
output reg [$clog2(`HEIGHT) - 1:0] next_y,
inout reg [1:0] player_direction,
input [`tile_row_num * `tile_col_num - 1:0] tilemap_walls,
input [`tile_row_num * `tile_col_num - 1:0] tilemap_dots
);
wire [20:0] tile_idx;
reg [1:0] cur_dir;
assign tile_idx = (`WIDTH / `tile_size)*(y/`tile_size) + x/`tile_size;
function [$clog2(`WIDTH) - 1:0] x_axes(input [1:0] dir, input [$clog2(`WIDTH) - 1:0] x, input [$clog2(`WIDTH) - 1:0] y, input [`tile_row_num * `tile_col_num - 1:0] tilemap_walls, input [20:0] tile_idx);
if( dir == `dir_up)
x_axes = x;
else if( dir == `dir_down)
x_axes = x;
else if( dir == `dir_left && tilemap_walls[tile_idx - 1] == 0)
x_axes = x - speed;
else if( dir == `dir_left && tilemap_walls[tile_idx - 1] == 1)
x_axes = x;
else if( dir == `dir_right && tilemap_walls[tile_idx + 1] == 0)
x_axes = x + speed;
else
x_axes = x;
endfunction
function [$clog2(`WIDTH) - 1:0] y_axes(input [1:0] dir, input [$clog2(`WIDTH) - 1:0] x, input [$clog2(`WIDTH) - 1:0] y, input [`tile_row_num * `tile_col_num - 1:0] tilemap_walls, input [20:0] tile_idx);
if( dir == `dir_up && tilemap_walls[tile_idx - `tile_col_num] == 0)
y_axes = y - speed;
else if(dir == `dir_up && tilemap_walls[tile_idx - `tile_col_num] == 1)
y_axes = y;
else if( dir == `dir_down && tilemap_walls[tile_idx + `tile_col_num] == 0)
y_axes = y + speed;
else if(dir == `dir_down && tilemap_walls[tile_idx + `tile_col_num] == 1)
y_axes = y;
else if( dir == `dir_left)
y_axes = y;
else if( dir == `dir_right)
y_axes = y;
endfunction
always @(posedge clk or negedge reset) begin
if (!reset) begin
next_x <= 20;
next_y <= 20;
player_direction <= `dir_left;
end
else begin
if(!w) begin
if (tilemap_walls[tile_idx - `tile_col_num] == 1) begin
cur_dir <= player_direction;
next_x <= x_axes(player_direction,x,y,tilemap_walls,tile_idx);
next_y <= y_axes(player_direction,x,y,tilemap_walls,tile_idx);
end
else begin
cur_dir <= `dir_up;
player_direction <= `dir_up;
next_x <= x;
next_y <= y - speed;
end
end
else if(!s) begin
if (tilemap_walls[tile_idx + `tile_col_num] == 1) begin
cur_dir <= player_direction;
next_x <= x_axes(player_direction,x,y,tilemap_walls,tile_idx);
next_y <= y_axes(player_direction,x,y,tilemap_walls,tile_idx);
end
else begin
cur_dir <= `dir_down;
player_direction <= `dir_down;
next_x <= x;
next_y <= y + speed;
end
end
else if(!a) begin
if (tilemap_walls[tile_idx - 1] == 1) begin
cur_dir <= player_direction;
next_x <= x_axes(player_direction,x,y,tilemap_walls,tile_idx);
next_y <= y_axes(player_direction,x,y,tilemap_walls,tile_idx);
end
else begin
cur_dir <= `dir_left;
player_direction <= `dir_left;
next_x <= x - speed;
next_y <= y;
end
end
else if(!d) begin
if (tilemap_walls[tile_idx+1] == 1) begin
cur_dir <= player_direction;
next_x <= x_axes(player_direction,x,y,tilemap_walls,tile_idx);
next_y <= y_axes(player_direction,x,y,tilemap_walls,tile_idx);
end
else begin
cur_dir <= `dir_right;
player_direction <= `dir_right;
next_x <= x + speed;
next_y <= y;
end
end
else begin
cur_dir <= player_direction;
next_x <= x_axes(player_direction,x,y,tilemap_walls,tile_idx);
next_y <= y_axes(player_direction,x,y,tilemap_walls,tile_idx);
end
end
end
endmodule