Skip to content

Commit

Permalink
working on screen layers.
Browse files Browse the repository at this point in the history
  • Loading branch information
asalga committed Aug 6, 2013
1 parent ffeb985 commit 9b23ea2
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 24 deletions.
26 changes: 17 additions & 9 deletions HUDLayer.pde
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
public class HUDLayer implements Layer{
/*
Shows the score, level on top of the gameplay screen.
*/
public class HUDLayer implements LayerObserver{

RetroLabel scoreLabel;
RetroFont solarWindsFont;
int score;
public HUDLayer(){
score = 0;
ScreenGameplay screenGameplay;

public HUDLayer(ScreenGameplay s){
screenGameplay = s;

solarWindsFont = new RetroFont("data/fonts/solarwinds.png", 7, 8, 1);

Expand All @@ -14,17 +17,22 @@ public class HUDLayer implements Layer{
scoreLabel.setVerticalSpacing(0);
scoreLabel.setHorizontalTrimming(true);
scoreLabel.pixelsFromTopLeft(5, 5);

}

public void draw(){
pushMatrix();
scale(3);
scoreLabel.draw();
popMatrix();
}

//
public void notifyObserver(){
String scoreStr = Utils.prependStringWithString("" + screenGameplay.getScore(), "0", 8);
scoreLabel.setText("Score:" + scoreStr);
}

public void update(){
score += frameCount/150;

scoreLabel.setText("Score: " + score);
}

public void setZIndex(int zIndex){
Expand Down
9 changes: 8 additions & 1 deletion Horadrix.pde
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,14 @@ void update(){
currScreen.update();

if(currScreen.getName() == "splash" && currScreen.isAlive() == false){
currScreen = new ScreenGameplay();
ScreenGameplay gameplay = new ScreenGameplay();

currScreen = gameplay;

LayerObserver hudLayer = new HUDLayer(gameplay);
gameplay.addObserver(hudLayer);

//BKLayer bkLayer = new BKLayer();
}
}

Expand Down
7 changes: 5 additions & 2 deletions Layer.pde → LayerObserver.pde
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@
A screen can have many layers associated with it. Layers are rendered
from smaller indices to larger.
*/
public interface Layer{
public interface LayerObserver{
public void draw();
public void update();
public void setZIndex(int zIndex);
// public void setZIndex(int zIndex);

//
public void notifyObserver();
}
49 changes: 40 additions & 9 deletions ScreenGameplay.pde
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
*/
public class ScreenGameplay implements IScreen{
public class ScreenGameplay implements IScreen, Subject{

// Tokens that have been remove from the board, but still need to be rendered for their
// death animation.
ArrayList<Token> dyingTokens;

ArrayList<Layer> layers;
ArrayList<LayerObserver> layerObserver;

// When a match is created, the matched tokens are removed from the board array
// and 'float' above the board and drop down until they arrive where they need to go.
Expand Down Expand Up @@ -73,6 +73,23 @@ public class ScreenGameplay implements IScreen{

int score = 0;

public void addObserver(LayerObserver o){
layerObserver.add(o);

// recalculate indices
}

public void removeObserver(LayerObserver o){
// recalc
}

public void notifyObservers(){
for(int i = 0; i < layerObserver.size(); i++){
layerObserver.get(i).notifyObserver();
}
}


/**
*/
ScreenGameplay(){
Expand All @@ -83,11 +100,8 @@ public class ScreenGameplay implements IScreen{
floatingTokens = new ArrayList<Token>();
dyingTokens = new ArrayList<Token>();


//
layers = new ArrayList<Layer>();

layers.add( new HUDLayer());
layerObserver = new ArrayList<LayerObserver>();

/*Layer bkLayer = new BackgroundLayer();
layers.add(bkLayer);
Expand Down Expand Up @@ -164,15 +178,17 @@ public class ScreenGameplay implements IScreen{
popStyle();
popMatrix();

layers.get(0).draw();
if(layerObserver != null){
layerObserver.get(0).draw();
}

debug.draw();
}

/**
*/
public void update(){
layers.get(0).update();
//layers.get(0).update();

// Once the player meets their quota...
if(gemCounter >= gemsRequiredForLevel){
Expand Down Expand Up @@ -328,7 +344,7 @@ public class ScreenGameplay implements IScreen{
//pushMatrix();
resetMatrix();
//debug.addString("debug time: " + debugTicker.getTotalTime());
debug.addString("score: " + score);
debug.addString("");// + score);
debug.addString("Level: " + currLevel);
debug.addString("destroyed: " + tokensDestroyed);
debug.addString("FPS: " + frameRate);
Expand Down Expand Up @@ -464,11 +480,26 @@ public class ScreenGameplay implements IScreen{
}
}



public int getScore(){
return score;
}



public void addToScore(int offset){
score += offset;
notifyObservers();
}

/*
*
*/
void animateSwapTokens(Token t1, Token t2){

addToScore(99);

// We need to cache these so we get get the wrong
// values when calling animateTo.
int t1Row = t1.getRow();
Expand Down
7 changes: 7 additions & 0 deletions Subject.pde
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/*
*/
public interface Subject{
public void addObserver(LayerObserver o);
public void removeObserver(LayerObserver o);
public void notifyObservers();
}
3 changes: 1 addition & 2 deletions Ticker.pde
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ public class Ticker{
}
}

public void setTime(int min, int sec){

public void setTime(int min, int sec){
totalTime = min * 60 + sec;
}

Expand Down
2 changes: 1 addition & 1 deletion Utils.pde
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public static class Utils{
}

/**
* Mostly used for adding zeros to number in string format, but general enough to be
* Mostly used for adding zeros to a number in string format, but general enough to be
* used for other purposes.
*/
public static String prependStringWithString(String baseString, String prefix, int newStrLength){
Expand Down

0 comments on commit 9b23ea2

Please sign in to comment.