Add State + MenuState, use AssetManager

This commit is contained in:
Matt Low 2019-12-24 00:59:13 +04:00
parent bb934b0d7c
commit e031cb4400
3 changed files with 91 additions and 14 deletions

View File

@ -1,16 +1,16 @@
package com.me.pacman; package com.me.pacman;
import com.badlogic.gdx.ApplicationAdapter; import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx; import com.badlogic.gdx.Gdx;
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.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch; 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.FitViewport;
import com.badlogic.gdx.utils.viewport.Viewport; 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 TITLE = "Pac-Dude";
public static final String VERSION = "v0.0.1"; 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_WIDTH = 224;
public static final int LEVEL_HEIGHT = 248; public static final int LEVEL_HEIGHT = 248;
public AssetManager assets;
public SpriteBatch batch;
public OrthographicCamera cam; public OrthographicCamera cam;
public Viewport viewport; public Viewport viewport;
public SpriteBatch batch;
public Texture level;
public Texture logo;
@Override @Override
public void create () { public void create () {
cam = new OrthographicCamera(); cam = new OrthographicCamera();
viewport = new FitViewport(LEVEL_WIDTH, LEVEL_HEIGHT, cam); viewport = new FitViewport(LEVEL_WIDTH, LEVEL_HEIGHT, cam);
viewport.apply(true); viewport.apply(true);
assets = new AssetManager();
batch = new SpriteBatch(); batch = new SpriteBatch();
level = new Texture("level.png");
logo = new Texture("logo.png"); setScreen(new MenuState(this));
logo.setFilter(Texture.TextureFilter.Linear, Texture.TextureFilter.Linear);
} }
@Override @Override
public void render () { public void render () {
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(); batch.begin();
batch.draw(level, 0, 0); super.render();
batch.draw(logo, 0, 124, 224, 120);
batch.end(); batch.end();
} }
@ -59,6 +58,6 @@ public class PacDude extends ApplicationAdapter {
@Override @Override
public void dispose () { public void dispose () {
batch.dispose(); batch.dispose();
level.dispose(); assets.dispose();
} }
} }

View File

@ -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) {
}
}

View File

@ -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);
}