-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWorldGenerator.java
More file actions
235 lines (184 loc) · 9.46 KB
/
WorldGenerator.java
File metadata and controls
235 lines (184 loc) · 9.46 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
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.Random;
// Handles creation and placement of game entities in the world.
public class WorldGenerator {
private Random random;
private int worldWidth;
private int worldHeight;
private BufferedImage[] treeImages;
private BufferedImage[] rockImages;
// Create a new WorldGenerator with the specified world dimensions.
public WorldGenerator(int worldWidth, int worldHeight) {
this.worldWidth = worldWidth;
this.worldHeight = worldHeight;
this.random = new Random();
}
public void loadImages() {
// Load tree images scaled to max height 150px
treeImages = ImageManager.loadTreeImages(150);
// Load rock images scaled to max width 50px
rockImages = ImageManager.loadRockImages(50);
// Debug output
int treesLoaded = 0;
int rocksLoaded = 0;
for (BufferedImage img : treeImages) {
if (img != null) treesLoaded++;
}
for (BufferedImage img : rockImages) {
if (img != null) rocksLoaded++;
}
System.out.println("Loaded " + treesLoaded + " tree images and " + rocksLoaded + " rock images");
}
/**
* Create solid objects at random positions
* Avoids placing objects on top of player start position.
*/
public ArrayList<SolidObject> createSolidObjects(int numObjects, int playerStartX, int playerStartY, int safeZoneRadius) {
ArrayList<SolidObject> solids = new ArrayList<SolidObject>();
final int MAX_ATTEMPTS_PER_OBJECT = 1000;
final int EDGE_MARGIN = 50;
for (int i = 0; i < numObjects; i++) {
int attempts = 0;
boolean objectPlaced = false;
while (attempts < MAX_ATTEMPTS_PER_OBJECT && !objectPlaced) {
attempts++;
boolean isTree = random.nextBoolean();
BufferedImage selectedImage;
if (isTree) {
selectedImage = treeImages[random.nextInt(treeImages.length)];
} else {
selectedImage = rockImages[random.nextInt(rockImages.length)];
}
if (selectedImage == null) {
break;
}
// Generate random position within world bounds
int objX = EDGE_MARGIN + random.nextInt(worldWidth - 100);
int objY = EDGE_MARGIN + random.nextInt(worldHeight - 100);
// Check if position is safe (not too close to player start)
double distToPlayer = Math.sqrt(
Math.pow(objX - playerStartX, 2) +
Math.pow(objY - playerStartY, 2)
);
if (distToPlayer < safeZoneRadius) {
continue;
}
// Check for overlap with existing objects
boolean overlaps = false;
Rectangle2D.Double newBounds = new Rectangle2D.Double(
objX, objY,
selectedImage.getWidth(),
selectedImage.getHeight()
);
for (SolidObject existing : solids) {
if (newBounds.intersects(existing.getBoundingRectangle())) {
overlaps = true;
break;
}
}
if (!overlaps) {
solids.add(new SolidObject(objX, objY, selectedImage));
objectPlaced = true;
}
}
if (!objectPlaced) {
System.out.println("Warning: Could not place object after " + MAX_ATTEMPTS_PER_OBJECT + " attempts");
}
}
System.out.println("Created " + solids.size() + " random solid objects (trees and rocks)");
return solids;
}
// reate collectibles at random positions, avoiding solid objects.
public ArrayList<Collectible> createCollectibles(ArrayList<SolidObject> solidObjects, int numCollectibles,
int collectibleSize, int minDistanceFromSolid) {
ArrayList<Collectible> collectibles = new ArrayList<Collectible>();
// Load coin strip image for animated collectibles
BufferedImage coinStrip = ImageManager.loadBufferedImage("images/coinStrip.png");
final int EDGE_MARGIN = 50;
final int MAX_ATTEMPTS = 1000;
// Generate random positions
int[][] positions = new int[numCollectibles][2];
for (int i = 0; i < numCollectibles; i++) {
boolean validPosition = false;
int attempts = 0;
int x = 0, y = 0;
while (!validPosition && attempts < MAX_ATTEMPTS) {
attempts++;
// Generate random position within world bounds
x = EDGE_MARGIN + random.nextInt(worldWidth - 2 * EDGE_MARGIN - collectibleSize);
y = EDGE_MARGIN + random.nextInt(worldHeight - 2 * EDGE_MARGIN - collectibleSize);
validPosition = true;
// Check distance from solid objects
for (SolidObject solid : solidObjects) {
Rectangle2D.Double solidBounds = solid.getBoundingRectangle();
double distance = getDistanceFromRect(x, y, solidBounds);
if (distance < minDistanceFromSolid) {
validPosition = false;
break;
}
}
// Check distance from other collectibles
if (validPosition) {
for (int j = 0; j < i; j++) {
double dist = Math.sqrt(
Math.pow(x - positions[j][0], 2) +
Math.pow(y - positions[j][1], 2)
);
if (dist < minDistanceFromSolid) {
validPosition = false;
break;
}
}
}
}
if (validPosition) {
positions[i][0] = x;
positions[i][1] = y;
} else {
positions[i][0] = EDGE_MARGIN + random.nextInt(worldWidth - 2 * EDGE_MARGIN);
positions[i][1] = EDGE_MARGIN + random.nextInt(worldHeight - 2 * EDGE_MARGIN);
System.out.println("Warning: Could not find valid position for collectible " + i + " after " + MAX_ATTEMPTS + " attempts");
}
}
if (coinStrip != null) {
System.out.println("Coin strip loaded: " + coinStrip.getWidth() + "x" + coinStrip.getHeight());
// Create animated collectibles
StripAnimation stripAnim = new StripAnimation(170, coinStrip.getHeight(), 18);
stripAnim.setAnimationSpeed(60);
BufferedImage[] coinFrames = stripAnim.extractFramesFromRow(coinStrip, 0);
System.out.println("Extracted " + coinFrames.length + " coin frames");
Animation coinAnimation = stripAnim.createAnimationFromFrames(coinFrames, 60, false);
// Create animated sprites at the randomly generated positions
for (int i = 0; i < positions.length; i++) {
AnimatedSprite coinSprite = new AnimatedSprite(null, positions[i][0], positions[i][1], collectibleSize, collectibleSize);
coinSprite.setAnimation(coinAnimation);
collectibles.add(new Collectible(positions[i][0], positions[i][1], collectibleSize, collectibleSize, coinSprite));
}
System.out.println("Created " + collectibles.size() + " animated coin collectibles at random positions");
} else {
System.out.println("Failed to load coinStrip.png, creating collectibles with placeholder sprites");
for (int i = 0; i < positions.length; i++) {
collectibles.add(new Collectible(positions[i][0], positions[i][1], collectibleSize, collectibleSize, null));
}
}
return collectibles;
}
public ArrayList<AnimatedSprite> createAnimatedSprites(javax.swing.JPanel panel) {
ArrayList<AnimatedSprite> sprites = new ArrayList<AnimatedSprite>();
AnimatedSprite sprite = new AnimatedSprite(panel, 500, 300, 50, 50);
Animation anim = new Animation(true);
anim.addFrame(ImageManager.loadImage("sprite1.png"), 200);
anim.addFrame(ImageManager.loadImage("sprite2.png"), 200);
sprite.setAnimation(anim);
sprites.add(sprite);
return sprites;
}
public double getDistanceFromRect(int x, int y, Rectangle2D.Double rect) {
double closestX = Math.max(rect.getMinX(), Math.min(x, rect.getMaxX()));
double closestY = Math.max(rect.getMinY(), Math.min(y, rect.getMaxY()));
double distance = Math.sqrt(Math.pow(x - closestX, 2) + Math.pow(y - closestY, 2));
return distance;
}
}