Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Observerbranch #39

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/bejeweled/game/GameLogic.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package bejeweled.game;

import java.util.Observable;
import java.util.Observer;

import javafx.scene.canvas.GraphicsContext;
import bejeweled.Sounds;
import bejeweled.board.Board;
Expand All @@ -12,7 +15,7 @@
* @author Timo
*
*/
public final class GameLogic {
public final class GameLogic implements Observer {

private Board board;
private static Time time;
Expand Down Expand Up @@ -142,4 +145,8 @@ public Score getScore() {
public void setScore(int s) {
score.setScore(s);
}

public void update(Observable obs, Object arg) {
score = (Score) obs;
}
}
1 change: 1 addition & 0 deletions src/bejeweled/game/GameScene.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ public void handle(ActionEvent e) {
gc = canvas.getGraphicsContext2D();
gc.setFont(new Font("Helvetica", 15));
gamelogic = new GameLogic();
gamelogic.getScore().addObserver(gamelogic);

// this will handle mouse clicks
this.setOnMousePressed(new EventHandler<MouseEvent>() {
Expand Down
9 changes: 7 additions & 2 deletions src/bejeweled/state/Score.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package bejeweled.state;

import java.util.Observable;

import bejeweled.Main;

/**
* Score class
* @author Job
*
*/
public final class Score {
public final class Score extends Observable {

private int score;
private int scorePerGem;
Expand All @@ -27,7 +29,8 @@ public void updateScore(int gems) {
if (gems*scorePerGem > goodscore) {
Main.shoutOut();
}

setChanged();
notifyObservers();
}

public int getScore() {
Expand All @@ -36,6 +39,8 @@ public int getScore() {

public void setScore(int s) {
score = s;
setChanged();
notifyObservers();
}

}