Skip to content

Commit 52bf207

Browse files
authored
Merge pull request #53 from pgk1216/pgk
Added game menu and fixed issue where player could get out of the playing area
2 parents 87319d7 + 2eab35f commit 52bf207

File tree

3 files changed

+85
-4
lines changed

3 files changed

+85
-4
lines changed

src/main/java/com/dinosaur/dinosaurexploder/model/PlayerComponent.java

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,41 @@
33
import com.almasb.fxgl.core.math.Vec2;
44
import com.almasb.fxgl.entity.SpawnData;
55
import com.almasb.fxgl.entity.component.Component;
6+
import com.dinosaur.dinosaurexploder.view.DinosaurGUI;
67
import javafx.geometry.Point2D;
78
import javafx.scene.image.Image;
89

9-
import static com.almasb.fxgl.dsl.FXGLForKtKt.spawn;
10+
import static com.almasb.fxgl.dsl.FXGLForKtKt.*;
1011

1112
public class PlayerComponent extends Component implements Player{
1213
int movementSpeed = 8;
1314
//entity is not initialized anywhere because it is linked in the factory
1415
public void moveUp(){
16+
if(entity.getY() < 0) {
17+
System.out.println("Out of bounds");
18+
return;
19+
}
1520
entity.translateY(-movementSpeed);
1621
}
1722
public void moveDown(){
23+
if(!(entity.getY() < DinosaurGUI.HEIGHT - entity.getHeight())) {
24+
System.out.println("Out of bounds");
25+
return;
26+
}
1827
entity.translateY(movementSpeed);
1928
}
2029
public void moveRight(){
30+
if(!(entity.getX() < DinosaurGUI.WIDTH - entity.getWidth())) {
31+
System.out.println("Out of bounds");
32+
return;
33+
}
2134
entity.translateX(movementSpeed);
2235
}
2336
public void moveLeft(){
37+
if(entity.getX() < 0) {
38+
System.out.println("Out of bounds");
39+
return;
40+
}
2441
entity.translateX(-movementSpeed);
2542
}
2643

@@ -31,7 +48,7 @@ public void shoot(){
3148
Image spcshpImg = new Image("assets/textures/spaceship.png");
3249
spawn("basicProjectile",
3350
new SpawnData(center.getX() - (projImg.getWidth()/2) +3, center.getY() - spcshpImg.getHeight()/2)
34-
.put("direction", direction.toPoint2D() )
51+
.put("direction", direction.toPoint2D() )
3552
);
3653
}
3754
}

src/main/java/com/dinosaur/dinosaurexploder/view/DinosaurGUI.java

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,29 @@
11
package com.dinosaur.dinosaurexploder.view;
22

33
import com.almasb.fxgl.app.GameSettings;
4+
import com.almasb.fxgl.app.scene.FXGLMenu;
5+
import com.almasb.fxgl.app.scene.SceneFactory;
6+
import javafx.scene.Scene;
47

58
public class DinosaurGUI {
9+
public static final int WIDTH = 550;
10+
public static final int HEIGHT = 750;
11+
612
public void initSettings(GameSettings settings) {
7-
settings.setWidth(550);
8-
settings.setHeight(750);
13+
settings.setWidth(WIDTH);
14+
settings.setHeight(HEIGHT);
915
settings.setTitle("Dinosaur Exploder");
16+
settings.setMainMenuEnabled(true);
17+
18+
// Custom main menu
19+
settings.setSceneFactory(new SceneFactory() {
20+
@Override
21+
public FXGLMenu newMainMenu() {
22+
return new DinosaurMenu();
23+
}
24+
});
25+
26+
1027
settings.setVersion("1.0");
1128
settings.setTicksPerSecond(60); // check info : settings.setProfilingEnabled(true);
1229
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.dinosaur.dinosaurexploder.view;
2+
3+
import com.almasb.fxgl.app.scene.FXGLMenu;
4+
import com.almasb.fxgl.app.scene.MenuType;
5+
import com.almasb.fxgl.dsl.FXGL;
6+
import com.almasb.fxgl.ui.FontType;
7+
import javafx.scene.Node;
8+
import javafx.scene.control.Button;
9+
import javafx.scene.layout.Pane;
10+
import javafx.scene.layout.StackPane;
11+
import javafx.scene.paint.Color;
12+
import javafx.scene.shape.Rectangle;
13+
14+
public class DinosaurMenu extends FXGLMenu {
15+
16+
public DinosaurMenu() {
17+
super(MenuType.MAIN_MENU);
18+
19+
var bg = new Rectangle(getAppWidth(), getAppHeight(), Color.BLACK);
20+
21+
var title = FXGL.getUIFactoryService().newText("Dinosaur Exploder", Color.LIME, FontType.MONO, 35);
22+
var startButton = new Button("Start Game");
23+
var quitButton = new Button("Quit");
24+
25+
startButton.setMinSize(200, 100);
26+
quitButton.setMinSize(200, 100);
27+
28+
title.setTranslateY(100);
29+
title.setTranslateX(getAppWidth() / 2 - 175);
30+
31+
startButton.setTranslateY(350);
32+
startButton.setTranslateX(getAppWidth() / 2 - 100);
33+
startButton.setStyle("-fx-font-size:20");
34+
35+
quitButton.setTranslateY(490);
36+
quitButton.setTranslateX(getAppWidth() / 2 - 100);
37+
quitButton.setStyle("-fx-font-size:20");
38+
39+
startButton.setOnAction(event -> fireNewGame());
40+
quitButton.setOnAction(event -> fireExit());
41+
42+
getContentRoot().getChildren().addAll(
43+
bg, title, startButton, quitButton
44+
);
45+
}
46+
47+
}

0 commit comments

Comments
 (0)