Java tutorial
/* Copyright [2014] [James Thomas Hayes] Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ package com.codefiddler.libgdx.spinit.level; import java.util.ArrayList; import java.util.List; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.assets.AssetErrorListener; import com.badlogic.gdx.assets.AssetManager; import com.badlogic.gdx.audio.Sound; import com.badlogic.gdx.graphics.Color; import com.badlogic.gdx.graphics.Texture; import com.badlogic.gdx.graphics.g2d.SpriteBatch; import com.badlogic.gdx.utils.Disposable; import com.codefiddler.libgdx.spinit.Bottle; import com.codefiddler.libgdx.spinit.Segment; import com.codefiddler.libgdx.spinit.TexturedSprite; import com.codefiddler.libgdx.spinit.assets.SoundEffects; public abstract class Level implements Disposable, AssetErrorListener { public static final String TAG = Level.class.getName(); private AssetManager assetManager; private Bottle bottle; private TexturedSprite wheelSprite; private SoundEffects soundEffects; private List<Segment> tickSprites = new ArrayList<Segment>(); private float triggerEnding = -1; private boolean levelEnded; public Level(SoundEffects soundEffects) { assetManager = new AssetManager(); assetManager.setErrorListener(this); assetManager.load("data/512px/bottle.png", Texture.class); assetManager.load(getWheelPath(), Texture.class); this.soundEffects = soundEffects; for (int i = 1; i <= howManyWheelSegments(); i++) { assetManager.load(getTickFilenamePrefix() + i + ".png", Texture.class); } // start loading assets and wait until finished assetManager.finishLoading(); bottle = new Bottle(assetManager.get("data/512px/bottle.png", Texture.class)); wheelSprite = new TexturedSprite(assetManager.get(getWheelPath(), Texture.class)); Gdx.app.debug(TAG, "# of assets loaded: " + assetManager.getAssetNames().size); for (int i = 1; i <= howManyWheelSegments(); i++) { Segment sprite = new Segment(assetManager.get(getTickFilenamePrefix() + i + ".png", Texture.class), getSegmentColor(), i, howManyWheelSegments()); tickSprites.add(sprite); } } public Color getSegmentColor() { return Color.RED; } public abstract String getTickFilenamePrefix(); public abstract String getWheelPath(); public abstract int howManyWheelSegments(); public void draw(SpriteBatch batch) { int howManySegments = howManyWheelSegments(); wheelSprite.draw(batch); for (int i = 0; i < howManySegments; i++) { Segment sprite = tickSprites.get(i); if (bottleIsWithSegment(i, howManySegments, bottle.getRot())) { sprite.select(); } else { sprite.deselect(); } } for (Segment tick : tickSprites) { tick.draw(batch); } bottle.draw(batch); if (shouldTriggerSound()) { soundEffects.play(); resetTriggerSound(); } } public static boolean bottleIsWithSegment(int i, int howManySegments, float bottleDirection) { float degreesPerSegment = 360f / howManySegments; int segmentIndex = howManySegments - i; float start = (segmentIndex * degreesPerSegment); float end = ((segmentIndex - 1) * degreesPerSegment); return bottleDirection < start && bottleDirection > end; } public void doEnding() { int howManySegments = howManyWheelSegments(); Sound sound = soundEffects.getSound(1); if (bottle.isFlyingOff()) { bottle.resetPosition(); } for (int i = 0; i < howManySegments; i++) { if (bottleIsWithSegment(i, howManySegments, bottle.getRot())) { Segment tickSprite = tickSprites.get(i); if (tickSprite.getSelected()) { sound = soundEffects.getOpps(i); } else { sound = soundEffects.getSound(i); } tickSprite.toggleSelect(); break; } } if (areAllTicksActive()) { sound = winnerSound(); triggerEnding = 0; } ; sound.play(); } public Sound winnerSound() { return soundEffects.getWinnerSound(); } public void dispose() { wheelSprite.dispose(); for (Segment tick : tickSprites) { tick.dispose(); } assetManager.dispose(); } public boolean shouldTriggerSound() { return bottle.triggerSound(); } /* (non-Javadoc) * @see com.codefiddler.libgdx.spinit.level.Level#resetTriggerSound() */ public void resetTriggerSound() { bottle.resetTriggerSound(); } /* (non-Javadoc) * @see com.codefiddler.libgdx.spinit.level.Level#pushingBottleSignal() */ public void pushingBottleSignal() { bottle.accelerate(); } /* (non-Javadoc) * @see com.codefiddler.libgdx.spinit.level.Level#notPushingBottleSignal() */ public void notPushingBottleSignal() { if (!bottle.hasStopped()) { bottle.decelerate(); if (bottle.hasStopped()) { doEnding(); } } if (triggerEnding != -1) { triggerEnding = triggerEnding + Gdx.graphics.getDeltaTime(); if (triggerEnding > 5) { for (Segment tick : tickSprites) { tick.toggleSelect(); } triggerEnding = -1; levelEnded = true; } } } private boolean areAllTicksActive() { for (Segment tick : tickSprites) { if (!tick.getSelected()) { return false; } ; } return true; } public boolean isLevelEnded() { return levelEnded; } public void resetLevel() { levelEnded = false; } public SoundEffects getSoundEffects() { return soundEffects; } public void error(String fileName, Class type, Throwable throwable) { Gdx.app.error(TAG, "Couldn't load asset '" + fileName + "'", (Exception) throwable); } }