-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayer.java
More file actions
133 lines (113 loc) · 5.05 KB
/
Player.java
File metadata and controls
133 lines (113 loc) · 5.05 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
123
124
125
126
127
128
129
130
131
132
133
import java.awt.*;
import java.util.List;
public class Player implements Constants {
// x and y are the coordinates of the top left corner of the player
private float x;
private float y;
private final int radius = 6;
private double angle = Math.PI;
private final int speed;
private final double sense;
public Player(float x, float y) { // Constructor
this.x = x;
this.y = y;
this.speed = 3;
this.sense = 0.1;
}
// Draw the player
public void draw(Graphics g) {
g.setColor(red);
g.fillOval((int) this.x, (int) this.y, 2 * radius, 2 * radius);
}
// Move the player
public void move(List<Block> blocks, MyKeyListener mkl) {
double dx = 0; // The x component of the movement vector
double dy = 0; // The y component of the movement vector
if (mkl.up) { // If the up key is pressed, move the player forward
dx += -1 * (speed * Math.cos(angle));
dy += -1 * (speed * Math.sin(angle));
}
if (mkl.down) { // If the down key is pressed, move the player backward
dx += (speed * Math.cos(angle));
dy += (speed * Math.sin(angle));
}
if (mkl.left) { // If the left key is pressed, move the player left
dx += -1 * (speed * Math.sin(angle));
dy += (speed * Math.cos(angle));
}
if (mkl.right) { // If the right key is pressed, move the player right
dx += (speed * Math.sin(angle));
dy += -1 * (speed * Math.cos(angle));
}
// Normalize the movement vector
double length = Math.sqrt(dx * dx + dy * dy);
if (length > speed) { // If the length is greater than the speed, normalize it
double factor = speed / length;
dx *= factor;
dy *= factor;
}
if (mkl.rotateLeft) { // If the left arrow key is pressed, rotate the player left
angle -= sense;
}
if (mkl.rotateRight) { // If the right arrow key is pressed, rotate the player right
angle += sense;
}
if (dx != 0 || dy != 0) { // If the player is moving, check for collisions
for (Block block : blocks) { // Check for collisions with each block
if (block.isColliding((float) (this.x + dx), (float) (this.y + dy), radius)) {
dx = 0;
dy = 0;
break;
}
}
}
this.x += dx; // Move the player
this.y += dy;
}
// Cast rays to render the 3D view
public void cast(List<Block> blocks, Graphics g) {
double startAngle = angle + (Math.PI / 3); // The angle of the first ray
for (int ray = 0; ray < NUM_RAYS; ray++) { // Cast NUM_RAYS rays
Block hitBlock = null; // The block that the ray hits
for (double depth = 0; depth < MAX_DEPTH; depth++) { // Cast the ray until it hits a block
double targetX = this.x - (depth * Math.sin(startAngle)); // The x coordinate of the target
double targetY = this.y + (depth * Math.cos(startAngle)); // The y coordinate of the target
g.setColor(yellow);
g.drawLine((int) this.x + radius, (int) this.y + radius,
(int) targetX + radius, (int) targetY + radius); // Draw the ray
for (Block block : blocks) { // Check if the ray hits a block
if (block.x <= targetX + radius && targetX + radius <= block.x + SIZE &&
block.y <= targetY + radius && targetY + radius <= block.y + SIZE) {
hitBlock = block;
break;
}
}
if (hitBlock != null) { // If the ray hits a block, render the 3D view
int color = 255 - (int) (depth / 2);
if (color <= 0) {
color = 0;
}
hitBlock.setColor(new Color(0, color, 0));
depth *= Math.cos(this.angle - startAngle); // Correct the fisheye effect
if (depth == 0) {
depth = 0.001;
}
int height = (int) ((HEIGHT / depth) * Math.cos(startAngle - this.angle)
* SIZE); // The height of the wall
if (height > HEIGHT) {
height = HEIGHT;
}
// The coordinates of the rectangle to be drawn
int rectX = (ray * WIDTH / NUM_RAYS) / 2 + WIDTH / 2;
int rectY = (HEIGHT - height) / 2;
int rectWidth = (WIDTH / NUM_RAYS + 1) / 2;
int rectHeight = height;
g.setColor(new Color(0, color, 0));
g.fillRect(rectX, rectY, rectWidth, rectHeight);
break;
}
}
startAngle += Math.PI / (NUM_RAYS * 3); // Increment the angle of the next ray
}
}
}