Josh Davy / Mbed OS Flip_OS_5

Dependencies:   el17jd

Committer:
joshdavy
Date:
Mon May 06 10:11:42 2019 +0000
Revision:
10:58cf89dd878c
Parent:
9:96969b1c6bde
Child:
11:db27d3838514
Main game done. Documentation to be done next.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
joshdavy 6:2ca1516ec1e2 1 #include "Level.h"
joshdavy 6:2ca1516ec1e2 2
joshdavy 6:2ca1516ec1e2 3 Level::Level() {}
joshdavy 6:2ca1516ec1e2 4
joshdavy 6:2ca1516ec1e2 5 Level::~Level() {}
joshdavy 6:2ca1516ec1e2 6
joshdavy 9:96969b1c6bde 7 void Level::init(Block blocks [],
joshdavy 8:21b6d4dbce44 8 int number_of_blocks,
joshdavy 8:21b6d4dbce44 9 Vector2D goal)
joshdavy 6:2ca1516ec1e2 10 {
joshdavy 8:21b6d4dbce44 11 _number_of_blocks = number_of_blocks;
joshdavy 9:96969b1c6bde 12
joshdavy 8:21b6d4dbce44 13 for (int i = 0; i<_number_of_blocks; i++) {
joshdavy 10:58cf89dd878c 14 _blocks[i] = blocks[i];
joshdavy 6:2ca1516ec1e2 15 }
joshdavy 8:21b6d4dbce44 16
joshdavy 9:96969b1c6bde 17 _goal = goal;
joshdavy 9:96969b1c6bde 18 _number_of_moving_blocks = 0;
joshdavy 9:96969b1c6bde 19 }
joshdavy 9:96969b1c6bde 20
joshdavy 9:96969b1c6bde 21 Block * Level::get_blocks()
joshdavy 9:96969b1c6bde 22 {
joshdavy 9:96969b1c6bde 23 return _blocks;
joshdavy 9:96969b1c6bde 24 }
joshdavy 9:96969b1c6bde 25
joshdavy 9:96969b1c6bde 26 int Level::get_number_of_blocks()
joshdavy 9:96969b1c6bde 27 {
joshdavy 9:96969b1c6bde 28 return _number_of_blocks;
joshdavy 9:96969b1c6bde 29 }
joshdavy 9:96969b1c6bde 30
joshdavy 9:96969b1c6bde 31 Vector2D Level::get_goal()
joshdavy 9:96969b1c6bde 32 {
joshdavy 9:96969b1c6bde 33 return _goal;
joshdavy 9:96969b1c6bde 34 }
joshdavy 9:96969b1c6bde 35
joshdavy 9:96969b1c6bde 36 void Level::update_moving_blocks()
joshdavy 9:96969b1c6bde 37 {
joshdavy 9:96969b1c6bde 38 MovingBlock current_block;
joshdavy 9:96969b1c6bde 39 for (int i = 0; i < _number_of_moving_blocks; i++) {
joshdavy 9:96969b1c6bde 40 current_block = _moving_blocks[i];
joshdavy 9:96969b1c6bde 41
joshdavy 9:96969b1c6bde 42
joshdavy 9:96969b1c6bde 43 if (current_block.extending) {
joshdavy 9:96969b1c6bde 44
joshdavy 9:96969b1c6bde 45 _blocks[current_block.index].first.x += 1;
joshdavy 9:96969b1c6bde 46 _blocks[current_block.index].second.x += 1;
joshdavy 9:96969b1c6bde 47
joshdavy 9:96969b1c6bde 48 } else {
joshdavy 9:96969b1c6bde 49
joshdavy 9:96969b1c6bde 50 _blocks[current_block.index].first.x -= 1;
joshdavy 9:96969b1c6bde 51 _blocks[current_block.index].second.x -= 1;
joshdavy 9:96969b1c6bde 52
joshdavy 9:96969b1c6bde 53 }
joshdavy 9:96969b1c6bde 54
joshdavy 9:96969b1c6bde 55
joshdavy 9:96969b1c6bde 56
joshdavy 9:96969b1c6bde 57
joshdavy 9:96969b1c6bde 58 if (_blocks[current_block.index].first.x >
joshdavy 10:58cf89dd878c 59 current_block.initial_pos + current_block.distance) {
joshdavy 9:96969b1c6bde 60 current_block.extending = false;
joshdavy 9:96969b1c6bde 61 }
joshdavy 9:96969b1c6bde 62 if (_blocks[current_block.index].first.x < current_block.initial_pos) {
joshdavy 9:96969b1c6bde 63 current_block.extending = true;
joshdavy 9:96969b1c6bde 64
joshdavy 9:96969b1c6bde 65 }
joshdavy 9:96969b1c6bde 66
joshdavy 9:96969b1c6bde 67 _moving_blocks[i] = current_block;
joshdavy 8:21b6d4dbce44 68 }
joshdavy 8:21b6d4dbce44 69
joshdavy 8:21b6d4dbce44 70
joshdavy 8:21b6d4dbce44 71
joshdavy 8:21b6d4dbce44 72 }
joshdavy 8:21b6d4dbce44 73
joshdavy 8:21b6d4dbce44 74
joshdavy 9:96969b1c6bde 75 void Level::declare_moving_block(int index,bool extending,int distance)
joshdavy 9:96969b1c6bde 76 {
joshdavy 9:96969b1c6bde 77 MovingBlock new_moving_block;
joshdavy 9:96969b1c6bde 78 new_moving_block.index = index;
joshdavy 9:96969b1c6bde 79 new_moving_block.distance = distance;
joshdavy 9:96969b1c6bde 80 new_moving_block.extending = extending;
joshdavy 9:96969b1c6bde 81 new_moving_block.initial_pos = _blocks[index].first.x;
joshdavy 9:96969b1c6bde 82
joshdavy 9:96969b1c6bde 83 if(!extending) {
joshdavy 9:96969b1c6bde 84 _blocks[index].first.x += distance;
joshdavy 9:96969b1c6bde 85 _blocks[index].second.x += distance;
joshdavy 9:96969b1c6bde 86 }
joshdavy 9:96969b1c6bde 87
joshdavy 9:96969b1c6bde 88
joshdavy 9:96969b1c6bde 89
joshdavy 9:96969b1c6bde 90 _moving_blocks[_number_of_moving_blocks] = new_moving_block;
joshdavy 9:96969b1c6bde 91 _number_of_moving_blocks += 1;
joshdavy 9:96969b1c6bde 92
joshdavy 9:96969b1c6bde 93
joshdavy 9:96969b1c6bde 94 }
joshdavy 8:21b6d4dbce44 95
joshdavy 6:2ca1516ec1e2 96 void Level::render(N5110 &lcd)
joshdavy 6:2ca1516ec1e2 97 {
joshdavy 8:21b6d4dbce44 98
joshdavy 8:21b6d4dbce44 99
joshdavy 8:21b6d4dbce44 100 for (int i = 0; i<_number_of_blocks; i++) {
joshdavy 8:21b6d4dbce44 101
joshdavy 8:21b6d4dbce44 102 lcd.drawRect(_blocks[i].first.x,_blocks[i].first.y,
joshdavy 8:21b6d4dbce44 103 _blocks[i].second.x - _blocks[i].first.x,
joshdavy 8:21b6d4dbce44 104 _blocks[i].second.y - _blocks[i].first.y,
joshdavy 8:21b6d4dbce44 105 FILL_BLACK);
joshdavy 6:2ca1516ec1e2 106 }
joshdavy 8:21b6d4dbce44 107
joshdavy 8:21b6d4dbce44 108 lcd.drawSprite(_goal.x,_goal.y,11,6,(int *) goalMap);
joshdavy 8:21b6d4dbce44 109
joshdavy 6:2ca1516ec1e2 110 }