ELEC2645 (2018/19) / Mbed 2 deprecated el17st

Dependencies:   mbed FATFileSystem

Committer:
rottenegg
Date:
Fri Apr 26 02:31:11 2019 +0000
Revision:
10:4fda7b01484a
Parent:
9:ac396c818874
Child:
13:95323e0f2fcb
CaMove: Added Additional Chaser Character Model.; SceneCreator: Added RNG input to cause Minor Changes.; SceneFunctions: Added Scene 2, 3 Functions.; Ready For Main GAME MANAGER...

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 5:860087ff295e 16 #include "N5110.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 5:860087ff295e 23 N5110 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 5:860087ff295e 85 @endcode
rottenegg 5:860087ff295e 86 */
rottenegg 5:860087ff295e 87
rottenegg 4:34bf3587cf42 88 #include "mbed.h"
rottenegg 4:34bf3587cf42 89 #include "N5110.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 4:34bf3587cf42 102 class CaMove {
rottenegg 9:ac396c818874 103 friend class AI;
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 5:860087ff295e 125 /** 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 126 *
rottenegg 5:860087ff295e 127 *@param lcd N5110 Class Object.
rottenegg 5:860087ff295e 128 *
rottenegg 5:860087ff295e 129 */
rottenegg 5:860087ff295e 130 void move(N5110 &lcd);
rottenegg 5:860087ff295e 131 /** Function to check if charater is in the screen.
rottenegg 5:860087ff295e 132 *
rottenegg 6:6f84347e9e14 133 *@return Returns true if charater is on the screen otherwise false.
rottenegg 5:860087ff295e 134 *
rottenegg 5:860087ff295e 135 */
rottenegg 5:860087ff295e 136 bool in_screen();
rottenegg 5:860087ff295e 137 /** Function to set an interative region (rectangle areas only). Everytime it is run creates an additional new region.
rottenegg 5:860087ff295e 138 *
rottenegg 5:860087ff295e 139 *@param xmin the top left x-location of region.
rottenegg 5:860087ff295e 140 *@param ymin the top left y-location of region.
rottenegg 5:860087ff295e 141 *@param xl Width of region.
rottenegg 5:860087ff295e 142 *@param yl Height of region.
rottenegg 5:860087ff295e 143 *
rottenegg 5:860087ff295e 144 */
rottenegg 5:860087ff295e 145 void set_region(int xmin, int ymin, int xl, int yl);
rottenegg 5:860087ff295e 146 /** Function to delete all interative regions. */
rottenegg 4:34bf3587cf42 147 void delete_regions();
rottenegg 5:860087ff295e 148 /** Function that checks if the charater triggered an interative region (set to run on button press). */
rottenegg 4:34bf3587cf42 149 void intercheck();
rottenegg 5:860087ff295e 150 /** Function that checks if an interative region was triggered.
rottenegg 5:860087ff295e 151 *
rottenegg 5:860087ff295e 152 *@returns Returns true only when an interactive region was triggered for that instance but otherwise remains false. */
rottenegg 5:860087ff295e 153 bool is_trg();
rottenegg 5:860087ff295e 154 /** Function that tells which interactive was triggered.
rottenegg 5:860087ff295e 155 *
rottenegg 6:6f84347e9e14 156 *@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 157 *
rottenegg 5:860087ff295e 158 */
rottenegg 5:860087ff295e 159 int get_treg();
rottenegg 9:ac396c818874 160 /** Function that initilizes chasing Charater Models. */
rottenegg 9:ac396c818874 161 void AIinit();
rottenegg 9:ac396c818874 162 /** Function that spawns the chasing Charater.
rottenegg 9:ac396c818874 163 *
rottenegg 9:ac396c818874 164 *@param x The x-location where Charater will Spawn.
rottenegg 9:ac396c818874 165 *@param y The y-location where Charater will Spawn.
rottenegg 9:ac396c818874 166 */
rottenegg 9:ac396c818874 167 void spawn(int x, int y);
rottenegg 9:ac396c818874 168 /** Function that moves the Charater per Frame.
rottenegg 9:ac396c818874 169 *@param lcd The N5110 class object.
rottenegg 9:ac396c818874 170 */
rottenegg 10:4fda7b01484a 171 void chase(N5110 &lcd, int girl);
rottenegg 9:ac396c818874 172 /** Function that tell if the player is caught by chaser Charater.
rottenegg 9:ac396c818874 173 *@returns Returns a boolean true is caught and false if not.
rottenegg 9:ac396c818874 174 */
rottenegg 9:ac396c818874 175 bool is_caught();
rottenegg 5:860087ff295e 176
rottenegg 5:860087ff295e 177 private :
rottenegg 5:860087ff295e 178 //Hardware Pins
rottenegg 5:860087ff295e 179 InterruptIn *_check;
rottenegg 5:860087ff295e 180 AnalogIn *_hoz;
rottenegg 5:860087ff295e 181 AnalogIn *_ver;
rottenegg 5:860087ff295e 182 //Enum declaration
rottenegg 5:860087ff295e 183 Direction _fc;
rottenegg 5:860087ff295e 184 //basic variables
rottenegg 4:34bf3587cf42 185 int _h;
rottenegg 4:34bf3587cf42 186 int _v;
rottenegg 4:34bf3587cf42 187 short int _itr;
rottenegg 5:860087ff295e 188 //Vector for storing interactive region x - y locations.
rottenegg 5:860087ff295e 189 std::vector<int> _vreg;
rottenegg 5:860087ff295e 190 //Volatile variables for ISR Interrupts
rottenegg 5:860087ff295e 191 volatile int _treg;
rottenegg 5:860087ff295e 192 volatile bool _trg;
rottenegg 9:ac396c818874 193 void rend(N5110 &lcd);
rottenegg 9:ac396c818874 194 //AI Componets
rottenegg 9:ac396c818874 195 bool enabled;
rottenegg 9:ac396c818874 196 int _ch;
rottenegg 9:ac396c818874 197 int _cv;
rottenegg 4:34bf3587cf42 198 };
rottenegg 4:34bf3587cf42 199
rottenegg 5:860087ff295e 200 #endif