Initial publish

Dependencies:   mbed

Fork of el17dg by Dmitrijs Griskovs

Committer:
Noximilien
Date:
Tue Apr 16 21:16:33 2019 +0000
Revision:
30:d454d0cb72bc
Parent:
29:579e00b7f118
Child:
31:becb8f6bf7b7
Hace modified some comments. Have added a feature of a force shield. Updated the tutorial, Have finished the settings mode.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Noximilien 23:240bc00ef25b 1 #ifndef PLAYER_H
Noximilien 23:240bc00ef25b 2 #define PLAYER_H
Noximilien 23:240bc00ef25b 3
Noximilien 23:240bc00ef25b 4 #include "constants.h"
Noximilien 25:749f1efc31fc 5 #include "game.h"
Noximilien 23:240bc00ef25b 6
Noximilien 23:240bc00ef25b 7 const int max_player_blasts = 5;
Noximilien 23:240bc00ef25b 8 const int ship_speed = 2;
Noximilien 29:579e00b7f118 9 const int blast_speed = 5;
Noximilien 23:240bc00ef25b 10
Noximilien 23:240bc00ef25b 11 GameObject blasts[max_player_blasts];
Noximilien 23:240bc00ef25b 12 GameObject player;
Noximilien 23:240bc00ef25b 13 CircleBounds player_bounds;
Noximilien 23:240bc00ef25b 14 CircleBounds blast_bounds;
Noximilien 30:d454d0cb72bc 15 Game force_shield_check;
Noximilien 23:240bc00ef25b 16
Noximilien 29:579e00b7f118 17 /**Player Class
Noximilien 29:579e00b7f118 18 * @brief A library for describing the player's ship.
Noximilien 29:579e00b7f118 19 * @author Dmitrijs Griskovs
Noximilien 29:579e00b7f118 20 * @date 15/04/2019
Noximilien 29:579e00b7f118 21 */
Noximilien 28:35af3843de8f 22 class Player{
Noximilien 23:240bc00ef25b 23 public:
Noximilien 23:240bc00ef25b 24
Noximilien 29:579e00b7f118 25 /** @brief A constructor of the player's ship.
Noximilien 29:579e00b7f118 26 * @details A constructor for the palyer's sprite body circle area and the
Noximilien 29:579e00b7f118 27 * blast circle area. It sets the circle radius for collsion callculations.
Noximilien 29:579e00b7f118 28 */
Noximilien 23:240bc00ef25b 29 Player() {
Noximilien 23:240bc00ef25b 30 player_bounds.center.x = 5;
Noximilien 30:d454d0cb72bc 31 player_bounds.center.y = 8;
Noximilien 23:240bc00ef25b 32 player_bounds.radius = 7;
Noximilien 23:240bc00ef25b 33
Noximilien 23:240bc00ef25b 34 blast_bounds.center.x = 0;
Noximilien 23:240bc00ef25b 35 blast_bounds.center.y = 1;
Noximilien 23:240bc00ef25b 36 blast_bounds.radius = 1;
Noximilien 23:240bc00ef25b 37 }
Noximilien 29:579e00b7f118 38 /** @brief Draws the player's blasts on the screen
Noximilien 29:579e00b7f118 39 * @details This function Will draw every activated blast to the left with the blast_speed.
Noximilien 29:579e00b7f118 40 * It will deactivate blasts when they leave the screen, for future reuse.
Noximilien 29:579e00b7f118 41 * If the blast does miss the enemy and leaves the screen limits, the function will
Noximilien 29:579e00b7f118 42 * substract 10 points in game_score.
Noximilien 29:579e00b7f118 43 */
Noximilien 23:240bc00ef25b 44 void updateAndDrawBlasts() {
Noximilien 23:240bc00ef25b 45 for (int i = 0; i < max_player_blasts; ++i) {
Noximilien 23:240bc00ef25b 46 if (blasts[i].active) {
Noximilien 23:240bc00ef25b 47 blasts[i].pos.x += blast_speed;
Noximilien 23:240bc00ef25b 48 if (blasts[i].pos.x >= screen_width){
Noximilien 23:240bc00ef25b 49 blasts[i].active = false;
Noximilien 25:749f1efc31fc 50 game_score -= 10;
Noximilien 29:579e00b7f118 51 score_count_for_difficulty -= 10;
Noximilien 23:240bc00ef25b 52 }
Noximilien 23:240bc00ef25b 53 lcd.setPixel(blasts[i].pos.x, blasts[i].pos.y, 1);
Noximilien 23:240bc00ef25b 54 lcd.setPixel(blasts[i].pos.x+1, blasts[i].pos.y, 1);
Noximilien 23:240bc00ef25b 55 lcd.setPixel(blasts[i].pos.x+2, blasts[i].pos.y, 1);
Noximilien 23:240bc00ef25b 56 }
Noximilien 23:240bc00ef25b 57 }
Noximilien 23:240bc00ef25b 58 }
Noximilien 29:579e00b7f118 59
Noximilien 29:579e00b7f118 60 /** @brief Makes a blast active and gives it the start positions.
Noximilien 29:579e00b7f118 61 * @details This function searches the array for the inactive blasts,
Noximilien 29:579e00b7f118 62 * If a blast is not active, it will set it to active and will start drawing
Noximilien 29:579e00b7f118 63 * it accross the screen until it reaches the LCD border line.
Noximilien 29:579e00b7f118 64 */
Noximilien 23:240bc00ef25b 65 void fireNewBlast() {
Noximilien 23:240bc00ef25b 66 // Search the array of blasts if inactive we can use it.
Noximilien 23:240bc00ef25b 67 int found = -1;
Noximilien 23:240bc00ef25b 68 for (int i = 0; i < max_player_blasts; ++i) {
Noximilien 23:240bc00ef25b 69 if (!blasts[i].active) {
Noximilien 23:240bc00ef25b 70 found = i;
Noximilien 23:240bc00ef25b 71 break;
Noximilien 23:240bc00ef25b 72 }
Noximilien 29:579e00b7f118 73 }
Noximilien 23:240bc00ef25b 74 if (found != -1) {
Noximilien 23:240bc00ef25b 75 blasts[found].active = true;
Noximilien 23:240bc00ef25b 76 blasts[found].pos.x = player.pos.x + spaceship1_width;
Noximilien 23:240bc00ef25b 77 blasts[found].pos.y = player.pos.y + (spaceship1_height/2);
Noximilien 23:240bc00ef25b 78 }
Noximilien 29:579e00b7f118 79 }
Noximilien 29:579e00b7f118 80
Noximilien 30:d454d0cb72bc 81 /** @brief Updates and draws player's ship (including with force shield sprite) positon.
Noximilien 29:579e00b7f118 82 * @details The function reads the analog input signal from joystick and
Noximilien 29:579e00b7f118 83 * moves the player's ship on the LCD accordingly.(move joystick, the ship
Noximilien 30:d454d0cb72bc 84 * moves up, move joystick right, the ship moves right). Also, this function checks
Noximilien 30:d454d0cb72bc 85 * whether the force shield was activated in game.cpp, if it was then the player's
Noximilien 30:d454d0cb72bc 86 * ship is replaced the exactky the same sip but with the added force shield.
Noximilien 29:579e00b7f118 87 */
Noximilien 23:240bc00ef25b 88 void playerShipMovement(){
Noximilien 23:240bc00ef25b 89 if(x_dir.read() > joy_threshold_max_x){
Noximilien 23:240bc00ef25b 90 player.pos.x -= ship_speed;
Noximilien 23:240bc00ef25b 91 }
Noximilien 23:240bc00ef25b 92 else if(x_dir.read() < joy_threshold_min_x){
Noximilien 23:240bc00ef25b 93 player.pos.x += ship_speed;
Noximilien 23:240bc00ef25b 94 }
Noximilien 23:240bc00ef25b 95 if(y_dir.read() > joy_threshold_max_y){
Noximilien 23:240bc00ef25b 96 player.pos.y -= ship_speed;
Noximilien 23:240bc00ef25b 97 }
Noximilien 23:240bc00ef25b 98 else if(y_dir.read() < joy_threshold_min_y){
Noximilien 23:240bc00ef25b 99 player.pos.y += ship_speed;
Noximilien 23:240bc00ef25b 100 }
Noximilien 28:35af3843de8f 101 shipMovementLimits();
Noximilien 30:d454d0cb72bc 102 if (force_shield_check.forceShildActivate()){
Noximilien 30:d454d0cb72bc 103 drawSpriteOnTop(player.pos, player_spaceship1_shield_sprite);
Noximilien 30:d454d0cb72bc 104 }
Noximilien 30:d454d0cb72bc 105 else{ drawSpriteOnTop(player.pos, player_spaceship1_sprite);}
Noximilien 28:35af3843de8f 106 }
Noximilien 28:35af3843de8f 107 private:
Noximilien 28:35af3843de8f 108 /** Prevents the player's ship to go beyond the playing zone limits.*/
Noximilien 28:35af3843de8f 109 void shipMovementLimits(){
Noximilien 29:579e00b7f118 110 // Limits player ship on screen
Noximilien 23:240bc00ef25b 111 if (player.pos.x < game_area_x){ player.pos.x = game_area_x;}
Noximilien 23:240bc00ef25b 112 if (player.pos.y < game_area_y) { player.pos.y = game_area_y;}
Noximilien 23:240bc00ef25b 113 int max_player_x = game_area_x + game_area_width - spaceship1_width;
Noximilien 23:240bc00ef25b 114 int max_player_y = game_area_y + game_area_height - spaceship1_height;
Noximilien 23:240bc00ef25b 115 if (player.pos.x > max_player_x) player.pos.x = max_player_x;
Noximilien 23:240bc00ef25b 116 if (player.pos.y > max_player_y) player.pos.y = max_player_y;
Noximilien 25:749f1efc31fc 117 }
Noximilien 23:240bc00ef25b 118 };
Noximilien 23:240bc00ef25b 119 #endif