ELEC2645 (2018/19) / Mbed 2 deprecated el17m2h_public

Dependencies:   mbed

Committer:
el17m2h
Date:
Thu May 09 14:30:45 2019 +0000
Revision:
37:71f2cd073739
Parent:
35:b99b563c3eb6
Added comments to the enemy class documentation.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
el17m2h 1:0001cb3eb053 1 #include "Floors.h"
el17m2h 29:15e9640646b7 2 Floors::Floors()
el17m2h 29:15e9640646b7 3 {
el17m2h 1:0001cb3eb053 4 }
el17m2h 29:15e9640646b7 5 Floors::~Floors()
el17m2h 29:15e9640646b7 6 {
el17m2h 1:0001cb3eb053 7 }
el17m2h 3:116913e97fd7 8
el17m2h 34:a9b14a4ccd46 9
el17m2h 34:a9b14a4ccd46 10 // Function to set the intial position of the floor at a specified input y-coordinate (from engine) and then sets a random
el17m2h 34:a9b14a4ccd46 11 // x-coordinate position.
el17m2h 29:15e9640646b7 12 void Floors::init(int y, int width, int height)
el17m2h 29:15e9640646b7 13 {
el17m2h 34:a9b14a4ccd46 14 _height = height; // defines the width and height as private variables
el17m2h 1:0001cb3eb053 15 _width = width;
el17m2h 31:5c4acae51026 16 end_game = false; // the read decision should be false initially so that the game is not ended as soon as it starts
el17m2h 34:a9b14a4ccd46 17 int decide = rand() % 2; // x-coordinate initially random: 2-30 or 40-71 so doodler falls to bottom floor specified in engine
el17m2h 31:5c4acae51026 18 if (decide == 0) { // right
el17m2h 29:15e9640646b7 19 _position.x = 2 + (rand()% 28);
el17m2h 31:5c4acae51026 20 } else { // left
el17m2h 34:a9b14a4ccd46 21 _position.x = 40 + (rand()% 31); // position is within screen parameters
el17m2h 22:0d2ac98a8b48 22 }
el17m2h 14:529f798adae4 23 _position.y = y;
el17m2h 31:5c4acae51026 24
el17m2h 1:0001cb3eb053 25 }
el17m2h 1:0001cb3eb053 26
el17m2h 35:b99b563c3eb6 27 // Prints the floor into the LCD screen by calling a function from the LCD to draw a 14 x 1 rectangle
el17m2h 29:15e9640646b7 28 void Floors::draw(N5110 &lcd)
el17m2h 29:15e9640646b7 29 {
el17m2h 14:529f798adae4 30 lcd.drawRect(_position.x, _position.y, _width, _height, FILL_BLACK);
el17m2h 31:5c4acae51026 31 eny.draw(lcd); // draws enemy
el17m2h 1:0001cb3eb053 32 }
el17m2h 1:0001cb3eb053 33
el17m2h 35:b99b563c3eb6 34 // Updates the position of the floor
el17m2h 29:15e9640646b7 35 void Floors::update(float doodler_pos_x, float doodler_pos_y, float _bullet_pos_x, float _bullet_pos_y)
el17m2h 31:5c4acae51026 36 { // when they leave the screen they will re-appear in random x-coordinate so that 10 floors are always on screen
el17m2h 31:5c4acae51026 37 // rectangle (1-82 & 9 - 48 )
el17m2h 29:15e9640646b7 38 _doodler_pos_x = doodler_pos_x;
el17m2h 29:15e9640646b7 39 _doodler_pos_y = doodler_pos_y;
el17m2h 31:5c4acae51026 40 if (_position.y > 45 ) { // the place decision bool is updated every time a floor re-appears
el17m2h 29:15e9640646b7 41 _position.y = 9;
el17m2h 31:5c4acae51026 42 _position.x = 1 + (rand()% 69);
el17m2h 30:863565e9859f 43 place = rand()% 6;
el17m2h 31:5c4acae51026 44 if (place == 2) { // 1/6 chance of placing a ghost
el17m2h 29:15e9640646b7 45 eny.update(_position);
el17m2h 29:15e9640646b7 46 put_enemy = true;
el17m2h 31:5c4acae51026 47 } else {
el17m2h 29:15e9640646b7 48 eny.erase();
el17m2h 29:15e9640646b7 49 put_enemy = false;
el17m2h 26:d16a5b1e0ace 50 }
el17m2h 20:a359092079b0 51 }
el17m2h 35:b99b563c3eb6 52 if (_doodler_pos_y < 16) { // shift floors once doodler reaches 16 y-coordinate position threshold on the screen
el17m2h 31:5c4acae51026 53 // I chose this value because testing with the doodler's jump, it allows the doodler to keep jumping on a floor
el17m2h 31:5c4acae51026 54 // without it moving down and dissapearing
el17m2h 29:15e9640646b7 55 _position.y += 1;
el17m2h 29:15e9640646b7 56 }
el17m2h 35:b99b563c3eb6 57 check_enemy(_bullet_pos_x, _bullet_pos_y);
el17m2h 29:15e9640646b7 58 }
el17m2h 29:15e9640646b7 59
el17m2h 35:b99b563c3eb6 60 // Function to check if doodler or bullet have collided with the enemy
el17m2h 31:5c4acae51026 61 void Floors::check_enemy(float _bullet_pos_x, float _bullet_pos_y)
el17m2h 31:5c4acae51026 62 {
el17m2h 29:15e9640646b7 63 enemy_position = eny.get_position();
el17m2h 31:5c4acae51026 64 if (put_enemy == true) {
el17m2h 29:15e9640646b7 65 eny.update(_position);
el17m2h 29:15e9640646b7 66 }
el17m2h 31:5c4acae51026 67 if ( // to check if the doodler has collided with the ghost
el17m2h 31:5c4acae51026 68 (_doodler_pos_x + 6 <= enemy_position.x + 12) &&
el17m2h 31:5c4acae51026 69 (_doodler_pos_x + 6 >= enemy_position.x) &&
el17m2h 31:5c4acae51026 70 (_doodler_pos_y <= enemy_position.y + 11) &&
el17m2h 31:5c4acae51026 71 (_doodler_pos_y >= enemy_position.y)
el17m2h 31:5c4acae51026 72 ) {
el17m2h 35:b99b563c3eb6 73 end_game = true; // if doodler collides with enemy, the end_game variable will state the game should end
el17m2h 29:15e9640646b7 74 }
el17m2h 31:5c4acae51026 75 if ( // to check if the bullet has collided with the ghost
el17m2h 31:5c4acae51026 76 (_bullet_pos_x <= enemy_position.x + 12) &&
el17m2h 31:5c4acae51026 77 (_bullet_pos_x >= enemy_position.x) &&
el17m2h 31:5c4acae51026 78 (_bullet_pos_y <= enemy_position.y + 11) &&
el17m2h 31:5c4acae51026 79 (_bullet_pos_y >= enemy_position.y)
el17m2h 31:5c4acae51026 80 ) {
el17m2h 35:b99b563c3eb6 81 eny.erase(); // if bullet collides with enemy the erase function is called
el17m2h 15:4efa04a6a376 82 }
el17m2h 9:5e53bca2a4c2 83 }
el17m2h 9:5e53bca2a4c2 84
el17m2h 35:b99b563c3eb6 85
el17m2h 35:b99b563c3eb6 86 // Returns the current floor's position. It is called in the engine to compare its position with the other classes.
el17m2h 33:de130e274391 87 Vector2D Floors::get_position() { Vector2D p = {_position.x,_position.y}; return p; }
el17m2h 5:8814d6de77d0 88
el17m2h 35:b99b563c3eb6 89
el17m2h 35:b99b563c3eb6 90 // Sets the position within the floor class by inputing the new values from the engine, where the function is called.
el17m2h 33:de130e274391 91 void Floors::set_position(Vector2D pos) { _position.x = pos.x; _position.y = pos.y; }
el17m2h 31:5c4acae51026 92
el17m2h 31:5c4acae51026 93 // The get_end_game function can be called in the engine to check if the game should end or not (if the doodler has collided
el17m2h 31:5c4acae51026 94 // with the enemy)
el17m2h 33:de130e274391 95 bool Floors::get_end_game() { return end_game; }