Render pellets on menu screen.

Add Level object which contains level data (in the form of
an 2D array of stateless LevelComponents), LevelLoader which loads
level data from a txt file.
This commit is contained in:
Matt Low 2019-12-24 02:27:05 +04:00
parent e031cb4400
commit eae226b5bb
7 changed files with 120 additions and 6 deletions

View File

@ -6,6 +6,7 @@ import com.badlogic.gdx.assets.AssetManager;
import com.badlogic.gdx.graphics.GL20; import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera; import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.SpriteBatch; import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.utils.viewport.FitViewport; import com.badlogic.gdx.utils.viewport.FitViewport;
import com.badlogic.gdx.utils.viewport.Viewport; import com.badlogic.gdx.utils.viewport.Viewport;
import com.me.pacman.state.MenuState; import com.me.pacman.state.MenuState;
@ -20,6 +21,7 @@ public class PacDude extends Game {
public AssetManager assets; public AssetManager assets;
public SpriteBatch batch; public SpriteBatch batch;
public ShapeRenderer sr;
public OrthographicCamera cam; public OrthographicCamera cam;
public Viewport viewport; public Viewport viewport;
@ -32,6 +34,7 @@ public class PacDude extends Game {
assets = new AssetManager(); assets = new AssetManager();
batch = new SpriteBatch(); batch = new SpriteBatch();
sr = new ShapeRenderer();
setScreen(new MenuState(this)); setScreen(new MenuState(this));
} }
@ -41,9 +44,7 @@ public class PacDude extends Game {
Gdx.gl.glClearColor(0, 0, 0, 1); Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.begin();
super.render(); super.render();
batch.end();
} }
@Override @Override
@ -51,6 +52,7 @@ public class PacDude extends Game {
viewport.update(width, height); viewport.update(width, height);
batch.setProjectionMatrix(cam.combined); batch.setProjectionMatrix(cam.combined);
sr.setProjectionMatrix(cam.combined);
super.resize(width, height); super.resize(width, height);
} }

View File

@ -0,0 +1,36 @@
package com.me.pacman.level;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
public class Level {
// Grid of components, [rows][columns]
public LevelComponent[][] components;
public Level(String level) {
LevelLoader loader = new LevelLoader(level);
components = loader.loadLevel();
}
public void render(SpriteBatch batch, ShapeRenderer sr, int offsetX, int offsetY) {
sr.begin(ShapeRenderer.ShapeType.Filled);
sr.setColor(Color.valueOf("#f8b090"));
for (int i = 0; i < components.length; i++) {
LevelComponent[] row = components[i];
for (int j = 0; j < row.length; j++) {
LevelComponent component = row[j];
switch (component) {
case PELLET:
sr.rect((j * 8) + (offsetX-1), (i * 8) + (offsetY-1), 2, 2);
break;
}
}
}
sr.end();
}
}

View File

@ -0,0 +1,13 @@
package com.me.pacman.level;
public enum LevelComponent {
PELLET,
POWER_PELLET,
HALLWAY,
WALL,
GHOST_CHAMBER,
GHOST_GATE,
EMPTY,
}

View File

@ -0,0 +1,54 @@
package com.me.pacman.level;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.files.FileHandle;
public class LevelLoader {
private FileHandle levelFile;
public LevelLoader(String level) {
levelFile = Gdx.files.internal("data/" + level + ".txt");
}
public LevelComponent[][] loadLevel() {
String levelData = levelFile.readString();
String[] lines = levelData.split("\n");
int rows = lines.length;
LevelComponent[][] components = new LevelComponent[rows][lines[0].length()];
for (int i = 0; i < rows; i++) {
String line = lines[i];
for (int j = 0; j < line.length(); j++) {
char c = line.charAt(j);
int row = rows - i - 1;
switch(c) {
case '*':
components[row][j] = LevelComponent.PELLET;
break;
case '%':
components[row][j] = LevelComponent.POWER_PELLET;
break;
case '#':
components[row][j] = LevelComponent.WALL;
break;
case '^':
components[row][j] = LevelComponent.GHOST_CHAMBER;
break;
case '_':
components[row][j] = LevelComponent.GHOST_GATE;
break;
case '-':
components[row][j] = LevelComponent.HALLWAY;
break;
default:
components[row][j] = LevelComponent.EMPTY;
}
}
}
return components;
}
}

View File

@ -2,12 +2,15 @@ package com.me.pacman.state;
import com.badlogic.gdx.graphics.Texture; import com.badlogic.gdx.graphics.Texture;
import com.me.pacman.PacDude; import com.me.pacman.PacDude;
import com.me.pacman.level.Level;
public class MenuState extends State { public class MenuState extends State {
private Texture level; private Texture levelBackground;
private Texture logo; private Texture logo;
private Level level;
public MenuState(PacDude game) { public MenuState(PacDude game) {
super(game); super(game);
} }
@ -18,15 +21,21 @@ public class MenuState extends State {
game.assets.load("logo.png", Texture.class); game.assets.load("logo.png", Texture.class);
game.assets.finishLoading(); game.assets.finishLoading();
level = game.assets.get("level.png", Texture.class); levelBackground = game.assets.get("level.png", Texture.class);
logo = game.assets.get("logo.png", Texture.class); logo = game.assets.get("logo.png", Texture.class);
logo.setFilter(Texture.TextureFilter.Linear, Texture.TextureFilter.Linear); logo.setFilter(Texture.TextureFilter.Linear, Texture.TextureFilter.Linear);
level = new Level("level");
} }
@Override @Override
public void render() { public void render() {
game.batch.draw(level, 0, 0); level.render(game.batch, game.sr, 12, 12);
game.batch.begin();
game.batch.draw(levelBackground, 0, 0);
game.batch.draw(logo, 0, 124, 224, 120); game.batch.draw(logo, 0, 124, 224, 120);
game.batch.end();
} }
@Override @Override

View File

@ -10,7 +10,7 @@ public class DesktopLauncher {
config.title = PacDude.TITLE + " - " + PacDude.VERSION; config.title = PacDude.TITLE + " - " + PacDude.VERSION;
config.width = (224 * 2); config.width = (224 * 2);
config.height = (248 * 2); config.height = (248 * 2);
config.resizable = true; config.resizable = false;
new LwjglApplication(new PacDude(), config); new LwjglApplication(new PacDude(), config);
} }
} }