-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sprite.java
192 lines (162 loc) · 5.78 KB
/
Sprite.java
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
package PacmanPack;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.image.ImageObserver;
import static PacmanPack.Pacman.*;
public abstract class Sprite implements ImageObserver {
protected int matrixRow;
protected int matrixCol;
protected double virtualRow;
protected double virtualCol;
protected Cell spriteCell;
protected Cell prevCell = null;
protected Cell startingCell;
protected double spriteSpeed = 0;
protected double spriteBaseSpeed = spriteSpeed;
protected int spriteLevel;
protected Pacman.Facing spriteFacing;
protected BufferedImage[] dance;
protected BufferedImage[] danceLeft;
protected BufferedImage[] danceRight;
protected BufferedImage[] danceUp;
protected BufferedImage[] danceDown;
protected BufferedImage[] danceBlue;
protected BufferedImage[] spriteDeath;
protected BufferedImage[] danceBlink;
protected BufferedImage[] eatenLeft;
protected BufferedImage[] eatenRight;
protected BufferedImage[] eatenUp;
protected BufferedImage[] eatenDown;
protected Animation[] normalDanceAnimation;
protected Animation[] blueAnimation;
protected Animation[] blinkAnimation;
protected Animation[] eatenAnimation;
protected Animation[] staticDanceAnimation;
protected Animation pacmanDeathAnimation;
private Animation animation;
protected boolean dancingInGhostHouse = false;
private double ghostHouseDanceOffset = 0;
private int ghostHouseDanceDir = 0;
public abstract Cell move(boolean chase);
protected Sprite(Cell cell, int spriteLevel) {
spriteCell = cell;
matrixRow = cell.getRow();
matrixCol = cell.getCol();
virtualRow = matrixRow;
virtualCol = matrixCol;
this.spriteLevel = spriteLevel;
}
public void setDancingInGhostHouse(boolean dancingInGhostHouse) {
this.dancingInGhostHouse = dancingInGhostHouse;
}
public double getVirtualRow() {
return virtualRow;
}
public double getVirtualCol() {
return virtualCol;
}
protected void startDeathAnimation() {
pacmanDeathAnimation = new Animation(spriteDeath, 5);
animation = pacmanDeathAnimation;
animation.playOnce();
}
protected void startAnimation(Animation animation) {
this.animation = animation;
animation.start();
}
public void draw(Graphics g) {
animation.update();
int screenY = (int) (8 + overrideRow() * H_COEFF - 32 / 2);
int screenX = (int) (8 + (overrideCol() - VIRT_BORDERS / 2) * W_COEFF - 32 / 2);
g.drawImage(animation.getSpriteFrame(), screenX, screenY, this);
}
protected double overrideRow() {
double overridenRow = virtualRow;
double danceRad = 0.65;
double offsetStep = 0.05;
if (dancingInGhostHouse && !standStill) {
if (ghostHouseDanceOffset < -danceRad) {
spriteFacing = Facing.DOWN;
ghostHouseDanceDir = 0;
} else if (ghostHouseDanceOffset > danceRad) {
spriteFacing = Facing.UP;
ghostHouseDanceDir = 1;
}
if (ghostHouseDanceDir == 0) {
ghostHouseDanceOffset += offsetStep;
} else {
ghostHouseDanceOffset -= offsetStep;
}
overridenRow += ghostHouseDanceOffset;
}
return overridenRow;
}
protected double overrideCol() {
double overridenCol = virtualCol;
if (matrixRow == 14) {
if (virtualCol == 13) {
overridenCol = 12.8;
} else if (virtualCol == 15) {
overridenCol = 14.6;
} else if (virtualCol == 16) {
overridenCol = 16.5;
}
} else if (12 <= matrixRow && matrixRow <= 13) {
if (14 <= virtualCol && virtualCol <= 15) {
overridenCol = 14.5;
}
}
return overridenCol;
}
@Override
public boolean imageUpdate(Image img, int infoflags, int x, int y, int width, int height) {
return false;
}
public Cell getCell() {
return spriteCell;
}
protected void locateSpriteCell() {
spriteCell = Pacman.field[matrixRow][matrixCol];
}
protected void moveToNextCell(Cell nextCell) {
if (nextCell.getType() == Type.WALL) {
nextCell = spriteCell;
}
double epsilon = 0.0001;
transferThroughLimbo();
moveVirtualCol(nextCell, epsilon);
moveVirtualRow(nextCell, epsilon);
}
private void transferThroughLimbo() {
if (spriteCell.getType() == Type.R_PORTAL && spriteFacing == Facing.RIGHT) {
virtualCol = 0;
} else if (spriteCell.getType() == Type.L_PORTAL && spriteFacing == Facing.LEFT) {
virtualCol = R_PORTAL_COL;
}
}
private void moveVirtualRow(Cell nextCell, double epsilon) {
if (Math.abs(nextCell.getRow() - virtualRow) < epsilon) {
matrixRow = (int) Math.round(virtualRow);
} else if (virtualRow < nextCell.getRow()) {
virtualRow += spriteSpeed;
} else if (virtualRow > nextCell.getRow()) {
virtualRow -= spriteSpeed;
}
}
private void moveVirtualCol(Cell nextCell, double epsilon) {
if (Math.abs(nextCell.getCol() - virtualCol) < epsilon) {
matrixCol = (int) Math.round(virtualCol);
} else if (virtualCol < nextCell.getCol()) {
virtualCol += spriteSpeed;
} else if (virtualCol > nextCell.getCol()) {
virtualCol -= spriteSpeed;
}
}
public void resetSpeed() {
spriteSpeed = spriteBaseSpeed;
}
public void snapToCell() {
virtualRow = matrixRow;
virtualCol = matrixCol;
}
}