From e031cb4400d0607cb777490e4250e76c1db3c93d Mon Sep 17 00:00:00 2001 From: Matt Low Date: Tue, 24 Dec 2019 00:59:13 +0400 Subject: [PATCH] Add State + MenuState, use AssetManager --- core/src/com/me/pacman/PacDude.java | 27 +++++++------- core/src/com/me/pacman/state/MenuState.java | 37 +++++++++++++++++++ core/src/com/me/pacman/state/State.java | 41 +++++++++++++++++++++ 3 files changed, 91 insertions(+), 14 deletions(-) create mode 100644 core/src/com/me/pacman/state/MenuState.java create mode 100644 core/src/com/me/pacman/state/State.java diff --git a/core/src/com/me/pacman/PacDude.java b/core/src/com/me/pacman/PacDude.java index da29b27..899e2b7 100644 --- a/core/src/com/me/pacman/PacDude.java +++ b/core/src/com/me/pacman/PacDude.java @@ -1,16 +1,16 @@ package com.me.pacman; -import com.badlogic.gdx.ApplicationAdapter; +import com.badlogic.gdx.Game; import com.badlogic.gdx.Gdx; +import com.badlogic.gdx.assets.AssetManager; import com.badlogic.gdx.graphics.GL20; import com.badlogic.gdx.graphics.OrthographicCamera; -import com.badlogic.gdx.graphics.Texture; import com.badlogic.gdx.graphics.g2d.SpriteBatch; -import com.badlogic.gdx.math.Matrix4; import com.badlogic.gdx.utils.viewport.FitViewport; import com.badlogic.gdx.utils.viewport.Viewport; +import com.me.pacman.state.MenuState; -public class PacDude extends ApplicationAdapter { +public class PacDude extends Game { public static final String TITLE = "Pac-Dude"; public static final String VERSION = "v0.0.1"; @@ -18,32 +18,31 @@ public class PacDude extends ApplicationAdapter { public static final int LEVEL_WIDTH = 224; public static final int LEVEL_HEIGHT = 248; + public AssetManager assets; + public SpriteBatch batch; + public OrthographicCamera cam; public Viewport viewport; - public SpriteBatch batch; - public Texture level; - public Texture logo; - @Override public void create () { cam = new OrthographicCamera(); viewport = new FitViewport(LEVEL_WIDTH, LEVEL_HEIGHT, cam); viewport.apply(true); + assets = new AssetManager(); batch = new SpriteBatch(); - level = new Texture("level.png"); - logo = new Texture("logo.png"); - logo.setFilter(Texture.TextureFilter.Linear, Texture.TextureFilter.Linear); + + setScreen(new MenuState(this)); } @Override public void render () { Gdx.gl.glClearColor(0, 0, 0, 1); Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); + batch.begin(); - batch.draw(level, 0, 0); - batch.draw(logo, 0, 124, 224, 120); + super.render(); batch.end(); } @@ -59,6 +58,6 @@ public class PacDude extends ApplicationAdapter { @Override public void dispose () { batch.dispose(); - level.dispose(); + assets.dispose(); } } diff --git a/core/src/com/me/pacman/state/MenuState.java b/core/src/com/me/pacman/state/MenuState.java new file mode 100644 index 0000000..026160a --- /dev/null +++ b/core/src/com/me/pacman/state/MenuState.java @@ -0,0 +1,37 @@ +package com.me.pacman.state; + +import com.badlogic.gdx.graphics.Texture; +import com.me.pacman.PacDude; + +public class MenuState extends State { + + private Texture level; + private Texture logo; + + public MenuState(PacDude game) { + super(game); + } + + @Override + public void setup() { + game.assets.load("level.png", Texture.class); + game.assets.load("logo.png", Texture.class); + game.assets.finishLoading(); + + level = game.assets.get("level.png", Texture.class); + logo = game.assets.get("logo.png", Texture.class); + logo.setFilter(Texture.TextureFilter.Linear, Texture.TextureFilter.Linear); + } + + @Override + public void render() { + game.batch.draw(level, 0, 0); + game.batch.draw(logo, 0, 124, 224, 120); + } + + @Override + public void update(float dt) { + + } + +} diff --git a/core/src/com/me/pacman/state/State.java b/core/src/com/me/pacman/state/State.java new file mode 100644 index 0000000..30087a9 --- /dev/null +++ b/core/src/com/me/pacman/state/State.java @@ -0,0 +1,41 @@ +package com.me.pacman.state; + +import com.badlogic.gdx.ScreenAdapter; +import com.me.pacman.PacDude; + +public abstract class State extends ScreenAdapter { + + protected final PacDude game; + private boolean disposed; + + public State(PacDude game) { + this.game = game; + this.setup(); + } + + @Override + public final void render(float delta) { + update(delta); + if (!disposed) { + render(); + } + } + + @Override + public void dispose() { + this.disposed = true; + } + + public PacDude getGame() { + return game; + } + + public abstract void setup(); + + public abstract void render(); + + public abstract void update(float dt); + +} + +