ELEC2645 (2018/19) / Mbed 2 deprecated el17st

Dependencies:   mbed FATFileSystem

Committer:
rottenegg
Date:
Thu May 09 06:27:11 2019 +0000
Revision:
22:0a12e52848fc
Parent:
21:f3b0ce18b44f
Child:
23:5afb0e9b5b11
CaMove Documentation Not showing?

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