A complex 2D-dungeon game on LPC1768 in SWJTU-Leeds Joint School XJEL2645 project. Referenced from the framework contributed by https://os.mbed.com/users/Siriagus/code/SimplePlatformGame/

Dependencies:   mbed N5110 ShiftReg PinDetect

Game.cpp

Committer:
Siriagus
Date:
2015-05-03
Revision:
9:da608ae65df9
Parent:
8:9ac6a428fa26
Child:
11:adb68da98262

File content as of revision 9:da608ae65df9:

#include "Game.h"

Serial pc(USBTX, USBRX); // TO DELETE - DEBUGGING ONLY

// BUG: Registrer jo bare btn press, må være en bedre måte, men er trøtt nå

void Game::init()
{
    // Player
    player.x = player.y = 10; 
    player.width = player.height = 5;
    onGround = false;
}

// Functions
void Game::update(float dt)
{
    // Handle input, should be its own function
    switch(input->joystick->getDirection())
    {

        case LEFT:
        case UP_LEFT:
        case DOWN_LEFT:
            player.vx = -2;
            player.goingLeft = true;
        break;
        
        case RIGHT:
        case UP_RIGHT:
        case DOWN_RIGHT:
            player.vx = 2;
            player.goingLeft = false;
        break;
        
        
        case UP:
            //player.vy = -4;
        break;
        case DOWN:
            player.vy += 1;
        break;
        
        case CENTER:
            player.vx = 0;
        break;
    }
    
    
    if (player.y < HEIGHT-10) player.vy += 1; // gravity
    else if (player.vy > 0) {player.vy = 0; onGround = true;}
        
    
    if (input->read(Input::ButtonA) && onGround) // && player.onGround)
    {
        player.vy = -4;
        onGround = false;
    }
    
    if (player.vy > 3) player.vy = 3;
    
    player.x += player.vx;
    player.y += player.vy;
    
    // Bullets - TODO: Give the bullets a direction - need to delete them when they go off the screen
    if (input->read(Input::ButtonB) && releasedBtnB)
    {
        Point* bullet = new Point;
        bullet->x = player.x-1;
        bullet->y = player.y + 2;
        bullet->vx = (player.goingLeft) ? -4 : 4;
        bullet->vy = 0;
        
        bullets.push_back(bullet);
        releasedBtnB = false;
    }
    else if (!input->read(Input::ButtonB))
        releasedBtnB = true;
    
    // Loop through bullets and move them
    for (std::vector<Point*>::iterator it = bullets.begin(); it != bullets.end();)
    {
        (*it)->x += (*it)->vx;
        
        // Check if outside
        int x = (*it)->x;
        if (x < 0 || x > WIDTH)
        {
            delete (*it);
            it = bullets.erase(it); // go to next element
        }
        else
            ++it;   // go to next element
        
        // TODO: Check for collisions
        // TODO: Go both ways
    }
}

void Game::render()
{
    // Draw player - TODO: Make this a part of sprite class (so they can draw themselves)
    int x0, x1, y0, y1;
    x0 = (player.x < 0) ? 0 : player.x;                       // x0 = max(0,x);
    y0 = (player.y < 0) ? 0 : player.y;                       // y0 = max(0,y);
    x1 = (player.width + player.x > WIDTH) ? WIDTH : player.width + player.x;       //x1 = min(WIDTH, width);
    y1 = (player.height + player.y > HEIGHT) ? HEIGHT : player.height + player.y;   //y1 = min(HEIGHT, height);
    
    for (int i = y0; i < y1; ++i)
    {   
        for (int j = x0; j < x1; ++j)
        {
            // If player is going right, obtain data from sprite in reverse order => render in reverse
            int xIndex = (player.goingLeft) ? (j-x0) : (player.width - 1 - (j-x0));
            
            if (Image::Player[i-y0][xIndex])
                lcd->setPixel(j,i);
        }
    }
    
    // Render bullets
    for (std::vector<Point*>::iterator it = bullets.begin(); it != bullets.end(); ++it)
    {
        int x, y;
        x = (*it)->x;
        y = (*it)->y;
        
        if (x >= 0 && x < WIDTH && y >= 0 && y < HEIGHT) // Boundary check
            lcd->setPixel(x,y);
    }
}