六) Entity游戏架构与Libgdx构建游戏superjumper,超级跳跃者小游戏(2)
1)GameScreen游戏主类
public class GameScreen extends ScreenAdapter {
static final int GAME_READY = 0;
static final int GAME_RUNNING = 1;
static final int GAME_PAUSED = 2;
static final int GAME_LEVEL_END = 3;
static final int GAME_OVER = 4;
SuperJumper game;
OrthographicCamera guiCam;
Vector3 touchPoint;
World world;
CollisionListener collisionListener;
Rectangle pauseBounds;
Rectangle resumeBounds;
Rectangle quitBounds;
int lastScore;
String scoreString;
PooledEngine engine;
private GlyphLayout layout = new GlyphLayout();
private int state;
public GameScreen (SuperJumper game) {
this.game = game;
state = GAME_READY;
guiCam = new OrthographicCamera(320, 480);
guiCam.position.set(320 / 2, 480 / 2, 0);
touchPoint = new Vector3();
//声明冲突监听
collisionListener = new CollisionListener() {
@Override
public void jump () {
Assets.playSound(Assets.jumpSound);
}
@Override
public void highJump () {
Assets.playSound(Assets.highJumpSound);
}
@Override
public void hit () {
Assets.playSound(Assets.hitSound);
}
@Override
public void coin () {
Assets.playSound(Assets.coinSound);
}
};
//创建游戏引擎,这个是Entity的第一步,这是一个唯一变量,一个游戏只允许有一个,任何的类,包括图
//片、粒子、子弹、特效等等都会被这个引擎渲染,渲染方法emgine.render(delta);’
engine = new PooledEngine();
//声明游戏世界,使用这个引擎进行控制渲染,World 类是自定义的了,和libgdx的world要进行区分
world = new World(engine);
//引擎注册各个系统, 每次渲染的时候就是调用各个系统override的函数 processEntity(Entity entity, float deltaTime) ,进行处理,每个系统只需要继承IteratingSystem接口类,然后实现processEntity方法
//对于每个系统而言,系统的任务就是将整个游戏的各组件的集合进行归纳处理。详见第三节,各个系统的代码
engine.addSystem(new BobSystem(world));
engine.addSystem(new SquirrelSystem());
engine.addSystem(new PlatformSystem());
engine.addSystem(new CameraSystem());
engine.addSystem(new BackgroundSystem());
engine.addSystem(new GravitySystem());
engine.addSystem(new MovementSystem());
engine.addSystem(new BoundsSystem());
engine.addSystem(new StateSystem());
engine.addSystem(new AnimationSystem());
engine.addSystem(new CollisionSystem(world, collisionListener));
engine.addSystem(new RenderingSystem(game.batcher));
//设置背景移动类 的相机,游戏背景的移动就是将相机视角 的位置设置到主角色的位置
engine.getSystem(BackgroundSystem.class).setCamera(engine.getSystem(RenderingSystem.class).getCamera());
world.create();
pauseBounds = new Rectangle(320 - 64, 480 - 64, 64, 64);
resumeBounds = new Rectangle(160 - 96, 240, 192, 36);
quitBounds = new Rectangle(160 - 96, 240 - 36, 192, 36);
lastScore = 0;
scoreString = "SCORE: 0";
//开始暂停
pauseSystems();
}
//渲染主程序代码
public void update (float deltaTime) {
if (deltaTime > 0.1f) deltaTime = 0.1f;
engine.update(deltaTime);//Entity的渲染核心,engine调用所有内部继承了IteratingSystem类的processEntity函数进行渲染处理。而processEntity函数就是处理每个组件集合,就是***Component得类,将所有东西都附加一个Component,比如主角,他有大小、跳和移动速度、跳掉和撞击、得分属性,那就声明一个BobComponent类组件包含。他有状态、时间属性,给他StateComponent组件类包含,TransformComponent包含他的位置、缩放、旋转属性、MovementComponent包含速度二维、加速度属性,等等,那么我们的BobSystem系统的processEntity就用来实时处理主角的这些属性变化。
//根据不同的游戏状态,切换不同的游戏场景
switch (state) {
case GAME_READY:
updateReady();
break;
case GAME_RUNNING:
updateRunning(deltaTime);
break;
case GAME_PAUSED:
updatePaused();
break;
case GAME_LEVEL_END:
updateLevelEnd();
break;
case GAME_OVER:
updateGameOver();
break;
}
}
private void updateReady () {
if (Gdx.input.justTouched()) {
state = GAME_RUNNING;
resumeSystems();
}
}
private void updateRunning (float deltaTime) {
if (Gdx.input.justTouched()) {
guiCam.unproject(touchPoint.set(Gdx.input.getX(), Gdx.input.getY(), 0));
if (pauseBounds.contains(touchPoint.x, touchPoint.y)) {
Assets.playSound(Assets.clickSound);
state = GAME_PAUSED;
pauseSystems();
return;
}
}
ApplicationType appType = Gdx.app.getType();
// should work also with Gdx.input.isPeripheralAvailable(Peripheral.Accelerometer)
float accelX = 0.0f;
if (appType == ApplicationType.Android || appType == ApplicationType.iOS) {
accelX = Gdx.input.getAccelerometerX();
} else {
if (Gdx.input.isKeyPressed(Keys.DPAD_LEFT)) accelX = 5f;
if (Gdx.input.isKeyPressed(Keys.DPAD_RIGHT)) accelX = -5f;
}
engine.getSystem(BobSystem.class).setAccelX(accelX);
if (world.score != lastScore) {
lastScore = world.score;
scoreString = "SCORE: " + lastScore;
}
if (world.state == World.WORLD_STATE_NEXT_LEVEL) {
game.setScreen(new WinScreen(game));
}
if (world.state == World.WORLD_STATE_GAME_OVER) {
state = GAME_OVER;
if (lastScore >= Settings.highscores[4])
scoreString = "NEW HIGHSCORE: " + lastScore;
else
scoreString = "SCORE: " + lastScore;
pauseSystems();
Settings.addScore(lastScore);
Settings.save();
}
}
private void updatePaused () {
if (Gdx.input.justTouched()) {
guiCam.unproject(touchPoint.set(Gdx.input.getX(), Gdx.input.getY(), 0));
if (resumeBounds.contains(touchPoint.x, touchPoint.y)) {
Assets.playSound(Assets.clickSound);
state = GAME_RUNNING;
resumeSystems();
return;
}
if (quitBounds.contains(touchPoint.x, touchPoint.y)) {
Assets.playSound(Assets.clickSound);
game.setScreen(new MainMenuScreen(game));
return;
}
}
}
private void updateLevelEnd () {
if (Gdx.input.justTouched()) {
engine.removeAllEntities();
world = new World(engine);
world.score = lastScore;
state = GAME_READY;
}
}
private void updateGameOver () {
if (Gdx.input.justTouched()) {
game.setScreen(new MainMenuScreen(game));
}
}
public void drawUI () {
guiCam.update();
game.batcher.setProjectionMatrix(guiCam.combined);
game.batcher.begin();
switch (state) {
case GAME_READY:
presentReady();
break;
case GAME_RUNNING:
presentRunning();
break;
case GAME_PAUSED:
presentPaused();
break;
case GAME_LEVEL_END:
presentLevelEnd();
break;
case GAME_OVER:
presentGameOver();
break;
}
game.batcher.end();
}
private void presentReady () {
game.batcher.draw(Assets.ready, 160 - 192 / 2, 240 - 32 / 2, 192, 32);
}
private void presentRunning () {
game.batcher.draw(Assets.pause, 320 - 64, 480 - 64, 64, 64);
Assets.font.draw(game.batcher, scoreString, 16, 480 - 20);
}
private void presentPaused () {
game.batcher.draw(Assets.pauseMenu, 160 - 192 / 2, 240 - 96 / 2, 192, 96);
Assets.font.draw(game.batcher, scoreString, 16, 480 - 20);
}
private void presentLevelEnd () {
String topText = "the princess is ...";
String bottomText = "in another castle!";
layout.setText(Assets.font, topText);
float topWidth = layout.width;
layout.setText(Assets.font, bottomText);
float bottomWidth = layout.width;
Assets.font.draw(game.batcher, topText, 160 - topWidth / 2, 480 - 40);
Assets.font.draw(game.batcher, bottomText, 160 - bottomWidth / 2, 40);
}
private void presentGameOver () {
game.batcher.draw(Assets.gameOver, 160 - 160 / 2, 240 - 96 / 2, 160, 96);
layout.setText(Assets.font, scoreString);
float scoreWidth = layout.width;
Assets.font.draw(game.batcher, scoreString, 160 - scoreWidth / 2, 480 - 20);
}
private void pauseSystems() {
engine.getSystem(BobSystem.class).setProcessing(false);
engine.getSystem(SquirrelSystem.class).setProcessing(false);
engine.getSystem(PlatformSystem.class).setProcessing(false);
engine.getSystem(GravitySystem.class).setProcessing(false);
engine.getSystem(MovementSystem.class).setProcessing(false);
engine.getSystem(BoundsSystem.class).setProcessing(false);
engine.getSystem(StateSystem.class).setProcessing(false);
engine.getSystem(AnimationSystem.class).setProcessing(false);
engine.getSystem(CollisionSystem.class).setProcessing(false);
}
private void resumeSystems() {
engine.getSystem(BobSystem.class).setProcessing(true);
engine.getSystem(SquirrelSystem.class).setProcessing(true);
engine.getSystem(PlatformSystem.class).setProcessing(true);
engine.getSystem(GravitySystem.class).setProcessing(true);
engine.getSystem(MovementSystem.class).setProcessing(true);
engine.getSystem(BoundsSystem.class).setProcessing(true);
engine.getSystem(StateSystem.class).setProcessing(true);
engine.getSystem(AnimationSystem.class).setProcessing(true);
engine.getSystem(CollisionSystem.class).setProcessing(true);
}
@Override
public void render (float delta) {
update(delta);
drawUI();
}
@Override
public void pause () {
if (state == GAME_RUNNING) {
state = GAME_PAUSED;
pauseSystems();
}
}
}