-
Notifications
You must be signed in to change notification settings - Fork 28
/
GameView.java
182 lines (146 loc) · 5.5 KB
/
GameView.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
package utils;
import javax.swing.*;
import java.awt.*;
import static utils.Types.*;
public class GameView extends JComponent {
private int cellSize, gridSize;
private Types.TILETYPE[][] objs;
private int[][] bombLife;
private Image backgroundImg;
/**
* Dimensions of the window.
*/
private Dimension dimension;
GameView(Types.TILETYPE[][] objects, int cellSize)
{
this.cellSize = cellSize;
this.gridSize = objects.length;
this.dimension = new Dimension(gridSize * cellSize, gridSize * cellSize);
copyObjects(objects, new int[gridSize][gridSize]);
backgroundImg = Types.TILETYPE.PASSAGE.getImage();
}
public void paintComponent(Graphics gx)
{
Graphics2D g = (Graphics2D) gx;
paintWithGraphics(g);
}
private void paintWithGraphics(Graphics2D g)
{
//For a better graphics, enable this: (be aware this could bring performance issues depending on your HW & OS).
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g.setColor(Color.BLACK);
g.fillRect(0, dimension.height, dimension.width, dimension.height);
for(int i = 0; i < gridSize; ++i) {
for(int j = 0; j < gridSize; ++j) {
Types.TILETYPE gobj = objs[i][j];
if (gobj == null) {
if (VERBOSE) {
System.out.println("OBJECT is NULL");
}
} else {
Rectangle rect = new Rectangle(j*cellSize, i*cellSize, cellSize, cellSize);
if(gobj != Types.TILETYPE.PASSAGE) {
//Background:
drawImage(g, backgroundImg, rect);
}
// Actual image (admits transparencies).
Image objImage = gobj.getImage();
if (objImage != null) {
if (gobj == Types.TILETYPE.BOMB) {
drawBomb(g, objImage, rect, bombLife[i][j], cellSize);
} else {
drawImage(g, objImage, rect);
}
}
}
}
}
g.setColor(Color.BLACK);
//player.draw(g); //if we want to give control to the agent to paint something (for debug), start here.
}
static void drawImage(Graphics2D gphx, Image img, Rectangle r)
{
int w = img.getWidth(null);
int h = img.getHeight(null);
float scaleX = (float)r.width/w;
float scaleY = (float)r.height/h;
gphx.drawImage(img, r.x, r.y, (int) (w*scaleX), (int) (h*scaleY), null);
}
private static void drawBomb(Graphics2D gphx, Image img, Rectangle r, int bombLife, int cellSize) {
drawImage(gphx, img, r);
// Draw bomb life
int scale = cellSize/BOMB_LIFE;
int startX = r.x;
int width = scale * bombLife;
int height = cellSize/10;
int startY = r.y + r.height - height;
int darkLineY = r.y + r.height - 1;
gphx.setColor(Color.red);
gphx.fillRect(startX, startY, width, height);
gphx.setColor(Color.black);
gphx.drawLine(startX, darkLineY, startX + width, darkLineY);
gphx.setColor(new Color(255, 94, 84));
gphx.drawLine(startX, startY, startX + width, startY);
}
/**
* All the objects in a board.
* @param objects in the game
*/
void paint(Types.TILETYPE[][] objects, int[][] bombLife)
{
copyObjects(objects, bombLife);
this.repaint();
}
private void copyObjects(Types.TILETYPE[][] objects, int[][] bombs)
{
objs = new Types.TILETYPE[gridSize][gridSize];
bombLife = new int[gridSize][gridSize];
for (int i = 0; i < gridSize; ++i) {
System.arraycopy(objects[i], 0, objs[i], 0, gridSize);
System.arraycopy(bombs[i], 0, bombLife[i], 0, gridSize);
}
}
/**
* Gets the dimensions of the window.
* @return the dimensions of the window.
*/
public Dimension getPreferredSize() {
return dimension;
}
public static void main(String[] args)
{
Types.TILETYPE[][] gobj = new Types.TILETYPE[4][4];
//gobj[0][0] = new GameObject();
gobj[0][1] = Types.TILETYPE.PASSAGE;
gobj[0][2] = Types.TILETYPE.PASSAGE;
gobj[0][3] = Types.TILETYPE.PASSAGE;
gobj[1][0] = Types.TILETYPE.BOMB;
gobj[1][1] = Types.TILETYPE.FLAMES;
gobj[1][2] = Types.TILETYPE.WOOD;
gobj[1][3] = Types.TILETYPE.RIGID;
gobj[2][0] = Types.TILETYPE.EXTRABOMB;
gobj[2][1] = Types.TILETYPE.INCRRANGE;
gobj[2][2] = Types.TILETYPE.KICK;
gobj[2][3] = Types.TILETYPE.PASSAGE;
gobj[3][0] = Types.TILETYPE.AGENT0;
gobj[3][1] = Types.TILETYPE.AGENT1;
gobj[3][2] = Types.TILETYPE.AGENT2;
gobj[3][3] = Types.TILETYPE.AGENT3;
GameView view = new GameView(gobj, Types.CELL_SIZE_MAIN);
JEasyFrame frame = new JEasyFrame(view, "Java-Pommerman", true);
WindowInput wi = new WindowInput();
frame.addWindowListener(wi);
wi.windowClosed = false;
for (int i = 0; i < 2000; ++i)
{
view.paint(gobj, new int[4][4]);
try {
Thread.sleep(50);
} catch(Exception e)
{
System.out.println("EXCEPTION " + e);
}
}
System.out.println("DONE");
}
}