ELEC2645 (2018/19) / Mbed 2 deprecated el17arm

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Sprites.h Source File

Sprites.h

00001 #ifndef SPRITES_H
00002 #define SPRITES_H
00003 
00004 #include "mbed.h"
00005 #include "N5110.h"
00006 #include "Gamepad.h"
00007 
00008 /////////// All sprite bitmaps ////////////////
00009 
00010 // right facing miner bitmap
00011 const int miner_right[24] =   {
00012         
00013         1,1,1,
00014         1,1,0,
00015         1,1,1,
00016         0,1,0,
00017         1,1,1,
00018         1,1,1,
00019         0,1,0,
00020         0,1,1,
00021 
00022     };
00023 // left facing miner bitmap
00024 const int miner_left[24] =   {
00025         
00026         1,1,1,
00027         0,1,1,
00028         1,1,1,
00029         0,1,0,
00030         1,1,1,
00031         1,1,1,
00032         0,1,0,
00033         1,1,0,
00034     };
00035 // enemy bitmap
00036 const int enemy[15] =   {
00037         
00038         1,1,1,
00039         1,0,1,
00040         1,1,1,
00041         0,1,0,
00042         1,1,1,
00043     };
00044 // key bitmap
00045 const int key[12] =   {
00046         
00047         1,1,0,0,
00048         1,0,1,1,
00049         1,1,0,1,
00050     };
00051 // blank bitmap to show key collected
00052 const int key_collected[12] =   {
00053         
00054         0,0,0,0,
00055         0,0,0,0,
00056         0,0,0,0,
00057     };
00058 // trap bitmap
00059 const int spike[9] =   {
00060         
00061         1,0,1,
00062         0,1,0,
00063         1,0,1,
00064     };
00065 // solid block bitmap
00066 const int solid_block[18] =   {
00067         
00068         1,1,1,1,1,1,
00069         1,0,1,1,0,1,
00070         1,1,1,1,1,1,
00071     };
00072 // level exit bitmap
00073 const int door[30] = {
00074         
00075         1,1,1,1,1,
00076         1,0,1,0,1,
00077         1,1,0,1,1,
00078         1,0,1,0,1,
00079         1,1,0,1,1,
00080         1,0,1,0,1,
00081 };
00082 
00083 
00084 ///////////////////////////////////////////////////////////////////////////////
00085 
00086 /** Sprites Class
00087 @details Draws sprites and sets all collision rules 
00088 @author Andrew Milner University of Leeds
00089 @date April 2019
00090 */ 
00091 
00092 class Sprites
00093 {
00094 
00095 public:
00096     /** Constructor
00097     */
00098     Sprites();
00099     /** Deconstructor
00100     */ 
00101     ~Sprites();
00102     /** Gets x and y position of player.
00103     */
00104     Vector2D get_pos();
00105     /** States starting position of player.
00106     @param x miner position.
00107     @param y miner position.
00108     */
00109     void miner_init(int x, int y);
00110     /** Updates player x position.
00111     @details Updates x positon depending on direction of joystick. Will only update 
00112     * position if no collision detected. Function also stops player leaving screen 
00113     * at the left and right sides of screen.
00114     @param d Direction enum from Gamepad library.
00115     */
00116     void miner_move(Direction d, N5110 &lcd);
00117     /** Draws sprite facing direction moving.
00118     @details _direction states if player left or right facing, function draws
00119     * left or right facing sprite according.
00120     */
00121     void miner_draw(N5110 &lcd);
00122     /** States when player is on a platform.
00123     @details if _jump true then player can jump, this prevents mid-air jumping.
00124     * Also states conditions for gravity so player will fall unless on top
00125     * of a platform.
00126     */    
00127     void miner_land(N5110 &lcd);
00128     /** Contains jump conditions and updates player position while jumping.
00129     @details When A button pressed players y position increases until preset value
00130     * is reached. Jump flag is true while player y is increasing and false all other
00131     * times, this prevents double jumping.
00132     */
00133     void miner_jump(N5110 &lcd, Gamepad &pad);
00134     /** Player falls if not on platform.
00135     */
00136     void miner_gravity(N5110 &lcd);
00137     /** initialises enemies
00138     @param i index these will always be 0, 1, 2 so each enemy is treated individually.
00139     @param x position.
00140     @param y position.
00141     @param distance in pixels enemy to travel.
00142     */
00143     void enemy_init(int i, int x, int y, int d);
00144     /** Updates enemy movement.
00145     @param i index these will always be 0 - 2 so each enemy is treated individually.
00146     @param velocity how many pixels enemies will move each loop.
00147     */
00148     void enemy_move(int i, double v, N5110 &lcd);
00149     /** States conditions for collision between enemy and player.
00150     @details uses get_pos() and detects if any overlap between player and enemies.
00151     @param i index these will always be 0, 1, 2 so each enemy is treated individually.
00152     @return will return true if collision detected.
00153     */
00154     bool enemy_collision(int i);
00155     /** States conditions for drawing and collection of keys.
00156     @details Each key is displayed while key flag is false, once collected, key is deleted 
00157     * and flag is changed to true.
00158     @param k index so each key is treated independently
00159     @param x key position.
00160     @param y key position.
00161     */
00162     void key_collect(int k, int x, int y, N5110 &lcd, Gamepad &pad);
00163     /** Counts keys collected.
00164     @return number of keys collected.
00165     */
00166     int keys_collected();
00167     /** makes _keys zero
00168     @details When game over occurs keys need to be reset to zero
00169     */
00170     void zero_keys();
00171     /** Draws level exit and detects player collision
00172     @details takes player position from get_pos() and detects if overlap between
00173     player and the exit sprite.
00174     @param x exit position.
00175     @param y exit position.
00176     */
00177     bool exit_level(int x, int y, N5110 &lcd);
00178     /** Draws traps and detects player collision
00179     @details takes player position from get_pos() and detects if overlap between
00180     player and the trap sprite.
00181     @param x trap position.
00182     @param y trap position.
00183     */
00184     bool trap(int x, int y, N5110 &lcd);
00185     /** Draws blocks and detects player collision.
00186     @param d direction so player can turn around if collision with block.
00187     @Param i index so collision rules between blocks dont clash, will always be numbered 0 - 4.
00188     @param x block position.
00189     @param y block position.
00190     */
00191     void blocks(Direction d, int i, int x, int y, N5110 &lcd);
00192     /** Checks if player has collided with any blocks.
00193     @return if player is contact with any block returns true.
00194     */
00195     bool block_collision();
00196     /** Draws sinking blocks and detects player contact.
00197     @param x1 sinking block starting position.
00198     @param y sinking block position.
00199     @param x2 Sinking block finish position.
00200     */
00201     void soft_blocks(int x1, int y, int x2, N5110 &lcd);
00202     
00203     bool _key[5]; /*initialise key flag, will only draw key if false, array identifies keys 
00204     individually, made global variable as key_reinit() needs to reset flag at start of each level*/
00205     
00206 private:
00207 
00208     int _direction;
00209     bool _gravity;
00210     bool _jump;
00211     int _y;
00212     int _x;
00213     bool _j_flag;
00214     int _j_counter;
00215     
00216     double _kx[5]; // key x position
00217     double _ky[5]; // key y position
00218     int _keys; // keeps count of how many keys collected
00219     
00220     bool _eflag[5];  // initialise enemy flag, array identifies each enemy individually 
00221     double _ex[5];   // enemy x position 
00222     double _ey[5];   // enemy y position 
00223     int _counter[5]; // enemy counter, counts pixels moved 
00224     int _distance[5];// distance enemy will travel before turning round 
00225     
00226     double _bx[5];   // block x position array identifies each block individually 
00227     double _by[5];   // block y position array 
00228     bool _collision[5]; // collision indicator for each individual block 
00229        
00230 };
00231 #endif