diff options
Diffstat (limited to '')
-rw-r--r-- | src/main/java/Scene.java | 3 | ||||
-rw-r--r-- | src/main/java/Subconscious.java | 24 | ||||
-rw-r--r-- | src/main/java/WorldScene.java | 20 |
3 files changed, 31 insertions, 16 deletions
diff --git a/src/main/java/Scene.java b/src/main/java/Scene.java index 139553c..f0da8e2 100644 --- a/src/main/java/Scene.java +++ b/src/main/java/Scene.java @@ -1,13 +1,12 @@ import java.awt.Graphics; public abstract class Scene { - protected int sceneYOffset = 0; - protected int sceneXOffset = 0; abstract public void render(Graphics g); public void mouseHover(int x, int y) {} public void mouseClicked(int x, int y) {} + public void zoom(int value) {} public void pan(int x, int y) {} } diff --git a/src/main/java/Subconscious.java b/src/main/java/Subconscious.java index cbb17dc..59824c2 100644 --- a/src/main/java/Subconscious.java +++ b/src/main/java/Subconscious.java @@ -96,9 +96,9 @@ public class Subconscious @Override public void run() { long beforeTime, afterTime, sleepTime; + int skipFrame = 0; window.setVisible(true); - beforeTime = System.currentTimeMillis(); running = true; @@ -111,6 +111,14 @@ public class Subconscious afterTime = System.currentTimeMillis(); sleepTime = RENDER_PERIOD_MS - (beforeTime - afterTime); + if (sleepTime < 0) { + skipFrame++; + continue; + } + + if (skipFrame > 0) + skipFrame--; + try { Thread.sleep(sleepTime); } catch (InterruptedException ex) {} @@ -138,13 +146,15 @@ public class Subconscious @Override public void mouseReleased(MouseEvent e) {} @Override public void mouseEntered(MouseEvent e) {} @Override public void mouseExited(MouseEvent e) {} - @Override public void mouseClicked(MouseEvent e) { + @Override + public void mouseClicked(MouseEvent e) { currentScene.mouseClicked(e.getX(), e.getY()); } @Override public void keyPressed(KeyEvent e) {} @Override public void keyTyped(KeyEvent e) {} - @Override public void keyReleased(KeyEvent e) { + @Override + public void keyReleased(KeyEvent e) { int keyCode = e.getKeyCode(); if (keyCode == KeyEvent.VK_Q && e.isControlDown()) { @@ -176,9 +186,10 @@ public class Subconscious } } - @Override public void mouseWheelMoved(MouseWheelEvent e) { + @Override + public void mouseWheelMoved(MouseWheelEvent e) { int value = e.getWheelRotation(); - currentScene.zoom(value*-3); + currentScene.zoom(value * -1); //rerender dbImage = null; } @@ -186,7 +197,8 @@ public class Subconscious @Override public void componentHidden(ComponentEvent e) {} @Override public void componentShown(ComponentEvent e) {} @Override public void componentMoved(ComponentEvent e) {} - @Override public void componentResized(ComponentEvent e) { + @Override + public void componentResized(ComponentEvent e) { dbImage = null; // create new image with the correct size } diff --git a/src/main/java/WorldScene.java b/src/main/java/WorldScene.java index 7888d6a..bde3a10 100644 --- a/src/main/java/WorldScene.java +++ b/src/main/java/WorldScene.java @@ -14,14 +14,12 @@ public class WorldScene extends Scene { private Map map; private int tileSize; - private int panX; - private int panY; + private int panX = 0; + private int panY = 0; public WorldScene(Dimension gridSize, int tileSize) { this.tileSize = tileSize; map = new Map(gridSize); - this.panX = 0; - this.panY = 0; // TODO remove hardcoded stuff @@ -50,8 +48,8 @@ public class WorldScene extends Scene { @Override public void pan(int x, int y) { - this.panX = this.panX + x; - this.panY = this.panY + y; + this.panX += x; + this.panY += y; } @Override @@ -107,12 +105,14 @@ public class WorldScene extends Scene { g2d.setColor(Palette.RED); break; } + int gap = this.tileSize / 10; g2d.fillRect( (this.tileSize * actor.getX()) + gap + this.panX, (this.tileSize * actor.getY()) + gap + this.panY, this.tileSize - gap * 2, this.tileSize - gap * 2 ); + if (!actor.isAlive()) { g2d.setColor(Palette.BLUE); g2d.fillRect( @@ -130,10 +130,12 @@ public class WorldScene extends Scene { this.updateTiles(); Tile tile = map.getTile( - (int) ((x+this.panX)/(double)this.tileSize) + this.sceneXOffset, - (int) ((y+this.panY)/(double)this.tileSize) + this.sceneYOffset + (int) ((x + this.panX)/(double)this.tileSize), + (int) ((y + this.panY)/(double)this.tileSize) ); + tile.selected = !tile.selected; + //TODO remove test stuff if (tile.getActor() != null) { Actor actor = tile.getActor(); @@ -142,10 +144,12 @@ public class WorldScene extends Scene { } } + // ugh, performance public void updateTiles() { for (Tile tile : this.map.grid) { tile.clearActor(); } + for (Actor actor : this.actors) { Tile tile = map.getTile(actor.getX(), actor.getY()); tile.setActor(actor); |