i'm making a game using javaFX and im using memento design pattern to save and load the game. The game overrides the current memento if you save again. Don't need to store it anywhere. My issue is when I load the saved state everything loads properly but my total score and time doesn't load back to the saved state. When I was debugging the value was set correctly but it didnt display correctly. Any suggestions on how I resolve this.
public class Memento {
private Level newLevel;
private List<Entity> entities = new ArrayList<>();
private List<MovingEntity> movingEntities = new ArrayList<>();
private List<Interactable> interactables = new ArrayList<>();
private GameEngine gameEngine;
private String source;
private double height;
private double width;
private double floorHeight;
private String size;
private int lives;
private double bestTime;
private int currentScore;
private int totalScore;
private long time;
private double xPos;
private double yPos;
private int levelId;
private boolean mushroomContact = true;
private int mushroomCtr = 0;
private StickMan stickMan;
public Memento(Level level, GameEngine gameEngine, int levelId) {
this.gameEngine = gameEngine;
this.source = level.getSource();
this.height = level.getHeight();
this.width = level.getWidth();
this.floorHeight = level.getFloorHeight();
this.size = level.getHeroSize();
this.lives = gameEngine.getLives();
this.bestTime = level.getBestTime();
this.currentScore = level.getCurrentScore();
this.totalScore = gameEngine.getTotalScore();
this.time = gameEngine.getCurrentTime();
this.levelId = levelId;
stickMan = (StickMan) level.getHero();
for(Entity entity: level.getEntities()){
if(entity instanceof Slime){
Slime slime = new Slime(entity.getImagePath(), entity.getXPos(), entity.getYPos(),((Slime) entity).getLeft(), ((Slime) entity).getStrategy());
entities.add(slime);
movingEntities.add(slime);
interactables.add(slime);
}else if(entity instanceof Projectile){
Projectile bullet = new Bullet(entity.getXPos(), entity.getYPos(), ((Projectile) entity).getLeft());
entities.add(bullet);
movingEntities.add(bullet);
}else if(entity instanceof Flag){
Flag flag = new Flag(entity.getXPos(), entity.getYPos());
entities.add(flag);
interactables.add(flag);
}else if(entity instanceof Mushroom){
if(stickMan.checkCollide(entity)){
mushroomContact = true;
}else{
mushroomContact = false;
}
Mushroom mushroom = new Mushroom(entity.getXPos(),entity.getYPos());
entities.add(mushroom);
interactables.add(mushroom);
}else if(entity instanceof Platform){
Platform platform = new Platform(entity.getXPos(),entity.getYPos());
entities.add(platform);
}else if(entity instanceof Win){
Win win = new Win(entity.getXPos(), entity.getYPos() - 300);
entities.add(win);
}else if(entity instanceof StickMan){
xPos = entity.getXPos();
yPos = entity.getYPos();
}
}
this.newLevel = new LevelManager(this.gameEngine, source,height,width, floorHeight,xPos,size, lives ,
bestTime,entities,movingEntities, interactables);
}
public Level getLevel() {
this.gameEngine.setTotalScore(totalScore);
this.gameEngine.setTime((int)time);
this.gameEngine.setLevelId(levelId);
this.newLevel.getHero().setXPos(xPos);
this.newLevel.getHero().setYPos(yPos);
this.newLevel.setCurrentScore(currentScore);
//check if stickman was in contact with mushroom before saving
if(mushroomContact){
this.newLevel.getHero().upgrade();
}
return newLevel;
}
}
private void totalScore(){
totalScoreText.setText("Total Score: " + model.getTotalScore());
totalScoreText.setFont(Font.font ("Times New Roman", FontWeight.BOLD, 15));
totalScoreText.setFill(Paint.valueOf("BLACK"));
totalScoreText.setLayoutX(currentScoreText.getLayoutX());
totalScoreText.setLayoutY(currentScoreText.getLayoutY() + 25);
totalScoreText.setTextAlignment(TextAlignment.LEFT);
}
private void displayCurrentTime(){
currentTimeText.setText("Current time: " + model.getCurrentTime() + " sec");
currentTimeText.setFont(Font.font ("Times New Roman", FontWeight.BOLD, 15));
currentTimeText.setFill(Paint.valueOf("BLACK"));
currentTimeText.setLayoutX(bestTimeText.getLayoutX());
currentTimeText.setLayoutY(bestTimeText.getLayoutY() + 25);
currentTimeText.setTextAlignment(TextAlignment.LEFT);
}
Aucun commentaire:
Enregistrer un commentaire