Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Level/Level.cpp
- Committer:
- joshdavy
- Date:
- 2019-04-24
- Revision:
- 9:96969b1c6bde
- Parent:
- 8:21b6d4dbce44
- Child:
- 10:58cf89dd878c
File content as of revision 9:96969b1c6bde:
#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].first.x = blocks[i].first.x;
_blocks[i].first.y = blocks[i].first.y;
_blocks[i].second.x = blocks[i].second.x;
_blocks[i].second.y = blocks[i].second.y;
}
_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);
}