Initial publish
Dependencies: mbed
Fork of el17dg by
game/player.h@28:35af3843de8f, 2019-04-10 (annotated)
- Committer:
- Noximilien
- Date:
- Wed Apr 10 15:42:10 2019 +0000
- Revision:
- 28:35af3843de8f
- Parent:
- 27:f05f4e738ba9
- Child:
- 29:579e00b7f118
Moved starSpawnDelay to stars.h. Made enemies move y-direction as the game score increase. Added more comments. Cleaned the code a bit. Changed struct to gameObject in the main.cpp. Made some functions less than 20 lines.
Who changed what in which revision?
User | Revision | Line number | New 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 | 23:240bc00ef25b | 9 | |
Noximilien | 23:240bc00ef25b | 10 | GameObject blasts[max_player_blasts]; |
Noximilien | 23:240bc00ef25b | 11 | GameObject player; |
Noximilien | 23:240bc00ef25b | 12 | CircleBounds player_bounds; |
Noximilien | 23:240bc00ef25b | 13 | CircleBounds blast_bounds; |
Noximilien | 23:240bc00ef25b | 14 | |
Noximilien | 28:35af3843de8f | 15 | |
Noximilien | 28:35af3843de8f | 16 | class Player{ |
Noximilien | 23:240bc00ef25b | 17 | public: |
Noximilien | 23:240bc00ef25b | 18 | |
Noximilien | 28:35af3843de8f | 19 | /** A constructor for the palyer's sprite body circle area and the blast circle |
Noximilien | 28:35af3843de8f | 20 | * area. It sets the circle radius for collsion callculations. |
Noximilien | 28:35af3843de8f | 21 | */ |
Noximilien | 23:240bc00ef25b | 22 | Player() { |
Noximilien | 23:240bc00ef25b | 23 | player_bounds.center.x = 5; |
Noximilien | 23:240bc00ef25b | 24 | player_bounds.center.y = 7; |
Noximilien | 23:240bc00ef25b | 25 | player_bounds.radius = 7; |
Noximilien | 23:240bc00ef25b | 26 | |
Noximilien | 23:240bc00ef25b | 27 | blast_bounds.center.x = 0; |
Noximilien | 23:240bc00ef25b | 28 | blast_bounds.center.y = 1; |
Noximilien | 23:240bc00ef25b | 29 | blast_bounds.radius = 1; |
Noximilien | 23:240bc00ef25b | 30 | } |
Noximilien | 27:f05f4e738ba9 | 31 | /** Will move every active blast to the left with blast speed. |
Noximilien | 28:35af3843de8f | 32 | * Will deactivate blasts when they live screen, for future reuse. |
Noximilien | 23:240bc00ef25b | 33 | */ |
Noximilien | 23:240bc00ef25b | 34 | void updateAndDrawBlasts() { |
Noximilien | 23:240bc00ef25b | 35 | const int blast_speed = 5; |
Noximilien | 23:240bc00ef25b | 36 | for (int i = 0; i < max_player_blasts; ++i) { |
Noximilien | 23:240bc00ef25b | 37 | if (blasts[i].active) { |
Noximilien | 23:240bc00ef25b | 38 | blasts[i].pos.x += blast_speed; |
Noximilien | 23:240bc00ef25b | 39 | if (blasts[i].pos.x >= screen_width){ |
Noximilien | 23:240bc00ef25b | 40 | blasts[i].active = false; |
Noximilien | 25:749f1efc31fc | 41 | game_score -= 10; |
Noximilien | 26:676874c42883 | 42 | score_count_for_difficulty -= 50; |
Noximilien | 23:240bc00ef25b | 43 | } |
Noximilien | 23:240bc00ef25b | 44 | lcd.setPixel(blasts[i].pos.x, blasts[i].pos.y, 1); |
Noximilien | 23:240bc00ef25b | 45 | lcd.setPixel(blasts[i].pos.x+1, blasts[i].pos.y, 1); |
Noximilien | 23:240bc00ef25b | 46 | lcd.setPixel(blasts[i].pos.x+2, blasts[i].pos.y, 1); |
Noximilien | 23:240bc00ef25b | 47 | } |
Noximilien | 23:240bc00ef25b | 48 | } |
Noximilien | 23:240bc00ef25b | 49 | } |
Noximilien | 23:240bc00ef25b | 50 | |
Noximilien | 27:f05f4e738ba9 | 51 | /** This function searches the array for the inactive blasts, |
Noximilien | 23:240bc00ef25b | 52 | * If a blast is set to not active, it will set it active and start drawing |
Noximilien | 23:240bc00ef25b | 53 | * it accross the screen until it reaches the LCD border line. |
Noximilien | 23:240bc00ef25b | 54 | */ |
Noximilien | 23:240bc00ef25b | 55 | void fireNewBlast() { |
Noximilien | 23:240bc00ef25b | 56 | // Search the array of blasts if inactive we can use it. |
Noximilien | 23:240bc00ef25b | 57 | int found = -1; |
Noximilien | 23:240bc00ef25b | 58 | for (int i = 0; i < max_player_blasts; ++i) { |
Noximilien | 23:240bc00ef25b | 59 | if (!blasts[i].active) { |
Noximilien | 23:240bc00ef25b | 60 | found = i; |
Noximilien | 23:240bc00ef25b | 61 | break; |
Noximilien | 23:240bc00ef25b | 62 | } |
Noximilien | 23:240bc00ef25b | 63 | } |
Noximilien | 23:240bc00ef25b | 64 | |
Noximilien | 23:240bc00ef25b | 65 | if (found != -1) { |
Noximilien | 23:240bc00ef25b | 66 | blasts[found].active = true; |
Noximilien | 23:240bc00ef25b | 67 | blasts[found].pos.x = player.pos.x + spaceship1_width; |
Noximilien | 23:240bc00ef25b | 68 | blasts[found].pos.y = player.pos.y + (spaceship1_height/2); |
Noximilien | 23:240bc00ef25b | 69 | } |
Noximilien | 23:240bc00ef25b | 70 | } |
Noximilien | 27:f05f4e738ba9 | 71 | /** The function reads the analog input signal from joystick and moves the |
Noximilien | 28:35af3843de8f | 72 | * player's ship on the LCD accordingly.(move joystick, the ship moves up |
Noximilien | 28:35af3843de8f | 73 | * move joystick right, the ship moves right). |
Noximilien | 23:240bc00ef25b | 74 | */ |
Noximilien | 23:240bc00ef25b | 75 | void playerShipMovement(){ |
Noximilien | 23:240bc00ef25b | 76 | if(x_dir.read() > joy_threshold_max_x){ |
Noximilien | 23:240bc00ef25b | 77 | player.pos.x -= ship_speed; |
Noximilien | 23:240bc00ef25b | 78 | } |
Noximilien | 23:240bc00ef25b | 79 | else if(x_dir.read() < joy_threshold_min_x){ |
Noximilien | 23:240bc00ef25b | 80 | player.pos.x += ship_speed; |
Noximilien | 23:240bc00ef25b | 81 | } |
Noximilien | 23:240bc00ef25b | 82 | if(y_dir.read() > joy_threshold_max_y){ |
Noximilien | 23:240bc00ef25b | 83 | player.pos.y -= ship_speed; |
Noximilien | 23:240bc00ef25b | 84 | } |
Noximilien | 23:240bc00ef25b | 85 | else if(y_dir.read() < joy_threshold_min_y){ |
Noximilien | 23:240bc00ef25b | 86 | player.pos.y += ship_speed; |
Noximilien | 23:240bc00ef25b | 87 | } |
Noximilien | 28:35af3843de8f | 88 | shipMovementLimits(); |
Noximilien | 28:35af3843de8f | 89 | drawSpriteOnTop(player.pos, player_spaceship1_sprite); |
Noximilien | 28:35af3843de8f | 90 | } |
Noximilien | 28:35af3843de8f | 91 | private: |
Noximilien | 28:35af3843de8f | 92 | /** Prevents the player's ship to go beyond the playing zone limits.*/ |
Noximilien | 28:35af3843de8f | 93 | void shipMovementLimits(){ |
Noximilien | 23:240bc00ef25b | 94 | //Limits player ship on screen |
Noximilien | 23:240bc00ef25b | 95 | if (player.pos.x < game_area_x){ player.pos.x = game_area_x;} |
Noximilien | 23:240bc00ef25b | 96 | if (player.pos.y < game_area_y) { player.pos.y = game_area_y;} |
Noximilien | 23:240bc00ef25b | 97 | int max_player_x = game_area_x + game_area_width - spaceship1_width; |
Noximilien | 23:240bc00ef25b | 98 | int max_player_y = game_area_y + game_area_height - spaceship1_height; |
Noximilien | 23:240bc00ef25b | 99 | if (player.pos.x > max_player_x) player.pos.x = max_player_x; |
Noximilien | 23:240bc00ef25b | 100 | if (player.pos.y > max_player_y) player.pos.y = max_player_y; |
Noximilien | 25:749f1efc31fc | 101 | } |
Noximilien | 23:240bc00ef25b | 102 | }; |
Noximilien | 23:240bc00ef25b | 103 | #endif |