Repository navigation
Expand file tree
/
Copy pathPlayer.java
More file actions
132 lines (98 loc) · 3.14 KB
/
Copy pathPlayer.java
File metadata and controls
132 lines (98 loc) · 3.14 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
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.geom.Arc2D;
public class Player {
private double x, y;
private final int startRow, startCol;
private Direction direction = Direction.NONE;
private Direction nextDirection = Direction.NONE;
private Direction facing = Direction.RIGHT;
private static final double SPEED = 2.0;
private double mouthAngle = 10;
private boolean mouthOpening = true;
public Player(int startRow, int startCol) {
this.startRow = startRow;
this.startCol = startCol;
resetPosition();
}
public void resetPosition() {
x = startCol * Maze.TILE_SIZE;
y = startRow * Maze.TILE_SIZE;
direction = Direction.NONE;
nextDirection = Direction.NONE;
facing = Direction.RIGHT;
}
public void setNextDirection(Direction d) {
nextDirection = d;
}
public int getRow() {
return (int) Math.round(y / Maze.TILE_SIZE);
}
public int getCol() {
return (int) Math.round(x / Maze.TILE_SIZE);
}
public double getPixelX() {
return x;
}
public double getPixelY() {
return y;
}
public Direction getDirectionFacing() {
return facing;
}
private boolean isAlignedToGrid() {
return Math.abs(x % Maze.TILE_SIZE) < 0.0001 && Math.abs(y % Maze.TILE_SIZE) < 0.0001;
}
private boolean canMove(Direction d, Maze maze) {
if (d == Direction.NONE) {
return false;
}
int row = getRow();
int col = getCol();
return !maze.isWall(row + d.dy, col + d.dx);
}
public void update(Maze maze) {
if (isAlignedToGrid()) {
x = Math.round(x / Maze.TILE_SIZE) * Maze.TILE_SIZE;
y = Math.round(y / Maze.TILE_SIZE) * Maze.TILE_SIZE;
if (nextDirection != direction && canMove(nextDirection, maze)) {
direction = nextDirection;
}
if (!canMove(direction, maze)) {
direction = Direction.NONE;
}
}
if (direction != Direction.NONE) {
x += direction.dx * SPEED;
y += direction.dy * SPEED;
facing = direction;
if (mouthOpening) {
mouthAngle += 4;
if (mouthAngle >= 42) {
mouthOpening = false;
}
} else {
mouthAngle -= 4;
if (mouthAngle <= 4) {
mouthOpening = true;
}
}
}
}
public void draw(Graphics2D g2d) {
int size = Maze.TILE_SIZE;
double base;
switch (facing) {
case UP: base = 90; break;
case DOWN: base = 270; break;
case LEFT: base = 180; break;
case RIGHT:
default: base = 0; break;
}
double startAngle = base + mouthAngle / 2.0;
double extentAngle = 360 - mouthAngle;
g2d.setColor(Color.YELLOW);
Arc2D pacShape = new Arc2D.Double(x, y, size, size, startAngle, extentAngle, Arc2D.PIE);
g2d.fill(pacShape);
}
}