ELEC2645 (2018/19) / Mbed 2 deprecated el17st

Dependencies:   mbed FATFileSystem

Committer:
rottenegg
Date:
Thu May 09 13:27:19 2019 +0000
Revision:
25:df39289eb8c0
Parent:
24:7f26feb4666d
This has a bug fixed with docs

Who changed what in which revision?

UserRevisionLine numberNew contents of line
rottenegg 4:34bf3587cf42 1 #ifndef CAMOVE_H
rottenegg 4:34bf3587cf42 2 #define CAMOVE_H
rottenegg 4:34bf3587cf42 3
rottenegg 25:df39289eb8c0 4
rottenegg 25:df39289eb8c0 5 #include "mbed.h"
rottenegg 25:df39289eb8c0 6 #include "Bitmap.h"
rottenegg 25:df39289eb8c0 7 #include <vector>
rottenegg 25:df39289eb8c0 8
rottenegg 25:df39289eb8c0 9
rottenegg 25:df39289eb8c0 10 /** Enum For Charater's Facing Direction. */
rottenegg 25:df39289eb8c0 11 enum Direction {
rottenegg 25:df39289eb8c0 12 Lt, /**< Charater Facing Left. */
rottenegg 25:df39289eb8c0 13 Rt, /**< Charater Facing Right. */
rottenegg 25:df39289eb8c0 14 Fd, /**< Charater Facing Forward. */
rottenegg 25:df39289eb8c0 15 Bd /**< Charater Facing Backward. */
rottenegg 25:df39289eb8c0 16 };
rottenegg 25:df39289eb8c0 17
rottenegg 25:df39289eb8c0 18
rottenegg 5:860087ff295e 19 /** CaMove Class
rottenegg 5:860087ff295e 20 @brief Class for Charater Animation and Movement Interation.
rottenegg 5:860087ff295e 21 @brief Controls Interrupts for Interation Button and The Joystick for Charater Movement
rottenegg 5:860087ff295e 22 @brief Uses the FilePaths.h to supply File Location Information and uses Bitmap.h to create animations on the LCD
rottenegg 5:860087ff295e 23
rottenegg 5:860087ff295e 24 @author Saad Tayyab
rottenegg 5:860087ff295e 25 @date 2th April 2019
rottenegg 5:860087ff295e 26
rottenegg 5:860087ff295e 27 @code
rottenegg 5:860087ff295e 28 //Required Libraries
rottenegg 5:860087ff295e 29 #include "mbed.h"
rottenegg 5:860087ff295e 30 #include "CaMove.h"
rottenegg 17:7d4d8905b608 31 #include "Bitmap.h"
rottenegg 5:860087ff295e 32 #include "SDFileSystem.h"
rottenegg 5:860087ff295e 33
rottenegg 5:860087ff295e 34 //Define SD Card
rottenegg 5:860087ff295e 35 SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd");
rottenegg 5:860087ff295e 36
rottenegg 5:860087ff295e 37 //Define LCD
rottenegg 17:7d4d8905b608 38 Bitmap lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11);
rottenegg 5:860087ff295e 39
rottenegg 5:860087ff295e 40 //Define CaMove
rottenegg 5:860087ff295e 41 CaMove CM(PTB9,PTB11,PTB10);
rottenegg 5:860087ff295e 42
rottenegg 5:860087ff295e 43 //A mini game instance where charater can click two black boxes in either corner
rottenegg 5:860087ff295e 44 //depending on clicked box the charater will either enter a new instance where he
rottenegg 5:860087ff295e 45 //can leave the screen and when he does the program ends or the instance where the
rottenegg 5:860087ff295e 46 //screen fades to black. The program ends with a circle on sceen.
rottenegg 5:860087ff295e 47
rottenegg 5:860087ff295e 48 int main() {
rottenegg 5:860087ff295e 49 //Initalize and Set Spawn Values
rottenegg 5:860087ff295e 50 CM.init(40,20,Fd);
rottenegg 5:860087ff295e 51 lcd.init();
rottenegg 5:860087ff295e 52
rottenegg 5:860087ff295e 53 //Setting Interative Regions
rottenegg 5:860087ff295e 54 CM.set_region(6,6,8,8);
rottenegg 5:860087ff295e 55 CM.set_region(60,30,8,8);
rottenegg 5:860087ff295e 56
rottenegg 5:860087ff295e 57 //While Loop that ends when Interative Region triggered
rottenegg 5:860087ff295e 58 while(!CM.is_trg()) {
rottenegg 5:860087ff295e 59 lcd.clear();
rottenegg 5:860087ff295e 60 lcd.drawRect(2,2,70,40,FILL_TRANSPARENT);
rottenegg 5:860087ff295e 61 lcd.drawRect(3,3,68,38,FILL_TRANSPARENT);
rottenegg 5:860087ff295e 62 lcd.drawRect(4,4,8,8,FILL_BLACK);
rottenegg 5:860087ff295e 63 lcd.drawRect(62,32,8,8,FILL_BLACK);
rottenegg 5:860087ff295e 64
rottenegg 5:860087ff295e 65 //Move the Charater and Update Movement Animation
rottenegg 5:860087ff295e 66 CM.move(lcd);
rottenegg 5:860087ff295e 67 lcd.refresh();
rottenegg 5:860087ff295e 68 wait(0.1);
rottenegg 5:860087ff295e 69 }
rottenegg 5:860087ff295e 70 lcd.clear();
rottenegg 5:860087ff295e 71 //Interative Region 1 Triggered
rottenegg 5:860087ff295e 72 if (CM.get_treg() == 0) {
rottenegg 5:860087ff295e 73
rottenegg 5:860087ff295e 74 //Delete Interartive Regions
rottenegg 5:860087ff295e 75 CM.delete_regions();
rottenegg 5:860087ff295e 76 CM.init(40,20,Bd);
rottenegg 5:860087ff295e 77
rottenegg 5:860087ff295e 78 //While Loop Dependent on if Charater is In Screen
rottenegg 5:860087ff295e 79 while(CM.in_screen()) {
rottenegg 5:860087ff295e 80 lcd.clear();
rottenegg 5:860087ff295e 81 CM.move(lcd);
rottenegg 5:860087ff295e 82 lcd.refresh();
rottenegg 5:860087ff295e 83 wait(0.2);
rottenegg 5:860087ff295e 84 }
rottenegg 5:860087ff295e 85 }
rottenegg 5:860087ff295e 86
rottenegg 5:860087ff295e 87 //Interative Region 2 Triggered
rottenegg 5:860087ff295e 88 if (CM.get_treg() == 1) {
rottenegg 5:860087ff295e 89 for(int i = 0; i < 84; i++) {
rottenegg 5:860087ff295e 90 lcd.drawRect(0,0,i,48,FILL_BLACK);
rottenegg 5:860087ff295e 91 lcd.refresh();
rottenegg 5:860087ff295e 92 wait(0.05);
rottenegg 5:860087ff295e 93 }
rottenegg 5:860087ff295e 94 }
rottenegg 5:860087ff295e 95 //Ending Screen
rottenegg 5:860087ff295e 96 lcd.clear();
rottenegg 5:860087ff295e 97 lcd.drawCircle(40,20,5,FILL_TRANSPARENT);
rottenegg 5:860087ff295e 98 lcd.refresh();
rottenegg 5:860087ff295e 99 }
rottenegg 5:860087ff295e 100 @endcode
rottenegg 5:860087ff295e 101 */
rottenegg 4:34bf3587cf42 102 class CaMove {
rottenegg 5:860087ff295e 103 public :
rottenegg 5:860087ff295e 104 /** Constructor
rottenegg 5:860087ff295e 105 *
rottenegg 5:860087ff295e 106 *@param Button Pin for the button to control interations.
rottenegg 5:860087ff295e 107 *@param Pot_h Pin for the joystick horizontal potentiometer.
rottenegg 5:860087ff295e 108 *@param Pot_v Pin for the joystick vertical potentiometer.
rottenegg 5:860087ff295e 109 *
rottenegg 5:860087ff295e 110 */
rottenegg 5:860087ff295e 111 CaMove(PinName Button,
rottenegg 5:860087ff295e 112 PinName Pot_h,
rottenegg 5:860087ff295e 113 PinName Pot_v);
rottenegg 5:860087ff295e 114 /** Destructor */
rottenegg 4:34bf3587cf42 115 ~CaMove();
rottenegg 5:860087ff295e 116 /** Function to initilze the charater spawn point.
rottenegg 5:860087ff295e 117 *
rottenegg 5:860087ff295e 118 *@param x X-location on screen to spawn at (0 to 84).
rottenegg 5:860087ff295e 119 *@param y Y-location on screen to spawn at (0 to 48).
rottenegg 5:860087ff295e 120 *@param D In which direction the charater should spawn.
rottenegg 5:860087ff295e 121 *
rottenegg 5:860087ff295e 122 */
rottenegg 5:860087ff295e 123 void init(int x,int y,Direction D);
rottenegg 21:f3b0ce18b44f 124 /** Function that initilizes chasing Charater Models.
rottenegg 21:f3b0ce18b44f 125 *@param chaser Enter Integer to select chaser (1-Death Zone , 2- Girl (slow), 4-Girl (fast), other-Ghost)
rottenegg 21:f3b0ce18b44f 126 */
rottenegg 21:f3b0ce18b44f 127 void AIinit(int chaser);
rottenegg 21:f3b0ce18b44f 128
rottenegg 24:7f26feb4666d 129
rottenegg 21:f3b0ce18b44f 130 //Player Functions
rottenegg 21:f3b0ce18b44f 131
rottenegg 21:f3b0ce18b44f 132 /** Function that moves the charater but also manages all animations related to its movement and collisions. Double Black lines cannot be crossed and Single Black lines can only be crossed one way.
rottenegg 5:860087ff295e 133 *
rottenegg 17:7d4d8905b608 134 *@param lcd Bitmap Class Object.
rottenegg 5:860087ff295e 135 *
rottenegg 5:860087ff295e 136 */
rottenegg 17:7d4d8905b608 137 void move(Bitmap &lcd);
rottenegg 5:860087ff295e 138 /** Function to check if charater is in the screen.
rottenegg 5:860087ff295e 139 *
rottenegg 6:6f84347e9e14 140 *@return Returns true if charater is on the screen otherwise false.
rottenegg 5:860087ff295e 141 *
rottenegg 5:860087ff295e 142 */
rottenegg 5:860087ff295e 143 bool in_screen();
rottenegg 21:f3b0ce18b44f 144
rottenegg 21:f3b0ce18b44f 145 //Interactive Regions Function pack
rottenegg 21:f3b0ce18b44f 146
rottenegg 5:860087ff295e 147 /** Function to set an interative region (rectangle areas only). Everytime it is run creates an additional new region.
rottenegg 5:860087ff295e 148 *
rottenegg 5:860087ff295e 149 *@param xmin the top left x-location of region.
rottenegg 5:860087ff295e 150 *@param ymin the top left y-location of region.
rottenegg 5:860087ff295e 151 *@param xl Width of region.
rottenegg 5:860087ff295e 152 *@param yl Height of region.
rottenegg 5:860087ff295e 153 *
rottenegg 5:860087ff295e 154 */
rottenegg 5:860087ff295e 155 void set_region(int xmin, int ymin, int xl, int yl);
rottenegg 5:860087ff295e 156 /** Function to delete all interative regions. */
rottenegg 4:34bf3587cf42 157 void delete_regions();
rottenegg 5:860087ff295e 158 /** Function that checks if the charater triggered an interative region (set to run on button press). */
rottenegg 4:34bf3587cf42 159 void intercheck();
rottenegg 5:860087ff295e 160 /** Function that checks if an interative region was triggered.
rottenegg 5:860087ff295e 161 *
rottenegg 5:860087ff295e 162 *@returns Returns true only when an interactive region was triggered for that instance but otherwise remains false. */
rottenegg 5:860087ff295e 163 bool is_trg();
rottenegg 5:860087ff295e 164 /** Function that tells which interactive was triggered.
rottenegg 5:860087ff295e 165 *
rottenegg 6:6f84347e9e14 166 *@return Returns an integer based on which region was triggered, regions are labelled according to order of declaration in program counts from 0. Will return -1 if no regions triggered.
rottenegg 5:860087ff295e 167 *
rottenegg 5:860087ff295e 168 */
rottenegg 5:860087ff295e 169 int get_treg();
rottenegg 21:f3b0ce18b44f 170
rottenegg 21:f3b0ce18b44f 171 //Chaser Functions
rottenegg 21:f3b0ce18b44f 172
rottenegg 9:ac396c818874 173 /** Function that spawns the chasing Charater.
rottenegg 9:ac396c818874 174 *
rottenegg 9:ac396c818874 175 *@param x The x-location where Charater will Spawn.
rottenegg 9:ac396c818874 176 *@param y The y-location where Charater will Spawn.
rottenegg 9:ac396c818874 177 */
rottenegg 9:ac396c818874 178 void spawn(int x, int y);
rottenegg 9:ac396c818874 179 /** Function that moves the Charater per Frame.
rottenegg 17:7d4d8905b608 180 *@param lcd The Bitmap class object.
rottenegg 9:ac396c818874 181 */
rottenegg 20:01efa2bce75d 182 void chase(Bitmap &lcd, int girl);
rottenegg 9:ac396c818874 183 /** Function that tell if the player is caught by chaser Charater.
rottenegg 9:ac396c818874 184 *@returns Returns a boolean true is caught and false if not.
rottenegg 9:ac396c818874 185 */
rottenegg 9:ac396c818874 186 bool is_caught();
rottenegg 5:860087ff295e 187
rottenegg 5:860087ff295e 188 private :
rottenegg 5:860087ff295e 189 //Hardware Pins
rottenegg 5:860087ff295e 190 InterruptIn *_check;
rottenegg 5:860087ff295e 191 AnalogIn *_hoz;
rottenegg 5:860087ff295e 192 AnalogIn *_ver;
rottenegg 5:860087ff295e 193 //Enum declaration
rottenegg 5:860087ff295e 194 Direction _fc;
rottenegg 21:f3b0ce18b44f 195 //Priavte Variables
rottenegg 4:34bf3587cf42 196 int _h;
rottenegg 4:34bf3587cf42 197 int _v;
rottenegg 4:34bf3587cf42 198 short int _itr;
rottenegg 18:14e5391beccf 199 bool LowerH, UpperH, MidH, LowerV, UpperV, MidV;
rottenegg 21:f3b0ce18b44f 200
rottenegg 21:f3b0ce18b44f 201 std::vector<int> _vreg; //Vector for storing interactive region x - y locations.
rottenegg 21:f3b0ce18b44f 202
rottenegg 5:860087ff295e 203 //Volatile variables for ISR Interrupts
rottenegg 5:860087ff295e 204 volatile int _treg;
rottenegg 5:860087ff295e 205 volatile bool _trg;
rottenegg 21:f3b0ce18b44f 206
rottenegg 21:f3b0ce18b44f 207 //AI Pack Variables
rottenegg 9:ac396c818874 208 bool enabled;
rottenegg 9:ac396c818874 209 int _ch;
rottenegg 9:ac396c818874 210 int _cv;
rottenegg 18:14e5391beccf 211 int increment;
rottenegg 18:14e5391beccf 212
rottenegg 21:f3b0ce18b44f 213 //Internal Functions
rottenegg 21:f3b0ce18b44f 214 //Player Internal Functions
rottenegg 21:f3b0ce18b44f 215 void check_collision(bool left,bool right,bool down,bool up);
rottenegg 21:f3b0ce18b44f 216 //Interaction Internal Functions
rottenegg 18:14e5391beccf 217 void set_statements(int Vvalue);
rottenegg 18:14e5391beccf 218 void check_sides(int Vvalue);
rottenegg 21:f3b0ce18b44f 219 //Aiding Functions
rottenegg 18:14e5391beccf 220 void set_treg(int Vvalue);
rottenegg 21:f3b0ce18b44f 221 //Rendering Functions
rottenegg 21:f3b0ce18b44f 222 void rend(Bitmap &lcd);
rottenegg 21:f3b0ce18b44f 223 void render_chaser(Bitmap &lcd,int chaser);
rottenegg 21:f3b0ce18b44f 224
rottenegg 4:34bf3587cf42 225 };
rottenegg 4:34bf3587cf42 226
rottenegg 5:860087ff295e 227 #endif