ELEC2645 (2018/19) / Mbed 2 deprecated el17zl

Dependencies:   mbed

Fork of el17zl by Zhenwen Liao

Committer:
franklzw
Date:
Wed May 08 17:12:44 2019 +0000
Revision:
17:9e6eb80478d3
Parent:
16:27c284d8b01b
Child:
18:e9e5df6ffb87
Final Version! Sokoban game with 3 level and a short introduction. Use the joystick to select from menu and enter each level. Use bottom A,B,X,Y to play the game, and tone can be adjusted by potentialmeter. Enjoy!

Who changed what in which revision?

UserRevisionLine numberNew contents of line
franklzw 2:9f0d9516a6cd 1 #ifndef PUSHINGENGINE_H
franklzw 2:9f0d9516a6cd 2 #define PUSHINGENGINE_H
franklzw 2:9f0d9516a6cd 3
franklzw 2:9f0d9516a6cd 4 #include "mbed.h"
franklzw 2:9f0d9516a6cd 5 #include "N5110.h"
franklzw 2:9f0d9516a6cd 6 #include "Gamepad.h"
franklzw 2:9f0d9516a6cd 7 #include "Ppl.h"
franklzw 2:9f0d9516a6cd 8 #include "Box.h"
franklzw 8:83891ea9a5d9 9 #include "Cross.h"
franklzw 10:726c1489894e 10 #include "Menu.h"
franklzw 8:83891ea9a5d9 11
franklzw 13:5930f0e5889d 12 /** PushingEngine Class
franklzw 13:5930f0e5889d 13 @author Zhenwen liao, University of Leeds
franklzw 13:5930f0e5889d 14 @brief Controls the Pushing and Determining Move in the Sokoban game
franklzw 13:5930f0e5889d 15 @date 04 2019
franklzw 13:5930f0e5889d 16 */
franklzw 2:9f0d9516a6cd 17 class PushingEngine
franklzw 2:9f0d9516a6cd 18 {
franklzw 2:9f0d9516a6cd 19
franklzw 2:9f0d9516a6cd 20 public:
franklzw 13:5930f0e5889d 21 /** Constructor */
franklzw 2:9f0d9516a6cd 22 PushingEngine();
franklzw 17:9e6eb80478d3 23
franklzw 13:5930f0e5889d 24 /** Destructor */
franklzw 2:9f0d9516a6cd 25 ~PushingEngine();
franklzw 2:9f0d9516a6cd 26
franklzw 13:5930f0e5889d 27 /** Initialise the first position of two boxes, ppl, two crosses and initial reading of Menu parameter
franklzw 13:5930f0e5889d 28 * @param box1_x @details the first x position of box1 (int)
franklzw 13:5930f0e5889d 29 * @param box1_y @details the first y position of box1 (int)
franklzw 13:5930f0e5889d 30 * @param box2_x @details the first x position of box2 (int)
franklzw 13:5930f0e5889d 31 * @param box2_y @details the first y position of box2 (int)
franklzw 13:5930f0e5889d 32 * @param ppl_x @details the first x position of ppl (int)
franklzw 13:5930f0e5889d 33 * @param ppl_y @details the first y position of ppl (int)
franklzw 13:5930f0e5889d 34 * @param cross1_x @details the first x position of cross1 (int)
franklzw 13:5930f0e5889d 35 * @param cross1_y @details the first y position of cross1 (int)
franklzw 13:5930f0e5889d 36 * @param cross2_x @details the first x position of cross2 (int)
franklzw 13:5930f0e5889d 37 * @param cross2_y @details the first y position of cross2 (int)
franklzw 13:5930f0e5889d 38 * @param menu_x @details the initial reading of level for menu (int)
franklzw 13:5930f0e5889d 39 * @param menu_y @details the initial reading of level confirmation for menu (int)
franklzw 13:5930f0e5889d 40 */
franklzw 8:83891ea9a5d9 41 void init(int box1_x,int box1_y,int box2_x,int box2_y,int ppl_x,int ppl_y,
franklzw 10:726c1489894e 42 int cross1_x,int cross1_y,int cross2_x,int cross2_y,int menu_x,int menu_y);
franklzw 17:9e6eb80478d3 43
franklzw 13:5930f0e5889d 44 /** Read the inputs from Gamepad
franklzw 13:5930f0e5889d 45 * @details Read the potentialmeter, botton A,B,X,Y
franklzw 17:9e6eb80478d3 46 * the reading of potentialmeter is used to control the tone of A,B,X,Y
franklzw 17:9e6eb80478d3 47 * being pressed
franklzw 13:5930f0e5889d 48 */
franklzw 2:9f0d9516a6cd 49 void read_input(Gamepad &pad);
franklzw 17:9e6eb80478d3 50
franklzw 13:5930f0e5889d 51 /** Update the Pushing and determining proccess
franklzw 13:5930f0e5889d 52 * @details First,check box and ppl touching or not
franklzw 13:5930f0e5889d 53 * Second,check if next move of ppl will kick the box out off screen
franklzw 13:5930f0e5889d 54 * Third, update the parameter accorading to the determination of above function
franklzw 13:5930f0e5889d 55 * Fourth, reset the indicator for function
franklzw 13:5930f0e5889d 56 * Fifth, check if ppl will cover the box after next move
franklzw 13:5930f0e5889d 57 * Sixth, check if box and cross match each other and set the scores
franklzw 13:5930f0e5889d 58 */
franklzw 6:6b083e22cb53 59 void update(Gamepad &pad,int barrier_x,int barrier_y);
franklzw 17:9e6eb80478d3 60
franklzw 13:5930f0e5889d 61 /** Draw all the oject on the screen*/
franklzw 6:6b083e22cb53 62 void draw(N5110 &lcd,int barrier_x,int barrier_y);
franklzw 17:9e6eb80478d3 63
franklzw 13:5930f0e5889d 64 /** Set the score */
franklzw 9:1fa7f087051e 65 int set_score();
franklzw 17:9e6eb80478d3 66
franklzw 17:9e6eb80478d3 67 /** Get the Menu
franklzw 13:5930f0e5889d 68 * @return the parameter of current Menu state
franklzw 13:5930f0e5889d 69 */
franklzw 11:f5d0ea7e4b74 70 Vector2D get_menu(N5110 &lcd,Gamepad &pad);
franklzw 17:9e6eb80478d3 71
franklzw 17:9e6eb80478d3 72 /** Reset the menu parameter to level 1 and not confrim*/
franklzw 17:9e6eb80478d3 73 void reset_menu();
franklzw 17:9e6eb80478d3 74
franklzw 17:9e6eb80478d3 75 /** Print a set of short introuduction of the game */
franklzw 17:9e6eb80478d3 76 void introduction(N5110 &lcd,Gamepad &pad);
franklzw 2:9f0d9516a6cd 77
franklzw 2:9f0d9516a6cd 78 private:
franklzw 17:9e6eb80478d3 79
franklzw 17:9e6eb80478d3 80 /**
franklzw 17:9e6eb80478d3 81 * @brief Check if ppl is touching box1
franklzw 17:9e6eb80478d3 82 * @details First get the current position of ppl and box1.
franklzw 17:9e6eb80478d3 83 * Then, calculate the x,y position difference between
franklzw 17:9e6eb80478d3 84 * box1 and ppl respectively.
franklzw 17:9e6eb80478d3 85 * If y positions are the same,x of ppl is 8-pixel bigger than x of box1
franklzw 17:9e6eb80478d3 86 * that is to say ppl is on the right hand side of the box1 and box can be
franklzw 17:9e6eb80478d3 87 * pushed to left. In this case, the indicactor for box1 _s will be set to 1.
franklzw 17:9e6eb80478d3 88 * And so as the rest of cases,indicactor will be set to 2 for pushing right,
franklzw 17:9e6eb80478d3 89 * 3 for pusing down, 4 for pushing up and 0 for ppl and box1 not touching.
franklzw 17:9e6eb80478d3 90 */
franklzw 4:750d3f9b54de 91 void check_ppl_box1_touching(Gamepad &pad);
franklzw 17:9e6eb80478d3 92
franklzw 17:9e6eb80478d3 93 /**
franklzw 17:9e6eb80478d3 94 * @brief Check if ppl is touching box2, Basically the same as check_ppl_box1_touching
franklzw 17:9e6eb80478d3 95 * @details If y positions are the same,x of ppl is 8-pixel bigger than x of box2
franklzw 17:9e6eb80478d3 96 * that is to say ppl is on the right hand side of the box2 and box can be
franklzw 17:9e6eb80478d3 97 * pushed to left. In this case, the indicactor for box1 _r will be set to 1.
franklzw 17:9e6eb80478d3 98 * And so as the rest of cases,indicactor will be set to 2 for pushing right,
franklzw 17:9e6eb80478d3 99 * 3 for pusing down, 4 for pushing up and 0 for ppl and box1 not touching.
franklzw 17:9e6eb80478d3 100 */
franklzw 4:750d3f9b54de 101 void check_ppl_box2_touching(Gamepad &pad);
franklzw 17:9e6eb80478d3 102
franklzw 17:9e6eb80478d3 103 /**
franklzw 17:9e6eb80478d3 104 * @brief Hold the position of Ppl when box1 face against the wall
franklzw 17:9e6eb80478d3 105 * @details There is a scenario which the box is against the wall, if ppl try to push
franklzw 17:9e6eb80478d3 106 * the box to wall side. The box will go off screen, and ppl will take its position.
franklzw 17:9e6eb80478d3 107 * To fix this, i implement this function which works with the check_ppl_box1_touching function.
franklzw 17:9e6eb80478d3 108 * First, check the position of the box,to see if it is on the egde of the screen.
franklzw 17:9e6eb80478d3 109 * Then, use the indicator from box&ppl touching function to see if the off-screen will happen.
franklzw 17:9e6eb80478d3 110 * If the x position of box1 is equal to 2 which means it cant be move left any more.
franklzw 17:9e6eb80478d3 111 * Then, we check if the touching indicator _s is equal to 1. If true, we will
franklzw 17:9e6eb80478d3 112 * set a new indicator _temp to 1. This indicator _temp is then use to freeze the ppl posision
franklzw 17:9e6eb80478d3 113 * in its own class fucntion. The rest of the cases is implemented in the same way.
franklzw 17:9e6eb80478d3 114 */
franklzw 6:6b083e22cb53 115 void hold_ppl_box1_wall(Gamepad &pad);
franklzw 17:9e6eb80478d3 116
franklzw 17:9e6eb80478d3 117 /**
franklzw 17:9e6eb80478d3 118 * @brief Hold the position of Ppl when box2 face against the wall. Basically the same as hold_ppl_box1_wall
franklzw 17:9e6eb80478d3 119 * @details There is a scenario which the box is against the wall, if ppl try to push
franklzw 17:9e6eb80478d3 120 * the box to wall side. The box will go off screen, and ppl will take its position.
franklzw 17:9e6eb80478d3 121 * To fix this, i implement this function which works with the check_ppl_box2_touching function.
franklzw 17:9e6eb80478d3 122 * First, check the position of the box,to see if it is on the egde of the screen.
franklzw 17:9e6eb80478d3 123 * Then, use the indicator from box&ppl touching function to see if the off-screen will happen.
franklzw 17:9e6eb80478d3 124 * If the x position of box1 is equal to 2 which means it cant be move left any more.
franklzw 17:9e6eb80478d3 125 * Then, we check if the touching indicator _r is equal to 1. If true, we will
franklzw 17:9e6eb80478d3 126 * set a new indicator _temp to 1. This indicator _temp is then use to freeze the ppl posision
franklzw 17:9e6eb80478d3 127 * in its own class fucntion. The rest of the cases is implemented in the same way.
franklzw 17:9e6eb80478d3 128 */
franklzw 6:6b083e22cb53 129 void hold_ppl_box2_wall(Gamepad &pad);
franklzw 17:9e6eb80478d3 130
franklzw 17:9e6eb80478d3 131 /** Check if ppl cover the box
franklzw 17:9e6eb80478d3 132 * @details Check if move ppl cover the box then result input back into the update function to determine
franklzw 17:9e6eb80478d3 133 * if ppl will be moved or stay at it last position
franklzw 17:9e6eb80478d3 134 * @return True if moving ppl will cover box; false if not
franklzw 17:9e6eb80478d3 135 */
franklzw 7:6f8aeadc4370 136 bool ppl_cover_box(Gamepad &pad);
franklzw 17:9e6eb80478d3 137
franklzw 17:9e6eb80478d3 138 /** Check box cover cross1 and score
franklzw 17:9e6eb80478d3 139 * @details Check if box cover which means the player get one score.
franklzw 17:9e6eb80478d3 140 * First, the positon of cross1, box1 and box2 are read. Then, compare both of cross1 to box1
franklzw 17:9e6eb80478d3 141 cross1 to box2. If any of these two comparsion return true, one score will be added
franklzw 17:9e6eb80478d3 142 */
franklzw 8:83891ea9a5d9 143 void box_cover_cross1_score(Gamepad &pad);
franklzw 17:9e6eb80478d3 144
franklzw 17:9e6eb80478d3 145 /** Check box cover cross2 and score
franklzw 17:9e6eb80478d3 146 * @details Check if box cover which means the player get one score.
franklzw 17:9e6eb80478d3 147 * First, the positon of cross2, box1 and box2 are read. Then, compare both of cross2 to box1
franklzw 17:9e6eb80478d3 148 cross2 to box2. If any of these two comparsion return true, one score will be added
franklzw 17:9e6eb80478d3 149 */
franklzw 8:83891ea9a5d9 150 void box_cover_cross2_score(Gamepad &pad);
franklzw 2:9f0d9516a6cd 151
franklzw 2:9f0d9516a6cd 152 Box _b1;
franklzw 2:9f0d9516a6cd 153 Box _b2;
franklzw 2:9f0d9516a6cd 154
franklzw 2:9f0d9516a6cd 155 // positions of the Boxes
franklzw 2:9f0d9516a6cd 156 int _b1x;
franklzw 2:9f0d9516a6cd 157 int _b2x;
franklzw 2:9f0d9516a6cd 158 int _b1y;
franklzw 2:9f0d9516a6cd 159 int _b2y;
franklzw 2:9f0d9516a6cd 160
franklzw 2:9f0d9516a6cd 161 Ppl _ppl;
franklzw 2:9f0d9516a6cd 162
franklzw 2:9f0d9516a6cd 163 // poitions of the ppl
franklzw 2:9f0d9516a6cd 164 int _pplx;
franklzw 2:9f0d9516a6cd 165 int _pply;
franklzw 17:9e6eb80478d3 166
franklzw 8:83891ea9a5d9 167 Cross _c1;
franklzw 8:83891ea9a5d9 168 Cross _c2;
franklzw 17:9e6eb80478d3 169
franklzw 8:83891ea9a5d9 170 // position of the crosses
franklzw 8:83891ea9a5d9 171 int _c1x;
franklzw 8:83891ea9a5d9 172 int _c2x;
franklzw 8:83891ea9a5d9 173 int _c1y;
franklzw 8:83891ea9a5d9 174 int _c2y;
franklzw 17:9e6eb80478d3 175
franklzw 10:726c1489894e 176 Menu _menu;
franklzw 17:9e6eb80478d3 177
franklzw 11:f5d0ea7e4b74 178 //reading of bottom
franklzw 5:b50ce6160013 179 int _bb;
franklzw 5:b50ce6160013 180 int _bx;
franklzw 5:b50ce6160013 181 int _by;
franklzw 5:b50ce6160013 182 int _ba;
franklzw 5:b50ce6160013 183 int _bjoy;
franklzw 10:726c1489894e 184 int _bj;
franklzw 2:9f0d9516a6cd 185
franklzw 17:9e6eb80478d3 186 //indicator for ppl and ppl touching
franklzw 5:b50ce6160013 187 int _s; //ppl with b1
franklzw 5:b50ce6160013 188 int _r; //ppl with b2
franklzw 11:f5d0ea7e4b74 189
franklzw 17:9e6eb80478d3 190 //indicator for holding ppl position
franklzw 17:9e6eb80478d3 191 int _temp;
franklzw 17:9e6eb80478d3 192
franklzw 8:83891ea9a5d9 193 int _score;
franklzw 2:9f0d9516a6cd 194
franklzw 2:9f0d9516a6cd 195 };
franklzw 2:9f0d9516a6cd 196
franklzw 2:9f0d9516a6cd 197 #endif