-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBombermanComponent.java
191 lines (167 loc) · 9.77 KB
/
BombermanComponent.java
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
package bomberman;
import javax.swing.*;
import java.awt.*;
import java.util.AbstractMap;
import java.util.EnumMap;
/**
* Based on the representations and logic in other classes this class paint the components of the game.
* There are also a few constants that are defined here, which are necessary for adjusting the
* visible components optimally on the frame, pixel-wise. It is also in this class the methods for drawing
* the simple graphics of the game, such as eyes and shapes of the different objects.
*/
public class BombermanComponent extends JComponent implements FloorListener
{
// Constants are static by definition.
private final static int SQUARE_SIZE = 40;
private final static int CHARACTER_ADJUSTMENT_FOR_PAINT = 15;
private final static int SQUARE_MIDDLE = SQUARE_SIZE/2;
private final static int BOMB_ADJUSTMENT_1 =5;
private final static int BOMB_ADJUSTMENT_2 =10;
// Defining painting parameters
private final static int PAINT_PARAMETER_13 = 13;
private final static int PAINT_PARAMETER_15 = 15;
private final static int PAINT_PARAMETER_17 = 17;
private final static int PAINT_PARAMETER_18 = 18;
private final static int PAINT_PARAMETER_19 = 19;
private final static int PAINT_PARAMETER_20 = 20;
private final static int PAINT_PARAMETER_24 = 24;
private final Floor floor;
private final AbstractMap<FloorTile, Color> colorMap;
public BombermanComponent(Floor floor) {
this.floor = floor;
colorMap = new EnumMap<>(FloorTile.class);
colorMap.put(FloorTile.FLOOR, Color.GREEN);
colorMap.put(FloorTile.UNBREAKABLEBLOCK, Color.BLACK);
colorMap.put(FloorTile.BREAKABLEBLOCK, Color.RED);
}
// This method is static since each square has the same size.
public static int getSquareSize() {
return SQUARE_SIZE;
}
// This method is static since each square has the same size.
public static int getSquareMiddle() {
return SQUARE_MIDDLE;
}
public Dimension getPreferredSize() {
super.getPreferredSize();
return new Dimension(this.floor.getWidth() * SQUARE_SIZE, this.floor.getHeight() * SQUARE_SIZE);
}
public void floorChanged() {
repaint();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
final Graphics2D g2d = (Graphics2D) g;
for (int rowIndex = 0; rowIndex < floor.getHeight(); rowIndex++) {
for (int colIndex = 0; colIndex < floor.getWidth(); colIndex++) {
g2d.setColor(colorMap.get(this.floor.getFloorTile(rowIndex, colIndex)));
if(floor.getFloorTile(rowIndex, colIndex)==FloorTile.BREAKABLEBLOCK){
paintBreakableBlock(rowIndex, colIndex, g2d);
}
else if(floor.getFloorTile(rowIndex, colIndex)==FloorTile.UNBREAKABLEBLOCK){
paintUnbreakableBlock(rowIndex, colIndex, g2d);
}
else{
paintFloor(rowIndex, colIndex, g2d);
}
}
}
// Paint player:
paintPlayer(floor.getPlayer(), g2d);
//Paint enemies
for (Enemy e: floor.getEnemyList()) {
paintEnemy(e, g2d);
}
//Paint powerups
for (AbstractPowerup p: floor.getPowerupList()) {
if (p.getName().equals("BombCounter")) {
g2d.setColor(Color.BLACK);
} else if (p.getName().equals("BombRadius")) {
g2d.setColor(Color.RED);
}
g2d.fillOval(p.getX()-CHARACTER_ADJUSTMENT_FOR_PAINT, p.getY()-CHARACTER_ADJUSTMENT_FOR_PAINT, p.getPowerupSize(), p.getPowerupSize());
}
//Paint bombs
for (Bomb b: floor.getBombList()) {
g2d.setColor(Color.BLACK);
int bombX = floor.squareToPixel(b.getColIndex());
int bombY = floor.squareToPixel(b.getRowIndex());
g2d.fillOval(bombX + BOMB_ADJUSTMENT_1, bombY + BOMB_ADJUSTMENT_1, Bomb.getBOMBSIZE(), Bomb.getBOMBSIZE());
g2d.setColor(Color.ORANGE);
g2d.fillOval(bombX + BOMB_ADJUSTMENT_2, bombY + BOMB_ADJUSTMENT_1, BOMB_ADJUSTMENT_1, BOMB_ADJUSTMENT_2);
}
//Paint explosions
g2d.setColor(Color.ORANGE);
for (Explosion tup: floor.getExplosionCoords()) {
g2d.fillOval(floor.squareToPixel(tup.getColIndex()) + BOMB_ADJUSTMENT_1, floor.squareToPixel(tup.getRowIndex()) +
BOMB_ADJUSTMENT_1, Bomb.getBOMBSIZE(), Bomb.getBOMBSIZE());
}
}
private void paintBreakableBlock(int rowIndex, int colIndex, Graphics g2d){
g2d.setColor(Color.GRAY);
g2d.fillRect(colIndex * SQUARE_SIZE, rowIndex * SQUARE_SIZE, SQUARE_SIZE, SQUARE_SIZE);
g2d.setColor(Color.BLACK);
g2d.drawLine(colIndex* SQUARE_SIZE+1, rowIndex*SQUARE_SIZE+10, colIndex*SQUARE_SIZE+SQUARE_SIZE, rowIndex*SQUARE_SIZE+10);
g2d.drawLine(colIndex* SQUARE_SIZE+1, rowIndex*SQUARE_SIZE+SQUARE_MIDDLE, colIndex*SQUARE_SIZE+SQUARE_SIZE, rowIndex*SQUARE_SIZE+SQUARE_MIDDLE);
g2d.drawLine(colIndex* SQUARE_SIZE+1, rowIndex*SQUARE_SIZE+SQUARE_MIDDLE+10, colIndex*SQUARE_SIZE+SQUARE_SIZE, rowIndex*SQUARE_SIZE+SQUARE_MIDDLE+10);
g2d.drawLine(colIndex* SQUARE_SIZE+1, rowIndex*SQUARE_SIZE+SQUARE_SIZE, colIndex*SQUARE_SIZE+SQUARE_SIZE, rowIndex*SQUARE_SIZE+SQUARE_SIZE);
g2d.drawLine(colIndex* SQUARE_SIZE+10, rowIndex*SQUARE_SIZE+1, colIndex*SQUARE_SIZE+10, rowIndex*SQUARE_SIZE+10);
g2d.drawLine(colIndex* SQUARE_SIZE+SQUARE_MIDDLE+10, rowIndex*SQUARE_SIZE+1, colIndex*SQUARE_SIZE+SQUARE_MIDDLE+10, rowIndex*SQUARE_SIZE+10);
g2d.drawLine(colIndex* SQUARE_SIZE+1, rowIndex*SQUARE_SIZE+10, colIndex*SQUARE_SIZE+1, rowIndex*SQUARE_SIZE+SQUARE_MIDDLE);
g2d.drawLine(colIndex* SQUARE_SIZE+SQUARE_MIDDLE+1, rowIndex*SQUARE_SIZE+10, colIndex*SQUARE_SIZE+SQUARE_MIDDLE+1, rowIndex*SQUARE_SIZE+SQUARE_MIDDLE);
g2d.drawLine(colIndex* SQUARE_SIZE+10, rowIndex*SQUARE_SIZE+1+SQUARE_MIDDLE, colIndex*SQUARE_SIZE+10, rowIndex*SQUARE_SIZE+SQUARE_MIDDLE+10);
g2d.drawLine(colIndex* SQUARE_SIZE+SQUARE_MIDDLE+10, rowIndex*SQUARE_SIZE+1+SQUARE_MIDDLE, colIndex*SQUARE_SIZE+SQUARE_MIDDLE+10, rowIndex*SQUARE_SIZE+SQUARE_MIDDLE+10);
g2d.drawLine(colIndex* SQUARE_SIZE+1, rowIndex*SQUARE_SIZE+SQUARE_MIDDLE+10, colIndex*SQUARE_SIZE+1, rowIndex*SQUARE_SIZE+SQUARE_SIZE);
g2d.drawLine(colIndex* SQUARE_SIZE+SQUARE_MIDDLE+1, rowIndex*SQUARE_SIZE+SQUARE_MIDDLE+10, colIndex*SQUARE_SIZE+SQUARE_MIDDLE+1, rowIndex*SQUARE_SIZE+SQUARE_SIZE);
}
private void paintUnbreakableBlock(int rowIndex, int colIndex, Graphics g2d){
g2d.fillRect(colIndex * SQUARE_SIZE, rowIndex * SQUARE_SIZE, SQUARE_SIZE, SQUARE_SIZE);
g2d.setColor(Color.WHITE);
g2d.drawLine(colIndex* SQUARE_SIZE, rowIndex*SQUARE_SIZE, colIndex*SQUARE_SIZE+SQUARE_SIZE, rowIndex*SQUARE_SIZE);
g2d.drawLine(colIndex* SQUARE_SIZE, rowIndex*SQUARE_SIZE+SQUARE_SIZE, colIndex*SQUARE_SIZE+SQUARE_SIZE, rowIndex*SQUARE_SIZE+SQUARE_SIZE);
g2d.drawLine(colIndex* SQUARE_SIZE, rowIndex*SQUARE_SIZE, colIndex*SQUARE_SIZE, rowIndex*SQUARE_SIZE+SQUARE_SIZE);
g2d.drawLine(colIndex* SQUARE_SIZE+SQUARE_SIZE, rowIndex*SQUARE_SIZE, colIndex*SQUARE_SIZE+SQUARE_SIZE, rowIndex*SQUARE_SIZE+SQUARE_SIZE);
}
private void paintFloor(int rowIndex, int colIndex, Graphics g2d){
g2d.setColor(Color.CYAN);
g2d.fillRect(colIndex * SQUARE_SIZE, rowIndex * SQUARE_SIZE, SQUARE_SIZE, SQUARE_SIZE);
g2d.setColor(Color.WHITE);
g2d.drawLine(colIndex* SQUARE_SIZE+5, rowIndex*SQUARE_SIZE+10, colIndex * SQUARE_SIZE + 10, rowIndex * SQUARE_SIZE + 5);
g2d.drawLine(colIndex* SQUARE_SIZE+5, rowIndex*SQUARE_SIZE+SQUARE_MIDDLE, colIndex * SQUARE_SIZE + SQUARE_MIDDLE, rowIndex * SQUARE_SIZE + 5);
g2d.drawLine(colIndex* SQUARE_SIZE+5, rowIndex*SQUARE_SIZE+SQUARE_MIDDLE+10, colIndex * SQUARE_SIZE + SQUARE_MIDDLE + 10, rowIndex * SQUARE_SIZE + 5);
}
private void paintEnemy(Enemy e, Graphics g2d){
// Paint body
g2d.setColor(Color.YELLOW);
g2d.fillOval(e.getX()-CHARACTER_ADJUSTMENT_FOR_PAINT, e.getY()-CHARACTER_ADJUSTMENT_FOR_PAINT, e.getSize(), e.getSize());
// Paint brows
g2d.setColor(Color.BLACK);
g2d.drawLine(e.getX()-CHARACTER_ADJUSTMENT_FOR_PAINT+6, e.getY()-CHARACTER_ADJUSTMENT_FOR_PAINT+6, e.getX()-CHARACTER_ADJUSTMENT_FOR_PAINT+PAINT_PARAMETER_13, e.getY()-CHARACTER_ADJUSTMENT_FOR_PAINT+10);
g2d.drawLine(e.getX()-CHARACTER_ADJUSTMENT_FOR_PAINT+PAINT_PARAMETER_24, e.getY()-CHARACTER_ADJUSTMENT_FOR_PAINT+6, e.getX()-CHARACTER_ADJUSTMENT_FOR_PAINT+PAINT_PARAMETER_17, e.getY()-CHARACTER_ADJUSTMENT_FOR_PAINT+10);
// Paint eyes
g2d.fillOval(e.getX()-CHARACTER_ADJUSTMENT_FOR_PAINT+4, e.getY()-CHARACTER_ADJUSTMENT_FOR_PAINT+9, 7, 7);
g2d.fillOval(e.getX()-CHARACTER_ADJUSTMENT_FOR_PAINT+PAINT_PARAMETER_19, e.getY()-CHARACTER_ADJUSTMENT_FOR_PAINT+9, 7, 7);
// Paint mouth
g2d.fillOval(e.getX()-CHARACTER_ADJUSTMENT_FOR_PAINT+5, e.getY()-CHARACTER_ADJUSTMENT_FOR_PAINT+PAINT_PARAMETER_20, PAINT_PARAMETER_20, 2);
// Fill eyes
g2d.setColor(Color.RED);
g2d.fillOval(e.getX()-CHARACTER_ADJUSTMENT_FOR_PAINT+5, e.getY()-CHARACTER_ADJUSTMENT_FOR_PAINT+10, 5, 5);
g2d.fillOval(e.getX()-CHARACTER_ADJUSTMENT_FOR_PAINT+PAINT_PARAMETER_20, e.getY()-CHARACTER_ADJUSTMENT_FOR_PAINT+10, 5, 5);
}
private void paintPlayer(Player player, Graphics g2d){
// Paint hat
g2d.setColor(Color.RED);
g2d.fillOval(player.getX()-CHARACTER_ADJUSTMENT_FOR_PAINT+PAINT_PARAMETER_15, player.getY()-CHARACTER_ADJUSTMENT_FOR_PAINT-2, PAINT_PARAMETER_15, PAINT_PARAMETER_15);
// Paint body
g2d.setColor(Color.WHITE);
g2d.fillOval(player.getX()-CHARACTER_ADJUSTMENT_FOR_PAINT, player.getY()-CHARACTER_ADJUSTMENT_FOR_PAINT, player.getSize(), player.getSize());
// Paint face
g2d.setColor(Color.PINK);
g2d.fillOval(player.getX()-CHARACTER_ADJUSTMENT_FOR_PAINT+3, player.getY()-CHARACTER_ADJUSTMENT_FOR_PAINT+3, player.getSize()-6, player.getSize()-6);
// Paint eyes
g2d.setColor(Color.BLACK);
g2d.drawLine(player.getX()-CHARACTER_ADJUSTMENT_FOR_PAINT+10, player.getY()-CHARACTER_ADJUSTMENT_FOR_PAINT+10, player.getX()-CHARACTER_ADJUSTMENT_FOR_PAINT+10, player.getY()-CHARACTER_ADJUSTMENT_FOR_PAINT+PAINT_PARAMETER_18);
g2d.drawLine(player.getX()-CHARACTER_ADJUSTMENT_FOR_PAINT+PAINT_PARAMETER_20, player.getY()-CHARACTER_ADJUSTMENT_FOR_PAINT+10, player.getX()-CHARACTER_ADJUSTMENT_FOR_PAINT+PAINT_PARAMETER_20, player.getY()-CHARACTER_ADJUSTMENT_FOR_PAINT+PAINT_PARAMETER_18);
}
}