diff --git a/highScore.ser b/highScore.ser new file mode 100644 index 0000000..2bccc74 Binary files /dev/null and b/highScore.ser differ diff --git a/src/main/java/com/dinosaur/dinosaurexploder/DinosaurApp.java b/src/main/java/com/dinosaur/dinosaurexploder/DinosaurApp.java index 7188744..590998c 100644 --- a/src/main/java/com/dinosaur/dinosaurexploder/DinosaurApp.java +++ b/src/main/java/com/dinosaur/dinosaurexploder/DinosaurApp.java @@ -1,5 +1,7 @@ package com.dinosaur.dinosaurexploder; + + import com.almasb.fxgl.app.GameApplication; import com.almasb.fxgl.app.GameSettings; import com.almasb.fxgl.dsl.FXGL; @@ -65,5 +67,6 @@ protected void initPhysics() { */ public static void main(String[] args) { launch(args); + } } diff --git a/src/main/java/com/dinosaur/dinosaurexploder/model/HighScore.java b/src/main/java/com/dinosaur/dinosaurexploder/model/HighScore.java new file mode 100644 index 0000000..0698478 --- /dev/null +++ b/src/main/java/com/dinosaur/dinosaurexploder/model/HighScore.java @@ -0,0 +1,14 @@ +package com.dinosaur.dinosaurexploder.model; +import java.io.*; +public class HighScore implements Serializable { + Integer high; + public HighScore(){ + this.high = 0; + } + public HighScore(Integer x){ + this.high = x; + } + public Integer getHigh(){ + return this.high; + } +} diff --git a/src/main/java/com/dinosaur/dinosaurexploder/model/LifeComponent.java b/src/main/java/com/dinosaur/dinosaurexploder/model/LifeComponent.java index 2bffdf7..973785f 100644 --- a/src/main/java/com/dinosaur/dinosaurexploder/model/LifeComponent.java +++ b/src/main/java/com/dinosaur/dinosaurexploder/model/LifeComponent.java @@ -7,12 +7,14 @@ import javafx.scene.text.Font; import javafx.scene.text.Text; +import java.io.ObjectOutputStream; +import java.io.*; /** * Summary : * This handles the life component of the Player implements the life interface and extends the Component */ public class LifeComponent extends Component implements Life { - + Integer life = 3; private Image heart = new Image(GameConstants.HEART_IMAGEPATH); @@ -42,6 +44,7 @@ public void onUpdate(double ptf) { test2.setLayoutX(test1.getLayoutX() + 30); test3.setLayoutX(test2.getLayoutX() + 30); // setting them for display + if (life == 3) { setEntity(test1); setEntity(test2); @@ -57,6 +60,7 @@ public void onUpdate(double ptf) { setEntity(test1); } else { + clearEntity(); } diff --git a/src/main/java/com/dinosaur/dinosaurexploder/model/ScoreComponent.java b/src/main/java/com/dinosaur/dinosaurexploder/model/ScoreComponent.java index 3074151..1a559b6 100644 --- a/src/main/java/com/dinosaur/dinosaurexploder/model/ScoreComponent.java +++ b/src/main/java/com/dinosaur/dinosaurexploder/model/ScoreComponent.java @@ -1,5 +1,8 @@ package com.dinosaur.dinosaurexploder.model; + +import java.io.*; + import com.almasb.fxgl.entity.component.Component; import javafx.scene.image.Image; import javafx.scene.image.ImageView; @@ -13,6 +16,8 @@ */ public class ScoreComponent extends Component implements Score{ Integer score = 0; + public static HighScore highScore = new HighScore(); + /** * Summary : * This method runs for every frame like a continues flow , without any stop until we put stop to it. @@ -21,19 +26,45 @@ public class ScoreComponent extends Component implements Score{ */ @Override public void onUpdate(double ptf) { + try + { + + FileInputStream file = new FileInputStream("highScore.ser"); + ObjectInputStream in = new ObjectInputStream(file); + + // Method for deserialization of object + highScore = (HighScore)in.readObject(); + + in.close(); + file.close(); + + + + } catch(IOException c) + { + highScore = new HighScore(); + } catch(ClassNotFoundException b) + { + System.out.println("ClassNotFoundException is caught"); + } entity.getViewComponent().clearChildren(); GridPane gridPane = new GridPane(); gridPane.setHgap(10); Text scoreText = new Text("Score: " + score.toString()); + Text highScoreText = new Text("HighScore: " + highScore.getHigh().toString()); Image image = new Image(GameConstants.GREENDINO_IMAGEPATH,25,20,false, false); ImageView imageView = new ImageView(); imageView.setImage(image); scoreText.setFill(Color.GREEN); scoreText.setFont(Font.font(GameConstants.ARCADECLASSIC_FONTNAME, 20)); + highScoreText.setFill(Color.GREEN); + highScoreText.setFont(Font.font(GameConstants.ARCADECLASSIC_FONTNAME, 20)); gridPane.add(scoreText,1, 0); + gridPane.add(highScoreText,1, 1); gridPane.add(imageView,2, 0); entity.getViewComponent().addChild(gridPane); + } /** @@ -57,8 +88,16 @@ public void setScore(int i) { * This method is overriding the superclass method to increment the Score to the current Score */ @Override - public void incrementScore(int i) { + public void incrementScore(int i){ score += i; + if(score > highScore.getHigh()){ highScore = new HighScore(score); + try{FileOutputStream fileOut = new FileOutputStream("highScore.ser"); + ObjectOutputStream out = new ObjectOutputStream(fileOut); + out.writeObject(highScore); + out.close(); + fileOut.close();} catch (IOException e){ + e.printStackTrace(); + }} }