From eae226b5bb0bdadc855024188e4aa87de8d00cf6 Mon Sep 17 00:00:00 2001 From: Matt Low Date: Tue, 24 Dec 2019 02:27:05 +0400 Subject: [PATCH] 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. --- core/assets/{ => data}/level.txt | 0 core/src/com/me/pacman/PacDude.java | 6 ++- core/src/com/me/pacman/level/Level.java | 36 +++++++++++++ .../com/me/pacman/level/LevelComponent.java | 13 +++++ core/src/com/me/pacman/level/LevelLoader.java | 54 +++++++++++++++++++ core/src/com/me/pacman/state/MenuState.java | 15 ++++-- .../me/pacman/desktop/DesktopLauncher.java | 2 +- 7 files changed, 120 insertions(+), 6 deletions(-) rename core/assets/{ => data}/level.txt (100%) create mode 100644 core/src/com/me/pacman/level/Level.java create mode 100644 core/src/com/me/pacman/level/LevelComponent.java create mode 100644 core/src/com/me/pacman/level/LevelLoader.java diff --git a/core/assets/level.txt b/core/assets/data/level.txt similarity index 100% rename from core/assets/level.txt rename to core/assets/data/level.txt diff --git a/core/src/com/me/pacman/PacDude.java b/core/src/com/me/pacman/PacDude.java index 899e2b7..ec53482 100644 --- a/core/src/com/me/pacman/PacDude.java +++ b/core/src/com/me/pacman/PacDude.java @@ -6,6 +6,7 @@ import com.badlogic.gdx.assets.AssetManager; import com.badlogic.gdx.graphics.GL20; import com.badlogic.gdx.graphics.OrthographicCamera; 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.Viewport; import com.me.pacman.state.MenuState; @@ -20,6 +21,7 @@ public class PacDude extends Game { public AssetManager assets; public SpriteBatch batch; + public ShapeRenderer sr; public OrthographicCamera cam; public Viewport viewport; @@ -32,6 +34,7 @@ public class PacDude extends Game { assets = new AssetManager(); batch = new SpriteBatch(); + sr = new ShapeRenderer(); setScreen(new MenuState(this)); } @@ -41,9 +44,7 @@ public class PacDude extends Game { Gdx.gl.glClearColor(0, 0, 0, 1); Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); - batch.begin(); super.render(); - batch.end(); } @Override @@ -51,6 +52,7 @@ public class PacDude extends Game { viewport.update(width, height); batch.setProjectionMatrix(cam.combined); + sr.setProjectionMatrix(cam.combined); super.resize(width, height); } diff --git a/core/src/com/me/pacman/level/Level.java b/core/src/com/me/pacman/level/Level.java new file mode 100644 index 0000000..ad53c08 --- /dev/null +++ b/core/src/com/me/pacman/level/Level.java @@ -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(); + } + +} diff --git a/core/src/com/me/pacman/level/LevelComponent.java b/core/src/com/me/pacman/level/LevelComponent.java new file mode 100644 index 0000000..1688b09 --- /dev/null +++ b/core/src/com/me/pacman/level/LevelComponent.java @@ -0,0 +1,13 @@ +package com.me.pacman.level; + +public enum LevelComponent { + + PELLET, + POWER_PELLET, + HALLWAY, + WALL, + GHOST_CHAMBER, + GHOST_GATE, + EMPTY, + +} diff --git a/core/src/com/me/pacman/level/LevelLoader.java b/core/src/com/me/pacman/level/LevelLoader.java new file mode 100644 index 0000000..0e37991 --- /dev/null +++ b/core/src/com/me/pacman/level/LevelLoader.java @@ -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; + } + +} diff --git a/core/src/com/me/pacman/state/MenuState.java b/core/src/com/me/pacman/state/MenuState.java index 026160a..6c0b949 100644 --- a/core/src/com/me/pacman/state/MenuState.java +++ b/core/src/com/me/pacman/state/MenuState.java @@ -2,12 +2,15 @@ package com.me.pacman.state; import com.badlogic.gdx.graphics.Texture; import com.me.pacman.PacDude; +import com.me.pacman.level.Level; public class MenuState extends State { - private Texture level; + private Texture levelBackground; private Texture logo; + private Level level; + public MenuState(PacDude game) { super(game); } @@ -18,15 +21,21 @@ public class MenuState extends State { game.assets.load("logo.png", Texture.class); 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.setFilter(Texture.TextureFilter.Linear, Texture.TextureFilter.Linear); + + level = new Level("level"); } @Override 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.end(); } @Override diff --git a/desktop/src/com/me/pacman/desktop/DesktopLauncher.java b/desktop/src/com/me/pacman/desktop/DesktopLauncher.java index a0539cc..6e8385c 100644 --- a/desktop/src/com/me/pacman/desktop/DesktopLauncher.java +++ b/desktop/src/com/me/pacman/desktop/DesktopLauncher.java @@ -10,7 +10,7 @@ public class DesktopLauncher { config.title = PacDude.TITLE + " - " + PacDude.VERSION; config.width = (224 * 2); config.height = (248 * 2); - config.resizable = true; + config.resizable = false; new LwjglApplication(new PacDude(), config); } }