aboutsummaryrefslogtreecommitdiffstats
path: root/src/MainPanel.java
blob: 8af12ee1f59c75db023d63a1be4c6822620ac474 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
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();
	}
}