diff options
author | mafaldo <mafaldo@heavyhammer.home> | 2018-02-10 15:12:05 +0100 |
---|---|---|
committer | mafaldo <mafaldo@heavyhammer.home> | 2018-02-10 15:12:05 +0100 |
commit | 0e8ebd8803718b37e9141649c6d8eb7036ec85ed (patch) | |
tree | 5b952c80b9d0616cf0bc7ba4243561352482e957 /src | |
parent | Switch to gradle, update gitignore (diff) | |
download | Subconscious-old-0e8ebd8803718b37e9141649c6d8eb7036ec85ed.tar.gz Subconscious-old-0e8ebd8803718b37e9141649c6d8eb7036ec85ed.zip |
Implement actor, create player
Diffstat (limited to 'src')
-rw-r--r-- | src/main/java/Actor.java | 58 | ||||
-rw-r--r-- | src/main/java/Enemy.java | 0 | ||||
-rw-r--r-- | src/main/java/Map.java | 2 | ||||
-rw-r--r-- | src/main/java/Palette.java | 13 | ||||
-rw-r--r-- | src/main/java/Player.java | 8 | ||||
-rw-r--r-- | src/main/java/Subconscious.java | 16 | ||||
-rw-r--r-- | src/main/java/Tile.java | 2 | ||||
-rw-r--r-- | src/main/java/WorldScene.java | 43 |
8 files changed, 132 insertions, 10 deletions
diff --git a/src/main/java/Actor.java b/src/main/java/Actor.java index 207d6e6..3d81d7c 100644 --- a/src/main/java/Actor.java +++ b/src/main/java/Actor.java @@ -1,3 +1,61 @@ +import java.awt.Dimension; +import java.awt.Point; + public class Actor { + public enum Type { + PLAYER, ENEMY + } + + + public final String name; + public final Type type; + protected final int MAXHP; + protected int hp; + protected int x, y; + protected Dimension gridSize; + + public Actor(String name, int MAXHP, Type type) { + this.name = name; + this.type = type; + this.MAXHP = MAXHP; + this.hp = this.MAXHP; + } + + public void damage(int dmg) { + this.hp = this.hp - dmg; + if (this.hp < 0) { + this.hp = 0; + } + } + + public void heal(int life) { + this.hp = this.hp + life; + if (this.hp > this.MAXHP) { + this.hp = this.MAXHP; + } + } + + public boolean move(int x, int y) { + if (x < this.gridSize.width && y < this.gridSize.height && x > 0 && y > 0) { + this.x = x; + this.y = y; + return true; + } else { + return false; + } + } + + public int getHp() { + return this.hp; + } + + public int getX() { + return this.x; + } + + public int getY() { + return this.y; + } + } diff --git a/src/main/java/Enemy.java b/src/main/java/Enemy.java new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/src/main/java/Enemy.java diff --git a/src/main/java/Map.java b/src/main/java/Map.java index 031e922..db686f6 100644 --- a/src/main/java/Map.java +++ b/src/main/java/Map.java @@ -23,6 +23,6 @@ public class Map { /* accessors */ public Tile getTile(int x, int y) { - return this.grid[y * this.size.width + y]; + return this.grid[y * this.size.width + x]; } } diff --git a/src/main/java/Palette.java b/src/main/java/Palette.java new file mode 100644 index 0000000..26838d8 --- /dev/null +++ b/src/main/java/Palette.java @@ -0,0 +1,13 @@ +import java.awt.Color; + +public class Palette { + public final static Color BLACK = new Color(29, 31, 33); + public final static Color WHITE = new Color(197, 200, 198); + public final static Color RED = new Color(165, 66, 66); + public final static Color YELLOW = new Color(250, 198, 116); + public final static Color GREEN = new Color(181, 189, 104); + public final static Color DARKGREEN = new Color(140, 148, 64); + public final static Color BLUE = new Color(95, 129, 157); + public final static Color ORANGE = new Color(222, 147, 95); + public final static Color BROWN = new Color(51, 41, 33); +} diff --git a/src/main/java/Player.java b/src/main/java/Player.java new file mode 100644 index 0000000..1e9d0b7 --- /dev/null +++ b/src/main/java/Player.java @@ -0,0 +1,8 @@ +import java.awt.Dimension; + +public class Player extends Actor { + + public Player(String name, Dimension gridSize) { + super(name, 100, Actor.Type.PLAYER); + } +} diff --git a/src/main/java/Subconscious.java b/src/main/java/Subconscious.java index b63eb23..8ce6b15 100644 --- a/src/main/java/Subconscious.java +++ b/src/main/java/Subconscious.java @@ -5,8 +5,11 @@ import java.awt.Graphics; import java.awt.Image; import java.awt.Dimension; +import java.awt.event.MouseListener; +import java.awt.event.MouseEvent; -public class Subconscious extends JPanel { + +public class Subconscious extends JPanel implements MouseListener { public static final String TITLE = "Subconscious"; @@ -26,6 +29,7 @@ public class Subconscious extends JPanel { // TODO remove demo currentScene = new WorldScene(new Dimension(200, 200), 50); + addMouseListener(this); } public void createWindow() { @@ -71,6 +75,16 @@ public class Subconscious extends JPanel { currentScene.render(g); } + /* listener */ + public void mouseClicked(MouseEvent e) { + int x = e.getX(); + int y = e.getY(); + } + public void mousePressed(MouseEvent e) {} + public void mouseReleased(MouseEvent e) {} + public void mouseEntered(MouseEvent e) {} + public void mouseExited(MouseEvent e) {} + /* main */ public static void main(String args[]) { Subconscious game = new Subconscious(); diff --git a/src/main/java/Tile.java b/src/main/java/Tile.java index cbdeda4..1a95820 100644 --- a/src/main/java/Tile.java +++ b/src/main/java/Tile.java @@ -1,7 +1,7 @@ public class Tile { public enum Type { - GRASS, + GRASS, WATER }; public final int x, y; diff --git a/src/main/java/WorldScene.java b/src/main/java/WorldScene.java index 6dff04a..2c11cba 100644 --- a/src/main/java/WorldScene.java +++ b/src/main/java/WorldScene.java @@ -3,10 +3,12 @@ import java.awt.Dimension; import java.awt.Graphics; import java.awt.Graphics2D; +import java.util.ArrayList; + public class WorldScene extends Scene { - private Actor actors[]; + private ArrayList<Actor> actors = new ArrayList<Actor>(); private Map map; private int tileSize; @@ -14,6 +16,9 @@ public class WorldScene extends Scene { public WorldScene(Dimension gridSize, int tileSize) { this.tileSize = tileSize; map = new Map(gridSize); + + Player player = new Player("pipo", gridSize); + this.actors.add(player); } @Override @@ -21,12 +26,36 @@ public class WorldScene extends Scene { Graphics2D g2d = (Graphics2D) g; for (Tile tile : this.map.grid) { - g2d.setColor(Color.GREEN); - g2d.fillRect( - this.tileSize * tile.x, - this.tileSize * tile.y, - this.tileSize, this.tileSize - ); + switch (tile.type) { + case GRASS: + g2d.setColor(Palette.GREEN); + break; + case WATER: + g2d.setColor(Palette.BLUE); + break; + } + g2d.fillRect( + this.tileSize * tile.x, + this.tileSize * tile.y, + this.tileSize, this.tileSize + ); } + + for (Actor actor : this.actors) { + switch (actor.type) { + case PLAYER: + g2d.setColor(Palette.ORANGE); + break; + case ENEMY: + g2d.setColor(Palette.RED); + break; + } + int gap = this.tileSize/10; + g2d.fillRect( + (this.tileSize * actor.x) + gap, + (this.tileSize * actor.y) + gap, + this.tileSize-gap, this.tileSize-gap + ); + } } } |