You are on page 1of 4

/**

* This Observer interface is the observer part of the Observer pattern.


* @author Robin Kadergran
*/
public interface Observer {
/**
* The override method for the observer pattern.
*/
default void update(){}
}

/**
* This Subject interface is the subject part of the Observer pattern.
* @author Robin Kadergran
*/
public interface Subject {

/**
* This override method registers the observer to the list.
* @param o Observer parameter.
*/
void register(Observer o);

/**
* This override method unregistered the observer to the list.
* @param o Observer parameter.
*/
void unRegister(Observer o);

/**
* This override method notifies all the observers in the list.
*/
void notifyObserver();
}
Observer in ”action”:

/**
* This subclass contains graphics, images and hitbox linked to the bass
character.
* @author Robin Kadergran
*/
public class Boss extends BaseCharacter implements Observer {
/**
* This constructor adds the Boss class to the observerList in
GameSettings.
* @param gameSetting class parameter.
*/
public Boss(GameSetting gameSetting) {
gameSetting.register(this);
}

/**
* This method overrides the superclass BaseCharacter and provides data to
the observer pattern to the viewer.
*/
@Override
public void update(){
LoadAnimations();
ImportEnemyDeathImg();
bossHitBox();
}

}
Subject in “action”:

/**
* The controller part of the MVC pattern and the subject part of the
Observer pattern.
* @author Robin Kadergran
*/
public class GameSetting implements Runnable, Subject {
/**
* This override method registers the observer to the observerList.
* @param newObserver Observer object.
*/
@Override
public void register(Observer newObserver) {
observerList.add(newObserver);
}

/**
* This override method is for future addition when enemies die and get
unregistered from the observerList.
* @param removeObserver Observer object.
*/
@Override
public void unRegister(Observer removeObserver) {
int observerIndex = observerList.indexOf(removeObserver);
System.out.println("Character " + (observerIndex + 1) + " is died and
looted!");
observerList.remove(observerIndex);
}

/**
* This override method notifies all the observers in the observerlist.
*/
@Override
public void notifyObserver() {
for (Observer observer: observerList){
observer.update();
}
}
/**
* The override run() method of the StartGameLoop()'s fixedThreadPool and
uses the notifyObserver()
* from the Observer pattern.
*/
@Override
public void run() {
long lastFrame = System.nanoTime();

while(true){
double timePerFrame = 1000000000.0 / FPS_SET;

long now = System.nanoTime();


if (now - lastFrame >= timePerFrame){
gamePanel.repaint();
lastFrame = now;
frames++;
}

if (System.currentTimeMillis() - lastcheck >= 1000){


lastcheck = System.currentTimeMillis();
notifyObserver();
frames = 0;
}
}
}

You might also like