-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMapScene.java
More file actions
407 lines (327 loc) · 14.3 KB
/
MapScene.java
File metadata and controls
407 lines (327 loc) · 14.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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
package primary.scenes;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.geometry.Pos;
import javafx.scene.Parent;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.text.Text;
import primary.NewGame;
import primary.Region;
import primary.SceneProbability;
import primary.Ship;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.ArrayList;
public class MapScene extends SceneLoader {
private BackgroundImage back;
{
try {
back = new BackgroundImage(
new Image(new File("src/resources/images/map_background.jpg")
.toURI().toURL().toString()),
BackgroundRepeat.REPEAT, BackgroundRepeat.REPEAT,
BackgroundPosition.DEFAULT, BackgroundSize.DEFAULT);
} catch (MalformedURLException e) {
System.out.println("malformued URL expetion at mapscene line 41");
}
}
@FXML
public void initialize() {
map();
stackpane.setBackground(new Background(back));
generateStatsBar();
}
@Override
public Parent build() {
FXMLLoader p = new FXMLLoader();
p.setController(this);
try {
return FXMLLoader.load(new File("src/resources/MapScene.fxml").toURI().toURL());
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/*
* Base layout
*/
@FXML
private BorderPane pane;
@FXML
private StackPane stackpane;
@FXML
private AnchorPane mapLayout;
private ArrayList<Button> locationButt;
//Location info containers
private Pane infoPane;
private VBox infoList;
//Map Buttons
private Button travelToLocation = new Button("Travel to");
private Button closeInfoPanel = new Button("Close");
//Graphics
//space to prevent planets from being too close to the screen borders
// [for layout generation ONLY]
private static int borderSpacing = 240;
//spacing between icon and info pane
private static double infoPaneSpacingLeftFactor = 2.4;
//increase spacing if pane will be shown on the left of the planet icon
private static int infoPaneSpacing = 85;
//add extra spacing to the left based on the length of the planet's name
private static double infoPaneSpacingNameFactor = 2;
//scaling factors to resize regions coordinates in displayable screen coordinates
private static int yScaling = 2000;
private static int xScaling = 2500;
public MapScene() {
/*
* Regions generation
*/
if (!regionsGenerated) {
regions = new ArrayList<>();
boolean tooClose;
primary.Region newRegion = null;
for (int i = 0; i < numberOfRegions; i++) {
tooClose = true;
//preventing the regions to be too close
while (tooClose) {
newRegion = new primary.Region();
tooClose = false;
if (regions.size() > 0) {
for (primary.Region region : regions) {
if ((Math
.abs(newRegion.getxCoordinate() - region.getxCoordinate())
< minimunRegionSpacing)
&& (Math
.abs(newRegion.getyCoordinate() - region.getyCoordinate())
< minimunRegionSpacing)) {
tooClose = true;
break;
}
}
}
}
regions.add(newRegion);
}
//Setting the first world generated has the spawn world
regions.get(0).setBeenVisited(true);
currentLocation = regions.get(0);
currentLocation.getRegionMarket().generateMarket(currentLocation);
regionsGenerated = true;
regions.get(2).setWinningRegion(true);
currentShip = new Ship("AFO", 5, 3, 50, 10, 3000);
}
/*
* Base layout
*/
locationButt = new ArrayList<>();
//Location info containers
infoPane = new Pane();
infoList = new VBox();
}
private void generateRegions() {
for (int x = 0; x < regions.size(); x++) {
//create mini-pane for this region
Region i = regions.get(x);
StackPane imagePane = new StackPane();
VBox regionBox = new VBox();
//detect if this region is current and assign appropriate graphic
Circle planet = new Circle(28);
if (currentLocation.equals(i)) {
planet.setStrokeWidth(5);
planet.setStroke(Color.GREEN);
}
locationButt.add(new Button(" "));
locationButt.get(x).setId("locationButt");
/*
determine if region has been visited and give it the correct associated properties
*/
Text regionName;
Text planetText;
if (i.hasBeenVisited()) {
//Location appearance
regionName = new Text(i.getName());
planetText = new Text("✹");
planetText.setFill(Color.GREEN);
regionBox.getChildren().add(imagePane);
} else {
//Location appearance
regionName = new Text("?????");
planetText = new Text("?");
planetText.setFill(Color.RED);
regionBox.getChildren().add(imagePane);
}
//Location Button and info set for this region
setLocationButton(x, i);
// add this region to the map
regionBox.getChildren().add(regionName);
regionBox.setAlignment(Pos.CENTER);
imagePane.getChildren().addAll(planet, planetText, locationButt.get(x));
regionName.setFill(Color.YELLOW);
regionName.setId("regionName");
planetText.setId("planetText");
mapLayout.getChildren().addAll(regionBox);
//normalizing coordinates to screen size
int tempX = (int) (i.getxCoordinate() * NewGame.getTheStage().getWidth()) / xScaling;
int tempY = (int) (i.getyCoordinate() * NewGame.getTheStage().getHeight()) / yScaling;
tempX = tempX + (int) ((NewGame.getTheStage().getWidth() - borderSpacing) / 2);
tempY = tempY + (int) ((NewGame.getTheStage().getHeight() - borderSpacing) / 2);
regionBox.setLayoutX(tempX);
regionBox.setLayoutY(tempY);
}
}
/**
* for any given button in the list, assign it this specific region
* and apply these properties
* @param x index of the button
* @param i input region to be modified by this button
*/
private void setLocationButton(int x, Region i) {
locationButt.get(x).setOnAction(e -> {
try {
//If a location gets clicked on:
//resetting the info
infoPane.getChildren().clear();
infoList.getChildren().clear();
infoPane.setStyle("-fx-border-width: 2px;");
Text locationInfo;
//Calculating distance between current location and every other planet
int distance = (int) Math.sqrt(
Math.pow(currentLocation.getxCoordinate() - i.getxCoordinate(), 2)
+ Math.pow(currentLocation.getyCoordinate() - i.getyCoordinate(), 2));
//Calculating fuel cost based on distance and pilot skill level
int fuelCost = (int) ((double) distance * fuelCostPerUnit
* (1.00 - 0.01 * travelDiscountPerPilotLevel
* player.getPilotSkill().getValue()));
costToSelectedLocation = fuelCost;
selectedLocation = i;
if (i.hasBeenVisited()) {
String visitedText = "Yes ";
if (currentLocation.equals(i)) {
visitedText = "Current Location ";
}
locationInfo = new Text(" Location: " + i.getName() + " \n"
+ " Tech Level: " + i.getTechLevel() + " \n"
+ " Coordinates: " + "(X:" + i.getxCoordinate()
+ " | Y:" + i.getyCoordinate() + ")" + " \n"
+ " Description: " + i.getDescription() + " \n"
+ " Visited: " + visitedText + " \n"
+ " Distance: " + distance + " \n"
+ " Fuel cost: " + fuelCost + " (-"
+ player.getPilotSkill().getValue() * 5 + "%) ");
locationInfo.setId("locationInfo");
} else {
locationInfo = new Text(" Location: " + "?????" + " \n"
+ " Tech Level: " + "?????" + " \n"
+ " Coordinates: " + "(X:" + i.getxCoordinate()
+ " | Y:" + i.getyCoordinate() + ")" + " \n"
+ " Description: " + "?????" + " \n"
+ " Visited: " + "Not yet " + " \n"
+ " Distance: " + distance + " \n"
+ " Fuel cost: " + fuelCost + " (-"
+ player.getPilotSkill().getValue() * 5 + "%) ");
locationInfo.setId("locationInfo");
}
//Containers to put inside panel info (info + buttons)
VBox infoVBox = new VBox();
HBox infoButtonHBox = new HBox();
infoButtonHBox.setAlignment(Pos.CENTER_RIGHT);
infoVBox.setAlignment(Pos.CENTER_RIGHT);
//Setting and add Travel and close buttons
HBox.setHgrow(travelToLocation, Priority.ALWAYS);
HBox.setHgrow(closeInfoPanel, Priority.ALWAYS);
travelToLocation.setMaxWidth(Double.MAX_VALUE);
closeInfoPanel.setMaxWidth(Double.MAX_VALUE);
//Checking if player has enough credits to traver to this location
if (currentShip.getFuel() >= fuelCost) {
travelToLocation.setStyle("-fx-background-color: rgba(0, 156, 0, 0.7)");
} else {
travelToLocation.setStyle("-fx-background-color: rgba(255, 0, 0, 0.48)");
}
travelToLocation.setId("travelToLocation");
closeInfoPanel.setId("closeInfoPanel");
infoButtonHBox.getChildren().addAll(travelToLocation, closeInfoPanel);
//setting new Info and putting together the containers
infoList.getChildren().add(locationInfo);
infoVBox.getChildren().addAll(infoList, infoButtonHBox);
infoPane.getChildren().add(infoVBox);
//Calculating optimal location for the infopane to appear in
int infoPaneX = i.getxCoordinate();
int infoPaneY = i.getyCoordinate();
infoPaneX = (int) (infoPaneX * NewGame.getTheStage().getWidth()) / xScaling;
infoPaneY = (int) (infoPaneY * NewGame.getTheStage().getHeight()) / yScaling;
infoPaneX = infoPaneX + (int) ((
NewGame.getTheStage().getWidth() - borderSpacing) / 2);
infoPaneY = infoPaneY + (int) ((
NewGame.getTheStage().getHeight() - borderSpacing) / 2);
if (i.getxCoordinate() > 0) {
infoPaneX = infoPaneX - (int) (infoPaneSpacing * infoPaneSpacingLeftFactor
+ (i.getName().length() * infoPaneSpacingNameFactor));
} else {
infoPaneX = infoPaneX + infoPaneSpacing;
}
if (i.getyCoordinate() > 0) {
infoPaneY = infoPaneY - infoPaneSpacing;
} else {
infoPaneY = infoPaneY + infoPaneSpacing;
}
infoPane.setLayoutX(infoPaneX);
infoPane.setLayoutY(infoPaneY);
infoPane.setId("infoPane");
} catch (Throwable f) {
f.printStackTrace();
}
});
}
private void map() {
generateRegions();
//Map buttons management
closeInfoPanel.setOnAction(e -> {
try {
infoPane.getChildren().clear();
infoPane.setStyle("-fx-border-width: 0px;");
} catch (Throwable f) {
f.printStackTrace();
}
});
travelToLocation.setOnAction(e -> {
try {
if (costToSelectedLocation <= currentShip.getFuel()) {
if (currentLocation != selectedLocation) {
currentShip.setFuel(currentShip.getFuel() - costToSelectedLocation);
SceneProbability prob = new SceneProbability();
prob.probability();
} else {
// probability roll only happens if we moving to new location
setStage(new RegionScene());
}
}
} catch (Throwable f) {
f.printStackTrace();
}
});
/*
* Background image
*/
MAP_BACKGROUND.fitWidthProperty().bind(pane.widthProperty());
MAP_BACKGROUND.fitHeightProperty().bind(pane.heightProperty());
/*
* Title
*/
Text title = new Text("Map");
title.setFill(Color.YELLOW);
pane.setTop(title);
//Adding planet info panel
mapLayout.getChildren().addAll(infoPane);
pane.setCenter(mapLayout);
pane.setBottom(generateStatsBar());
BorderPane.setAlignment(pane.getTop(), Pos.CENTER);
BorderPane.setAlignment(pane.getCenter(), Pos.CENTER);
BorderPane.setAlignment(pane.getBottom(), Pos.CENTER);
title.setId("mapTitle");
pane.getStylesheets().add("css/Styles.css");
}
}