Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
Fork of el17dg by
game/player.h
- Committer:
 - Noximilien
 - Date:
 - 2019-04-03
 - Revision:
 - 25:749f1efc31fc
 - Parent:
 - 23:240bc00ef25b
 - Child:
 - 26:676874c42883
 
File content as of revision 25:749f1efc31fc:
#ifndef PLAYER_H
#define PLAYER_H
#include "constants.h"
#include "game.h"
const int max_player_blasts = 5;
const int ship_speed = 2;
GameObject blasts[max_player_blasts];
GameObject player;
CircleBounds player_bounds;
CircleBounds blast_bounds;
class Player: public GameObject {
public:
Player() {
    player_bounds.center.x = 5;
    player_bounds.center.y = 7;
    player_bounds.radius = 7;
    blast_bounds.center.x = 0;
    blast_bounds.center.y = 1;
    blast_bounds.radius = 1;
}
    /**@brief
     * Will move every active blast to the left with blast speed.
     * Will deactivate blasts when they live screen, for future reuse
     */
    void updateAndDrawBlasts() {
        const int blast_speed = 5;
        for (int i = 0; i < max_player_blasts; ++i) {
            if (blasts[i].active) {
                blasts[i].pos.x += blast_speed;
                if (blasts[i].pos.x >= screen_width){
                    blasts[i].active = false;
                    game_score -= 10;
                }
                lcd.setPixel(blasts[i].pos.x,   blasts[i].pos.y, 1);
                lcd.setPixel(blasts[i].pos.x+1, blasts[i].pos.y, 1);
                lcd.setPixel(blasts[i].pos.x+2, blasts[i].pos.y, 1);
            }
        }
    }
        
    /**@brief
      * This function searches the array for the inactive blasts,
      * If a blast is set to not active, it will set it active and start drawing
      * it accross the screen until it reaches the LCD border line.
      */
    void fireNewBlast() {
    // Search the array of blasts if inactive we can use it.
        int found = -1;
        for (int i = 0; i < max_player_blasts; ++i) {
            if (!blasts[i].active) {
                found = i;
                break;
            }
        }
        
        if (found != -1) {
            blasts[found].active = true;
            blasts[found].pos.x = player.pos.x + spaceship1_width;
            blasts[found].pos.y = player.pos.y + (spaceship1_height/2);
        }
    }    
    /**@brief
      * The function reads the analog input signal from joystick and moves the 
      * player's ship on the LCD accordingly.(move joystic, the ship moves up
      * move joystick right, the ship moves right). Also, It prevents the player's 
      * ship to go beyond the playing zone limits.
      */
    void playerShipMovement(){
        if(x_dir.read() > joy_threshold_max_x){
           player.pos.x -= ship_speed;
        }
        else if(x_dir.read() < joy_threshold_min_x){
           player.pos.x += ship_speed;
        }
        if(y_dir.read() > joy_threshold_max_y){
           player.pos.y -= ship_speed; 
        }
        else if(y_dir.read() < joy_threshold_min_y){
           player.pos.y += ship_speed; 
        }
        //Limits player ship on screen
        if (player.pos.x < game_area_x){ player.pos.x = game_area_x;}     
        if (player.pos.y < game_area_y) { player.pos.y = game_area_y;}
        int max_player_x = game_area_x + game_area_width - spaceship1_width;
        int max_player_y = game_area_y + game_area_height - spaceship1_height;
        if (player.pos.x > max_player_x) player.pos.x = max_player_x;
        if (player.pos.y > max_player_y) player.pos.y = max_player_y;
    }  
};
#endif
            
    