Initial publish

Dependencies:   mbed

Fork of el17dg by Dmitrijs Griskovs

game/player.h

Committer:
Noximilien
Date:
2019-04-10
Revision:
28:35af3843de8f
Parent:
27:f05f4e738ba9
Child:
29:579e00b7f118

File content as of revision 28:35af3843de8f:

#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:

/** A constructor for the palyer's sprite body circle area and the blast circle 
  * area. It sets the circle radius for collsion callculations.
  */
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;
}
   /** 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;
                    score_count_for_difficulty -= 50;
                }
                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);
            }
        }
    }
        
    /** 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);
        }
    }    
    /** The function reads the analog input signal from joystick and moves the 
      * player's ship on the LCD accordingly.(move joystick, the ship moves up
      * move joystick right, the ship moves right). 
      */
    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; 
        }
        shipMovementLimits();
        drawSpriteOnTop(player.pos, player_spaceship1_sprite);
    }
private:
/** Prevents the player's ship to go beyond the playing zone limits.*/
    void shipMovementLimits(){
        //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