package towerGame import java awt import static towerGame GameLogic TI

  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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
package towerGame;
import java.awt.*;
import static towerGame.GameLogic.TILE_W;
import static towerGame.GameLogic.TILE_H;
public class Player extends Sprite
{
public static final double GRAVITY = 0.5;
public static final double X_ACCEL = 0.6;
public static final double X_FRICTION = 0.9;
public static final double Y_FRICTION = 0.98;
public static final int BOUND_W = 16;
public static final int BOUND_H = 32;
public static final double MAX_SPEED = 16.0;
public static final double RUN_SPEED = 8.0;
public static final double JUMP_SPEED = 8.0;
public static final int ANIM_SPEED = 5;
public static final double Y_ACCEL = 1.25;
public static final int JUMP_TIME = 15;
public static final double HURT_Y_SPEED = -15.0;
public static final double HURT_X_SPEED = 10.0;
public static final double ICE_FRICTION = 0.999;
public static final double ICE_ACCEL = 0.1;
private double xSpeed;
private double ySpeed;
private State state;
private int airTime = 0;
private boolean facingLeft = false;
private int animCount = 0;
private static abstract class State
{
public void enterState() throws Exception {}
public void draw(Graphics2D g, int camX, int camY) throws Exception {}
public void logic(boolean[] keyState) throws Exception {}
public void leaveState() throws Exception {}
}
public final State STATE_ALIVE = new State()
{
private boolean upDown = false;
public void enterState()
{
upDown = false;
}
public void logic(boolean[] keyState) throws Exception
{
animCount++;
move();
World world = GameLogic.get().getWorld();
boolean hitFloorLeft = world.isFloor(x-(BOUND_W/2-1), y+BOUND_H/2);
boolean hurtyFloorLeft = world.isHurty(x-(BOUND_W/2-1), y+BOUND_H/2);
boolean hitFloorRight = world.isFloor(x+(BOUND_W/2-1), y+BOUND_H/2);
boolean hurtyFloorRight = world.isHurty(x+(BOUND_W/2-1), y+BOUND_H/2);
boolean hitFloor = ySpeed >= 0 && (hitFloorLeft || hitFloorRight);
boolean hurtyFloor = ySpeed >= 0 && (hurtyFloorLeft || hurtyFloorRight);
boolean hitLeftWallUpper = world.isWall(x-BOUND_W/2, y-BOUND_W/2);
boolean hurtyLeftWallUpper = world.isHurty(x-BOUND_W/2, y-BOUND_W/2);
boolean hitLeftWallLower = world.isWall(x-BOUND_W/2, y+BOUND_W/2);
boolean hurtyLeftWallLower = world.isHurty(x-BOUND_W/2, y+BOUND_W/2);
boolean hitLeftWall = hitLeftWallLower || hitLeftWallUpper;
boolean hurtyLeftWall = hurtyLeftWallLower || hurtyLeftWallUpper;
boolean hitRightWallUpper = world.isWall(x+BOUND_W/2, y-BOUND_W/2);
boolean hurtyRightWallUpper = world.isHurty(x+BOUND_W/2, y-BOUND_W/2);
boolean hitRightWallLower = world.isWall(x+BOUND_W/2, y+BOUND_W/2);
boolean hurtyRightWallLower = world.isHurty(x+BOUND_W/2, y+BOUND_W/2);
boolean hitRightWall = hitRightWallLower || hitRightWallUpper;
boolean hurtyRightWall = hurtyRightWallLower || hurtyRightWallUpper;
boolean hitCeilingLeft = world.isCeiling(x-(BOUND_W/2-1),y-BOUND_H/2);
boolean hurtyCeilingLeft = world.isHurty(x-(BOUND_W/2-1), y-BOUND_H/2);
boolean hitCeilingRight = world.isCeiling(x+(BOUND_W/2-1), y-BOUND_H/2);
boolean hurtyCeilingRight = world.isHurty(x+(BOUND_W/2-1), y-BOUND_H/2);
boolean hitCeiling = ySpeed <= 0 && (hitCeilingLeft || hitCeilingRight);
boolean hurtyCeiling = ySpeed <= 0 && (hurtyCeilingLeft || hurtyCeilingRight);
//check for out of level death
if(y > world.getHeight()*TILE_H){
changeState(STATE_DEAD);
ySpeed = HURT_Y_SPEED;
return;
}
//check for hurty tile death
if(hurtyFloor || hurtyCeiling)
{
/*changeState(STATE_DEAD);
ySpeed = HURT_Y_SPEED;
return;*/
}
else if(hurtyLeftWall)
{
/*changeState(STATE_DEAD);
xSpeed = HURT_X_SPEED;
ySpeed = HURT_Y_SPEED;
return;*/
}
else if(hurtyRightWall)
{
/*changeState(STATE_DEAD);
xSpeed = -HURT_X_SPEED;
ySpeed = HURT_Y_SPEED;
return;*/
}
//not hurt - lives on
if(hitFloor)
{
y = (int)y/TILE_H*TILE_H + TILE_H - BOUND_H/2;
ySpeed = 0;
}
if(hitCeiling)
{
y = (int)y/TILE_H*TILE_H + BOUND_H/2;
ySpeed = 0;
}
if(hitLeftWall)
{
x = (int)x/TILE_W*TILE_W + BOUND_W/2;
xSpeed = 0;
}
if(hitRightWall)
{
x = (int)x/TILE_W*TILE_W + TILE_W - BOUND_W/2;
xSpeed = 0;
}
boolean atFloorLeft = world.isFloor(x-(BOUND_W/2-1), y+BOUND_H/2+1);
boolean atSlippyFloorLeft = world.isSlippy(x-(BOUND_W/2-1), y+BOUND_H/2+1);
boolean atFloorRight = world.isFloor(x+(BOUND_W/2-1), y+BOUND_H/2+1);
boolean atSlippyFloorRight = world.isSlippy(x+(BOUND_W/2-1), y+BOUND_H/2+1);
boolean atFloor = atFloorLeft || atFloorRight;
boolean atSlippyFloor = atSlippyFloorLeft || atSlippyFloorRight;
boolean atLeftWallUpper = world.isWall(x-(BOUND_W/2+1), y-BOUND_W/2);
boolean atLeftWallLower = world.isWall(x-(BOUND_W/2+1), y+BOUND_W/2);
boolean atLeftWall = atLeftWallLower || atLeftWallUpper;
boolean atRightWallUpper = world.isWall(x+(BOUND_W/2+1), y-BOUND_W/2);
boolean atRightWallLower = world.isWall(x+(BOUND_W/2+1), y+BOUND_W/2);
boolean atRightWall = atRightWallLower || atRightWallUpper;
if(!atFloor){
airTime ++;
}else{
airTime = 0;
}
boolean running = false;
if(keyState[Main.KEY_LEFT])
{
if(xSpeed > -RUN_SPEED){
if(atSlippyFloor){
xSpeed -= ICE_ACCEL;
}else{
xSpeed -= X_ACCEL;
}
}
facingLeft = true;
if(atFloor){
running = true;
}
}
else if(keyState[Main.KEY_RIGHT])
{
if(xSpeed < RUN_SPEED){
if(atSlippyFloor){
xSpeed += ICE_ACCEL;
}else{
xSpeed += X_ACCEL;
}
}
facingLeft = false;
if(atFloor){
running = true;
}
}
if(keyState[Main.KEY_FIRE1] && airTime < JUMP_TIME)
{
if(ySpeed > -JUMP_SPEED){
ySpeed -= Y_ACCEL;
}
}
if(keyState[Main.KEY_UP] && !upDown){
upDown = true;
}
if(!keyState[Main.KEY_UP] && upDown){
upDown = false;
//pressed and released UP, check for doorway
int door = -1;
if((door = world.getDoorway((int)(x/TILE_W), (int)(y/TILE_H))) != -1)
{
if(door == GameLogic.END_DOOR)
{
GameLogic.get().gameCompleted();
}
else
{
GameLogic.get().switchWorlds();
world = GameLogic.get().getWorld();
int[] doorPos = world.getDoorwayPos(door);
x = doorPos[0] * TILE_W + TILE_W/2;
y = doorPos[1] * TILE_H + TILE_H/2;
xSpeed = 0;
ySpeed = 0;
}
}
}
if(running)
{
//running
frameY = facingLeft ? 2 : 1;
frameX = (animCount/ANIM_SPEED) % 6;
}
else
{
if(atFloor)
{
//stopped
frameY = 0;
frameX = facingLeft ? 1 : 0;
}
else
{
if(ySpeed <= 0)
{
//jumping
frameY = 3;
frameX = (facingLeft ? 2 : 0) + (animCount/ANIM_SPEED) % 2;
}
else
{
//falling
frameY = 4;
frameX = (facingLeft ? 2 : 0) + (animCount/ANIM_SPEED) % 2;
}
}
}
limitSpeed(atSlippyFloor);
}
};
public final State STATE_DEAD = new State()
{
public void logic(boolean[] keyState)
{
animCount ++;
move();
frameY = 5;
if(ySpeed <= 0){
frameX = 0;
}else{
frameX = 1 + (animCount/ANIM_SPEED) % 2;
}
limitSpeed(false);
}
};
public Player() throws Exception
{
sheet = GameLogic.imgPlayer;
frameW = 32;
frameH = 32;
frameX = 0;
frameY = 0;
changeState(STATE_ALIVE);
}
private void move()
{
x += xSpeed;
y += ySpeed;
if(x > World.WORLD_W*TILE_W){
x -= World.WORLD_W*TILE_W;
}
if(x < 0){
x += World.WORLD_W*TILE_W;
}
}
private void limitSpeed(boolean ice)
{
if(ice){
xSpeed *= ICE_FRICTION;
}else{
xSpeed *= X_FRICTION;
}
if(xSpeed > MAX_SPEED){
xSpeed = MAX_SPEED;
}
ySpeed += GRAVITY;
ySpeed *= Y_FRICTION;
if(ySpeed > MAX_SPEED){
ySpeed = MAX_SPEED;
}
}
private void changeState(State newState) throws Exception
{
if(state!=null){
state.leaveState();
}
state = newState;
if(state!=null){
state.enterState();
}
}
public void draw(Graphics2D g, int camX, int camY) throws Exception
{
super.draw(g, camX, camY);
state.draw(g, camX, camY);
//g.setColor(Color.RED);
//g.drawRect(Main.SCRN_W/2 + (int)(x-camX-32), Main.SCRN_H/2 + (int)(y-camY-32), 64, 64);
}
public void logic(boolean[] keyState) throws Exception
{
state.logic(keyState);
}
public final State getState()
{
return state;
}
}