Josh Davy / Mbed OS Flip_OS_5

Dependencies:   el17jd

Level/Level.cpp

Committer:
joshdavy
Date:
2019-05-06
Revision:
10:58cf89dd878c
Parent:
9:96969b1c6bde
Child:
11:db27d3838514

File content as of revision 10:58cf89dd878c:

#include "Level.h"

Level::Level() {}

Level::~Level() {}

void Level::init(Block blocks [],
                 int number_of_blocks,
                 Vector2D goal)
{
    _number_of_blocks = number_of_blocks;

    for (int i = 0; i<_number_of_blocks; i++) {
        _blocks[i] = blocks[i];
    }

    _goal = goal;
    _number_of_moving_blocks = 0;
}

Block * Level::get_blocks()
{
    return _blocks;
}

int Level::get_number_of_blocks()
{
    return _number_of_blocks;
}

Vector2D Level::get_goal()
{
    return _goal;
}

void Level::update_moving_blocks()
{
    MovingBlock current_block;
    for (int i = 0; i < _number_of_moving_blocks; i++) {
        current_block = _moving_blocks[i];


        if (current_block.extending) {

            _blocks[current_block.index].first.x += 1;
            _blocks[current_block.index].second.x += 1;

        } else  {

            _blocks[current_block.index].first.x -= 1;
            _blocks[current_block.index].second.x -= 1;

        }




        if (_blocks[current_block.index].first.x >
            current_block.initial_pos + current_block.distance) {
            current_block.extending = false;
        }
        if (_blocks[current_block.index].first.x < current_block.initial_pos) {
            current_block.extending = true;

        }

        _moving_blocks[i] = current_block;
    }



}


void Level::declare_moving_block(int index,bool extending,int distance)
{
    MovingBlock new_moving_block;
    new_moving_block.index = index;
    new_moving_block.distance = distance;
    new_moving_block.extending = extending;
    new_moving_block.initial_pos = _blocks[index].first.x;
    
    if(!extending) {
        _blocks[index].first.x += distance;
        _blocks[index].second.x += distance;
    }
    
    

    _moving_blocks[_number_of_moving_blocks] = new_moving_block;
    _number_of_moving_blocks += 1;


}

void Level::render(N5110 &lcd)
{


    for (int i = 0; i<_number_of_blocks; i++) {

        lcd.drawRect(_blocks[i].first.x,_blocks[i].first.y,
                     _blocks[i].second.x - _blocks[i].first.x,
                     _blocks[i].second.y - _blocks[i].first.y,
                     FILL_BLACK);
    }

    lcd.drawSprite(_goal.x,_goal.y,11,6,(int *) goalMap);

}