Simplify rendering logic, no longer calculating offset in many places

This commit is contained in:
Matt Low 2020-01-05 14:47:23 +04:00
parent b770c2e40d
commit 2bc78baa74
4 changed files with 28 additions and 20 deletions

View File

@ -20,15 +20,16 @@ public abstract class Entity {
this.age = 0; this.age = 0;
} }
public void render(SpriteBatch batch, int offsetX, int offsetY) { public void render() {
TextureRegion texture = getSprite(); TextureRegion texture = getSprite();
if (texture == null) { if (texture == null) {
return; 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) { 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);
} }
} }

View File

@ -74,12 +74,13 @@ public class Ghost extends MovableEntity {
} }
@Override @Override
public void render(SpriteBatch batch, int offsetX, int offsetY) { public void render() {
super.render(batch, offsetX, offsetY); super.render();
// draw eyes so the ghost can see // draw eyes so the ghost can see
if (state.frightTimer <= 0 || caught) if (state.frightTimer <= 0 || caught) {
batch.draw(sprite[1][currDirection.ordinal()], (int) (pos.x * 8) + (offsetX - 8), (pos.y * 8) + (offsetY - 8)); state.renderSprite(sprite[1][currDirection.ordinal()], pos.x, pos.y);
}
} }
protected float getNormalSpeed() { protected float getNormalSpeed() {

View File

@ -1,14 +1,22 @@
package com.me.pacman.state; package com.me.pacman.state;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.me.pacman.PacDude; import com.me.pacman.PacDude;
import com.me.pacman.level.Level; import com.me.pacman.level.Level;
public abstract class LevelState extends State { 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 Level level;
public LevelState(PacDude game) { public LevelState(PacDude game) {
super(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));
}
} }

View File

@ -307,41 +307,39 @@ public class PlayState extends LevelState {
@Override @Override
public void render() { public void render() {
level.render(0, 16);
game.assets.getFont().setColor(Color.WHITE); game.assets.getFont().setColor(Color.WHITE);
game.assets.getFont().draw(game.batch, "" + score, 40, 279); game.assets.getFont().draw(game.batch, "" + score, 40, 279);
for (int i = 0; i < lives; i++) { for (int i = 0; i < lives; i++) {
game.batch.draw(lifeSprite, i * 16, 0); game.batch.draw(lifeSprite, i * 16, 0);
} }
if (state == GameState.GHOST_CAUGHT_POINTS_WAIT) { level.render(LEVEL_OFFSET_X, LEVEL_OFFSET_Y);
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);
}
if (state == GameState.ROUND_WON) { if (state == GameState.ROUND_WON) {
// draw flashing level background // 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; return;
} else { } 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) { switch (state) {
case PRE_NEW_GAME: case PRE_NEW_GAME:
case PACMAN_CAUGHT: case PACMAN_CAUGHT:
case ROUND_WON:
case GAME_OVER: case GAME_OVER:
break; break;
case GHOST_CAUGHT_POINTS_WAIT:
renderSprite(game.assets.points[0][ghostsCaught-1], lastGhostCaptured.pos.x, lastGhostCaptured.pos.y);
default: default:
for (Ghost ghost : ghosts) { for (Ghost ghost : ghosts) {
if (state == GameState.GHOST_CAUGHT_POINTS_WAIT && ghost == lastGhostCaptured) { 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); continue;
} else {
ghost.render(game.batch, 0, 16);
} }
ghost.render();
} }
} }