Scoring System Implementation Explanation Sprint 2 - UQdeco2800/2021-ext-studio-2 GitHub Wiki

Overview

Although we completed the whole feature of scoring system in sprint 1. In sprint 2 we did some design updates as well as programming updates for it. The design updates are here: MainGameScreen Clock: https://github.com/UQdeco2800/2021-ext-studio-2/wiki/Scoreboard MainGameScreen Scoreboard: https://github.com/UQdeco2800/2021-ext-studio-2/wiki/Clock

Programming Updates

ScoringSystemV1

We changed the getScore() method from getScore(int...arg) to getScore() which returns the global variable score. This is more clear and convenient to use.

We also added a new method called addToScore(int bonus). Bonus points are achievement scores and bag system scores. This method add bonus points to global variable score.

The changes I just introduced were made in the first week of sprint2. We came up a better solution to maintain this feature. Using ServiceLocator.

ScoreService

In the com/deco2800/game/services package. We created a new ScoreService.java class. The function of this class bascially the same as ScoringSystemV1.java but much simpler than that. The ScoreService has the following advantages:

  1. It counts the time using GameTime.java in the engine without using Timer and TimerTask in sprint1.
  2. The class is very short and simple. It is easy to understand. Hence, more easily to maintain the scoring system.
/** controls the game score */
public class ScoreService {
    private static Logger logger = LoggerFactory.getLogger(ScoreService.class);
    private int bonus;
    private GameTime gametime;

    /**
     * Constructor of ScoreService. Initialise a new gametime
     */
    public ScoreService() {
        gametime = new GameTime();
        bonus = 0;
    }

    /**
     * add bonus score to scores
     * @param bonus the bonus score from other feature
     */
    public void addToScore(int bonus) {
        this.bonus += bonus;
    }

    /**
     * returns base score + bonus
     * @return score
     */
    public int getScore() {
        return getBaseScore() + bonus;
    }

    /** give score in seconds */
    private int getBaseScore() {
        return (int) gametime.getTime()/1000;
    }

How to use ScoreService

In the ServiceLocator.java we implement this method so that ScoreService can be accessed in this way: ServiceLocator.getScoreService.addToScore(bonus) and ServiceLocator.getScoreService.getScore()

  public static ScoreService getScoreService() {
    return scoreService;
  }

UML illustration

  1. How to use GameTime to count the seconds for score.
  2. How does other classes use getScore().
  3. How does bag system update score.