diff --git a/Lib/src/com/yarcat/chemistrylines/game/GameLogic.java b/Lib/src/com/yarcat/chemistrylines/game/GameLogic.java index 7e3dc32..a7a7fd0 100644 --- a/Lib/src/com/yarcat/chemistrylines/game/GameLogic.java +++ b/Lib/src/com/yarcat/chemistrylines/game/GameLogic.java @@ -28,8 +28,8 @@ public interface GameListener { /** Called on change on the game field */ public void onFieldChange(GameLogic game); - /** Called on change on the game field */ - public void onScoreChange(GameLogic game); + /** Called once before game starts. */ + public void onInit(GameLogic game); } /** @@ -70,4 +70,6 @@ public interface GameListener { public Scorer getScorer(); public void setChangeListener(GameListener listener); + + public void init(); } diff --git a/Lib/src/com/yarcat/chemistrylines/game/LinesGame.java b/Lib/src/com/yarcat/chemistrylines/game/LinesGame.java index 86882e6..268b282 100644 --- a/Lib/src/com/yarcat/chemistrylines/game/LinesGame.java +++ b/Lib/src/com/yarcat/chemistrylines/game/LinesGame.java @@ -20,7 +20,7 @@ public abstract class LinesGame implements GameLogic { private final int mNewPortionSize; private final CompoundScanner mScanner; private final Scorer mScorer; - private GameListener mChangeListener; + private GameListener mGameListener; private FieldCleaner mFieldCleaner; private GameLogger mGameLog; @@ -93,7 +93,7 @@ private void addMoreItems() { } private void onFieldChange() { - mChangeListener.onFieldChange(this); + mGameListener.onFieldChange(this); } private void onElementsAdded(int[] addedCells) { @@ -124,7 +124,7 @@ public FieldCleaner getFieldCleaner() { @Override public void foundCompound(CompoundReference ref) { mGameLog.compoundRemoved(mField, ref); - updateScore(ref); + mScorer.update(ref); } }; @@ -141,7 +141,7 @@ public Field getField() { @Override public void setChangeListener(GameListener listener) { - mChangeListener = listener; + mGameListener = listener; } @Override @@ -149,8 +149,10 @@ public Scorer getScorer() { return mScorer; } - private void updateScore(CompoundReference ref) { - mScorer.update(ref); - mChangeListener.onScoreChange(this); + @Override + public void init() { + addItems(); + mScorer.init(); + mGameListener.onInit(this); } } diff --git a/Lib/src/com/yarcat/chemistrylines/game/Scorer.java b/Lib/src/com/yarcat/chemistrylines/game/Scorer.java index 3f1187d..4563c89 100644 --- a/Lib/src/com/yarcat/chemistrylines/game/Scorer.java +++ b/Lib/src/com/yarcat/chemistrylines/game/Scorer.java @@ -13,7 +13,35 @@ public interface Scorer { public void update(CompoundReference ref); - abstract class BaseInt implements Scorer { + public void init(); + + public interface ScoreListener { + public void onScoreChange(Scorer scorer); + } + + public void setScoreListener(ScoreListener l); + + abstract class Base implements Scorer { + ScoreListener mListener; + + @Override + public void setScoreListener(ScoreListener l) { + mListener = l; + } + + void onScoreChange() { + if (mListener != null) { + mListener.onScoreChange(this); + } + } + + @Override + public void init() { + onScoreChange(); + } + } + + abstract class BaseInt extends Base { int mScore; public BaseInt() { @@ -26,7 +54,7 @@ public String get() { } } - abstract class BaseFloat implements Scorer { + abstract class BaseFloat extends Base { float mScore; public BaseFloat() { @@ -45,6 +73,7 @@ class AtomScore extends BaseInt { @Override public void update(CompoundReference ref) { mScore += ref.getCompound().atomCount(); + onScoreChange(); } } @@ -54,6 +83,7 @@ class AtomExpScore extends BaseInt { public void update(CompoundReference ref) { int n = ref.getCompound().atomCount(); mScore += n < 4 ? 1 : 1 << (n - 3); + onScoreChange(); } } @@ -62,10 +92,11 @@ class AtomicWeightScore extends BaseFloat { @Override public void update(CompoundReference ref) { mScore += ref.getCompound().atomicWeight(); + onScoreChange(); } } - class ScoreContainer implements Scorer { + class ScoreContainer extends Base { Map mContents; @@ -78,18 +109,21 @@ public ScoreContainer() { @Override public String get() { - String r=""; - for (String name: mContents.keySet()) { - r = String.format("%s%s\t%s\n", r, name, mContents.get(name).get()); + String r = ""; + for (String name : mContents.keySet()) { + r = + String.format("%s%s\t%s\n", r, name, mContents + .get(name).get()); } return r; } @Override public void update(CompoundReference ref) { - for(Scorer s : mContents.values()) { + for (Scorer s : mContents.values()) { s.update(ref); } + onScoreChange(); } } } diff --git a/Swing/src/com/yarcat/chemistrylines/swing/SwingChemistryLines.java b/Swing/src/com/yarcat/chemistrylines/swing/SwingChemistryLines.java index 1037c55..c8b931f 100644 --- a/Swing/src/com/yarcat/chemistrylines/swing/SwingChemistryLines.java +++ b/Swing/src/com/yarcat/chemistrylines/swing/SwingChemistryLines.java @@ -3,29 +3,25 @@ import java.awt.event.MouseEvent; import java.awt.event.MouseListener; -import javax.swing.JTextArea; - import com.yarcat.chemistrylines.game.GameLogic; import com.yarcat.chemistrylines.game.GameLogic.GameListener; import com.yarcat.chemistrylines.game.GameLogic.InvalidMove; import com.yarcat.chemistrylines.swing.SwingField.FieldButton; import com.yarcat.chemistrylines.view.SelectionInView; -class SwingChemistryLines implements MouseListener, GameListener { +class SwingChemistryLines implements GameListener, MouseListener { private final SwingField mFieldUI; private final GameLogic mGame; - private final JTextArea mScoreUI; private SwingPreview mPreviewUI; private DefferedCleanerUI mCleanerUI; public SwingChemistryLines(GameLogic game, SwingField fieldUI, - SwingPreview previewUI, JTextArea scoreUI) { + SwingPreview previewUI) { mFieldUI = fieldUI; mGame = game; mPreviewUI = previewUI; mCleanerUI = null; - mScoreUI = scoreUI; } void refresh() { @@ -34,12 +30,6 @@ void refresh() { if (mCleanerUI != null) { mCleanerUI.refresh(); } - refreshScore(); - } - - private void refreshScore() { - mScoreUI.setText(""); - mScoreUI.append(mGame.getScorer().get()); } private void refreshField() { @@ -121,7 +111,7 @@ public void onFieldChange(GameLogic game) { } @Override - public void onScoreChange(GameLogic game) { - refreshScore(); + public void onInit(GameLogic game) { + refresh(); } } diff --git a/Swing/src/com/yarcat/chemistrylines/swing/SwingScore.java b/Swing/src/com/yarcat/chemistrylines/swing/SwingScore.java new file mode 100644 index 0000000..508698d --- /dev/null +++ b/Swing/src/com/yarcat/chemistrylines/swing/SwingScore.java @@ -0,0 +1,20 @@ +package com.yarcat.chemistrylines.swing; + +import javax.swing.JTextArea; + +import com.yarcat.chemistrylines.game.Scorer; +import com.yarcat.chemistrylines.game.Scorer.ScoreListener; + +public class SwingScore implements ScoreListener { + public final JTextArea textArea; + + public SwingScore() { + textArea = new JTextArea(); + } + + @Override + public void onScoreChange(Scorer scorer) { + textArea.setText(""); + textArea.append(scorer.get()); + } +} diff --git a/Swing/src/com/yarcat/chemistrylines/swing/SwingUIFactory.java b/Swing/src/com/yarcat/chemistrylines/swing/SwingUIFactory.java index 661d72f..925f1b4 100644 --- a/Swing/src/com/yarcat/chemistrylines/swing/SwingUIFactory.java +++ b/Swing/src/com/yarcat/chemistrylines/swing/SwingUIFactory.java @@ -49,7 +49,6 @@ class Builder { SwingPreview mPreviewUI; PreviewButton[] mPreview; SwingChemistryLines mGameUI; - JTextArea mScoreUI; public SwingChemistryLines newInstance() { Field field = new RectField(mCols, mRows); @@ -59,9 +58,7 @@ public SwingChemistryLines newInstance() { mGame = mGameFactory.newInstance(field); mFieldUI = new SwingField(mGame, mButtons); mPreviewUI = new SwingPreview(mGame, mPreview); - mScoreUI = new JTextArea(); - mGameUI = - new SwingChemistryLines(mGame, mFieldUI, mPreviewUI, mScoreUI); + mGameUI = new SwingChemistryLines(mGame, mFieldUI, mPreviewUI); mGame.setChangeListener(mGameUI); JFrame f = @@ -76,8 +73,7 @@ public SwingChemistryLines newInstance() { f.pack(); f.setVisible(true); - mGame.addItems(); - mGameUI.refresh(); + mGame.init(); return mGameUI; } @@ -134,6 +130,7 @@ protected Container createPreviewPane(PreviewButton[] preview) { private Component createScorePane() { Panel p = new Panel(); p.add(style.defaultColor(new JLabel("Score:"))); + JTextArea mScoreUI = createScoreUI().textArea; p.add(mScoreUI); style.frame(mScoreUI); mScoreUI.setEditable(false); @@ -144,6 +141,12 @@ private Component createScorePane() { return p; } + private SwingScore createScoreUI() { + SwingScore r = new SwingScore(); + mGame.getScorer().setScoreListener(r); + return r; + } + protected Container createRightPane() { Panel p = new Panel(); style.defaultColor(p);