Permalink
Browse files

added observer pattern to gameview and levelbar and updating progress

  • Loading branch information...
Patys committed Mar 18, 2017
1 parent 03d707a commit 8443f31eb1235b369544b8f5abc9685f5af3b5bf
@@ -2,20 +2,30 @@
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.Touchable;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;
import com.patys.llgame.Card;
import com.patys.llgame.MetaGame;
public class GameView extends Table{
public class GameView extends Subject {
private DefaultSkin skin;
private Table table;
private Card currentCard;
public GameView() {
skin = new DefaultSkin();
table = new Table();
updateGame();
}
public void startGame() {
public void updateGame() {
table.clearChildren();
Card mainCard = MetaGame.cardManager.getRandomCard();
Card card1 = MetaGame.cardManager.getCardByWord(mainCard.word);
@@ -29,10 +39,10 @@ public void startGame() {
card3 = MetaGame.cardManager.getRandomCard();
}
this.add();
this.add(getPreparedCardWithWord(mainCard)).space(5).center();
this.row();
addCardsToTableInRandomWay(this, card1, card2, card3);
table.add();
table.add(getPreparedCardWithWord(mainCard)).space(5).center();
table.row();
addCardsToTableInRandomWay(table, card1, card2, card3);
}
private Table getPreparedCardWithWord(Card card) {
@@ -42,7 +52,8 @@ private Table getPreparedCardWithWord(Card card) {
Label meaning = new Label(card.word, skin);
graphicalCard.add(meaning).spaceBottom(50).center();
currentCard = card;
return graphicalCard;
}
@@ -60,9 +71,39 @@ private Table getPreparedCardWithMeaning(Card card) {
graphicalCard.row();
graphicalCard.add(wrong).left().padLeft(15).expandX();
graphicalCard.setTouchable(Touchable.enabled);
graphicalCard.addListener(addListener(card));
return graphicalCard;
}
private ClickListener addListener(final Card card) {
return new ClickListener(){
@Override
public void clicked(InputEvent event, float x, float y) {
checkAnswer(currentCard, card);
}
};
}
private void checkAnswer(Card word, Card meaning) {
if(word != null && meaning != null) {
if(word.meaning.equals(meaning.meaning)) {
MetaGame.cardManager.getCardByWord(word.word).good += 1;
MetaGame.experience += 10;
MetaGame.points += 1;
}
else {
MetaGame.cardManager.getCardByWord(word.word).wrong += 1;
if(MetaGame.points > 0) {
MetaGame.points -= 1;
}
}
this.setState(1);
updateGame();
}
}
private void addCardsToTableInRandomWay(Table table, Card card1, Card card2, Card card3) {
// simple randomize
Boolean addedCardOne = false;
@@ -74,22 +115,30 @@ private void addCardsToTableInRandomWay(Table table, Card card1, Card card2, Car
case 1:
if(!addedCardOne){
addedCardOne = true;
this.add(getPreparedCardWithMeaning(card1)).space(5).left();
table.add(getPreparedCardWithMeaning(card1)).space(5).left();
}
break;
case 2:
if(!addedCardTwo) {
addedCardTwo = true;
this.add(getPreparedCardWithMeaning(card2)).space(5).left();
table.add(getPreparedCardWithMeaning(card2)).space(5).left();
}
break;
case 3:
if(!addedCardThree) {
addedCardThree = true;
this.add(getPreparedCardWithMeaning(card3)).space(5).left();
table.add(getPreparedCardWithMeaning(card3)).space(5).left();
}
break;
}
}
}
public Table getTable() {
return table;
}
public void setTable(Table table) {
this.table = table;
}
}
@@ -6,24 +6,53 @@
import com.badlogic.gdx.utils.Align;
import com.patys.llgame.MetaGame;
public class LevelBar extends Table {
public class LevelBar extends Observer {
private DefaultSkin skin;
private Table table;
public LevelBar() {
public LevelBar(Subject subject) {
skin = new DefaultSkin();
table = new Table();
this.subject = subject;
this.subject.attach(this);
updateTable();
}
private void updateTable() {
table.clearChildren();
Label level = new Label("Poziom: " + Integer.toString(MetaGame.level), skin);
Label experience = new Label("Doœwiadczenie: " + Integer.toString(MetaGame.experience), skin);
Label points = new Label("Punkty: " + Integer.toString(MetaGame.points), skin);
int averge = (int) ((level.getWidth() + experience.getWidth())/2);
int space = Gdx.graphics.getWidth() - averge*3;
this.add(level).expandX().fill().align(Align.left);
this.add().spaceRight(space);
this.add(experience).expandX().fill().align(Align.right);
this.row();
this.add(points).expandX().fill().align(Align.right);
table.add(level).expandX().fill().align(Align.left);
table.add().spaceRight(space);
table.add(experience).expandX().fill().align(Align.right);
table.row();
table.add(points).expandX().fill().align(Align.right);
}
private void updateLevel() {
if(MetaGame.experience > ((MetaGame.level + 1)*20)) {
MetaGame.level += 1;
MetaGame.experience = 0;
}
}
public Table getTable() {
return table;
}
public void setTable(Table table) {
this.table = table;
}
@Override
public void update() {
updateLevel();
updateTable();
}
}
@@ -12,9 +12,7 @@
private TextButton exitButton;
private DefaultSkin skin;
private GameView gameView;
public MainView() {
public MainView(final UserInterface userInterface) {
skin = new DefaultSkin();
// Configure a TextButtonStyle and name it "default". Skin resources are stored by type, so this doesn't overwrite the font.
@@ -33,23 +31,19 @@ public MainView() {
playButton.padBottom(5);
exitButton = new TextButton("Wyjœcie", skin);
gameView = new GameView();
this.add(playButton).space(10);
this.row();
this.add(exitButton);
playButton.addListener(new ChangeListener() {
@Override
public void changed (ChangeEvent event, Actor actor) {
createGame();
createGame(userInterface);
}
});
}
private void createGame() {
this.reset();
gameView.startGame();
this.add(gameView);
private void createGame(UserInterface userInterface) {
userInterface.startGame();
}
}
@@ -0,0 +1,6 @@
package com.patys.llgame.UserInterface;
public abstract class Observer {
protected Subject subject;
public abstract void update();
}
@@ -0,0 +1,28 @@
package com.patys.llgame.UserInterface;
import java.util.ArrayList;
import java.util.List;
public abstract class Subject {
private List<Observer> observers = new ArrayList<Observer>();
private int state;
public int getState() {
return state;
}
public void setState(int state) {
this.state = state;
notifyAllObservers();
}
public void attach(Observer observer) {
observers.add(observer);
}
public void notifyAllObservers() {
for (Observer observer : observers) {
observer.update();
}
}
}
@@ -5,10 +5,26 @@
public class UserInterface {
private Table table;
private LevelBar levelBar;
private GameView gameView;
private MainView mainView;
public UserInterface(Table table) {
gameView = new GameView();
levelBar = new LevelBar(gameView);
mainView = new MainView(this);
this.table = table;
this.table.add(new LevelBar()).top().spaceBottom(20);
this.table.add(levelBar.getTable());
this.table.row();
this.table.add(new MainView()).expandX();
this.table.add(mainView).expandX();
}
public void startGame() {
table.clearChildren();
table.add(levelBar.getTable());
table.row();
table.add(gameView.getTable());
}
}

0 comments on commit 8443f31

Please sign in to comment.