From ffd2ec134c38ec782d7d29c9658ca35cad2b91d9 Mon Sep 17 00:00:00 2001 From: mafaldo Date: Sat, 10 Feb 2018 11:14:23 +0100 Subject: First commit --- Makefile | 18 ++++ README.md | 0 src/EntityType.java | 3 + src/MainPanel.java | 265 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/Subconscious.java | 24 +++++ src/Tile.java | 22 +++++ src/TileType.java | 3 + 7 files changed, 335 insertions(+) create mode 100644 Makefile create mode 100644 README.md create mode 100644 src/EntityType.java create mode 100644 src/MainPanel.java create mode 100644 src/Subconscious.java create mode 100644 src/Tile.java create mode 100644 src/TileType.java diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..13da9d5 --- /dev/null +++ b/Makefile @@ -0,0 +1,18 @@ +MAINCLASS := Subconscious + +run: build + cd bin && java $(MAINCLASS) + +pack: build + echo "Main-Class: $(MAINCLASS)" > jar/manifest.txt + jar cvfm jar/$(MAINCLASS).jar jar/manifest.txt -C bin/ . + +build: dirs + javac -d bin/ src/*.java + +dirs: + mkdir -p src bin jar + +clean: + rm jar/* + rm bin/* diff --git a/README.md b/README.md new file mode 100644 index 0000000..e69de29 diff --git a/src/EntityType.java b/src/EntityType.java new file mode 100644 index 0000000..a4ccf30 --- /dev/null +++ b/src/EntityType.java @@ -0,0 +1,3 @@ +public enum EntityType { + NULL, PLAYER, ENEMY, TREE +} diff --git a/src/MainPanel.java b/src/MainPanel.java new file mode 100644 index 0000000..8af12ee --- /dev/null +++ b/src/MainPanel.java @@ -0,0 +1,265 @@ +import javax.swing.*; +import java.awt.*; +import java.awt.event.*; +import java.util.Random; + +public class MainPanel extends JPanel implements MouseListener, MouseMotionListener { + + public int width = 20; + public Tile[][] tileArray = new Tile[width][width]; + public int size; + public boolean selectionMode = false; + public boolean attackMode = false; + public boolean action = false; + public boolean miss = false; + public int[] selection = {0, 0}; + public int[] attaction = {0, 0}; + public int[] location = {0, 0}; + private int len; + private Color RED = new Color(165, 66, 66); + private Color YELLOW = new Color(250, 198, 116); + private Color GREEN = new Color(181, 189, 104); + private Color BLACK = new Color(29, 31, 33); + private Color WHITE = new Color(197, 200, 198); + private Color BLUE = new Color(95, 129, 157); + private Color ORANGE = new Color(222, 147, 95); + private Color DARKGREEN = new Color(140, 148, 64); + private Color BROWN = new Color(51, 41, 33); + private Random rand = new Random(); + + public MainPanel () { + for (int row = 0; row < width; row++) { + for (int col = 0; col < width; col++) { + tileArray[row][col] = new Tile(row, col, this.size, this.width); + } + } + + tileArray[0][0].entityType = EntityType.PLAYER; + for (int i = 0; i < 35; i++) { + int row = rand.nextInt(width); + int col = rand.nextInt(width); + int type = rand.nextInt(4); + if (tileArray[row][col].entityType == EntityType.NULL) { + if (type == 0) { + tileArray[row][col].entityType = EntityType.ENEMY; + } else { + tileArray[row][col].entityType = EntityType.TREE; + } + } + } + + this.addMouseListener(this); + this.addMouseMotionListener(this); + } + + public void paintComponent(Graphics g) { + if (this.getWidth() > this.getHeight()) { + this.size = this.getHeight(); + } else { + this.size = this.getWidth(); + } + + super.paintComponent(g); + + Graphics2D g2d = (Graphics2D) g.create(); + g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, + RenderingHints.VALUE_ANTIALIAS_ON); + + int bar = this.size/20; + + g2d.setColor(WHITE); + g2d.fillRect(0, 0, this.getWidth(), this.getHeight()); + g2d.setColor(BLACK); + g2d.fillRect(0, this.size-bar, this.size-bar, bar); + g2d.setColor(WHITE); + g2d.setFont(new Font("Arial", Font.BOLD, bar-bar/10)); + if (this.miss) { + g2d.drawString("MISS", this.size-bar-(bar-bar/10)*3, this.size-bar/10); + } + + if (action) { + g2d.drawString("PAS", 0, this.size-bar/10); + g2d.drawString("ATK", (bar-bar/10)*3, this.size-bar/10); + } + + for (int row = 0; row < width; row++) { + for (int col = 0; col < width; col++) { + this.tileArray[row][col].calc(row, col, this.size-bar); + int x = this.tileArray[row][col].x; + int y = this.tileArray[row][col].y; + this.len = this.tileArray[row][col].len; + + int border = len/100; + + if (this.tileArray[row][col].tileType == TileType.GRASS) { + g2d.setColor(GREEN); + g2d.fillRect(x, y, len, len); + g2d.setColor(BLACK); + g2d.setStroke(new BasicStroke(len/50)); + g2d.drawRect(x, y, len, len); + } else if ( + this.tileArray[row][col].tileType == TileType.WATER) { + g2d.setColor(Color.BLUE); + g2d.fillRect(x, y, len, len); + g2d.setColor(BLACK); + g2d.setStroke(new BasicStroke(len/50)); + g2d.drawRect(x, y, len, len); + } + + int spacing = len/12; + + + if (this.tileArray[row][col].entityType == EntityType.PLAYER) { + g2d.setColor(BLUE); + g2d.fillRect( + x+spacing, y+spacing, len-spacing*2, len-spacing*2); + this.location[0] = x; + this.location[1] = y; + this.selection[0] = row; + this.selection[1] = col; + } + + if (this.tileArray[row][col].entityType == EntityType.ENEMY) { + g2d.setColor(RED); + g2d.fillRect( + x+spacing, y+spacing, len-spacing*2, len-spacing*2); + } + + if (this.tileArray[row][col].entityType == EntityType.TREE) { + g2d.setColor(DARKGREEN); + g2d.fillRect( + x+spacing, y+spacing, len-spacing*2, len-spacing*2); + g2d.setColor(BROWN); + g2d.fillRect(x+len/3, y+len/3, len/3, len/3); + } + + if (this.tileArray[row][col].selected) { + if (this.tileArray[row][col].entityType == EntityType.NULL) { + g2d.setColor(YELLOW); + g2d.fillRect(x+len/3, y+len/3, len/3, len/3); + } + } + + if (this.tileArray[row][col].attack) { + g2d.setColor(ORANGE); + g2d.setStroke(new BasicStroke(this.len/6)); + g2d.drawRect(x+len/4, y+len/4, len*2/4, len*2/4); + } + } + } + + } + + public void mousePressed(MouseEvent e) {} + public void mouseReleased(MouseEvent e) {} + public void mouseEntered(MouseEvent e) {} + public void mouseMoved(MouseEvent e) { + int x = e.getX(); + int y = e.getY(); + + if (attackMode) { + for (int row = 0; row < width; row++) { + for (int col = 0; col < width; col++) { + this.tileArray[row][col].attack = false; + if ( + x > this.tileArray[row][col].x + && x < this.tileArray[row][col].x + + this.tileArray[row][col].len + && y > this.tileArray[row][col].y + && y < this.tileArray[row][col].y + + this.tileArray[row][col].len + ) { + this.tileArray[row][col].attack = true; + } + } + } + } + this.repaint(); + } + public void mouseExited(MouseEvent e) {} + public void mouseDragged(MouseEvent e) {} + + public void mouseClicked(MouseEvent e) { + int x = e.getX(); + int y = e.getY(); + int bar = this.size/20; + boolean stop = false; + this.miss = false; + + if ( + x > 0 + && x < (bar-bar/10)*3 + && y > this.size-bar + && y < this.size + ) { + this.action = false; + } else if ( + x > (bar-bar/10)*3 + && x < (bar-bar/10)*6 + && y > this.size-bar + && y < this.size + ) { + this.attackMode = true; + this.action = false; + } else { + for (int row = 0; row < width; row++) { + for (int col = 0; col < width; col++) { + if ( + x > this.tileArray[row][col].x + && x < this.tileArray[row][col].x + + this.tileArray[row][col].len + && y > this.tileArray[row][col].y + && y < this.tileArray[row][col].y + + this.tileArray[row][col].len + ) { + if (this.selectionMode) { + if (this.tileArray[row][col].selected) { + this.tileArray[this.selection[0]][this.selection[1]].entityType = EntityType.NULL; + this.tileArray[row][col].entityType = EntityType.PLAYER; + this.selectionMode = false; + this.action = true; + } + } else if (this.attackMode) { + int dist = (int) Math.sqrt(Math.pow(row - this.selection[0], 2) + + Math.pow(col - this.selection[1], 2)); + if (rand.nextInt(13) > dist) { + if (this.tileArray[row][col].entityType == EntityType.ENEMY) { + this.tileArray[row][col].entityType = EntityType.NULL; + } else if (this.tileArray[row][col].entityType == EntityType.TREE) { + this.tileArray[row][col].entityType = EntityType.NULL; + } + } else { + this.miss = true; + } + this.tileArray[row][col].attack = false; + this.attackMode = false; + } else if (this.tileArray[row][col].entityType == EntityType.PLAYER) { + this.selectionMode = true; + this.selection[0] = row; + this.selection[1] = col; + for (int srow = -4; srow <= 4; srow++) { + for (int scol = -4; scol <= 4; scol++) { + if (Math.abs(srow) + Math.abs(scol) <= 4) { + try { + if (this.tileArray[row+srow][col+scol].entityType == EntityType.NULL) { + this.tileArray[row+srow][col+scol].selected = true; + } + } catch (ArrayIndexOutOfBoundsException ex) {} + } + } + } + } + } + } + } + if (!selectionMode) { + for (int row = 0; row < width; row++) { + for (int col = 0; col < width; col++) { + this.tileArray[row][col].selected = false; + } + } + } + } + this.repaint(); + } +} diff --git a/src/Subconscious.java b/src/Subconscious.java new file mode 100644 index 0000000..dc15964 --- /dev/null +++ b/src/Subconscious.java @@ -0,0 +1,24 @@ +import javax.swing.*; +import java.awt.*; + +public class Subconscious { + + public static final Dimension WINDOW_SIZE = new Dimension(400, 400); + + public Subconscious() { + JFrame frame = new JFrame("Subconscious"); + + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + frame.setSize(WINDOW_SIZE); + frame.setLocationRelativeTo(null); + + MainPanel mainPanel= new MainPanel(); + frame.add(mainPanel); + + frame.setVisible(true); + } + + public static void main(String[] args) { + Subconscious subconscious = new Subconscious(); + } +} diff --git a/src/Tile.java b/src/Tile.java new file mode 100644 index 0000000..8624f08 --- /dev/null +++ b/src/Tile.java @@ -0,0 +1,22 @@ +public class Tile { + + public int x; + public int y; + public int len; + public TileType tileType = TileType.GRASS; + public EntityType entityType = EntityType.NULL; + public boolean selected = false; + public boolean attack = false; + public int width; + + public Tile (int row, int col, int window, int width) { + this.width = width; + this.calc(row, col, window); + } + + public void calc (int row, int col, int window) { + this.len = (int) (window/(double)this.width); + this.x = col*len; + this.y = row*len; + } +} diff --git a/src/TileType.java b/src/TileType.java new file mode 100644 index 0000000..82a5b5b --- /dev/null +++ b/src/TileType.java @@ -0,0 +1,3 @@ +public enum TileType { + GRASS, ROAD, HOUSE, WATER +} -- cgit v1.2.1