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.
Revision 11:4c4a0fe18ac2, committed 2017-05-03
- Comitter:
- musallambseiso
- Date:
- Wed May 03 20:02:57 2017 +0000
- Parent:
- 10:b856d73db923
- Commit message:
- Removed redundant code, perfected Doxygen, added inline comments.
Changed in this revision
Friendly.cpp | Show annotated file Show diff for this revision Revisions of this file |
Friendly.h | Show annotated file Show diff for this revision Revisions of this file |
--- a/Friendly.cpp Tue May 02 22:12:52 2017 +0000 +++ b/Friendly.cpp Wed May 03 20:02:57 2017 +0000 @@ -1,7 +1,5 @@ #include "Friendly.h" -// Class for friendly ship (the one you control) - Friendly::Friendly() { } @@ -10,13 +8,19 @@ { } -void Friendly::init() // initialization + +// Initializion method: + +void Friendly::init() { - _x = 2; // starting x position (fixed) - _y = 18; // starting y position (centre) + _x = 2; // Starting x position (fixed). + _y = 18; // starting y position (fixed, centre). } -void Friendly::draw(N5110 &lcd) // draws friendly ship in screen buffer + +// Draws friendly ship onto LCD: + +void Friendly::draw(N5110 &lcd) { lcd.drawLine(_x,_y,_x,_y+5,1); lcd.drawLine(_x+1,_y,_x+3,_y,1); @@ -26,52 +30,73 @@ lcd.drawLine(_x+6,_y+2,_x+6,_y+3,1); } -void Friendly::update(Direction d,float mag) + +// Updates friendly ship's position based on basic vertical and horizontal movement: + +void Friendly::update_basic(Direction d,float mag) { - _speed = int(mag*4.0f); - if (d == N) { + _speed = int(mag*4.0f); // Speed of movement depends on analog stick (mag). + + if (d == N) { // If direction is north, move upwards (y). _y-=_speed; - } else if (d == S) { + } else if (d == S) { // If direction is south, move downwards (y). _y+=_speed; - } else if (d == W) { + } else if (d == W) { // If direction is west, move to the left (x). _x-=_speed; - } else if (d == E) { + } else if (d == E) { // If direction is east, move to the right (x). _x+=_speed; - } else if (d == NW) { + } +} + + +// Updates friendly ship's position based on diagonal movement: + +void Friendly::update_diagonal(Direction d,float mag) +{ + _speed = int(mag*4.0f); + + if (d == NW) { // If direction is northwest, move upwards (y) and to the left (x). _y-=_speed; _x-=_speed; - } else if (d == NE) { + } else if (d == NE) { // If direction is northeast, move upwards (y) and to the right (x). _y-=_speed; _x+=_speed; - } else if (d == SW) { + } else if (d == SW) { // If direction is southwest, move downwards (y) and to the left (x). _y+=_speed; _x-=_speed; - } else if (d == SE) { + } else if (d == SE) { // If direction is southeast, move downwards (y) and to the right (x). _y+=_speed; _x+=_speed; } } -void Friendly::check_pos() { - if (_y < 1) { + +// Prevents friendly ship from going off-screen: + +void Friendly::check_pos() +{ + if (_y < 1) { // Prevents ship from moving past upper gridline. _y = 1; } - if (_y > 33) { + if (_y > 33) { // Prevents ship from moving past lower gridline. _y = 33; } - if (_x < 1) { + if (_x < 1) { // Prevents ship from moving past left gridline. _x = 1; } - if (_x > 78) { + if (_x > 78) { // Prevents ship from moving past right gridline. _x = 78; } } -Vector2D Friendly::get_pos() { +// Obtains friendly's current position: + +Vector2D Friendly::get_pos() +{ Vector2D p = {_x,_y}; return p; } \ No newline at end of file
--- a/Friendly.h Tue May 02 22:12:52 2017 +0000 +++ b/Friendly.h Wed May 03 20:02:57 2017 +0000 @@ -5,6 +5,17 @@ #include "N5110.h" #include "Gamepad.h" +/** Friendly Class +@brief Used for controlling the friendly ship in the Nemesis game. Includes drawing, checking, and updating functions. +@brief Incorporates N5110.h and Gamepad.h files by Craig A. Evans. + +@brief Revision 1.0 + +@author Musallam M. M. Bseiso +@date 3rd May 2017 +*/ + + class Friendly { public: @@ -29,19 +40,34 @@ /** Draw Friendly * * Draws the friendly ship onto the LCD, in accordance with the parameters initialized in the "init" method. + * @param N5110 - nokia LCD library + * @param lcd - pointer to nokia LCD library */ void draw(N5110 &lcd); - /** Update Friendly + /** Update Friendly (basic) * * Updates the friendly ship's x and y position. X and y positions are altered by adding/subtracting speeds, * which depend on the direction (d) of the analog stick. The speed is defined as the magnitude of the movement - * of the analog stick multiplied by an arbitrary number (set as 4). + * of the analog stick multiplied by an arbitrary number (set as 4). This method only deals with the basic + * horizontal and vertical movements of the friendly ship (North, South, West, East). * @param d - direction of analog stick * @param mag - magnitude of movement of analog stick */ - void update(Direction d,float mag); + void update_basic(Direction d,float mag); + + + /** Update Friendly (diagonal) + * + * Updates the friendly ship's x and y position. X and y positions are altered by adding/subtracting speeds, + * which depend on the direction (d) of the analog stick. The speed is defined as the magnitude of the movement + * of the analog stick multiplied by an arbitrary number (set as 4). This method only deals with the diagonal + * movements of the friendly ship (Northwest, Northeast, Southwest, Southeast). + * @param d - direction of analog stick + * @param mag - magnitude of movement of analog stick + */ + void update_diagonal(Direction d,float mag); /** Check Friendly Position