diff options
author | mafaldo <mafaldo@heavyhammer.home> | 2018-02-11 12:21:15 +0100 |
---|---|---|
committer | mafaldo <mafaldo@heavyhammer.home> | 2018-02-11 12:21:15 +0100 |
commit | a9a5bc0189d316de863bbdc8c3a425706b6ade84 (patch) | |
tree | e92db501cfa040cad315b535cb326abb3f086348 /src | |
parent | tiles bound to actors, remove Player and Enemy (diff) | |
download | Subconscious-old-a9a5bc0189d316de863bbdc8c3a425706b6ade84.tar.gz Subconscious-old-a9a5bc0189d316de863bbdc8c3a425706b6ade84.zip |
add pan, add zoom, add weapon
Diffstat (limited to 'src')
-rw-r--r-- | src/main/java/Actor.java | 16 | ||||
-rw-r--r-- | src/main/java/Scene.java | 2 | ||||
-rw-r--r-- | src/main/java/Subconscious.java | 37 | ||||
-rw-r--r-- | src/main/java/Weapon.java | 52 | ||||
-rw-r--r-- | src/main/java/WorldScene.java | 60 |
5 files changed, 150 insertions, 17 deletions
diff --git a/src/main/java/Actor.java b/src/main/java/Actor.java index 1e0ba80..55fcc3a 100644 --- a/src/main/java/Actor.java +++ b/src/main/java/Actor.java @@ -14,6 +14,7 @@ public class Actor { private int hp; private int x, y; private Dimension gridSize; + private Weapon weapon; public Actor(String name, int MAXHP, Type type, Dimension gridSize) { this.name = name; @@ -21,6 +22,7 @@ public class Actor { this.MAXHP = MAXHP; this.hp = this.MAXHP; this.alive = true; + this.gridSize = gridSize; } public void damage(int dmg) { @@ -62,4 +64,18 @@ public class Actor { public int getY() { return this.y; } + + public void equipWeapon(Weapon weapon) { + this.weapon = weapon; + } + + public Weapon dropWeapon() { + Weapon tmp = this.weapon; + this.weapon = null; + return tmp; + } + + public boolean shoot(Actor actor, double probability) { + return this.weapon.shoot(actor, probability); + } } diff --git a/src/main/java/Scene.java b/src/main/java/Scene.java index 0d8a908..139553c 100644 --- a/src/main/java/Scene.java +++ b/src/main/java/Scene.java @@ -8,4 +8,6 @@ public abstract class Scene { 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 d462385..cbb17dc 100644 --- a/src/main/java/Subconscious.java +++ b/src/main/java/Subconscious.java @@ -7,6 +7,8 @@ import java.awt.Dimension; import java.awt.event.MouseListener; import java.awt.event.MouseEvent; +import java.awt.event.MouseWheelListener; +import java.awt.event.MouseWheelEvent; import java.awt.event.KeyListener; import java.awt.event.KeyEvent; import java.awt.event.ComponentListener; @@ -20,7 +22,8 @@ import java.awt.Toolkit; public class Subconscious extends JPanel - implements Runnable, MouseListener, KeyListener, ComponentListener { + implements Runnable, MouseListener, KeyListener, + ComponentListener, MouseWheelListener { public static final String TITLE = "Subconscious"; public static final long RENDER_PERIOD_MS = 10; @@ -46,6 +49,7 @@ public class Subconscious addMouseListener(this); addKeyListener(this); addComponentListener(this); + addMouseWheelListener(this); setFocusable(true); requestFocus(); @@ -146,6 +150,37 @@ public class Subconscious if (keyCode == KeyEvent.VK_Q && e.isControlDown()) { running = false; } + + if (keyCode == KeyEvent.VK_DOWN) { + currentScene.pan(0, -10); + //rerender + dbImage = null; + } + + if (keyCode == KeyEvent.VK_UP) { + currentScene.pan(0, 10); + //rerender + dbImage = null; + } + + if (keyCode == KeyEvent.VK_LEFT) { + currentScene.pan(10, 0); + //rerender + dbImage = null; + } + + if (keyCode == KeyEvent.VK_RIGHT) { + currentScene.pan(-10, 0); + //rerender + dbImage = null; + } + } + + @Override public void mouseWheelMoved(MouseWheelEvent e) { + int value = e.getWheelRotation(); + currentScene.zoom(value*-3); + //rerender + dbImage = null; } @Override public void componentHidden(ComponentEvent e) {} diff --git a/src/main/java/Weapon.java b/src/main/java/Weapon.java new file mode 100644 index 0000000..824ad6f --- /dev/null +++ b/src/main/java/Weapon.java @@ -0,0 +1,52 @@ +public class Weapon { + private final int RANGE; + private final int DAMAGE; + private final int WARMUP; + private final int BURSTCOUNT; + + private int coolDown; + + public Weapon(int damage, int range, int burstcount, int warmup) { + this.coolDown = 0; + this.DAMAGE = damage; + this.RANGE = range; + this.BURSTCOUNT = burstcount; + this.WARMUP = warmup; + } + + public int getDamage() { + return this.DAMAGE; + } + + public int getBurst() { + return this.BURSTCOUNT; + } + + public int getRange() { + return this.RANGE; + } + + public boolean canShoot() { + if (coolDown <= 0) { + return true; + } else { + return false; + } + } + + public boolean shoot(Actor actor, double probability) { + if (coolDown <= 0) { + actor.damage((int)((this.DAMAGE*probability)*(double)this.BURSTCOUNT)); + this.coolDown = this.WARMUP; + return true; + } else { + return false; + } + } + + public void tick() { + if (coolDown > 0) { + coolDown--; + } + } +} diff --git a/src/main/java/WorldScene.java b/src/main/java/WorldScene.java index 7d08077..7888d6a 100644 --- a/src/main/java/WorldScene.java +++ b/src/main/java/WorldScene.java @@ -14,15 +14,24 @@ public class WorldScene extends Scene { private Map map; private int tileSize; + private int panX; + private int panY; public WorldScene(Dimension gridSize, int tileSize) { this.tileSize = tileSize; map = new Map(gridSize); + this.panX = 0; + this.panY = 0; // TODO remove hardcoded stuff Actor player = new Actor("pipo", 100, Actor.Type.PLAYER, gridSize); + player.equipWeapon(new Weapon(1000, 10, 1, 0)); + Actor enemy = new Actor("marco", 100, Actor.Type.ENEMY, gridSize); + enemy.move(5, 5); + this.actors.add(player); + this.actors.add(enemy); } private Composite makeAlpha(Graphics2D g2d, float alpha) { @@ -33,6 +42,19 @@ public class WorldScene extends Scene { } @Override + public void zoom(int value) { + if (this.tileSize > 5 || value > 0) { + this.tileSize = this.tileSize + value; + } + } + + @Override + public void pan(int x, int y) { + this.panX = this.panX + x; + this.panY = this.panY + y; + } + + @Override public void render(Graphics g) { Graphics2D g2d = (Graphics2D) g; @@ -48,8 +70,8 @@ public class WorldScene extends Scene { } g2d.fillRect( - this.tileSize * tile.x, - this.tileSize * tile.y, + this.tileSize * tile.x + this.panX, + this.tileSize * tile.y + this.panY, this.tileSize, this.tileSize ); @@ -58,8 +80,8 @@ public class WorldScene extends Scene { if (tile.selected) { g2d.setColor(Palette.RED); g2d.fillRect( - this.tileSize * tile.x, - this.tileSize * tile.y, + this.tileSize * tile.x + this.panX, + this.tileSize * tile.y + this.panY, this.tileSize, this.tileSize ); } @@ -67,8 +89,8 @@ public class WorldScene extends Scene { // draw grid (with composite) g2d.setPaint(Palette.BLACK); g2d.drawRect( - this.tileSize * tile.x, - this.tileSize * tile.y, + this.tileSize * tile.x + this.panX, + this.tileSize * tile.y + this.panY, this.tileSize, this.tileSize ); @@ -79,11 +101,7 @@ public class WorldScene extends Scene { for (Actor actor : this.actors) { switch (actor.type) { case PLAYER: - if (actor.isAlive()) { - g2d.setColor(Palette.ORANGE); - } else { - g2d.setColor(Palette.BLUE); - } + g2d.setColor(Palette.ORANGE); break; case ENEMY: g2d.setColor(Palette.RED); @@ -91,26 +109,36 @@ public class WorldScene extends Scene { } int gap = this.tileSize / 10; g2d.fillRect( - (this.tileSize * actor.getX()) + gap, - (this.tileSize * actor.getY()) + gap, + (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( + (this.tileSize * actor.getX()) + gap + this.panX, + (this.tileSize * actor.getY()) + this.tileSize/4 + this.panY, + this.tileSize - gap * 2, this.tileSize/4 + ); + } } } @Override public void mouseClicked(int x, int y) { + //TODO fix mouse tracking with pan this.updateTiles(); Tile tile = map.getTile( - (int) (x/(double)this.tileSize) + sceneXOffset, - (int) (y/(double)this.tileSize) + sceneYOffset + (int) ((x+this.panX)/(double)this.tileSize) + this.sceneXOffset, + (int) ((y+this.panY)/(double)this.tileSize) + this.sceneYOffset ); //TODO remove test stuff if (tile.getActor() != null) { Actor actor = tile.getActor(); - actor.damage(1000); + Actor player = this.actors.get(0); + player.shoot(actor, 1); } } |