Saving‐Loading Feature - UQcsse3200/2024-studio-1 GitHub Wiki

Introduction

The Saving-Loading feature provides users the ability to save progress of the current game and continue playing at another time in the future.

Overview

When user clicks on the "Save" button in the pause menu, the game will be saved into a JSON and can be loaded when needed.

Screenshot 2024-09-10 at 23 46 53

Technical Information

The "Save" button and save game functionality are saved in MainGameExitDisplay.java.

The newly-added EntityCoordinates class is used to save the coordinates of the enitities in the map.

public class EntityCoordinates {
    private float x;
    private float y;

    public EntityCoordinates(float x, float y) {
      this.x = x;
      this.y = y;
    }

    public float getX() {
      return x;
    }

    public float getY() {
      return y;
    }
  }

All entity coordinates will be added into the entities array as in the loop below. Then the coordinates in the array will be added to the save.json file located in the configs folder.

public void saveGame() {
    Array<EntityCoordinates> entities = new Array<>();
    for (Entity entity : ServiceLocator.getEntityService().getEntities()) {
       Vector2 pos = entity.getPosition();
       float x = pos.x;
       float y = pos.y;
       EntityCoordinates coordinates = new EntityCoordinates(x, y);
       entities.add(coordinates);
    }
    String filePath = "configs/save.json";
    FileLoader.writeClass(entities, filePath, FileLoader.Location.LOCAL);
    logger.debug("Game saved to: " + filePath);
  }