package com.lucasdnd.ags.ui.popup; import org.newdawn.slick.Animation; import org.newdawn.slick.GameContainer; import org.newdawn.slick.Graphics; import org.newdawn.slick.Image; import org.newdawn.slick.SlickException; import org.newdawn.slick.SpriteSheet; import org.newdawn.slick.state.StateBasedGame; import com.lucasdnd.ags.system.ResourceLoader; public class TimeMeter extends Popup { // Position float originalXPos, originalYPos; // The Sprite private SpriteSheet circle; private Animation timeAnimation; private float animationSpeed; private float timeLimit; public TimeMeter(float xPos, float yPos, float xOffset, float yOffset, float entityWidth, float entityHeight, int timeLimit) throws SlickException { // Basic attributes this.xPos = xPos; this.yPos = yPos; originalXPos = xPos; originalYPos = yPos; this.xOffset = xOffset; this.yOffset = yOffset; width = height = 30; // Visibility visible = false; // Create the Circle SpriteSheet circle = ResourceLoader.getInstance().timeSpriteSheet; circle.setFilter(Image.FILTER_NEAREST); timeAnimation = new Animation(circle, 0, 0, 26, 0, true, 1, true); timeAnimation.setLooping(false); // Calculate the animation speed based on the Time Limit this.timeLimit = timeLimit; animationSpeed = 1000f / timeLimit / ((float)timeAnimation.getFrameCount() - 1f); timeAnimation.setSpeed(animationSpeed); } /** * Starts the Time Meter */ public void show() { visible = true; timeAnimation.restart(); } public void hide() { visible = false; timeAnimation.restart(); timeAnimation.stop(); } public void update(GameContainer container, StateBasedGame game, int delta, int simulationSpeed, float customerXPos, float customerYPos) throws SlickException { if(visible) { // Update the Position this.xPos = customerXPos; this.yPos = customerYPos; // Update the Animation speed according to the delta if(simulationSpeed == 0) { timeAnimation.stop(); } else { timeAnimation.setSpeed(animationSpeed * simulationSpeed); timeAnimation.start(); } // Check if the Animation ended if(timeAnimation.getFrame() >= timeAnimation.getFrameCount() - 1) { visible = false; } } } public void render(GameContainer container, StateBasedGame game, Graphics g) throws SlickException { // Only if visible if(visible) { g.drawAnimation(timeAnimation, xPos, yPos - 16f); } } }