ELEC2645 (2018/19) / Mbed 2 deprecated el17arm

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Sprites.cpp Source File

Sprites.cpp

00001 #include "Sprites.h"
00002 
00003 Sprites::Sprites()
00004 {
00005     _j_flag = false; // indicates when player can jump so player cannot perform midair jump
00006 }
00007 
00008 Sprites::~Sprites()
00009 {
00010 }
00011 // player starting position
00012 void Sprites::miner_init(int x, int y)
00013 {
00014     _x = x;
00015     _y = y;
00016     
00017 }
00018 
00019 // vector gets player position
00020 Vector2D Sprites::get_pos()
00021 {
00022     Vector2D p = {_x,_y};
00023     return p;
00024     //printf("x pos = %d \n",_x);
00025     //printf("y pos = %d \n",_Y);
00026 }
00027 
00028 // player movement and updates which way player is facing
00029 void Sprites::miner_move(Direction d, N5110 &lcd)
00030 {
00031     if ((d==E || d==NE) && block_collision() == false) {
00032         _x++;
00033         _direction = 1; //chooses right facing sprite
00034     }
00035     if ((d==W || d==NW) && block_collision() == false) {
00036         _x--;
00037         _direction = 0; //chooses left facing sprite
00038     }
00039     // ensures player cannot move off side of screen
00040     if(_x > 81) {
00041         _x=WIDTH-3;
00042     }
00043     if(_x < 0) {
00044         _x=0;
00045     }
00046 }
00047 
00048 // draws left or right facing sprite based on miner_move function
00049 void Sprites::miner_draw(N5110 &lcd)
00050 {
00051     if (_direction == 1) {
00052         lcd.drawSprite(_x,_y,8,3,(int *)miner_right);
00053     }
00054     if (_direction == 0) {
00055         lcd.drawSprite(_x,_y,8,3,(int *)miner_left);
00056     }
00057     //printf("direction = %d \n", _direction);  
00058 }
00059 // ensures player cannot multiple jump and lands on platforms
00060 void Sprites::miner_land(N5110 &lcd)
00061 {
00062     _jump = lcd.getPixel(_x+2,_y+8) || lcd.getPixel(_x,_y+8) || lcd.getPixel(_x+1,_y+8);
00063     // _jump only true if part of player is on a platform
00064     _gravity = !lcd.getPixel(_x,_y+8) && !lcd.getPixel(_x+2, _y+8);
00065     //_gravity true when players feet are not touching a platform
00066 }
00067 
00068 // updates player position while jumping
00069 void Sprites::miner_jump(N5110 &lcd, Gamepad &pad)
00070 {
00071     // jumps when player on ground, sets jump flag to true to prevent mid air jumping
00072     if(pad.check_event(Gamepad::A_PRESSED) && _jump == true) {
00073         _j_flag = true;
00074     }
00075     // jumps until preset jump limit is reached
00076     if(_j_flag == true) {
00077         _y--;
00078         _j_counter++;
00079         pad.tone(400 +_j_counter*10,0.1);
00080     }
00081     // resets jump flag and counter once jump is complete
00082     if(_j_counter > 7) {
00083         _j_flag = false;
00084         _j_counter = 0;
00085     }
00086     //printf("_jump = %d \n",_jump);
00087     //printf("_j_counter = %d \n",_j_counter);
00088 }
00089 
00090 // makes player fall if not on platform
00091 void Sprites::miner_gravity(N5110 &lcd)
00092 {
00093     if(_gravity == true) {
00094         _y++;
00095         //printf("_y = %d \n",_y;
00096     }
00097 }
00098 
00099 // initilises starting positions of all enemies
00100 void Sprites::enemy_init(int i, int x, int y, int d)
00101 {
00102     _counter[i] = 0;    // resets enemy counter when level complete
00103     _eflag[i] = false; // stops function once enemies inialised
00104     _ex[i] = x;        // sets x position of enemy
00105     _ey[i] = y;        // sets y position of enemy
00106     _distance[i] = d;  // sets distance enemy will move before turning
00107 }
00108 
00109 // updates enemy movements
00110 void Sprites::enemy_move(int i, double v, N5110 &lcd)
00111 {
00112     // draws enemies, indexed so each enemy treated indepently,
00113     lcd.drawSprite(_ex[i],_ey[i],5,3, (int *) enemy);
00114     if (_eflag[i] == false) {      // flag states when enemy should move in opposite direction
00115         _ex[i] = _ex[i] + v;  // enemy moves in x direction
00116         _counter[i]++;             // counter to control distance
00117     }
00118     if(_counter[i] == _distance[i]) {
00119         _eflag[i] = true;
00120     }
00121     if (_eflag[i] == true) {
00122         _ex[i] = _ex[i] - v;  // enemy will now move in opposite direction
00123         _counter[i]--;             // counter goes downwards
00124     }
00125     if (_counter[i] == 0) {
00126         _eflag[i] = false;         // when counter reaches zero, restarts from top
00127     }
00128 }
00129 // detects if player comes into contact with enemy
00130 bool Sprites::enemy_collision(int i)
00131 {
00132     Vector2D p = get_pos(); // gets player position for collision detection
00133     //checks collision by checking for overlap of sprites
00134     if (p.x < _ex[i] + 2 && p.x + 2 > _ex[i] && p.y < _ey[i] + 4
00135             && p.y + 8 > _ey[i]) {
00136         return true;
00137     }
00138     return false;
00139     //printf("_ex = %d \n",_ex);
00140     //printf("p.x = %d \n",p.x);
00141     //printf("_ey = %d \n",_ey);
00142     //printf("p.y = %d \n",p.y);
00143 }
00144 
00145 // detects when key is collected and adds to total, all parameters
00146 void Sprites::key_collect(int k, int x, int y, N5110 &lcd, Gamepad &pad)
00147 {
00148     Vector2D p = get_pos();
00149     _kx[k] = x;
00150     _ky[k] = y;
00151 
00152     // draws keys all parameters indexed so multiple keys are treated independently
00153     if(_key[k] == false) {
00154         lcd.drawSprite(_kx[k],_ky[k],3,4,(int *)key);
00155     }
00156     //checks collision by checking for overlap of sprites
00157     if ((p.x < _kx[k] + 5 && p.x + 3 > _kx[k] && p.y < _ky[k] + 3
00158             && p.y + 9 > _ky[k]) && _key[k] == false) {
00159         _keys++;
00160         pad.tone(850,0.1);
00161         lcd.drawSprite(_kx[k],_ky[k],3,4,(int *)key_collected); // deletes key on screen
00162         _key[k]= true;    // stops key being redrawn once collected
00163         //printf("_key = %d \n",_key[k]);
00164     }
00165 }
00166 // keeps count of keys collected
00167 int Sprites::keys_collected()
00168 {
00169     int k = _keys;
00170     //printf("keys = %d \n",_keys);
00171     return k;
00172 }
00173 
00174 void Sprites::zero_keys() {
00175     _keys = 0;
00176 }
00177 
00178 // checks if sprite is at the level exit
00179 bool Sprites::exit_level(int x, int y, N5110 &lcd)
00180 {
00181     Vector2D p = get_pos(); // gets player position
00182     lcd.drawSprite(x,y,6,5,(int *)door); // draws level exit
00183 
00184     // //checks collision by checking for overlap of sprites
00185     if(p.x < x + 5 && p.x + 3 > x && p.y < y + 2
00186             && p.y + 9 > y) {
00187         return true;
00188     }
00189     return false;
00190 }
00191 
00192 bool Sprites::trap(int x, int y, N5110 &lcd)
00193 {
00194     Vector2D p = get_pos(); // gets miner position
00195     lcd.drawSprite(x,y,3,3,(int *)spike); // draws traps
00196 
00197     //checks collision by checking for overlap of sprites
00198     if (p.x < x + 3 && p.x + 3 > x && p.y < y + 2
00199             && p.y + 8 > y) {
00200         return true;
00201     }
00202     return false;
00203 }
00204 
00205 void Sprites::blocks(Direction d, int b, int x, int y, N5110 &lcd)
00206 {
00207     Vector2D p = get_pos(); // gets miner position
00208     _bx[b] = x;   // sets each block position independently so no collision rule clashes
00209     _by[b] = y;
00210 
00211     lcd.drawSprite(_bx[b],_by[b],3,6,(int *)solid_block); // draws blocks
00212 
00213     //checks collision by checking for overlap of sprites
00214     if (p.x < _bx[b] + 7 && p.x + 4 > _bx[b] && p.y < _by[b] + 3 && p.y + 9 > _by[b]) {
00215         _collision[b] = true;
00216         
00217         // allows player to move away from block after collision or jump over block
00218         if ((p.x +3  <= _bx[b] && d == W) || (p.x >= _bx[b] +6  && d == E) || (p.y + 7 <  _by[b])) {
00219             _collision[b] = false;
00220         }
00221         // player cannot jump through blocks from below
00222         if (p.x < _bx[b] + 5 && p.x + 3  > _bx[b] && p.y < _by[b] +3 ) {
00223             _j_flag = false;
00224         }
00225     } else {
00226         _collision[b] = false;
00227     }
00228     //printf("_collision = %d \n",_collision[b]);
00229 }
00230 
00231 // checks block collision with each block placed
00232 bool Sprites::block_collision()
00233 {
00234     if (_collision[0] || _collision[1] || _collision[2] || _collision[3]
00235             || _collision[4]) {
00236         return true;
00237     }
00238     return false;
00239 }
00240 
00241 void Sprites::soft_blocks(int x1, int y, int x2, N5110 &lcd)
00242 {
00243     Vector2D p = get_pos(); //gets miner position
00244 
00245     lcd.drawLine(x1,y,x2,y,1);      // draws top solid part
00246     lcd.drawLine(x1,y+1,x2,y+1,2); // draws dotted bottom part
00247 
00248     // checks contact with top part
00249     if (p.x < x2 && p.x + 2 > x1 && p.y < y && p.y + 9 > y) {
00250         lcd.drawLine(x1,y,x2,y,0); // removes top part
00251     }
00252     // checks contact with bottom part
00253     if (p.x < x2 && p.x + 2 > x1 && p.y < y +1 && p.y + 9 > y +1) {
00254         lcd.drawLine(x1,y+1,x2,y+1,0); // removes bottom part
00255     }
00256 }