diff --git a/src/mightypork/rogue/App.java b/src/mightypork/rogue/App.java index 98203cb..ba6fe05 100644 --- a/src/mightypork/rogue/App.java +++ b/src/mightypork/rogue/App.java @@ -9,10 +9,9 @@ import javax.swing.JOptionPane; import mightypork.rogue.display.DisplaySystem; import mightypork.rogue.display.Screen; -import mightypork.rogue.display.ScreenSplash; +import mightypork.rogue.display.ScreenTestAnimations; import mightypork.rogue.input.InputSystem; import mightypork.rogue.input.KeyStroke; -import mightypork.rogue.input.events.MouseMotionEvent; import mightypork.rogue.sounds.SoundSystem; import mightypork.rogue.tasks.TaskTakeScreenshot; import mightypork.rogue.util.Utils; @@ -24,7 +23,6 @@ import mightypork.utils.time.TimerDelta; import mightypork.utils.time.TimerInterpolating; import org.lwjgl.input.Keyboard; -import org.lwjgl.opengl.Display; public class App implements Destroyable { @@ -273,28 +271,20 @@ public class App implements Destroyable { /** timer */ private TimerDelta timerRender; - private TimerInterpolating timerGui; private void mainLoop() { - screen = new ScreenSplash(); + screen = new ScreenTestAnimations(); screen.setActive(true); timerRender = new TimerDelta(); - timerGui = new TimerInterpolating(Const.FPS_GUI_UPDATE); while (!display.isCloseRequested()) { display.beginFrame(); - // gui update - timerGui.sync(); - int ticks = timerGui.getSkipped(); - if (ticks >= 1) { - input.poll(); - timerGui.startNewFrame(); - } + input.poll(); double delta = timerRender.getDelta(); diff --git a/src/mightypork/rogue/display/DisplaySystem.java b/src/mightypork/rogue/display/DisplaySystem.java index 5c07564..7bbb5ea 100644 --- a/src/mightypork/rogue/display/DisplaySystem.java +++ b/src/mightypork/rogue/display/DisplaySystem.java @@ -6,43 +6,43 @@ import static org.lwjgl.opengl.GL11.*; import java.awt.image.BufferedImage; import java.nio.ByteBuffer; -import org.lwjgl.BufferUtils; -import org.lwjgl.LWJGLException; -import org.lwjgl.opengl.Display; -import org.lwjgl.opengl.DisplayMode; - import mightypork.rogue.App; -import mightypork.rogue.Const; import mightypork.rogue.display.events.ScreenChangeEvent; import mightypork.utils.logging.Log; import mightypork.utils.math.coord.Coord; import mightypork.utils.patterns.Destroyable; import mightypork.utils.patterns.Initializable; -import mightypork.utils.time.Updateable; + +import org.lwjgl.BufferUtils; +import org.lwjgl.LWJGLException; +import org.lwjgl.opengl.Display; +import org.lwjgl.opengl.DisplayMode; public class DisplaySystem implements Initializable, Destroyable { private boolean initialized; - + private DisplayMode windowDisplayMode; private int targetFps; - - + + public DisplaySystem() { initialize(); } + @Override public void initialize() { - if(initialized) return; - + if (initialized) return; + initChannels(); - + initialized = true; } - + + /** * Initialize event channels */ @@ -51,6 +51,7 @@ public class DisplaySystem implements Initializable, Destroyable { App.msgbus().registerMessageType(ScreenChangeEvent.class, ScreenChangeEvent.Listener.class); } + @Override public void destroy() { @@ -103,7 +104,7 @@ public class DisplaySystem implements Initializable, Destroyable { Display.setDisplayMode(windowDisplayMode); Display.update(); } - + App.broadcast(new ScreenChangeEvent(true, Display.isFullscreen(), getSize())); } catch (Throwable t) { @@ -180,10 +181,10 @@ public class DisplaySystem implements Initializable, Destroyable { */ public void beginFrame() { - if(Display.wasResized()) { + if (Display.wasResized()) { App.broadcast(new ScreenChangeEvent(false, Display.isFullscreen(), getSize())); } - + glLoadIdentity(); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); } diff --git a/src/mightypork/rogue/display/Screen.java b/src/mightypork/rogue/display/Screen.java index 5d93e24..01b8022 100644 --- a/src/mightypork/rogue/display/Screen.java +++ b/src/mightypork/rogue/display/Screen.java @@ -2,19 +2,15 @@ package mightypork.rogue.display; import static org.lwjgl.opengl.GL11.*; - -import java.util.Random; - import mightypork.rogue.App; import mightypork.rogue.display.events.ScreenChangeEvent; import mightypork.rogue.input.KeyBinder; import mightypork.rogue.input.KeyBindingPool; import mightypork.rogue.input.KeyStroke; import mightypork.rogue.input.events.KeyboardEvent; -import mightypork.rogue.input.events.MouseMotionEvent; import mightypork.rogue.input.events.MouseButtonEvent; +import mightypork.rogue.input.events.MouseMotionEvent; import mightypork.utils.math.coord.Coord; -import mightypork.utils.patterns.Destroyable; import mightypork.utils.patterns.Initializable; import mightypork.utils.time.Updateable; @@ -54,6 +50,7 @@ public abstract class Screen implements KeyBinder, Updateable, Initializable, Ke /** * Prepare for being shown + * * @param shown true to show, false to hide */ public final void setActive(boolean shown) @@ -110,14 +107,16 @@ public abstract class Screen implements KeyBinder, Updateable, Initializable, Ke glMatrixMode(GL_MODELVIEW); } - + /** * Initialize screen layout and key bindings.
- * Called when the screen is created, not when it comes to front. For that, use onEnter(). + * Called when the screen is created, not when it comes to front. For that, + * use onEnter(). */ @Override public abstract void initialize(); + /** * Called when the screen becomes active */ @@ -172,6 +171,9 @@ public abstract class Screen implements KeyBinder, Updateable, Initializable, Ke } + /** + * Update and render the screen + */ @Override public final void update(double delta) { diff --git a/src/mightypork/rogue/display/ScreenSplash.java b/src/mightypork/rogue/display/ScreenSplash.java deleted file mode 100644 index 67c9952..0000000 --- a/src/mightypork/rogue/display/ScreenSplash.java +++ /dev/null @@ -1,167 +0,0 @@ -package mightypork.rogue.display; - - -import org.lwjgl.input.Keyboard; -import org.lwjgl.opengl.Display; - -import mightypork.rogue.App; -import mightypork.rogue.input.KeyStroke; -import mightypork.rogue.input.events.MouseButtonEvent; -import mightypork.rogue.input.events.MouseMotionEvent; -import mightypork.rogue.util.RenderUtils; -import mightypork.utils.math.Polar; -import mightypork.utils.math.color.RGB; -import mightypork.utils.math.coord.Coord; -import mightypork.utils.math.easing.Easing; -import mightypork.utils.time.AnimDouble; -import mightypork.utils.time.AnimDoubleDeg; - - -public class ScreenSplash extends Screen { - - private AnimDoubleDeg degAnim = new AnimDoubleDeg(0, Easing.SINE); - - //@formatter:off - private AnimDouble[] anims = new AnimDouble[] { - new AnimDouble(0, Easing.NONE), - new AnimDouble(0, Easing.LINEAR), - - new AnimDouble(0, Easing.QUADRATIC_IN), - new AnimDouble(0, Easing.QUADRATIC_OUT), - new AnimDouble(0, Easing.QUADRATIC), - - new AnimDouble(0, Easing.CUBIC_IN), - new AnimDouble(0, Easing.CUBIC_OUT), - new AnimDouble(0, Easing.CUBIC), - - new AnimDouble(0, Easing.QUADRATIC_IN), - new AnimDouble(0, Easing.QUADRATIC_OUT), - new AnimDouble(0, Easing.QUADRATIC), - - new AnimDouble(0, Easing.QUINTIC_IN), - new AnimDouble(0, Easing.QUINTIC_OUT), - new AnimDouble(0, Easing.QUINTIC_IN_OUT), - - new AnimDouble(0, Easing.EXPO_IN), - new AnimDouble(0, Easing.EXPO_OUT), - new AnimDouble(0, Easing.EXPO), - - new AnimDouble(0, Easing.SINE_IN), - new AnimDouble(0, Easing.SINE_OUT), - new AnimDouble(0, Easing.SINE), - - new AnimDouble(0, Easing.CIRC_IN), - new AnimDouble(0, Easing.CIRC_OUT), - new AnimDouble(0, Easing.CIRC), - }; - //@formatter:on - - @Override - public void initialize() - { - bindKeyStroke(new KeyStroke(Keyboard.KEY_RIGHT), new Runnable() { - - @Override - public void run() - { - for (AnimDouble a : anims) { - a.animate(0, 1, 3); - } - } - }); - - bindKeyStroke(new KeyStroke(Keyboard.KEY_LEFT), new Runnable() { - - @Override - public void run() - { - for (AnimDouble a : anims) { - a.animate(1, 0, 3); - } - } - }); - } - - - @Override - protected void renderScreen() - { - double screenH = Display.getHeight(); - double screenW = Display.getWidth(); - double perBoxH = screenH / anims.length; - double padding = perBoxH*0.1; - double boxSide = perBoxH-padding*2; - - for (int i = 0; i < anims.length; i++) { - AnimDouble a = anims[i]; - - RenderUtils.setColor(i%3==0?RGB.GREEN:RGB.BLUE); - RenderUtils.quadSize( - padding + a.getCurrentValue() * (screenW - perBoxH - padding*2), - screenH - perBoxH * i - perBoxH + padding, - boxSide, - boxSide - ); - } - - RenderUtils.setColor(RGB.YELLOW); - RenderUtils.translate(new Coord(Display.getWidth() / 2, Display.getHeight() / 2)); - RenderUtils.rotateZ(degAnim.getCurrentValue()); - RenderUtils.quadSize(-10, -10, 20, 200); - } - - - @Override - public void receive(MouseMotionEvent event) - { - } - - - @Override - public void receive(MouseButtonEvent event) - { - if(event.isDown()) { - Coord vec = App.disp().getSize().half().vecTo(event.getPos()); - - Polar p = Polar.fromCoord(vec); - - degAnim.fadeTo(p.getAngleDeg() - 90, 0.2); - } - } - - - @Override - protected void onEnter() - { - // TODO Auto-generated method stub - - } - - - @Override - protected void onLeave() - { - // TODO Auto-generated method stub - - } - - - @Override - protected void onSizeChanged(Coord size) - { - // TODO Auto-generated method stub - - } - - - @Override - protected void updateScreen(double delta) - { - degAnim.update(delta); - - for (AnimDouble a : anims) { - a.update(delta); - } - } - -} diff --git a/src/mightypork/rogue/display/ScreenTestAnimations.java b/src/mightypork/rogue/display/ScreenTestAnimations.java new file mode 100644 index 0000000..d4da94d --- /dev/null +++ b/src/mightypork/rogue/display/ScreenTestAnimations.java @@ -0,0 +1,200 @@ +package mightypork.rogue.display; + + +import java.util.Random; + +import mightypork.rogue.App; +import mightypork.rogue.input.KeyStroke; +import mightypork.rogue.input.events.MouseButtonEvent; +import mightypork.rogue.input.events.MouseMotionEvent; +import mightypork.rogue.util.RenderUtils; +import mightypork.utils.math.Polar; +import mightypork.utils.math.color.RGB; +import mightypork.utils.math.coord.Coord; +import mightypork.utils.math.easing.Easing; +import mightypork.utils.time.animation.AnimDouble; +import mightypork.utils.time.animation.AnimDoubleDeg; + +import org.lwjgl.input.Keyboard; +import org.lwjgl.opengl.Display; + + +public class ScreenTestAnimations extends Screen { + + private Random rand = new Random(); + + private AnimDoubleDeg degAnim = new AnimDoubleDeg(0, Easing.ELASTIC_OUT); + + //@formatter:off + private AnimDouble[] anims = new AnimDouble[] { + new AnimDouble(0, Easing.BOUNCE_OUT), + new AnimDouble(0, Easing.BOUNCE_OUT), + new AnimDouble(0, Easing.BOUNCE_OUT), + new AnimDouble(0, Easing.BOUNCE_OUT), + new AnimDouble(0, Easing.BOUNCE_OUT), + new AnimDouble(0, Easing.BOUNCE_OUT), + new AnimDouble(0, Easing.BOUNCE_OUT), + new AnimDouble(0, Easing.BOUNCE_OUT), + new AnimDouble(0, Easing.BOUNCE_OUT), + new AnimDouble(0, Easing.BOUNCE_OUT), + new AnimDouble(0, Easing.BOUNCE_OUT), + new AnimDouble(0, Easing.BOUNCE_OUT), + new AnimDouble(0, Easing.BOUNCE_OUT), + new AnimDouble(0, Easing.BOUNCE_OUT), + new AnimDouble(0, Easing.BOUNCE_OUT), + new AnimDouble(0, Easing.BOUNCE_OUT), + new AnimDouble(0, Easing.BOUNCE_OUT), + new AnimDouble(0, Easing.BOUNCE_OUT), + new AnimDouble(0, Easing.BOUNCE_OUT), + new AnimDouble(0, Easing.BOUNCE_OUT), + +// new AnimDouble(0, Easing.NONE), +// new AnimDouble(0, Easing.LINEAR), +// +// new AnimDouble(0, Easing.QUADRATIC_IN), +// new AnimDouble(0, Easing.QUADRATIC_OUT), +// new AnimDouble(0, Easing.QUADRATIC_IN_OUT), +// +// new AnimDouble(0, Easing.CUBIC_IN), +// new AnimDouble(0, Easing.CUBIC_OUT), +// new AnimDouble(0, Easing.CUBIC_IN_OUT), +// +// new AnimDouble(0, Easing.QUADRATIC_IN), +// new AnimDouble(0, Easing.QUADRATIC_OUT), +// new AnimDouble(0, Easing.QUADRATIC_IN_OUT), +// +// new AnimDouble(0, Easing.QUINTIC_IN), +// new AnimDouble(0, Easing.QUINTIC_OUT), +// new AnimDouble(0, Easing.QUINTIC_IN_OUT), +// +// new AnimDouble(0, Easing.EXPO_IN), +// new AnimDouble(0, Easing.EXPO_OUT), +// new AnimDouble(0, Easing.EXPO_IN_OUT), +// +// new AnimDouble(0, Easing.SINE_IN), +// new AnimDouble(0, Easing.SINE_OUT), +// new AnimDouble(0, Easing.SINE_IN_OUT), +// +// new AnimDouble(0, Easing.CIRC_IN), +// new AnimDouble(0, Easing.CIRC_OUT), +// new AnimDouble(0, Easing.CIRC_IN_OUT), +// +// new AnimDouble(0, Easing.BOUNCE_IN), +// new AnimDouble(0, Easing.BOUNCE_OUT), +// new AnimDouble(0, Easing.BOUNCE_IN_OUT), +// +// new AnimDouble(0, Easing.BACK_IN), +// new AnimDouble(0, Easing.BACK_OUT), +// new AnimDouble(0, Easing.BACK_IN_OUT), +// +// new AnimDouble(0, Easing.ELASTIC_IN), +// new AnimDouble(0, Easing.ELASTIC_OUT), +// new AnimDouble(0, Easing.ELASTIC_IN_OUT), + }; + //@formatter:on + + @Override + public void initialize() + { + bindKeyStroke(new KeyStroke(Keyboard.KEY_RIGHT), new Runnable() { + + @Override + public void run() + { + for (AnimDouble a : anims) { + a.animate(0, 1, 1+rand.nextDouble()*1); + } + } + }); + + bindKeyStroke(new KeyStroke(Keyboard.KEY_LEFT), new Runnable() { + + @Override + public void run() + { + for (AnimDouble a : anims) { + a.animate(1, 0, 1+rand.nextDouble()*1); + } + } + }); + } + + + @Override + protected void renderScreen() + { + double screenH = Display.getHeight(); + double screenW = Display.getWidth(); + double perBoxH = screenH / anims.length; + double padding = perBoxH * 0.1; + double boxSide = perBoxH - padding * 2; + + for (int i = 0; i < anims.length; i++) { + AnimDouble a = anims[i]; + + RenderUtils.setColor(RGB.GREEN); + RenderUtils.quadSize(padding + a.getCurrentValue() * (screenW - perBoxH), screenH - perBoxH * i - perBoxH + padding, boxSide, boxSide); + } + + RenderUtils.setColor(RGB.YELLOW); + RenderUtils.translate(new Coord(Display.getWidth() / 2, Display.getHeight() / 2)); + RenderUtils.rotateZ(degAnim.getCurrentValue()); + RenderUtils.quadSize(-10, -10, 20, 200); + } + + + @Override + public void receive(MouseMotionEvent event) + { + // + } + + + @Override + public void receive(MouseButtonEvent event) + { + if (event.isDown()) { + Coord vec = App.disp().getSize().half().vecTo(event.getPos()); + + Polar p = Polar.fromCoord(vec); + + degAnim.fadeTo(p.getAngleDeg() - 90, 1.5); + } + } + + + @Override + protected void onEnter() + { + // TODO Auto-generated method stub + + } + + + @Override + protected void onLeave() + { + // TODO Auto-generated method stub + + } + + + @Override + protected void onSizeChanged(Coord size) + { + // TODO Auto-generated method stub + + } + + + @Override + protected void updateScreen(double delta) + { + degAnim.update(delta); + + for (AnimDouble a : anims) { + a.update(delta); + } + } + +} diff --git a/src/mightypork/rogue/display/constraints/Constraint.java b/src/mightypork/rogue/display/constraints/Constraint.java index 12bbf1f..ba85052 100644 --- a/src/mightypork/rogue/display/constraints/Constraint.java +++ b/src/mightypork/rogue/display/constraints/Constraint.java @@ -1,5 +1,6 @@ package mightypork.rogue.display.constraints; + import mightypork.utils.math.coord.Coord; @@ -11,9 +12,10 @@ public abstract class Constraint implements ConstraintContext { public Constraint(ConstraintContext context) { this.context = context; } - - - public void setContext(ConstraintContext context) { + + + public void setContext(ConstraintContext context) + { this.context = context; } @@ -22,12 +24,16 @@ public abstract class Constraint implements ConstraintContext { { return context; } - - protected Coord origin() { + + + protected Coord origin() + { return context.getRect().getOrigin(); } - - protected Coord size() { + + + protected Coord size() + { return context.getRect().getSize(); } diff --git a/src/mightypork/rogue/display/constraints/ConstraintContext.java b/src/mightypork/rogue/display/constraints/ConstraintContext.java index 4395a02..fe33008 100644 --- a/src/mightypork/rogue/display/constraints/ConstraintContext.java +++ b/src/mightypork/rogue/display/constraints/ConstraintContext.java @@ -1,9 +1,10 @@ package mightypork.rogue.display.constraints; -import mightypork.utils.math.coord.Coord; + import mightypork.utils.math.coord.Rect; public interface ConstraintContext { + public Rect getRect(); } diff --git a/src/mightypork/rogue/input/InputSystem.java b/src/mightypork/rogue/input/InputSystem.java index 0bd3d6a..97d1143 100644 --- a/src/mightypork/rogue/input/InputSystem.java +++ b/src/mightypork/rogue/input/InputSystem.java @@ -8,6 +8,7 @@ import mightypork.rogue.input.events.MouseMotionEvent; import mightypork.utils.math.coord.Coord; import mightypork.utils.patterns.Destroyable; import mightypork.utils.patterns.Initializable; + import org.lwjgl.LWJGLException; import org.lwjgl.input.Keyboard; import org.lwjgl.input.Mouse; @@ -112,7 +113,7 @@ public class InputSystem implements KeyBinder, Destroyable, Initializable { int wheeld = Mouse.getEventDWheel(); if (button != -1 || wheeld != 0) App.broadcast(new MouseButtonEvent(pos, button, down, wheeld)); - if(!move.isZero()) App.broadcast(new MouseMotionEvent(pos, move)); + if (!move.isZero()) App.broadcast(new MouseMotionEvent(pos, move)); } diff --git a/src/mightypork/rogue/input/KeyBinder.java b/src/mightypork/rogue/input/KeyBinder.java index e1edb61..56a1e23 100644 --- a/src/mightypork/rogue/input/KeyBinder.java +++ b/src/mightypork/rogue/input/KeyBinder.java @@ -14,6 +14,7 @@ public interface KeyBinder { /** * Remove handler from a keystroke (id any) + * * @param stroke stroke */ abstract void unbindKeyStroke(KeyStroke stroke); diff --git a/src/mightypork/rogue/input/KeyBinding.java b/src/mightypork/rogue/input/KeyBinding.java index 8f31420..9af594c 100644 --- a/src/mightypork/rogue/input/KeyBinding.java +++ b/src/mightypork/rogue/input/KeyBinding.java @@ -14,7 +14,7 @@ public class KeyBinding implements KeyboardEvent.Listener { public KeyBinding(KeyStroke stroke, Runnable handler) { this.keystroke = stroke; this.handler = handler; - + wasActive = keystroke.isActive(); } @@ -35,13 +35,13 @@ public class KeyBinding implements KeyboardEvent.Listener { public void receive(KeyboardEvent event) { // ignore unrelated events - if(!keystroke.getKeys().contains(event.getKey())) return; + if (!keystroke.getKeys().contains(event.getKey())) return; // run handler when event was met if (keystroke.isActive() && !wasActive) { handler.run(); } - + wasActive = keystroke.isActive(); } diff --git a/src/mightypork/rogue/input/KeyBindingPool.java b/src/mightypork/rogue/input/KeyBindingPool.java index 9b71d0c..2951f98 100644 --- a/src/mightypork/rogue/input/KeyBindingPool.java +++ b/src/mightypork/rogue/input/KeyBindingPool.java @@ -30,7 +30,7 @@ public class KeyBindingPool implements KeyBinder, KeyboardEvent.Listener { { for (KeyBinding kb : bindings) { if (kb.matches(stroke)) { - Log.w("Duplicate KeyBinding ("+stroke+"), using newest handler."); + Log.w("Duplicate KeyBinding (" + stroke + "), using newest handler."); kb.setHandler(task); return; } @@ -42,6 +42,7 @@ public class KeyBindingPool implements KeyBinder, KeyboardEvent.Listener { /** * Remove handler from keystroke (id any) + * * @param stroke stroke */ @Override @@ -58,11 +59,12 @@ public class KeyBindingPool implements KeyBinder, KeyboardEvent.Listener { } } + @Override public void receive(KeyboardEvent event) { - for(KeyBinding kb: bindings) { - kb.receive(event); + for (KeyBinding kb : bindings) { + kb.receive(event); } } } diff --git a/src/mightypork/rogue/input/events/KeyboardEvent.java b/src/mightypork/rogue/input/events/KeyboardEvent.java index cc478da..56eb201 100644 --- a/src/mightypork/rogue/input/events/KeyboardEvent.java +++ b/src/mightypork/rogue/input/events/KeyboardEvent.java @@ -1,10 +1,10 @@ package mightypork.rogue.input.events; -import org.lwjgl.input.Keyboard; - import mightypork.utils.patterns.subscription.Handleable; +import org.lwjgl.input.Keyboard; + /** * A keyboard event @@ -41,7 +41,8 @@ public class KeyboardEvent implements Handleable { { return down; } - + + /** * @return true if key was just released */ @@ -75,11 +76,12 @@ public class KeyboardEvent implements Handleable { */ public void receive(KeyboardEvent event); } - + + @Override public String toString() { - return Keyboard.getKeyName(key)+":"+(down?"DOWN":"UP"); + return Keyboard.getKeyName(key) + ":" + (down ? "DOWN" : "UP"); } } diff --git a/src/mightypork/rogue/sounds/BaseAudioPlayer.java b/src/mightypork/rogue/sounds/BaseAudioPlayer.java index ac4a152..067041c 100644 --- a/src/mightypork/rogue/sounds/BaseAudioPlayer.java +++ b/src/mightypork/rogue/sounds/BaseAudioPlayer.java @@ -5,7 +5,7 @@ import mightypork.utils.objects.Mutable; public abstract class BaseAudioPlayer { - + /** the track */ private AudioX audio; diff --git a/src/mightypork/rogue/sounds/LoopPlayer.java b/src/mightypork/rogue/sounds/LoopPlayer.java index 858ae49..ef937e4 100644 --- a/src/mightypork/rogue/sounds/LoopPlayer.java +++ b/src/mightypork/rogue/sounds/LoopPlayer.java @@ -2,9 +2,9 @@ package mightypork.rogue.sounds; import mightypork.utils.objects.Mutable; -import mightypork.utils.time.AnimDouble; import mightypork.utils.time.Pauseable; import mightypork.utils.time.Updateable; +import mightypork.utils.time.animation.AnimDouble; import org.lwjgl.openal.AL10; diff --git a/src/mightypork/rogue/sounds/SoundSystem.java b/src/mightypork/rogue/sounds/SoundSystem.java index f828271..b0094e5 100644 --- a/src/mightypork/rogue/sounds/SoundSystem.java +++ b/src/mightypork/rogue/sounds/SoundSystem.java @@ -34,7 +34,6 @@ public class SoundSystem implements Updateable, Destroyable { private static Coord listener = new Coord(); - static { // initialize sound system SoundStore.get().setMaxSources(MAX_SOURCES); @@ -114,18 +113,19 @@ public class SoundSystem implements Updateable, Destroyable { p.setFadeTimes(fadeIn, fadeOut); loops.put(key, p); } - - + + /** * Create {@link AudioX} for a resource + * * @param res a resource name * @return the resource - * * @throws IllegalArgumentException if resource is already registered */ - private AudioX getResource(String res) { + private AudioX getResource(String res) + { AudioX a = new AudioX(res); - if(resources.contains(a)) throw new IllegalArgumentException("Sound resource "+res+" is already registered."); + if (resources.contains(a)) throw new IllegalArgumentException("Sound resource " + res + " is already registered."); resources.add(a); return a; } @@ -298,10 +298,10 @@ public class SoundSystem implements Updateable, Destroyable { @Override public void destroy() { - for(AudioX r: resources) { + for (AudioX r : resources) { r.destroy(); } - + SoundStore.get().clear(); AL.destroy(); } diff --git a/src/mightypork/rogue/util/RenderUtils.java b/src/mightypork/rogue/util/RenderUtils.java index 6fa2844..8892d5d 100644 --- a/src/mightypork/rogue/util/RenderUtils.java +++ b/src/mightypork/rogue/util/RenderUtils.java @@ -4,7 +4,6 @@ package mightypork.rogue.util; import static org.lwjgl.opengl.GL11.*; import mightypork.rogue.textures.TextureManager; import mightypork.rogue.textures.TxQuad; -import mightypork.utils.math.Calc.Deg; import mightypork.utils.math.color.HSV; import mightypork.utils.math.color.RGB; import mightypork.utils.math.coord.Coord; diff --git a/src/mightypork/rogue/util/Utils.java b/src/mightypork/rogue/util/Utils.java index eadec42..9183f53 100644 --- a/src/mightypork/rogue/util/Utils.java +++ b/src/mightypork/rogue/util/Utils.java @@ -7,7 +7,9 @@ package mightypork.rogue.util; * @author MightyPork */ public class Utils { - public static Thread runAsThread(Runnable r) { + + public static Thread runAsThread(Runnable r) + { Thread t = new Thread(r); t.start(); return t; diff --git a/src/mightypork/utils/math/Calc.java b/src/mightypork/utils/math/Calc.java index c8ff741..bafe366 100644 --- a/src/mightypork/utils/math/Calc.java +++ b/src/mightypork/utils/math/Calc.java @@ -746,7 +746,7 @@ public class Calc { * @return current number to render */ public static double interpolate(double from, double to, double time, Easing easing) - { + { return from + (to - from) * easing.get(time); } @@ -757,12 +757,12 @@ public class Calc { * @param from last angle * @param to new angle * @param time time 0..1 - * @param easing easing function + * @param easing easing function * @return current angle to render */ public static double interpolateDeg(double from, double to, double time, Easing easing) { - return Deg.norm(from - Deg.delta(to, from) * easing.get(time)); + return Deg.norm(from - Deg.delta(to, from) * easing.get(time)); } @@ -772,12 +772,12 @@ public class Calc { * @param from last angle * @param to new angle * @param time time 0..1 - * @param easing easing function + * @param easing easing function * @return current angle to render */ public static double interpolateRad(double from, double to, double time, Easing easing) { - return Rad.norm(from - Rad.delta(to, from) * easing.get(time)); + return Rad.norm(from - Rad.delta(to, from) * easing.get(time)); } diff --git a/src/mightypork/utils/math/coord/CoordAnimated.java b/src/mightypork/utils/math/coord/CoordAnimated.java index 71be196..e8d7fb9 100644 --- a/src/mightypork/utils/math/coord/CoordAnimated.java +++ b/src/mightypork/utils/math/coord/CoordAnimated.java @@ -1,5 +1,6 @@ package mightypork.utils.math.coord; + import mightypork.utils.math.Calc; import mightypork.utils.time.Updateable; @@ -16,6 +17,7 @@ public class CoordAnimated extends Coord implements Updateable { private Coord start; private double time = 0; + /** * Update delta timing * @@ -34,6 +36,7 @@ public class CoordAnimated extends Coord implements Updateable { } } + /** * Remember position (other changes will be for animation) */ @@ -45,6 +48,7 @@ public class CoordAnimated extends Coord implements Updateable { offs = Coord.zero(); } + /** * Start animation * @@ -59,6 +63,7 @@ public class CoordAnimated extends Coord implements Updateable { offs = start.vecTo(this); } + /** * Stop animation, assign to current value */ @@ -69,6 +74,7 @@ public class CoordAnimated extends Coord implements Updateable { animTime = 0; } + /** * Get if animation is finished * @@ -79,6 +85,7 @@ public class CoordAnimated extends Coord implements Updateable { return animTime >= time; } + /** * Get current value (animated) * @@ -87,12 +94,12 @@ public class CoordAnimated extends Coord implements Updateable { public Coord animGetCurrent() { if (time == 0) return copy(); // avoid zero division - + if (start == null) start = new Coord(); if (offs == null) offs = new Coord(); - + if (animIsFinished()) return this; - + return start.add(offs.mul(animTime / time)); } diff --git a/src/mightypork/utils/math/easing/Easing.java b/src/mightypork/utils/math/easing/Easing.java index cf5b51e..2079a58 100644 --- a/src/mightypork/utils/math/easing/Easing.java +++ b/src/mightypork/utils/math/easing/Easing.java @@ -1,322 +1,311 @@ package mightypork.utils.math.easing; -import mightypork.utils.math.Calc; - - /** - * Easing function.
- * The easing function must be time-invariant and it's domain is [0-1]. + * EasingFunction function. * * @author MightyPork */ -public interface Easing { +public abstract class Easing { /** - * Get value of the easing function at given time. + * Get value at time t. * - * @param time number in range 0..1 - * @return value at given time + * @param t time parameter (t = 1..1) + * @return value at given t (0..1, can exceed if needed) */ - public double get(double time); + public abstract double get(double t); - public static final Easing NONE = new Easing() { - @Override - public double get(double time) - { - double t = Calc.clampd(time, 0, 1); + /** + * Reverse an easing + * + * @param original original easing + * @return reversed easing + */ + public static Easing reverse(Easing original) + { + return new Reverse(original); + } - return (t < 0.5 ? 0 : 1); - } - }; - public static final Easing LINEAR = new Easing() { + /** + * Combine two easings + * + * @param in initial easing + * @param out terminal easing + * @return product + */ + public static Easing combine(Easing in, Easing out) + { + return new Composite(in, out); + } - @Override - public double get(double time) - { - double t = Calc.clampd(time, 0, 1); - return t; - } - }; + /** + * Create "bilinear" easing - compose of straight and reverse. + * + * @param in initial easing + * @return product + */ + public static Easing inOut(Easing in) + { + return combine(in, reverse(in)); + } - public static final Easing QUADRATIC_IN = new Easing() { + /** + * Reverse EasingFunction + * + * @author MightyPork + */ + private static class Reverse extends Easing { - @Override - public double get(double time) - { - double t = Calc.clampd(time, 0, 1); + private Easing ea; - return t * t; + + /** + * @param in Easing to reverse + */ + public Reverse(Easing in) { + this.ea = in; } - }; - public static final Easing QUADRATIC_OUT = new Easing() { @Override - public double get(double time) + public double get(double t) { - double t = Calc.clampd(time, 0, 1); - - return 1 - (t - 1) * (t - 1); + return 1 - ea.get(1 - t); } - }; + } + + /** + * Composite EasingFunction (0-0.5 EasingFunction A, 0.5-1 EasingFunction B) + * + * @author MightyPork + */ + private static class Composite extends Easing { - public static final Easing QUADRATIC = new Easing() { + private Easing in; + private Easing out; - @Override - public double get(double time) - { - double t = Calc.clampd(time, 0, 1); - if (t < 0.5) return QUADRATIC_IN.get(2 * t) * 0.5; - return 0.5 + QUADRATIC_OUT.get(2 * t - 1) * 0.5; + /** + * Create a composite EasingFunction + * + * @param in initial EasingFunction + * @param out terminal EasingFunction + */ + public Composite(Easing in, Easing out) { + this.in = in; + this.out = out; } - }; - public static final Easing CUBIC_IN = new Easing() { @Override - public double get(double time) + public double get(double t) { - double t = Calc.clampd(time, 0, 1); - - return t * t * t; + if (t < 0.5) return in.get(2 * t) * 0.5; + return 0.5 + out.get(2 * t - 1) * 0.5; } - }; - public static final Easing CUBIC_OUT = new Easing() { + } + + /** No easing; At t=0.5 goes high. */ + public static final Easing NONE = new Easing() { @Override - public double get(double time) + public double get(double t) { - double t = Calc.clampd(time, 0, 1); - - return (t - 1) * (t - 1) * (t - 1) + 1; + return (t < 0.5 ? 0 : 1); } }; - public static final Easing CUBIC = new Easing() { + + /** Linear (y=t) easing */ + public static final Easing LINEAR = new Easing() { @Override - public double get(double time) + public double get(double t) { - double d = 1; - double t = time; - double b = 0; - double c = (1 - 0); - t /= d / 2; - if (t < 1) return c / 2 * t * t * t + b; - t -= 2; - return c / 2 * (t * t * t + 2) + b; + return t; } }; - public static final Easing QUARTIC_IN = new Easing() { + + /** Quadratic (y=t^2) easing in */ + public static final Easing QUADRATIC_IN = new Easing() { @Override - public double get(double time) + public double get(double t) { - double d = 1; - double t = time; - double b = 0; - double c = (1 - 0); - t /= d; - return c * t * t * t * t + b; + return t * t; } }; - public static final Easing QUARTIC_OUT = new Easing() { + + /** Quadratic (y=t^2) easing out */ + public static final Easing QUADRATIC_OUT = reverse(QUADRATIC_IN); + + /** Quadratic (y=t^2) easing both */ + public static final Easing QUADRATIC_IN_OUT = inOut(QUADRATIC_IN); + + /** Cubic (y=t^3) easing in */ + public static final Easing CUBIC_IN = new Easing() { @Override - public double get(double time) + public double get(double t) { - double d = 1; - double t = time; - double b = 0; - double c = (1 - 0); - - t /= d; - t--; - return -c * (t * t * t * t - 1) + b; + return t * t * t; } }; - public static final Easing QUARTIC = new Easing() { + + /** Cubic (y=t^3) easing out */ + public static final Easing CUBIC_OUT = reverse(CUBIC_IN); + + /** Cubic (y=t^3) easing both */ + public static final Easing CUBIC_IN_OUT = inOut(CUBIC_IN); + + /** Quartic (y=t^4) easing in */ + public static final Easing QUARTIC_IN = new Easing() { @Override - public double get(double time) + public double get(double t) { - double d = 1; - double t = time; - double b = 0; - double c = (1 - 0); - - t /= d / 2; - if (t < 1) return c / 2 * t * t * t * t + b; - t -= 2; - return -c / 2 * (t * t * t * t - 2) + b; + return t * t * t * t; } }; + + /** Quartic (y=t^4) easing out */ + public static final Easing QUARTIC_OUT = reverse(QUADRATIC_IN); + + /** Quartic (y=t^4) easing both */ + public static final Easing QUARTIC_IN_OUT = inOut(QUADRATIC_IN); + + /** Quintic (y=t^5) easing in */ public static final Easing QUINTIC_IN = new Easing() { @Override - public double get(double time) + public double get(double t) { - double d = 1; - double t = time; - double b = 0; - double c = (1 - 0); - t /= d; - return c * t * t * t * t * t + b; + return t * t * t * t * t; } }; - public static final Easing QUINTIC_OUT = new Easing() { - @Override - public double get(double time) - { - double d = 1; - double t = time; - double b = 0; - double c = (1 - 0); - t /= d; - t--; - return c * (t * t * t * t * t + 1) + b; - } - }; - public static final Easing QUINTIC_IN_OUT = new Easing() { + /** Quintic (y=t^5) easing out */ + public static final Easing QUINTIC_OUT = reverse(QUINTIC_IN); - @Override - public double get(double time) - { - double d = 1; - double t = time; - double b = 0; - double c = (1 - 0); - t /= d / 2; - if (t < 1) return c / 2 * t * t * t * t * t + b; - t -= 2; - return c / 2 * (t * t * t * t * t + 2) + b; - } - }; + /** Quintic (y=t^5) easing both */ + public static final Easing QUINTIC_IN_OUT = inOut(QUINTIC_IN); + + /** Sine easing in */ public static final Easing SINE_IN = new Easing() { @Override - public double get(double time) + public double get(double t) { - double d = 1; - double t = time; - double b = 0; - double c = (1 - 0); - return -c * Math.cos(t / d * (Math.PI / 2)) + c + b; + return 1 - Math.cos(t * (Math.PI / 2)); } }; - public static final Easing SINE_OUT = new Easing() { - @Override - public double get(double time) - { - double d = 1; - double t = time; - double b = 0; - double c = (1 - 0); + /** Sine easing out */ + public static final Easing SINE_OUT = reverse(SINE_IN); - return c * Math.sin(t / d * (Math.PI / 2)) + b; - } - }; - public static final Easing SINE = new Easing() { + /** Sine easing both */ + public static final Easing SINE_IN_OUT = inOut(SINE_IN); - @Override - public double get(double time) - { - double d = 1; - double t = time; - double b = 0; - double c = (1 - 0); - return -c / 2 * (Math.cos(Math.PI * t / d) - 1) + b; - } - }; + /** Exponential easing in */ public static final Easing EXPO_IN = new Easing() { @Override - public double get(double time) + public double get(double t) { - double d = 1; - double t = time; - double b = 0; - double c = (1 - 0); - - return c * Math.pow(2, 10 * (t / d - 1)) + b; + return Math.pow(2, 10 * (t - 1)); } }; - public static final Easing EXPO_OUT = new Easing() { - @Override - public double get(double time) - { - double d = 1; - double t = time; - double b = 0; - double c = (1 - 0); + /** Exponential easing out */ + public static final Easing EXPO_OUT = reverse(EXPO_IN); - return c * (-Math.pow(2, -10 * t / d) + 1) + b; - } - }; - public static final Easing EXPO = new Easing() { + /** Exponential easing both */ + public static final Easing EXPO_IN_OUT = inOut(EXPO_IN); + + /** Circular easing in */ + public static final Easing CIRC_IN = new Easing() { @Override - public double get(double time) + public double get(double t) { - double d = 1; - double t = time; - double b = 0; - double c = (1 - 0); - - t /= d / 2; - if (t < 1) return c / 2 * Math.pow(2, 10 * (t - 1)) + b; - t--; - return c / 2 * (-Math.pow(2, -10 * t) + 2) + b; + return 1 - Math.sqrt(1 - t * t); } }; - public static final Easing CIRC_IN = new Easing() { + + /** Circular easing out */ + public static final Easing CIRC_OUT = reverse(CIRC_IN); + + /** Circular easing both */ + public static final Easing CIRC_IN_OUT = inOut(CIRC_IN); + + /** Bounce easing in */ + public static final Easing BOUNCE_OUT = new Easing() { @Override - public double get(double time) + public double get(double t) { - double d = 1; - double t = time; - double b = 0; - double c = (1 - 0); - t /= d; - return -c * (Math.sqrt(1 - t * t) - 1) + b; + if (t < (1 / 2.75f)) { + return (7.5625f * t * t); + + } else if (t < (2 / 2.75f)) { + t -= (1.5f / 2.75f); + return (7.5625f * t * t + 0.75f); + + } else if (t < (2.5 / 2.75)) { + t -= (2.25f / 2.75f); + return (7.5625f * t * t + 0.9375f); + + } else { + t -= (2.625f / 2.75f); + return (7.5625f * t * t + 0.984375f); + } } }; - public static final Easing CIRC_OUT = new Easing() { + + /** Bounce easing out */ + public static final Easing BOUNCE_IN = reverse(BOUNCE_OUT); + + /** Bounce easing both */ + public static final Easing BOUNCE_IN_OUT = inOut(BOUNCE_IN); + + /** Back easing in */ + public static final Easing BACK_IN = new Easing() { @Override - public double get(double time) + public double get(double t) { - double d = 1; - double t = time; - double b = 0; - double c = (1 - 0); - - t--; - return c * Math.sqrt(1 - t * t) + b; + float s = 1.70158f; + return t * t * ((s + 1) * t - s); } }; - public static final Easing CIRC = new Easing() { + + /** Back easing out */ + public static final Easing BACK_OUT = reverse(BACK_IN); + + /** Back easing both */ + public static final Easing BACK_IN_OUT = inOut(BACK_IN); + + /** Elastic easing in */ + public static final Easing ELASTIC_IN = new Easing() { @Override - public double get(double time) + public double get(double t) { - double d = 1; - double t = time; - double b = 0; - double c = (1 - 0); - - t /= d / 2; - if (t < 1) return -c / 2 * (Math.sqrt(1 - t * t) - 1) + b; - t -= 2; - return c / 2 * (Math.sqrt(1 - t * t) + 1) + b; + if (t == 0) return 0; + if (t == 1) return 1; + + double p = .3f; + double s = p / 4; + return -(Math.pow(2, 10 * (t -= 1)) * Math.sin((t - s) * (2 * Math.PI) / p)); } }; + + /** Elastic easing out */ + public static final Easing ELASTIC_OUT = reverse(ELASTIC_IN); + + /** Elastic easing both */ + public static final Easing ELASTIC_IN_OUT = inOut(ELASTIC_IN); } diff --git a/src/mightypork/utils/objects/Mutable.java b/src/mightypork/utils/objects/Mutable.java index 5cc59c5..e965ed0 100644 --- a/src/mightypork/utils/objects/Mutable.java +++ b/src/mightypork/utils/objects/Mutable.java @@ -61,8 +61,8 @@ public class Mutable { if (this == obj) return true; if (obj == null) return false; if (!(obj instanceof Mutable)) return false; - - Mutable other = (Mutable) obj; + + Mutable other = (Mutable) obj; if (o == null) { if (other.o != null) return false; } else if (!o.equals(other.o)) { @@ -70,11 +70,12 @@ public class Mutable { } return true; } - + + @Override public String toString() { - if(o == null) return ""; + if (o == null) return ""; return o.toString(); } } diff --git a/src/mightypork/utils/objects/VarargsParser.java b/src/mightypork/utils/objects/VarargsParser.java index 56769c9..23c1e3f 100644 --- a/src/mightypork/utils/objects/VarargsParser.java +++ b/src/mightypork/utils/objects/VarargsParser.java @@ -15,6 +15,7 @@ import java.util.Map; * * * + * * Object[] array = { "one", 1, "two", 4, "three", 9, "four", 16 }; * Map<String, Integer> args = new VarargsParser<String, Integer>().parse(array); * diff --git a/src/mightypork/utils/patterns/Destroyable.java b/src/mightypork/utils/patterns/Destroyable.java index 17569be..51caf15 100644 --- a/src/mightypork/utils/patterns/Destroyable.java +++ b/src/mightypork/utils/patterns/Destroyable.java @@ -7,6 +7,7 @@ package mightypork.utils.patterns; * @author MightyPork */ public interface Destroyable { + /** * Destroy this object */ diff --git a/src/mightypork/utils/patterns/Initializable.java b/src/mightypork/utils/patterns/Initializable.java index 12d50cc..31a7ba7 100644 --- a/src/mightypork/utils/patterns/Initializable.java +++ b/src/mightypork/utils/patterns/Initializable.java @@ -7,6 +7,7 @@ package mightypork.utils.patterns; * @author MightyPork */ public interface Initializable { + /** * Initialize if not initialized yet */ diff --git a/src/mightypork/utils/patterns/subscription/MessageBus.java b/src/mightypork/utils/patterns/subscription/MessageBus.java index 1d6afe7..30ca56a 100644 --- a/src/mightypork/utils/patterns/subscription/MessageBus.java +++ b/src/mightypork/utils/patterns/subscription/MessageBus.java @@ -1,5 +1,6 @@ package mightypork.utils.patterns.subscription; + import java.util.LinkedHashSet; import java.util.Set; @@ -29,7 +30,7 @@ public class MessageBus implements Subscribable { // if the channel already exists, return this instance instead. for (MessageChannel ch : channels) { if (ch.equals(channel)) { - Log.w("Channel of type "+channel+" already registered."); + Log.w("Channel of type " + channel + " already registered."); return ch; } } diff --git a/src/mightypork/utils/patterns/subscription/MessageChannel.java b/src/mightypork/utils/patterns/subscription/MessageChannel.java index dd96dde..f3a41f6 100644 --- a/src/mightypork/utils/patterns/subscription/MessageChannel.java +++ b/src/mightypork/utils/patterns/subscription/MessageChannel.java @@ -23,9 +23,9 @@ public final class MessageChannel, CLIENT> im public MessageChannel(Class messageClass, Class clientClass) { - - if(messageClass == null || clientClass == null) throw new IllegalArgumentException("Null Message or Client class."); - + + if (messageClass == null || clientClass == null) throw new IllegalArgumentException("Null Message or Client class."); + this.clientClass = clientClass; this.messageClass = messageClass; } @@ -124,7 +124,7 @@ public final class MessageChannel, CLIENT> im if (this == obj) return true; if (obj == null) return false; if (!(obj instanceof MessageChannel)) return false; - + MessageChannel other = (MessageChannel) obj; if (!clientClass.getName().equals(other.clientClass.getName())) return false; @@ -133,11 +133,12 @@ public final class MessageChannel, CLIENT> im return true; } - + + @Override public String toString() { - return "CHANNEL( "+messageClass.getSimpleName()+" -> "+clientClass.getSimpleName()+" )"; + return "CHANNEL( " + messageClass.getSimpleName() + " -> " + clientClass.getSimpleName() + " )"; } diff --git a/src/mightypork/utils/time/AnimDouble.java b/src/mightypork/utils/time/animation/AnimDouble.java similarity index 94% rename from src/mightypork/utils/time/AnimDouble.java rename to src/mightypork/utils/time/animation/AnimDouble.java index 7148e26..0860c42 100644 --- a/src/mightypork/utils/time/AnimDouble.java +++ b/src/mightypork/utils/time/animation/AnimDouble.java @@ -1,8 +1,10 @@ -package mightypork.utils.time; +package mightypork.utils.time.animation; import mightypork.utils.math.Calc; import mightypork.utils.math.easing.Easing; +import mightypork.utils.time.Pauseable; +import mightypork.utils.time.Updateable; /** @@ -194,7 +196,12 @@ public class AnimDouble implements Updateable, Pauseable { this.from = from; this.to = to; - this.duration = time * (1 - getProgressFromValue(current)); + + double progress = getProgressFromValue(current); + + this.from = (progress > 0 ? current : from); + + this.duration = time * (1 - progress); this.elapsedTime = 0; } @@ -203,6 +210,8 @@ public class AnimDouble implements Updateable, Pauseable { { double p = 0; + if (from == to) return 0; + if (value >= from && value <= to) { // up p = ((value - from) / (to - from)); } else if (value >= to && value <= from) { // down diff --git a/src/mightypork/utils/time/AnimDoubleDeg.java b/src/mightypork/utils/time/animation/AnimDoubleDeg.java similarity index 95% rename from src/mightypork/utils/time/AnimDoubleDeg.java rename to src/mightypork/utils/time/animation/AnimDoubleDeg.java index dfcd7ae..d281d5e 100644 --- a/src/mightypork/utils/time/AnimDoubleDeg.java +++ b/src/mightypork/utils/time/animation/AnimDoubleDeg.java @@ -1,4 +1,4 @@ -package mightypork.utils.time; +package mightypork.utils.time.animation; import mightypork.utils.math.Calc; diff --git a/src/mightypork/utils/time/AnimDoubleRad.java b/src/mightypork/utils/time/animation/AnimDoubleRad.java similarity index 95% rename from src/mightypork/utils/time/AnimDoubleRad.java rename to src/mightypork/utils/time/animation/AnimDoubleRad.java index d2447e0..3787571 100644 --- a/src/mightypork/utils/time/AnimDoubleRad.java +++ b/src/mightypork/utils/time/animation/AnimDoubleRad.java @@ -1,4 +1,4 @@ -package mightypork.utils.time; +package mightypork.utils.time.animation; import mightypork.utils.math.Calc;