From abf7c565175a94ca6c67c754fb875a46fd2afdc9 Mon Sep 17 00:00:00 2001 From: Jacob Frank Date: Sat, 16 Jan 2016 22:31:32 -0800 Subject: [PATCH] WIP #12: Add damage shake effect --- .../config/ConfigurationLoader.java | 1 + .../engconsole/ui/UserInterfaceFrame.java | 8 ++- .../artemis/engconsole/ui/damcon/Damcon.java | 66 ++++++++++++++++--- 3 files changed, 64 insertions(+), 11 deletions(-) diff --git a/src/com/brindyblitz/artemis/engconsole/config/ConfigurationLoader.java b/src/com/brindyblitz/artemis/engconsole/config/ConfigurationLoader.java index 90f2343..c41dc53 100644 --- a/src/com/brindyblitz/artemis/engconsole/config/ConfigurationLoader.java +++ b/src/com/brindyblitz/artemis/engconsole/config/ConfigurationLoader.java @@ -20,6 +20,7 @@ public class ConfigurationLoader { private static final int[] RESERVED_KEYS = new int[] { KeyEvent.VK_BACK_SLASH, + KeyEvent.VK_BACK_QUOTE, KeyEvent.VK_SPACE, KeyEvent.VK_ENTER, KeyEvent.VK_0, diff --git a/src/com/brindyblitz/artemis/engconsole/ui/UserInterfaceFrame.java b/src/com/brindyblitz/artemis/engconsole/ui/UserInterfaceFrame.java index b6d6790..dcc97fe 100644 --- a/src/com/brindyblitz/artemis/engconsole/ui/UserInterfaceFrame.java +++ b/src/com/brindyblitz/artemis/engconsole/ui/UserInterfaceFrame.java @@ -101,7 +101,7 @@ public void keyPressed(KeyEvent e) { System.out.println("Beams: " + this.engineeringConsoleManager.getSystemEnergyAllocated(BEAMS) + "%"); System.out.println("Torpedoes: " + this.engineeringConsoleManager.getSystemEnergyAllocated(TORPEDOES) + "%"); System.out.println("Sensors: " + this.engineeringConsoleManager.getSystemEnergyAllocated(SENSORS) + "%"); - System.out.println("Manuvering: " + this.engineeringConsoleManager.getSystemEnergyAllocated(MANEUVERING) + "%"); + System.out.println("Maneuvering: " + this.engineeringConsoleManager.getSystemEnergyAllocated(MANEUVERING) + "%"); System.out.println("Impulse: " + this.engineeringConsoleManager.getSystemEnergyAllocated(IMPULSE) + "%"); System.out.println("Warp: " + this.engineeringConsoleManager.getSystemEnergyAllocated(WARP_JUMP_DRIVE) + "%"); System.out.println("Front Shields: " + this.engineeringConsoleManager.getSystemEnergyAllocated(FORE_SHIELDS) + "%"); @@ -109,7 +109,11 @@ public void keyPressed(KeyEvent e) { System.out.println("\n\n\n"); } else if (kc == KeyEvent.VK_BACK_QUOTE) { - this.damcon.setDamageShake(true); + if (e.isShiftDown()) { + this.damcon.toggleDamageShake(); + } else { + this.damcon.startDamageShake(1000l, 0.7d); + } } else if (kc == KeyEvent.VK_SPACE) { this.engineeringConsoleManager.resetEnergy(); diff --git a/src/com/brindyblitz/artemis/engconsole/ui/damcon/Damcon.java b/src/com/brindyblitz/artemis/engconsole/ui/damcon/Damcon.java index f8f9e70..4b3a26c 100644 --- a/src/com/brindyblitz/artemis/engconsole/ui/damcon/Damcon.java +++ b/src/com/brindyblitz/artemis/engconsole/ui/damcon/Damcon.java @@ -17,6 +17,8 @@ import java.io.File; import java.io.FileNotFoundException; import java.util.Enumeration; +import java.util.Random; +import java.util.TimerTask; public class Damcon implements MouseListener, MouseMotionListener, MouseWheelListener { private static final int WIDTH = 400, HEIGHT = 300; @@ -46,6 +48,10 @@ public class Damcon implements MouseListener, MouseMotionListener, MouseWheelLis private Point lastMouseDragPosition = new Point(); private boolean rotating = false, dotified = false; + private static final int[] wireframeLineTypes = + new int[] { LineAttributes.PATTERN_DASH, LineAttributes.PATTERN_DASH_DOT, LineAttributes.PATTERN_DOT, LineAttributes.PATTERN_SOLID }; + private static final Random random = new Random(); + public Damcon(EngineeringConsoleManager engineeringConsoleManager) { this.engineeringConsoleManager = engineeringConsoleManager; @@ -82,7 +88,7 @@ private void loadAndWireframeifyModel() { String OBJ_PATH = new File(System.getProperty("user.dir"), "art/models/obj-from-blender/artemis2.obj").getPath(); try { this.scene = new ObjectFile(ObjectFile.RESIZE).load(OBJ_PATH); - wireframeifyScene(scene, false); + wireframeifyScene(scene, LineAttributes.PATTERN_SOLID); } catch (FileNotFoundException | IncorrectFormatException | ParsingErrorException e) { e.printStackTrace(System.err); @@ -129,8 +135,8 @@ private TransformGroup getCamera() { /////////////// // Wireframe // /////////////// - private static void wireframeifyScene(Scene scene, boolean dots) { - Appearance wireframe = getWireframeAppearance(dots); + private static void wireframeifyScene(Scene scene, int line_attribute_pattern) { + Appearance wireframe = getWireframeAppearance(line_attribute_pattern); // TODO: This works for the Artemis OBJ model. If the scene graph has multiple Shape3D nodes, this would need to be set on all of them. Is that necessary or can we guarantee it won't be needed? Enumeration children = scene.getSceneGroup().getAllChildren(); @@ -149,7 +155,7 @@ private static void wireframeifyScene(Scene scene, boolean dots) { } } - private static Appearance getWireframeAppearance(boolean dots) { + private static Appearance getWireframeAppearance(int line_attributes_pattern) { Appearance appearance = new Appearance(); // Set transparency @@ -159,9 +165,7 @@ private static Appearance getWireframeAppearance(boolean dots) { // Enable automatic anti-aliasing LineAttributes line_attributes = new LineAttributes(); line_attributes.setLineAntialiasingEnable(true); - if (dots) { - line_attributes.setLinePattern(LineAttributes.PATTERN_DOT); - } + line_attributes.setLinePattern(line_attributes_pattern); appearance.setLineAttributes(line_attributes); // Set color @@ -340,8 +344,52 @@ public void mouseWheelMoved(MouseWheelEvent e) { // Misc // ////////// - public void setDamageShake(boolean enabled) { + public void toggleDamageShake() { dotified = !dotified; - wireframeifyScene(this.scene, dotified); + setDamageShake(dotified); + } + + public void setDamageShake (boolean enabled) { + setLineType(enabled ? LineAttributes.PATTERN_DOT : LineAttributes.PATTERN_SOLID); + } + + public void setLineType(int line_attributes_pattern) { + wireframeifyScene(this.scene, line_attributes_pattern); + } + + public void startDamageShake(long duration_ms, double intensity) { + java.util.Timer timer = new java.util.Timer(); + timer.schedule(new DamageShake(duration_ms, intensity), 0, 10); + } + + private class DamageShake extends TimerTask { + private long end; + private double intensity; + + public DamageShake(long duration, double intensity) { + this.end = System.currentTimeMillis() + duration; + this.intensity = intensity; + } + + @Override + public void run() { + if (System.currentTimeMillis() >= end) { + setDamageShake(false); + this.cancel(); + return; + } + + // toggleDamageShake(); + + //if (random.nextDouble() < this.intensity) { + // toggleDamageShake(); + //} + + // setDamageShake(random.nextDouble() < this.intensity); + + if (random.nextDouble() < this.intensity) { + setLineType(wireframeLineTypes[random.nextInt(wireframeLineTypes.length)]); + } + } } } \ No newline at end of file