From 02a4c7d5edf3e91bcbdab4ffd5a2f349920aae59 Mon Sep 17 00:00:00 2001 From: Matt Low Date: Sun, 5 Jan 2020 23:48:39 +0400 Subject: [PATCH] Refactor --- core/src/com/me/pacman/entity/Entity.java | 3 +- core/src/com/me/pacman/entity/Ghost.java | 3 +- core/src/com/me/pacman/state/LevelState.java | 2 +- core/src/com/me/pacman/state/PlayState.java | 114 +++++++++---------- 4 files changed, 60 insertions(+), 62 deletions(-) diff --git a/core/src/com/me/pacman/entity/Entity.java b/core/src/com/me/pacman/entity/Entity.java index e15325a..6bd8dd9 100644 --- a/core/src/com/me/pacman/entity/Entity.java +++ b/core/src/com/me/pacman/entity/Entity.java @@ -1,6 +1,5 @@ package com.me.pacman.entity; -import com.badlogic.gdx.graphics.g2d.SpriteBatch; import com.badlogic.gdx.graphics.g2d.TextureRegion; import com.badlogic.gdx.math.Vector2; import com.me.pacman.PacDude; @@ -26,7 +25,7 @@ public abstract class Entity { return; } - state.renderSprite(texture, pos.x, pos.y); + state.drawSprite(texture, pos.x, pos.y); if (PacDude.DEBUG) { state.level.renderTile(LevelTile.DEBUG, (int) pos.x, (int) pos.y, LevelState.LEVEL_OFFSET_X, LevelState.LEVEL_OFFSET_Y); diff --git a/core/src/com/me/pacman/entity/Ghost.java b/core/src/com/me/pacman/entity/Ghost.java index 93c6b66..efb8eb6 100644 --- a/core/src/com/me/pacman/entity/Ghost.java +++ b/core/src/com/me/pacman/entity/Ghost.java @@ -1,6 +1,5 @@ package com.me.pacman.entity; -import com.badlogic.gdx.graphics.g2d.SpriteBatch; import com.badlogic.gdx.graphics.g2d.TextureRegion; import com.badlogic.gdx.math.Vector2; import com.me.pacman.entity.ai.Behaviour; @@ -79,7 +78,7 @@ public class Ghost extends MovableEntity { // draw eyes so the ghost can see if (state.frightTimer <= 0 || caught) { - state.renderSprite(sprite[1][currDirection.ordinal()], pos.x, pos.y); + state.drawSprite(sprite[1][currDirection.ordinal()], pos.x, pos.y); } } diff --git a/core/src/com/me/pacman/state/LevelState.java b/core/src/com/me/pacman/state/LevelState.java index ae06858..bcbf2c1 100644 --- a/core/src/com/me/pacman/state/LevelState.java +++ b/core/src/com/me/pacman/state/LevelState.java @@ -15,7 +15,7 @@ public abstract class LevelState extends State { super(game); } - public void renderSprite(TextureRegion sprite, float x, float y) { + public void drawSprite(TextureRegion sprite, float x, float y) { game.batch.draw(sprite, (int) (x * 8) + (LEVEL_OFFSET_X - 8), (int) (y * 8) + (LEVEL_OFFSET_Y - 8)); } diff --git a/core/src/com/me/pacman/state/PlayState.java b/core/src/com/me/pacman/state/PlayState.java index 1425cae..667debf 100644 --- a/core/src/com/me/pacman/state/PlayState.java +++ b/core/src/com/me/pacman/state/PlayState.java @@ -235,6 +235,63 @@ public class PlayState extends LevelState { } } + @Override + public void render() { + game.assets.getFont().setColor(Color.WHITE); + game.assets.getFont().draw(game.batch, "" + score, 40, 279); + for (int i = 0; i < lives; i++) { + game.batch.draw(lifeSprite, i * 16, 0); + } + + level.render(LEVEL_OFFSET_X, LEVEL_OFFSET_Y); + + if (state == GameState.ROUND_WON) { + // draw flashing level background + game.batch.draw((int) (stateTimer * 4) % 2 == 0? levelBackground : winBackground, LEVEL_OFFSET_X, LEVEL_OFFSET_Y); + return; + } else { + game.batch.draw(levelBackground, LEVEL_OFFSET_X, LEVEL_OFFSET_Y); + } + + if (state != GameState.GHOST_CAUGHT_POINTS_WAIT) { + pacman.render(); + } + + switch (state) { + case PRE_NEW_GAME: + case PACMAN_CAUGHT: + case GAME_OVER: + break; + case GHOST_CAUGHT_POINTS_WAIT: + drawSprite(game.assets.points[0][ghostsCaught-1], lastGhostCaptured.pos.x, lastGhostCaptured.pos.y); + default: + for (Ghost ghost : ghosts) { + if (state == GameState.GHOST_CAUGHT_POINTS_WAIT && ghost == lastGhostCaptured) { + continue; + } + ghost.render(); + } + } + + if (paused) { + game.assets.getFont().setColor(Color.YELLOW); + game.assets.getFont().draw(game.batch, "paused", 90, 151); + } else { + switch (state) { + case PRE_NEW_GAME: + case NEW_ROUND_WAIT: + case START_ROUND_WAIT: + game.assets.getFont().setColor(Color.YELLOW); + game.assets.getFont().draw(game.batch, "ready!", 92, 127); + break; + case GAME_OVER: + game.assets.getFont().setColor(Color.RED); + game.assets.getFont().draw(game.batch, "game over", 78, 127); + break; + } + } + } + private void pelletEaten(float x, float y) { level.setTile(x, y, LevelTile.EMPTY); @@ -305,63 +362,6 @@ public class PlayState extends LevelState { setGameState(GameState.GHOST_CAUGHT_POINTS_WAIT); } - @Override - public void render() { - game.assets.getFont().setColor(Color.WHITE); - game.assets.getFont().draw(game.batch, "" + score, 40, 279); - for (int i = 0; i < lives; i++) { - game.batch.draw(lifeSprite, i * 16, 0); - } - - level.render(LEVEL_OFFSET_X, LEVEL_OFFSET_Y); - - if (state == GameState.ROUND_WON) { - // draw flashing level background - game.batch.draw((int) (stateTimer * 4) % 2 == 0? levelBackground : winBackground, LEVEL_OFFSET_X, LEVEL_OFFSET_Y); - return; - } else { - game.batch.draw(levelBackground, LEVEL_OFFSET_X, LEVEL_OFFSET_Y); - } - - if (state != GameState.GHOST_CAUGHT_POINTS_WAIT) { - pacman.render(); - } - - switch (state) { - case PRE_NEW_GAME: - case PACMAN_CAUGHT: - case GAME_OVER: - break; - case GHOST_CAUGHT_POINTS_WAIT: - renderSprite(game.assets.points[0][ghostsCaught-1], lastGhostCaptured.pos.x, lastGhostCaptured.pos.y); - default: - for (Ghost ghost : ghosts) { - if (state == GameState.GHOST_CAUGHT_POINTS_WAIT && ghost == lastGhostCaptured) { - continue; - } - ghost.render(); - } - } - - if (paused) { - game.assets.getFont().setColor(Color.YELLOW); - game.assets.getFont().draw(game.batch, "paused", 90, 151); - } else { - switch (state) { - case PRE_NEW_GAME: - case NEW_ROUND_WAIT: - case START_ROUND_WAIT: - game.assets.getFont().setColor(Color.YELLOW); - game.assets.getFont().draw(game.batch, "ready!", 92, 127); - break; - case GAME_OVER: - game.assets.getFont().setColor(Color.RED); - game.assets.getFont().draw(game.batch, "game over", 78, 127); - break; - } - } - } - private void updateScatterTimer(float dt) { if (scatterChaseTransition < 6) { scatterChaseTimer -= dt;