Subversion Repositories AndroidProjects

Rev

Blame | Last modification | View Log | RSS feed

package com.gebauz.PonPonChun.game;

import com.badlogic.gdx.Gdx;
import com.gebauz.Bauzoid.file.File;
import com.gebauz.Bauzoid.game.Game;
import com.gebauz.Bauzoid.game.GameObject;
import com.gebauz.Bauzoid.graphics.Graphics;
import com.gebauz.Bauzoid.math.Matrix4;
import com.gebauz.Bauzoid.math.Vector4;
import com.gebauz.PonPonChun.PonPonChunCustomServices;
import com.gebauz.PonPonChun.game.entities.Background;
import com.gebauz.PonPonChun.game.entities.EntityManager;
import com.gebauz.PonPonChun.game.entities.HeadsUpDisplay;
import com.gebauz.PonPonChun.game.entities.PlayFieldOld;

/** GameLogic class
 * Holds the basic gameplay logic. Game Modes (defining criteria for winning, etc.) are implemented separately.
 * @author chris
 *
 */

public class GameLogicOld extends GameLogic
{
        private EntityManager mEntityManager = null;
       
        private Background mBackground = null;
        private PlayFieldOld mPlayField = null;
        private HeadsUpDisplay mHeadsUpDisplay = null;
       
       
        public enum GameMode
        {
                GAMEMODE_ENDLESS,
                GAMEMODE_PUZZLE,
                GAMEMODE_TIMETRIAL,
        }
       
        private GameMode mGameMode = GameMode.GAMEMODE_ENDLESS;
       
        public GameLogicOld(Game game)
        {
                super(game);
               
                mEntityManager = new EntityManager(this);
        }
       
        /** Initializes base GameLogic, also initializes all added entities. Entities must be added before this call. */
        public void init(GameMode gameMode)
        {
                mGameMode = gameMode;
               
                mPlayField = (PlayFieldOld)mEntityManager.addEntity(new PlayFieldOld(this));
                mHeadsUpDisplay = (HeadsUpDisplay)mEntityManager.addEntity(new HeadsUpDisplay(this));
                mBackground = (Background)mEntityManager.addEntity(new Background(this));
               
                mEntityManager.init();
               
                mPlayField.loadPlayField(Gdx.files.internal("data/levels/level01.txt"));
                mPlayField.setAdvanceSpeed(0.1f);
                //randomizeForEndless(7);
        }
       
        /** Clears up all entities. */
        public void exit()
        {
                mEntityManager.exit();         
        }
       
        /** Update the Game Logic.
         * NOTE: The EntityManager does not update entities because that is up to the game implementation (especially if the game implementation requires a certain order).
         */

        public void update(float deltaTime)
        {
                mPlayField.update(deltaTime);
                mHeadsUpDisplay.update(deltaTime);
                mBackground.update(deltaTime);
        }
       
        /** Render the Game Logic.
         * NOTE: The EntityManager does not render entities because that is up to the game implementation (especially if the game implementation requires a certain order).
         */

        public void render()
        {
                mBackground.render();
               
                //getGraphics().renderStates.pushViewMatrix();
                //getGraphics().renderStates.view = Matrix4.createTranslation(-54.0f, 0.0f, 0.0f);
                mPlayField.render();
                //getGraphics().renderStates.popViewMatrix();
                mHeadsUpDisplay.render();
               
                //PonPonChunCustomServices.getInstance().getFont().drawText("DEBUG:", 10, 10, new Vector4(0, 0, 0, 1));
                //PonPonChunCustomServices.getInstance().getFont().drawText(mPlayField.getDebugOut(), 10, 30, new Vector4(0, 0, 0, 1));
        }
       
        public void randomizeForEndless(int startRow)
        {
                for (int i = 0; i < PlayFieldOld.NUM_CELLS; i++)
                {
                        mPlayField.getCell(i).setType(PlayFieldOld.CELL_EMPTY);
                }
               
                // row by row randomization that foregoes triplets
                for (int y = startRow; y < (PlayFieldOld.PLAYFIELD_HEIGHT+1); y++)
                {
                        generateRow(y);
                }
               
                // sweep vertically to check for vertical triplets
                for (int x = 0; x < PlayFieldOld.PLAYFIELD_WIDTH; x++)
                {
                        int[] lastTypes = new int[2];
                       
                        lastTypes[0] = mPlayField.getCellWithHidden(x, startRow).getType();
                        lastTypes[1] = mPlayField.getCellWithHidden(x, startRow+1).getType();
                       
                        for (int y = startRow+2; y < (PlayFieldOld.PLAYFIELD_HEIGHT+1); y++)
                        {
                                PlayFieldOld.Cell cell = mPlayField.getCellWithHidden(x, y);
                               
                                int type = cell.getType();
                                while ((type == lastTypes[0]) &&
                                           (type == lastTypes[1]))
                                {
                                        type = getGame().getRandomInt(0, PlayFieldOld.CellType.values().length-1);
                                }
                               
                                cell.setType(type);
                                lastTypes[1] = lastTypes[0];
                                lastTypes[0] = type;
                        }
                }
        }
       
        public void generateRow(int row)
        {
                int[] lastTypes = new int[] { -1, -1 };
               
                for (int x = 0; x < PlayFieldOld.PLAYFIELD_WIDTH; x++)
                {
                        PlayFieldOld.Cell cell = mPlayField.getCellWithHidden(x, row);

                        // needs to be different
                        int type = getGame().getRandomInt(0, PlayFieldOld.CellType.values().length-1);
                        while ((type == lastTypes[0]) && (type == lastTypes[1]))
                        {
                                type = getGame().getRandomInt(0, PlayFieldOld.CellType.values().length-1);
                        }
                               
                        cell.setType(type);
                        lastTypes[1] = lastTypes[0];
                        lastTypes[0] = type;
                }      
        }
               
        public final EntityManager getEntityManager()
        {
                return mEntityManager;
        }
       
        /*public final Graphics getGraphics()
        {
                return getGame().getGraphics();
        }*/

       
        public final GameMode GetGameMode()
        {
                return mGameMode;
        }
       
        //=== Entities getters
        public final PlayFieldOld getPlayField() { return mPlayField; }
        public final  HeadsUpDisplay getHeadsUpDisplay() { return mHeadsUpDisplay; }
        public final  Background getBackground() { return mBackground; }

}