From 2bc78baa746f5bb51f7d83fd589f02da7521496a Mon Sep 17 00:00:00 2001 From: Matt Low Date: Sun, 5 Jan 2020 14:47:23 +0400 Subject: [PATCH] Simplify rendering logic, no longer calculating offset in many places --- core/src/com/me/pacman/entity/Entity.java | 7 +++--- core/src/com/me/pacman/entity/Ghost.java | 9 ++++---- core/src/com/me/pacman/state/LevelState.java | 8 +++++++ core/src/com/me/pacman/state/PlayState.java | 24 +++++++++----------- 4 files changed, 28 insertions(+), 20 deletions(-) diff --git a/core/src/com/me/pacman/entity/Entity.java b/core/src/com/me/pacman/entity/Entity.java index 87584fb..e15325a 100644 --- a/core/src/com/me/pacman/entity/Entity.java +++ b/core/src/com/me/pacman/entity/Entity.java @@ -20,15 +20,16 @@ public abstract class Entity { this.age = 0; } - public void render(SpriteBatch batch, int offsetX, int offsetY) { + public void render() { TextureRegion texture = getSprite(); if (texture == null) { return; } - batch.draw(texture, (int) (pos.x * 8) + (offsetX - 8), (pos.y * 8) + (offsetY - 8)); + + state.renderSprite(texture, pos.x, pos.y); if (PacDude.DEBUG) { - state.level.renderTile(LevelTile.DEBUG, (int) pos.x, (int) pos.y, offsetX, offsetY); + 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 c63cb70..93c6b66 100644 --- a/core/src/com/me/pacman/entity/Ghost.java +++ b/core/src/com/me/pacman/entity/Ghost.java @@ -74,12 +74,13 @@ public class Ghost extends MovableEntity { } @Override - public void render(SpriteBatch batch, int offsetX, int offsetY) { - super.render(batch, offsetX, offsetY); + public void render() { + super.render(); // draw eyes so the ghost can see - if (state.frightTimer <= 0 || caught) - batch.draw(sprite[1][currDirection.ordinal()], (int) (pos.x * 8) + (offsetX - 8), (pos.y * 8) + (offsetY - 8)); + if (state.frightTimer <= 0 || caught) { + state.renderSprite(sprite[1][currDirection.ordinal()], pos.x, pos.y); + } } protected float getNormalSpeed() { diff --git a/core/src/com/me/pacman/state/LevelState.java b/core/src/com/me/pacman/state/LevelState.java index 1f9b48a..ae06858 100644 --- a/core/src/com/me/pacman/state/LevelState.java +++ b/core/src/com/me/pacman/state/LevelState.java @@ -1,14 +1,22 @@ package com.me.pacman.state; +import com.badlogic.gdx.graphics.g2d.TextureRegion; import com.me.pacman.PacDude; import com.me.pacman.level.Level; public abstract class LevelState extends State { + public static final int LEVEL_OFFSET_X = 0; + public static final int LEVEL_OFFSET_Y = 16; + public Level level; public LevelState(PacDude game) { super(game); } + public void renderSprite(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 ab5348e..f828d7e 100644 --- a/core/src/com/me/pacman/state/PlayState.java +++ b/core/src/com/me/pacman/state/PlayState.java @@ -307,41 +307,39 @@ public class PlayState extends LevelState { @Override public void render() { - level.render(0, 16); - 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); } - if (state == GameState.GHOST_CAUGHT_POINTS_WAIT) { - game.batch.draw(game.assets.points[0][ghostsCaught-1], (lastGhostCaptured.pos.x * 8) - 8, (lastGhostCaptured.pos.y * 8) + 8); - } else { - pacman.render(game.batch, 0, 16); - } + 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, 0, 16); + game.batch.draw((int) (stateTimer * 4) % 2 == 0? levelBackground : winBackground, LEVEL_OFFSET_X, LEVEL_OFFSET_Y); return; } else { - game.batch.draw(levelBackground, 0, 16); + 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 ROUND_WON: 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) { - game.batch.draw(game.assets.points[0][ghostsCaught - 1], (ghost.pos.x * 8) - 8, (ghost.pos.y * 8) + 8); - } else { - ghost.render(game.batch, 0, 16); + continue; } + ghost.render(); } }