forked from phu004/JavaRTS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpalmTree.java
More file actions
348 lines (236 loc) · 11.3 KB
/
Copy pathpalmTree.java
File metadata and controls
348 lines (236 loc) · 11.3 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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
package entity;
import java.awt.Rectangle;
import core.*;
//palmTree model
public class palmTree extends solidObject{
//the polygons of the model
public polygon3D[] polygons;
public int angle;
public int tileIndex;
public vector shadowvertex0, tempshadowvertex0,shadowvertex1, tempshadowvertex1,shadowvertex2, tempshadowvertex2,shadowvertex3, tempshadowvertex3;
//a screen space boundary which is used to test if the object is visible from camera point of view
public final static Rectangle visibleBoundary = new Rectangle(-50,-50,868, 612);
//a screen space boundary which is used to test if the entire object is within the screen
public final static Rectangle screenBoundary = new Rectangle(40,40,688, 432);
//screen space boundary which is used to test if the shadow of the object is within the screen
public final static Rectangle shadowBoundary1 = new Rectangle(0,0,768, 512);
//palmTrees never moves
public final static vector movenment = new vector(0,0,0);
//angles between leave branch
public static int[] angles = new int[5];
public palmTree(float x, float y, float z){
//uncontrollable unit, but act as a small sized static collidable agent
ID = -1;
teamNo = -1;
type = 100;
boundary2D = new Rect((int)(x*64), (int)(z*64), 1, 1);
tileIndex = boundary2D.x1/16 + (127 - (boundary2D.y1)/16)*128;
if(tileIndex >= 0 && tileIndex < 128*128)
mainThread.gridMap.tiles[tileIndex][4] = this;
else
tileIndex = 0;
this.angle = (int)(360*Math.random());
x = x+0.05f - (float)(Math.random()/10);
y = y+0.05f - (float)(Math.random()/10);
z = z+0.05f - (float)(Math.random()/10);
start = new vector(x,y,z);
centre = start.myClone();
tempCentre = start.myClone();
shadowvertex0 =start.myClone();
shadowvertex0.add(-0.35f,-0.2f, -0.35f);
tempshadowvertex0 = new vector(0,0,0);
shadowvertex1 =start.myClone();
shadowvertex1.add(-0.35f,-0.2f, 0);
tempshadowvertex1 = new vector(0,0,0);
shadowvertex2 =start.myClone();
shadowvertex2.add(0,-0.2f, -0.35f);
tempshadowvertex2 = new vector(0,0,0);
shadowvertex3 =start.myClone();
shadowvertex3.add(0,-0.2f, 0f);
tempshadowvertex3 = new vector(0,0,0);
float scale_i = (float)(Math.random() * 0.3) - 0.15f;
float scale_j = (float)(Math.random() * 0.3) - 0.15f;
float scale_k = (float)(Math.random() * 0.3) - 0.15f;
float scale_j_x = (float)(Math.random() * 0.3) - 0.15f;
float scale_j_y = (float)(Math.random() * 0.3) - 0.15f;
iDirection = new vector(0.7f+0.3f*1.1f + scale_i,0,0);
jDirection = new vector(scale_j_x,(1.1f+0.3f) + scale_j ,scale_j_y);
kDirection = new vector(0,0,0.7f+0.3f*1.1f + scale_k);
//adjust orientation of the model
iDirection.rotate_XZ(angle);
kDirection.rotate_XZ(angle);
int color = 110 << 16 | 205 << 8 | 10;
postProcessingThread.theMiniMap.background[tileIndex] = color;
postProcessingThread.theMiniMap.background[tileIndex + 1] = color;
postProcessingThread.theMiniMap.background[tileIndex + 128] = color;
postProcessingThread.theMiniMap.background[tileIndex + 129] = color;
boundary2D.owner = this;
currentCommand = StandBy;
angles[0] = 0;
makePolygons();
}
public palmTree(float x, float y, float z, float scale_i, float scale_j, float scale_k, float scale_j_x, float scale_j_y, int angle, int angle1, int angle2, int angle3, int angle4, int angle5){
//uncontrollable unit, but act as a small sized static collidable agent
type = 100;
boundary2D = new Rect((int)(x*64), (int)(z*64), 1, 1);
tileIndex = boundary2D.x1/16 + (127 - (boundary2D.y1)/16)*128;
mainThread.gridMap.tiles[tileIndex][4] = this;
start = new vector(x,y,z);
centre = start.myClone();
tempCentre = start.myClone();
shadowvertex0 =start.myClone();
shadowvertex0.add(-0.75f,-0.2f, -0.95f);
tempshadowvertex0 = new vector(0,0,0);
shadowvertex1 =start.myClone();
shadowvertex1.add(-0.75f,-0.2f, 0);
tempshadowvertex1 = new vector(0,0,0);
shadowvertex2 =start.myClone();
shadowvertex2.add(0,-0.2f, -0.95f);
tempshadowvertex2 = new vector(0,0,0);
shadowvertex3 =start.myClone();
shadowvertex3.add(0,-0.2f, 0f);
tempshadowvertex3 = new vector(0,0,0);
iDirection = new vector(0.7f+0.3f*1.1f + scale_i,0,0);
jDirection = new vector(scale_j_x,(1.1f+0.3f) + scale_j ,scale_j_y);
kDirection = new vector(0,0,0.7f+0.3f*1.1f + scale_k);
iDirection.rotate_XZ(angle);
kDirection.rotate_XZ(angle);
int color = 110 << 16 | 205 << 8 | 10;
postProcessingThread.theMiniMap.background[tileIndex] = color;
postProcessingThread.theMiniMap.background[tileIndex + 1] = color;
postProcessingThread.theMiniMap.background[tileIndex + 128] = color;
postProcessingThread.theMiniMap.background[tileIndex + 129] = color;
boundary2D.owner = this;
currentCommand = StandBy;
angles[0] = angle1;
angles[1] = angle2;
angles[2] = angle3;
angles[3] = angle4;
angles[4] = angle5;
makePolygons();
}
//Construct polygons for this model.
//The polygon data is hard-coded here
private void makePolygons(){
vector[] v;
start.add(0,-0.25f,0);
polygons = new polygon3D[8 + 5*6];
if(angles[0] ==0){
for(int i = 0; i < 5; i++){
angles[i] = 72*(i+1) + 15 - (int)(30*Math.random());
}
}
//body
v = new vector[]{put(-0.001, 0.1, -0.01), put(0.016, 0.1, -0.01), put(0.01, 0, -0.01), put(-0.014, 0, -0.01)};
polygons[0] = new polygon3D(v, v[0], v[1], v [3], mainThread.textures[4], 0.1f,0.5f,1);
v = new vector[]{put(-0.001, 0.1, 0.01), put(-0.001, 0.1, -0.01), put(-0.014, 0, -0.01), put(-0.014, 0, 0.014)};
polygons[1] = new polygon3D(v, v[0], v[1], v [3], mainThread.textures[4], 0.1f,0.5f,1);
v = new vector[]{put(0.016, 0.1, 0.01), put(-0.001, 0.1, 0.01), put(-0.014, 0, 0.014), put(0.01, 0, 0.014)};
polygons[2] = new polygon3D(v, v[0], v[1], v [3], mainThread.textures[4], 0.1f,0.5f,1);
v = new vector[]{put(0.016, 0.1, -0.01), put(0.016, 0.1, 0.01), put(0.01, 0, 0.014), put(0.01, 0, -0.01)};
polygons[3] = new polygon3D(v, v[0], v[1], v [3], mainThread.textures[4], 0.1f,0.5f,1);
v = new vector[]{put(0.002, 0.3, -0.008), put(0.013, 0.3, -0.008), put(0.016, 0.1, -0.01), put(-0.001, 0.1, -0.01)};
polygons[4] = new polygon3D(v, v[0], v[1], v [3], mainThread.textures[4], 0.1f,0.5f,1);
v = new vector[]{put(0.002, 0.3, 0.006), put(0.002, 0.3, -0.008), put(-0.001, 0.1, -0.01),put(-0.001, 0.1, 0.01)};
polygons[5] = new polygon3D(v, v[0], v[1], v [3], mainThread.textures[4], 0.1f,0.5f,1);
v = new vector[]{put(0.013, 0.3, 0.006), put(0.002, 0.3, 0.006), put(-0.001, 0.1, 0.01),put(0.016, 0.1, 0.01)};
polygons[6] = new polygon3D(v, v[0], v[1], v [3], mainThread.textures[4], 0.1f,0.5f,1);
v = new vector[]{put(0.013, 0.3, -0.008), put(0.013, 0.3, 0.006), put(0.016, 0.1, 0.01), put(0.016, 0.1, -0.01)};
polygons[7] = new polygon3D(v, v[0], v[1], v [3], mainThread.textures[4], 0.1f,0.5f,1);
//leaves
start.add(0.005f, 0,0);
int index = 8;
int currentAngle = 0;
for(int i = 0; i < 5; i++){
v = new vector[]{put(0.015, 0.3, 0.01), put(0, 0.3, 0), put(0, 0.34, 0.05), put(0.015, 0.32, 0.05)};
polygons[index] = new polygon3D(v, v[0], v[1], v [3], mainThread.textures[5], 1,1,1);
v = new vector[]{put(0, 0.3, 0), put(-0.015, 0.3, 0.01), put(-0.015, 0.32, 0.05), put(0, 0.34, 0.05)};
polygons[index+1] = new polygon3D(v, v[0], v[1], v [3], mainThread.textures[5], 1,1,1);
v = new vector[]{put(0, 0.34, 0.05), put(0, 0.33, 0.09), put(0.015, 0.31, 0.09), put(0.015, 0.32, 0.05)};
polygons[index + 2] = new polygon3D(v, v[0], v[1], v [3], mainThread.textures[5], 1,1,1);
v = new vector[]{put(0, 0.34, 0.05), put(-0.015, 0.32, 0.05), put(-0.015, 0.31, 0.09), put(0, 0.33, 0.09)};
polygons[index + 3] = new polygon3D(v, v[0], v[1], v [3], mainThread.textures[5], 1,1,1);
v = new vector[]{put(0, 0.33, 0.09), put(-0.015, 0.31, 0.09), put(0, 0.29, 0.12)};
polygons[index + 4] = new polygon3D(v, v[0], v[1], v [2], mainThread.textures[5], 1,1,1);
v = new vector[]{put(0.015, 0.31, 0.09), put(0, 0.33, 0.09), put(0, 0.29, 0.12)};
polygons[index + 5] = new polygon3D(v, v[0], v[1], v [2], mainThread.textures[5], 1,1,1);
iDirection.rotate_XZ(angles[i]-currentAngle);
kDirection.rotate_XZ(angles[i]-currentAngle);
currentAngle = angles[i];
index+=6;
}
for(int i = 0; i < polygons.length; i++){
polygons[i].Ambient_I+=10;
polygons[i].findDiffuse();
polygons[i].parentObject = this;
}
}
//update the model
public void update(){
mainThread.gridMap.currentObstacleMap[tileIndex] = false;
//update center in camera coordinate
tempCentre.set(centre);
tempCentre.subtract(camera.position);
tempCentre.rotate_XZ(camera.XZ_angle);
tempCentre.rotate_YZ(camera.YZ_angle);
tempCentre.updateLocation();
if(tempCentre.screenX > 918 || tempCentre.screenX < - 150 || tempCentre.screenY < - 150 || tempCentre.screenY > 662){
visible = false;
return;
}
//test if the palm tree is visible in camera point of view
if(visibleBoundary.contains(tempCentre.screenX, tempCentre.screenY)){
visible = true;
if(screenBoundary.contains(tempCentre.screenX, tempCentre.screenY))
withinViewScreen = true;
else
withinViewScreen = false;
}else{
visible = false;
}
tempshadowvertex0.set(shadowvertex0);
tempshadowvertex0.subtract(camera.position);
tempshadowvertex0.rotate_XZ(camera.XZ_angle);
tempshadowvertex0.rotate_YZ(camera.YZ_angle);
tempshadowvertex0.updateLocation();
tempshadowvertex1.set(shadowvertex1);
tempshadowvertex1.subtract(camera.position);
tempshadowvertex1.rotate_XZ(camera.XZ_angle);
tempshadowvertex1.rotate_YZ(camera.YZ_angle);
tempshadowvertex1.updateLocation();
tempshadowvertex2.set(shadowvertex2);
tempshadowvertex2.subtract(camera.position);
tempshadowvertex2.rotate_XZ(camera.XZ_angle);
tempshadowvertex2.rotate_YZ(camera.YZ_angle);
tempshadowvertex2.updateLocation();
tempshadowvertex3.set(shadowvertex3);
tempshadowvertex3.subtract(camera.position);
tempshadowvertex3.rotate_XZ(camera.XZ_angle);
tempshadowvertex3.rotate_YZ(camera.YZ_angle);
tempshadowvertex3.updateLocation();
//if the object is visible then draw it on the shadow buffer from light point of view
if(shadowBoundary1.contains(tempshadowvertex0.screenX, tempshadowvertex0.screenY) ||
shadowBoundary1.contains(tempshadowvertex1.screenX, tempshadowvertex1.screenY) ||
shadowBoundary1.contains(tempshadowvertex2.screenX, tempshadowvertex2.screenY) ||
shadowBoundary1.contains(tempshadowvertex3.screenX, tempshadowvertex3.screenY)
){
for(int i = 0; i < polygons.length; i++){
polygons[i].update_lightspace();
}
}
}
public vector getMovement(){
return movenment;
}
//draw model
public void draw(){
if(!visible)
return;
for(int i = 0; i < polygons.length; i++){
polygons[i].update();
}
for(int i = 0; i < polygons.length; i++){
polygons[i].draw();
}
}
}