From f266f1167830b543fbcdf2bdf77a82a8ca3f1349 Mon Sep 17 00:00:00 2001 From: Andor Salga Date: Sun, 17 Nov 2013 12:59:28 -0500 Subject: [PATCH] refactoring. --- AssetStore.java | 4 --- FPSTimer.pde | 21 --------------- Horadrix.pde | 13 ++++++---- RetroFont.pde | 4 +++ RetroLabel.pde | 19 ++++++++++---- ScreenGameplay.pde | 64 +++++++++++++++++++--------------------------- ScreenSplash.pde | 3 ++- ScreenStory.pde | 62 ++++++++++++++++++++++++++++++++++++++++++++ Token.pde | 15 ++++++----- 9 files changed, 126 insertions(+), 79 deletions(-) delete mode 100644 FPSTimer.pde create mode 100644 ScreenStory.pde diff --git a/AssetStore.java b/AssetStore.java index 413597d..8cbf5f7 100644 --- a/AssetStore.java +++ b/AssetStore.java @@ -11,8 +11,6 @@ public class AssetStore{ private String BASE_IMG_PATH = "data/images/gems/diablo/"; private PImage[] images; private String[] imageNames = { "A.png","B.png", "C.png", "D.png", "E.png", "F.png", "G.png"}; - //"red_gem.gif", "green_gem.gif", "blue_gem.gif", "yellow_gem.gif", "white_gem.gif", "skull_gem.gif", "purple_gem.gif"}; - //"red_normal.gif", "green_normal.gif", "blue_normal.gif", "yellow_normal.gif", "white_normal.gif", "skull_normal.gif", "purple_normal.gif"}; /* */ @@ -20,8 +18,6 @@ public PImage get(int asset){ return images[asset]; } - //public PImage get(String asset){} - /* As soon as this is contructed, load all the assets */ private AssetStore(){ diff --git a/FPSTimer.pde b/FPSTimer.pde deleted file mode 100644 index b9df80f..0000000 --- a/FPSTimer.pde +++ /dev/null @@ -1,21 +0,0 @@ -/* -*/ -public static class FPSTimer{ - - private static float resolution; - private static int fps; - - //private static Ticker ticker = new Horadrix.Ticker(); - - public void setResolution(float res){ - if(res >= 0){ - resolution = res; - } - } - - /* - */ - public int getFPS(){ - return 42;//globalApplet.frameRate; - } -} diff --git a/Horadrix.pde b/Horadrix.pde index 0fe402f..8ab1a93 100644 --- a/Horadrix.pde +++ b/Horadrix.pde @@ -11,8 +11,8 @@ import ddf.minim.*; final boolean DEBUG_CONSOLE_ON = false; -final boolean DEBUG_ON = true; -final boolean SHOW_ALL_TOKENS = true; +final boolean DEBUG_ON = false; +final boolean SHOW_ALL_TOKENS = false; // This includes the entire board, including the 'queued' tokens not visible // to the user, that sit above the token the user interacts with. @@ -25,7 +25,7 @@ final int START_ROW_INDEX = 8; final int TOKEN_SIZE = 32; final int CANVAS_WIDTH = 620; -final int CANVAS_HEIGHT = 650;//650; +final int CANVAS_HEIGHT = 400; // We define the board size in pixels and allow it to be any size // and have the tokens center themselves inside those dimensions. @@ -38,7 +38,7 @@ final int BOARD_H_IN_PX = TOKEN_SIZE * 8; //273; // Where on the canvas the tokens start to be rendered. final int START_X = (int)(CANVAS_WIDTH/2.0f - BOARD_W_IN_PX/2.0f); -final int START_Y = (int)(CANVAS_HEIGHT/2.0f - BOARD_H_IN_PX/2.0f) + 150; +final int START_Y = (int)(CANVAS_HEIGHT/2.0f - BOARD_H_IN_PX/2.0f);// + 150; // Used by the AssetStore PApplet globalApplet; @@ -46,10 +46,10 @@ PApplet globalApplet; Token[][] board = new Token[BOARD_ROWS][BOARD_COLS]; ScreenSet screens = new ScreenSet(); +ScreenStory screenStory; SoundManager soundManager; - /* Wrap println so we can easily disable all console output on release */ @@ -72,9 +72,12 @@ void setup(){ soundManager.init(); soundManager.setMute(true); + screenStory = new ScreenStory(); + screens.add(new ScreenSplash()); screens.add(new ScreenGameplay()); screens.add(new ScreenGameOver()); + screens.add(screenStory); screens.transitionTo("splash"); } diff --git a/RetroFont.pde b/RetroFont.pde index 45bc6a8..93829aa 100644 --- a/RetroFont.pde +++ b/RetroFont.pde @@ -80,6 +80,10 @@ public class RetroFont{ //PImage fontSheet = loadImage(imageFilename); public PImage getGlyph(char ch){ int asciiCode = Utils.charCodeAt(ch); + + if(asciiCode-32 >= 96 || asciiCode-32 <= 0){ + return chars[0]; + } return chars[asciiCode-32]; } diff --git a/RetroLabel.pde b/RetroLabel.pde index e2cd358..19d45a4 100644 --- a/RetroLabel.pde +++ b/RetroLabel.pde @@ -51,15 +51,24 @@ public class RetroLabel extends RetroPanel{ dirty = true; int newWidth = 0; - for(int letter = 0; letter < text.length(); letter++){ + int newHeight = font.getGlyphHeight(); + + int longestLine = 0; - PImage glyph = getGlyph(text.charAt(letter)); + for(int letter = 0; letter < text.length(); letter++){ - if(glyph != null){ - newWidth += glyph.width + horizontalSpacing; + if((text.charAt(letter)) == 10){ + newHeight += font.getGlyphHeight(); + } + else{ + PImage glyph = getGlyph(text.charAt(letter)); + + if(glyph != null){ + newWidth += glyph.width + horizontalSpacing; + } } } - + h = newHeight; w = newWidth; } diff --git a/ScreenGameplay.pde b/ScreenGameplay.pde index 4d9019c..4e1f681 100644 --- a/ScreenGameplay.pde +++ b/ScreenGameplay.pde @@ -84,8 +84,7 @@ public class ScreenGameplay implements IScreen, Subject{ LayerObserver hudLayer = new HUDLayer(this); gemsRequiredForLevel = currLevel * 5; - - //floatingTokens = new ArrayList(); + dyingTokens = new ArrayList(); bk = loadImage("data/images/boards/board.png"); @@ -124,7 +123,6 @@ public class ScreenGameplay implements IScreen, Subject{ background(0); pushMatrix(); - translate(START_X, START_Y); //rect(0,0, BOARD_W_IN_PX, BOARD_H_IN_PX); @@ -152,10 +150,6 @@ public class ScreenGameplay implements IScreen, Subject{ dyingTokens.get(i).draw(); } - //for(int i = 0; i < floatingTokens.size(); i++){ - // floatingTokens.get(i).draw(); - // } - if(swapToken1 != null){ swapToken1.draw(); } @@ -165,7 +159,6 @@ public class ScreenGameplay implements IScreen, Subject{ drawBoard(); - // In some cases it is necessary to see the non-visible tokens // above the visible board. Other cases, I want that part covered. // for example, when tokens are falling. @@ -225,7 +218,13 @@ public class ScreenGameplay implements IScreen, Subject{ // DROP TOKENS if(Keyboard.isKeyDown(KEY_D)){ dropTokens(); - } + } + + // NEXT LEVEL + if(Keyboard.isKeyDown(KEY_L)){ + goToNextLevel(); + } + timer.tick(); float td = timer.getDeltaSec(); @@ -302,6 +301,7 @@ public class ScreenGameplay implements IScreen, Subject{ } } + // TODO: refactor? // Iterate over all the tokens that are dying and increase the score. for(int i = 0; i < dyingTokens.size(); i++){ @@ -320,26 +320,26 @@ public class ScreenGameplay implements IScreen, Subject{ dyingTokens.remove(i); tokensDestroyed++; + } } + if(DEBUG_ON){ + debug.addString("dyingTokens: " + dyingTokens.size()); + } + // Update all tokens on board. This includes the falling tokens - for(int r = BOARD_ROWS-1; r >= 0 ; r--){ + for(int r = BOARD_ROWS - 1; r >= 0 ; r--){ for(int c = 0; c < BOARD_COLS; c++){ Token t = board[r][c]; t.update(td); - if(t.isFalling() && t.arrivedAtDest()){ //fallingDown + if(t.isFalling() && t.arrivedAtDest()){ t.dropIntoCell(); numTokensArrivedAtDest++; - // If the top token arrived at its destination, it means we can safely - // fill up tokens above it. + // If the top token arrived at its destination, it means we can safely fill up tokens above it. if(t.getFillCellMarker()){ - //markTokensForRemoval(false); - //removeMarkedTokens(true); - //dropTokens(); - //board[r][c].setFillCellMarker(false); fillInvisibleSectionOfColumn(t.getColumn()); setFillMarker(t.getColumn()); } @@ -528,7 +528,6 @@ public class ScreenGameplay implements IScreen, Subject{ * */ void animateSwapTokens(Token t1, Token t2){ - println("animate swap tokens"); int t1Row = t1.getRow(); int t1Col = t1.getColumn(); @@ -635,29 +634,16 @@ public class ScreenGameplay implements IScreen, Subject{ } /** + TODO: refactor 'ok' From bottom to top, search to find first gap After finding the first gap, set the marker Find first token, set dst to marker Increment marker by 1 - Find next token - - For all the tokens above that gap, until very top - a) detach tokens from board - b) give them appropriate positions - c) give them a velocity - d) give them destination positions - e) place tokens in special floating tokens array to keep track of them. - f) update tokens and allow them to add themselves back in + Find next token */ void dropTokens(){ for(int c = 0; c < BOARD_COLS; c++){ - - // TODO: fix - //if(board[0][c].fallingDown){ - // continue; - //} - boolean ok = false; int dst = BOARD_ROWS; int src; @@ -679,14 +665,11 @@ public class ScreenGameplay implements IScreen, Subject{ } } - //println("drop"); while(src >= 0){ - // move the first token if(ok){ Token tokenToMove = board[src][c]; tokenToMove.fallTo(dst, c); - //tokenToMove.fallingDown = true; } do{ src--; @@ -1138,6 +1121,11 @@ public class ScreenGameplay implements IScreen, Subject{ */ void goToNextLevel(){ + screenStory.nextLevel(); + screens.transitionTo("story"); + + + // Should the score be reset? // score = 0; gemCounter = 0; @@ -1156,6 +1144,8 @@ public class ScreenGameplay implements IScreen, Subject{ numGemsOnBoard = currLevel + 1; - fillBoardWithRandomTokens(); + //animateLevel(); + generateNewBoard(); + //fillBoardWithRandomTokens(); } } diff --git a/ScreenSplash.pde b/ScreenSplash.pde index 8735276..87c222e 100644 --- a/ScreenSplash.pde +++ b/ScreenSplash.pde @@ -46,7 +46,8 @@ public class ScreenSplash implements IScreen{ public void update(){ ticker.tick(); if(ticker.getTotalTime() > 0.5f){ - screens.transitionTo("gameplay"); + //screens.transitionTo("gameplay"); + screens.transitionTo("story"); } } diff --git a/ScreenStory.pde b/ScreenStory.pde new file mode 100644 index 0000000..044fa75 --- /dev/null +++ b/ScreenStory.pde @@ -0,0 +1,62 @@ +/* +*/ +public class ScreenStory implements IScreen{ + + private int storyPointer = 0; + + RetroFont solarWindsFont; + + RetroLabel storyLabel; + RetroLabel continueInstruction; + + private String[] story = new String[]{ + //There once were some dino + "MATCH 5 GEMS IN 5 MINUTES", + "Match 10 gems in 8 minutes", + "Match 15 gems in 13 minutes", + "Match 20 gems in 15 minutes" + }; + + public ScreenStory(){ + solarWindsFont = new RetroFont("data/fonts/solarwinds.png", 14, 16, 2); + + storyLabel = new RetroLabel(solarWindsFont); + storyLabel.setText(story[storyPointer]); + storyLabel.pixelsFromCenter(0, 0); + storyLabel.setDebug(false); + + continueInstruction = new RetroLabel(solarWindsFont); + continueInstruction.setText("Click to continue"); + continueInstruction.pixelsFromCenter(0, 50); + } + + public void draw(){ + background(0); + storyLabel.draw(); + continueInstruction.draw(); + } + + public void update(){ + + } + + // Mouse methods + public void mousePressed(){} + public void mouseReleased(){ + println("*******"); + screens.transitionTo("gameplay"); + } + public void mouseDragged(){} + public void mouseMoved(){} + + public void keyPressed(){} + public void keyReleased(){} + + public String getName(){ + return "story"; + } + + public void nextLevel(){ + screens.transitionTo("gameplay"); + } +} diff --git a/Token.pde b/Token.pde index 51dff5b..967a27c 100644 --- a/Token.pde +++ b/Token.pde @@ -14,8 +14,8 @@ public class Token{ private final int DYING = 4; private final int DEAD = 5; - private final float MOVE_SPEED = TOKEN_SIZE * 1.25f; // token size per second - private final float DROP_SPEED = 35; + private final float MOVE_SPEED = TOKEN_SIZE * 2.25f; // token size per second + private final float DROP_SPEED = 85; private int id; private int state; @@ -39,6 +39,8 @@ public class Token{ // !!! We can refactor this private int moveDirection; + float test; + private boolean isFillCellMarker; private int type; @@ -60,6 +62,7 @@ public class Token{ id = Utils.nextID(); isSelected = false; + test = 2.25f;//random(1, 2); row = 0; column = 0; @@ -254,7 +257,7 @@ public class Token{ } else if(state == DYING){ // Shrink the token if it is dying. - scaleSize -= td * 2.5f; + scaleSize -= td * test; if(scaleSize <= 0){ //scaleSize = 0.0f; @@ -286,10 +289,10 @@ public class Token{ } /* - Token needs to be valid and idle for it to be swapped. + Token needs to be valid and idle for it to be swapped. */ public boolean canBeSwapped(){ - if(type == TokenType.NULL || /*fallingDown ||*/ state != IDLE){ + if(type == TokenType.NULL || state != IDLE || row < START_ROW_INDEX){ return false; } return true; @@ -416,7 +419,7 @@ public class Token{ rectMode(CENTER); // draw a grey box to easily identify dead or null tokens - if(DEBUG_ON && state == DEAD || state == DYING || type == TokenType.NULL){ + if(DEBUG_ON && (state == DEAD || state == DYING || type == TokenType.NULL)){ pushStyle(); fill(128,128); rect(0, 0, TOKEN_SIZE, TOKEN_SIZE);