|
35 | 35 | import static io.exterminator3618.client.Physics.checkPowerUpCollision; |
36 | 36 | import io.exterminator3618.client.components.Ball; |
37 | 37 | import io.exterminator3618.client.components.Brick; |
| 38 | +import io.exterminator3618.client.components.ExtraLifePowerUp; |
| 39 | +import io.exterminator3618.client.components.HeavyBallPowerUp; |
| 40 | +import io.exterminator3618.client.components.MultiBallBrick; |
| 41 | +import io.exterminator3618.client.components.NormalBrick; |
38 | 42 | import io.exterminator3618.client.components.Paddle; |
39 | 43 | import io.exterminator3618.client.components.PowerUp; |
40 | 44 | import io.exterminator3618.client.components.PowerUpBrick; |
| 45 | +import io.exterminator3618.client.components.SlowBallPowerUp; |
| 46 | +import io.exterminator3618.client.components.SolidBrick; |
| 47 | +import io.exterminator3618.client.components.SplitBallPowerUp; |
| 48 | +import io.exterminator3618.client.components.StickyPaddlePowerUp; |
41 | 49 | import io.exterminator3618.client.components.StrongBrick; |
42 | 50 | import io.exterminator3618.client.components.TextButton; |
| 51 | +import io.exterminator3618.client.components.WidenPaddlePowerUp; |
43 | 52 | import io.exterminator3618.client.managers.SoundManager; |
44 | 53 | import io.exterminator3618.client.utils.Assets; |
| 54 | +import io.exterminator3618.client.utils.GameSaveData; |
45 | 55 | import io.exterminator3618.client.utils.LevelLoader; |
46 | 56 | import io.exterminator3618.client.utils.Renderer; |
47 | 57 |
|
@@ -128,6 +138,168 @@ public void loadLevel(int levelNumber, Ball oldball) { |
128 | 138 | ball.setStuckToPaddle(true); |
129 | 139 | } |
130 | 140 |
|
| 141 | + |
| 142 | + public GameSaveData.SaveData exportState() { |
| 143 | + GameSaveData.SaveData d = new GameSaveData.SaveData(); |
| 144 | + d.level = currentLevel; |
| 145 | + d.score = score; |
| 146 | + d.lives = lives; |
| 147 | + |
| 148 | + GameSaveData.Obj b = new GameSaveData.Obj(); |
| 149 | + b.x = ball.getX(); |
| 150 | + b.y = ball.getY(); |
| 151 | + b.width = ball.getWidth(); |
| 152 | + b.height = ball.getHeight(); |
| 153 | + b.vx = ball.getVelocityX(); |
| 154 | + b.vy = ball.getVelocityY(); |
| 155 | + b.region = ball.getRegionName(); |
| 156 | + b.bool1 = ball.isHeavyBall(); |
| 157 | + b.bool2 = ball.isStuckToPaddle(); |
| 158 | + b.i1 = ball.getStuckOffsetX(); |
| 159 | + b.cb = ball.getComboCount(); |
| 160 | + d.ball = b; |
| 161 | + |
| 162 | + GameSaveData.Obj p = new GameSaveData.Obj(); |
| 163 | + p.x = paddle.getX(); |
| 164 | + p.y = paddle.getY(); |
| 165 | + p.width = paddle.getWidth(); |
| 166 | + p.height = paddle.getHeight(); |
| 167 | + p.region = paddle.getRegionName(); |
| 168 | + p.bool1 = paddle.isSticky(); |
| 169 | + d.paddle = p; |
| 170 | + |
| 171 | + d.extraBalls = new ArrayList<>(); |
| 172 | + for (Ball eb : extraBalls) { |
| 173 | + GameSaveData.Obj o = new GameSaveData.Obj(); |
| 174 | + o.x = eb.getX(); |
| 175 | + o.y = eb.getY(); |
| 176 | + o.width = eb.getWidth(); |
| 177 | + o.height = eb.getHeight(); |
| 178 | + o.vx = eb.getVelocityX(); |
| 179 | + o.vy = eb.getVelocityY(); |
| 180 | + o.region = eb.getRegionName(); |
| 181 | + o.bool1 = eb.isHeavyBall(); |
| 182 | + o.bool2 = eb.isStuckToPaddle(); |
| 183 | + o.i1 = eb.getStuckOffsetX(); |
| 184 | + d.extraBalls.add(o); |
| 185 | + } |
| 186 | + |
| 187 | + d.bricks = new ArrayList<>(); |
| 188 | + for (Brick bk : bricks) { |
| 189 | + if (bk.isDestroyed()) continue; |
| 190 | + GameSaveData.BrickState bs = new GameSaveData.BrickState(); |
| 191 | + bs.x = bk.getX(); |
| 192 | + bs.y = bk.getY(); |
| 193 | + bs.width = bk.getWidth(); |
| 194 | + bs.height = bk.getHeight(); |
| 195 | + bs.region = bk.getRegionName(); |
| 196 | + bs.hp = bk.getHitPoints(); |
| 197 | + bs.type = bk.getType(); |
| 198 | + d.bricks.add(bs); |
| 199 | + } |
| 200 | + |
| 201 | + d.activePowerUps = new ArrayList<>(); |
| 202 | + if (activePowerUps != null) { |
| 203 | + for (PowerUp apu : activePowerUps) { |
| 204 | + GameSaveData.PowerUpState ps = new GameSaveData.PowerUpState(); |
| 205 | + ps.type = apu.getType(); |
| 206 | + ps.remaining = apu.getRemainingDuration(); |
| 207 | + d.activePowerUps.add(ps); |
| 208 | + } |
| 209 | + } |
| 210 | + return d; |
| 211 | + } |
| 212 | + |
| 213 | + public void importState(GameSaveData.SaveData d) { |
| 214 | + this.currentLevel = Math.max(1, d.level); |
| 215 | + this.score = Math.max(0, d.score); |
| 216 | + setLives(d.lives); |
| 217 | + |
| 218 | + // fresh lists |
| 219 | + powerUps = new ArrayList<>(); |
| 220 | + activePowerUps = new ArrayList<>(); |
| 221 | + extraBalls = new ArrayList<>(); |
| 222 | + |
| 223 | + // paddle |
| 224 | + paddle = new Paddle(d.paddle.x, d.paddle.y, d.paddle.width, d.paddle.height, d.paddle.region); |
| 225 | + paddle.setSticky(d.paddle.bool1); |
| 226 | + |
| 227 | + // ball |
| 228 | + ball = new Ball(d.ball.x, d.ball.y, d.ball.width, d.ball.height, d.ball.region, BALL_SPEED, 90); |
| 229 | + ball.setVelocity(d.ball.vx, d.ball.vy); |
| 230 | + ball.setHeavyBall(d.ball.bool1); |
| 231 | + ball.setStuckToPaddle(d.ball.bool2); |
| 232 | + ball.setStuckOffsetX(d.ball.i1); |
| 233 | + ball.setComboCount(d.ball.cb); |
| 234 | + |
| 235 | + // extra balls |
| 236 | + for (GameSaveData.Obj o : d.extraBalls) { |
| 237 | + Ball eb = new Ball(o.x, o.y, o.width, o.height, o.region, BALL_SPEED, 90); |
| 238 | + eb.setVelocity(o.vx, o.vy); |
| 239 | + eb.setHeavyBall(o.bool1); |
| 240 | + eb.setStuckToPaddle(o.bool2); |
| 241 | + eb.setStuckOffsetX(o.i1); |
| 242 | + extraBalls.add(eb); |
| 243 | + } |
| 244 | + |
| 245 | + // bricks |
| 246 | + bricks = new ArrayList<>(); |
| 247 | + for (GameSaveData.BrickState bs : d.bricks) { |
| 248 | + switch (bs.type) { |
| 249 | + case "solid_brick": |
| 250 | + Brick sb = new SolidBrick(bs.x, bs.y); |
| 251 | + bricks.add(sb); |
| 252 | + break; |
| 253 | + case "normal": |
| 254 | + Brick nb = new NormalBrick(bs.x, bs.y, bs.width, bs.height, bs.region); |
| 255 | + bricks.add(nb); |
| 256 | + break; |
| 257 | + case "multiball": |
| 258 | + Brick mb = new MultiBallBrick(bs.x, bs.y); |
| 259 | + bricks.add(mb); |
| 260 | + break; |
| 261 | + case "strong": |
| 262 | + Brick stb = new StrongBrick(bs.x, bs.y, bs.width, bs.height, bs.region, bs.hp); |
| 263 | + bricks.add(stb); |
| 264 | + break; |
| 265 | + default: |
| 266 | + // Unknown brick type; skip |
| 267 | + break; |
| 268 | + } |
| 269 | + } |
| 270 | + |
| 271 | + // active powerups: re-apply with saved remaining duration |
| 272 | + if (d.activePowerUps != null) { |
| 273 | + for (GameSaveData.PowerUpState ps : d.activePowerUps) { |
| 274 | + PowerUp pu = createPowerUpByType(ps.type); |
| 275 | + if (pu != null) { |
| 276 | + pu.applyEffect(this); |
| 277 | + pu.setRemainingDuration(ps.remaining); |
| 278 | + activePowerUps.add(pu); |
| 279 | + } |
| 280 | + } |
| 281 | + } |
| 282 | + } |
| 283 | + |
| 284 | + private PowerUp createPowerUpByType(String type) { |
| 285 | + switch (type) { |
| 286 | + case "Widen Paddle": |
| 287 | + return new WidenPaddlePowerUp(0, 0); |
| 288 | + case "Heavy Ball": |
| 289 | + return new HeavyBallPowerUp(0, 0); |
| 290 | + case "Sticky Paddle": |
| 291 | + return new StickyPaddlePowerUp(0, 0); |
| 292 | + case "Extra Life": |
| 293 | + return new ExtraLifePowerUp(0, 0); |
| 294 | + case "Split Ball": |
| 295 | + return new SplitBallPowerUp(0, 0); |
| 296 | + case "Slow Ball": |
| 297 | + return new SlowBallPowerUp(0, 0); |
| 298 | + default: |
| 299 | + return null; |
| 300 | + } |
| 301 | + } |
| 302 | + |
131 | 303 | @Override |
132 | 304 | public void show() { |
133 | 305 | camera = new OrthographicCamera(); |
@@ -523,6 +695,14 @@ public List<Ball> getExtraBalls() { |
523 | 695 | return extraBalls; |
524 | 696 | } |
525 | 697 |
|
| 698 | + public int getScore() { |
| 699 | + return score; |
| 700 | + } |
| 701 | + |
| 702 | + public int getCurrentLevel() { |
| 703 | + return currentLevel; |
| 704 | + } |
| 705 | + |
526 | 706 | public SoundManager getSoundManager() { |
527 | 707 | return soundManager; |
528 | 708 | } |
|
0 commit comments