ELEC2645 (2018/19) / Mbed 2 deprecated el17ajf

Dependencies:   mbed

Fork of el17ajf by Angus Findlay

Game/Game.cpp

Committer:
el17ajf
Date:
2019-03-19
Revision:
15:afeefa3ceb61
Parent:
13:59e17cab320a
Child:
17:cc448ab7266f

File content as of revision 15:afeefa3ceb61:

#include "Game.h"
#include "Input.h"
#include "mbed.h" // TODO

/* BUGS:
 *
 * SWITCH BOUNCE
 * NO DOUBLE ROW CLEAR
 * 
 */

Game::Game() {
    currentTetromino = Tetromino::getTetrominoOfType(
        Tetromino::getRandomTetrominoType());
    nextTetrominoType = Tetromino::getRandomTetrominoType();
    move_frames = 3;
    frames = 0;
    last_move_frame = 0;
    start_x = Grid::GRID_WIDTH / 2 - 1;
    currentTetromino = currentTetromino.teleportedTo(start_x);
}

Game::~Game() {
    
}

void Game::update() {
    if (frames++ > last_move_frame + move_frames) {
        last_move_frame = frames;
        moveCurrentTetrominoDown();
    }
    
    if (Input::buttonHit(Input::LEFT)) {
        if (grid.isSpaceForTetromino(currentTetromino.movedLeft())) {
            currentTetromino = currentTetromino.movedLeft();
        }
    }
    if (Input::buttonHit(Input::RIGHT)) {
        if (grid.isSpaceForTetromino(currentTetromino.movedRight())) {
            currentTetromino = currentTetromino.movedRight();
        } else {
            printf("NO SPACE TO THE RIGHT");
        }
    }
    if (Input::buttonHit(Input::UP)) {
        if (grid.isSpaceForTetromino(currentTetromino.rotatedClockwise())) {
            currentTetromino = currentTetromino.rotatedClockwise();
        }
    }
    if (Input::buttonHit(Input::DOWN)) {
        dropCurrentTetromino();
    }
}

void Game::moveCurrentTetrominoDown() {
    if (grid.isSpaceForTetromino(currentTetromino.movedDown())) {
        currentTetromino = currentTetromino.movedDown();
    } else {
        grid.placeTetromino(currentTetromino);
        currentTetromino = Tetromino::getTetrominoOfType(nextTetrominoType);
        // centre it
        currentTetromino = currentTetromino.teleportedTo(start_x);
        if (!grid.isSpaceForTetromino(currentTetromino)) {
            // no space for tetromino, game over
            gameOver();
        }
        nextTetrominoType = Tetromino::getRandomTetrominoType();
    }
}

void Game::dropCurrentTetromino() {
    while (grid.isSpaceForTetromino(currentTetromino.movedDown())) {
        currentTetromino = currentTetromino.movedDown();
    }
    moveCurrentTetrominoDown();
}

void Game::draw() {
    currentTetromino.draw();
    grid.draw();
}

void Game::gameOver() {
    // TODO
}