forked from PhilippvK/playforia-minigolf
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameBackgroundCanvas.java
More file actions
273 lines (238 loc) · 10.7 KB
/
GameBackgroundCanvas.java
File metadata and controls
273 lines (238 loc) · 10.7 KB
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
266
267
268
269
270
271
272
273
package agolf.game;
import agolf.GameContainer;
import agolf.GolfGameFrame;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import org.moparforia.shared.tracks.Track;
import org.moparforia.shared.tracks.stats.TrackStats;
public class GameBackgroundCanvas extends Canvas {
protected static final Color aColor75 = new Color(240, 240, 255);
public static final int[] anIntArray78 = new int[] {3, 5, 8, 49};
public static final int[] anIntArray79 = new int[] {2, 3, 5, 25};
protected GameContainer gameContainer;
private Image backgroundImg;
protected Image image;
private Graphics graphics;
public Track track;
public TrackStats trackStats;
public Map map;
private int trackWidth = 735;
private int trackHeight = 375;
protected GameBackgroundCanvas(GameContainer gameContainer, Image backgroundImage) {
this.gameContainer = gameContainer;
this.backgroundImg = backgroundImage;
this.setSize(this.trackWidth, this.trackHeight);
}
public void addNotify() {
super.addNotify();
this.repaint();
}
public void paint(Graphics g) {
this.update(g);
}
public void update(Graphics g) {
if (this.image == null) {
g.setColor(GolfGameFrame.colourGameBackground);
g.fillRect(0, 0, this.trackWidth, this.trackHeight);
} else {
g.drawImage(this.image, 0, 0, this);
}
}
// this useless func is called when we get start packet
// initialize variables
// draws map as grass
// this.gameCanvas.createMap(16777216);
// 16777216 == grass
protected void initializeGameArea() {
int tileCode = 16777216;
if (this.image == null) {
this.image = this.createImage(this.trackWidth, this.trackHeight);
if (this.image == null) {
this.image = this.backgroundImg;
}
this.graphics = this.image.getGraphics();
}
Image var2 = this.gameContainer.imageManager.createImage(
this.gameContainer.spriteManager.getPixelsFromTileCode(tileCode), 15, 15);
this.graphics.setColor(aColor75);
for (int y = 0; y < 25; ++y) {
for (int x = 0; x < 49; ++x) {
if (tileCode == 0) {
this.graphics.fillRect(x * 15, y * 15, 15, 15);
} else {
this.graphics.drawImage(var2, x * 15, y * 15, this);
}
}
}
this.repaint();
}
protected Image getTileImageAt(int tileX, int tileY) {
int[] imageData = this.gameContainer.spriteManager.getPixelsFromTileCode(
this.map.getTile(tileX, tileY).getCode());
if (this.gameContainer.graphicsQualityIndex >= 2) {
for (int x = 0; x < 15; ++x) {
for (int y = 0; y < 15; ++y) {
for (int z = 1; z <= 7 && tileX * 15 + y - z > 0 && tileY * 15 + x - z > 0; ++z) {
if (this.castsShadow(tileX * 15 + y - z, tileY * 15 + x - z)) {
this.shiftPixel(imageData, y, x, -8, 15);
}
}
this.shiftPixel(imageData, y, x, (int) (Math.random() * 11.0D) - 5, 15);
}
}
}
Image resultImage = this.gameContainer.imageManager.createImage(imageData, 15, 15);
this.graphics.drawImage(resultImage, tileX * 15, tileY * 15, this);
return resultImage;
}
public void drawMap() {
int[] mapPixels = new int[275625];
int[] currentTileImageData = null;
Tile oldTile = null;
boolean trackTestMode =
this.gameContainer.synchronizedTrackTestMode.get(); // controls when to draw starting positions
Tile currentTile;
int yPixels;
int xPixels;
int index;
Map map = this.map;
for (int tileY = 0; tileY < 25; ++tileY) {
for (int tileX = 0; tileX < 49; ++tileX) {
currentTile = map.getTile(tileX, tileY);
if (!currentTile.equals(oldTile)) {
currentTile = map.getTile(tileX, tileY);
int currentCode = currentTile.getSpecialsettingCode(this.track.getSpecialSettings());
currentTileImageData = this.gameContainer.spriteManager.getPixelsFromTileCode(currentCode);
// draws debug points on starting positions
if (trackTestMode && currentTile.getCode() == 2) {
yPixels = currentTile.getYPixelsFromSpecialId();
if (yPixels != -1) {
for (xPixels = 6; xPixels <= 8; ++xPixels) {
for (index = 6; index <= 8; ++index) {
currentTileImageData[xPixels * 15 + index] = yPixels;
}
}
}
}
oldTile = map.getTile(tileX, tileY);
}
for (xPixels = 0; xPixels < 15; ++xPixels) {
for (yPixels = 0; yPixels < 15; ++yPixels) {
mapPixels[(tileY * 15 + xPixels) * 735 + tileX * 15 + yPixels] =
currentTileImageData[xPixels * 15 + yPixels];
}
}
}
}
try {
int var14;
int var15;
if (this.gameContainer.graphicsQualityIndex > 0) {
for (yPixels = 0; yPixels < 375; ++yPixels) {
for (xPixels = 0; xPixels < 735; ++xPixels) {
boolean var25;
boolean var27;
// creates light and dark spot for solids
// top and left side is brighter
// bottom and right side is darker
if (this.castsShadow(xPixels, yPixels)) {
var25 = this.castsShadow(xPixels - 1, yPixels - 1);
var27 = this.castsShadow(xPixels + 1, yPixels + 1);
if (!var25
&& var27
&& !this.castsShadow(xPixels, yPixels - 1)
&& !this.castsShadow(xPixels - 1, yPixels)) {
this.shiftPixel(mapPixels, xPixels, yPixels, 128, 735);
} else {
if (!var25 && var27) {
// shift pixels towards 255/white
this.shiftPixel(mapPixels, xPixels, yPixels, 24, 735);
}
if (!var27 && var25) {
// shift pixels towards 0/black
this.shiftPixel(mapPixels, xPixels, yPixels, -24, 735);
}
}
// draws shadow
if (this.gameContainer.graphicsQualityIndex >= 2) {
for (index = 1; index <= 7 && xPixels + index < 735 && yPixels + index < 375; ++index) {
if (!this.castsShadow(
xPixels + index, yPixels + index)) { // dont draw shadow on blocks
var14 = xPixels + index;
var15 = yPixels + index;
// shift pixels towards black to create shadow
this.shiftPixel(mapPixels, var14, var15, -8, 735);
}
}
}
}
// creates light and dark spots to teleport starts
if (map.isTeleportStart(xPixels, yPixels)) {
var25 = map.isTeleportStart(xPixels - 1, yPixels - 1);
var27 = map.isTeleportStart(xPixels + 1, yPixels + 1);
if (!var25
&& var27
&& !map.isTeleportStart(xPixels, yPixels - 1)
&& !map.isTeleportStart(xPixels - 1, yPixels)) {
this.shiftPixel(mapPixels, xPixels, yPixels, 16, 735);
} else {
if (!var25 && var27) {
// shift pixels towards 255/white
this.shiftPixel(mapPixels, xPixels, yPixels, 16, 735);
}
if (!var27 && var25) {
// shift pixels towards 0/black
this.shiftPixel(mapPixels, xPixels, yPixels, -16, 735);
}
}
}
// creates grain effect on tiles
if (this.gameContainer.graphicsQualityIndex >= 2) {
index = (int) (Math.random() * 11.0D) - 5;
this.shiftPixel(mapPixels, xPixels, yPixels, index, 735);
}
}
}
}
} catch (OutOfMemoryError e) {
}
this.graphics.drawImage(
this.gameContainer.imageManager.createImage(mapPixels, this.trackWidth, this.trackHeight), 0, 0, this);
}
private boolean castsShadow(int x, int y) {
// trackSpecialSettings[3]
// 3:false => illusion walls shadowless 3:true => illusion walls shadows
return this.map.castShadow(x, y, this.track.getSpecialSettings());
}
// adds offset to r,b,g channels of pixels[x][y] then clamps the value to valid range
private void shiftPixel(int[] pixels, int x, int y, int offset, int width) {
int pixel = pixels[y * width + x] & 16777215;
int red = pixel / 65536 % 256;
int green = pixel / 256 % 256;
int blue = pixel % 256;
red += offset;
if (red < 0) {
red = 0;
}
if (red > 255) {
red = 255;
}
green += offset;
if (green < 0) {
green = 0;
}
if (green > 255) {
green = 255;
}
blue += offset;
if (blue < 0) {
blue = 0;
}
if (blue > 255) {
blue = 255;
}
pixels[y * width + x] = -16777216 + red * 256 * 256 + green * 256 + blue;
}
}