Skip to content

Commit

Permalink
Merge pull request #53 from pgk1216/pgk
Browse files Browse the repository at this point in the history
Added game menu and fixed issue where player could get out of the playing area
  • Loading branch information
NoahAldahan authored Apr 23, 2022
2 parents 87319d7 + 2eab35f commit 52bf207
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,41 @@
import com.almasb.fxgl.core.math.Vec2;
import com.almasb.fxgl.entity.SpawnData;
import com.almasb.fxgl.entity.component.Component;
import com.dinosaur.dinosaurexploder.view.DinosaurGUI;
import javafx.geometry.Point2D;
import javafx.scene.image.Image;

import static com.almasb.fxgl.dsl.FXGLForKtKt.spawn;
import static com.almasb.fxgl.dsl.FXGLForKtKt.*;

public class PlayerComponent extends Component implements Player{
int movementSpeed = 8;
//entity is not initialized anywhere because it is linked in the factory
public void moveUp(){
if(entity.getY() < 0) {
System.out.println("Out of bounds");
return;
}
entity.translateY(-movementSpeed);
}
public void moveDown(){
if(!(entity.getY() < DinosaurGUI.HEIGHT - entity.getHeight())) {
System.out.println("Out of bounds");
return;
}
entity.translateY(movementSpeed);
}
public void moveRight(){
if(!(entity.getX() < DinosaurGUI.WIDTH - entity.getWidth())) {
System.out.println("Out of bounds");
return;
}
entity.translateX(movementSpeed);
}
public void moveLeft(){
if(entity.getX() < 0) {
System.out.println("Out of bounds");
return;
}
entity.translateX(-movementSpeed);
}

Expand All @@ -31,7 +48,7 @@ public void shoot(){
Image spcshpImg = new Image("assets/textures/spaceship.png");
spawn("basicProjectile",
new SpawnData(center.getX() - (projImg.getWidth()/2) +3, center.getY() - spcshpImg.getHeight()/2)
.put("direction", direction.toPoint2D() )
.put("direction", direction.toPoint2D() )
);
}
}
21 changes: 19 additions & 2 deletions src/main/java/com/dinosaur/dinosaurexploder/view/DinosaurGUI.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,29 @@
package com.dinosaur.dinosaurexploder.view;

import com.almasb.fxgl.app.GameSettings;
import com.almasb.fxgl.app.scene.FXGLMenu;
import com.almasb.fxgl.app.scene.SceneFactory;
import javafx.scene.Scene;

public class DinosaurGUI {
public static final int WIDTH = 550;
public static final int HEIGHT = 750;

public void initSettings(GameSettings settings) {
settings.setWidth(550);
settings.setHeight(750);
settings.setWidth(WIDTH);
settings.setHeight(HEIGHT);
settings.setTitle("Dinosaur Exploder");
settings.setMainMenuEnabled(true);

// Custom main menu
settings.setSceneFactory(new SceneFactory() {
@Override
public FXGLMenu newMainMenu() {
return new DinosaurMenu();
}
});


settings.setVersion("1.0");
settings.setTicksPerSecond(60); // check info : settings.setProfilingEnabled(true);
}
Expand Down
47 changes: 47 additions & 0 deletions src/main/java/com/dinosaur/dinosaurexploder/view/DinosaurMenu.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.dinosaur.dinosaurexploder.view;

import com.almasb.fxgl.app.scene.FXGLMenu;
import com.almasb.fxgl.app.scene.MenuType;
import com.almasb.fxgl.dsl.FXGL;
import com.almasb.fxgl.ui.FontType;
import javafx.scene.Node;
import javafx.scene.control.Button;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;

public class DinosaurMenu extends FXGLMenu {

public DinosaurMenu() {
super(MenuType.MAIN_MENU);

var bg = new Rectangle(getAppWidth(), getAppHeight(), Color.BLACK);

var title = FXGL.getUIFactoryService().newText("Dinosaur Exploder", Color.LIME, FontType.MONO, 35);
var startButton = new Button("Start Game");
var quitButton = new Button("Quit");

startButton.setMinSize(200, 100);
quitButton.setMinSize(200, 100);

title.setTranslateY(100);
title.setTranslateX(getAppWidth() / 2 - 175);

startButton.setTranslateY(350);
startButton.setTranslateX(getAppWidth() / 2 - 100);
startButton.setStyle("-fx-font-size:20");

quitButton.setTranslateY(490);
quitButton.setTranslateX(getAppWidth() / 2 - 100);
quitButton.setStyle("-fx-font-size:20");

startButton.setOnAction(event -> fireNewGame());
quitButton.setOnAction(event -> fireExit());

getContentRoot().getChildren().addAll(
bg, title, startButton, quitButton
);
}

}

0 comments on commit 52bf207

Please sign in to comment.