Skip to content

Commit

Permalink
WIP #12: Add damage shake effect
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobmaxfrank committed Jan 17, 2016
1 parent 1dd583a commit abf7c56
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,15 +101,19 @@ 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) + "%");
System.out.println("Rear Shields: " + this.engineeringConsoleManager.getSystemEnergyAllocated(AFT_SHIELDS) + "%");

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();
Expand Down
66 changes: 57 additions & 9 deletions src/com/brindyblitz/artemis/engconsole/ui/damcon/Damcon.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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<Node> children = scene.getSceneGroup().getAllChildren();
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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)]);
}
}
}
}

0 comments on commit abf7c56

Please sign in to comment.