Ben Evans / Mbed 2 deprecated Defender_Game

Dependencies:   mbed

Committer:
evanso
Date:
Wed May 13 23:07:52 2020 +0000
Revision:
28:a5958497d5ce
Parent:
27:8bb2bd97c319
Child:
31:6015e8ed859c
Added Random Movement base class which is inherited by alien bullets and alien direction. Alien now shoots bullets randomly towards the spaceship.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
evanso 16:1ee3d3804557 1 #include "Weapons.h"
evanso 16:1ee3d3804557 2
evanso 16:1ee3d3804557 3 Weapons::Weapons() {
evanso 16:1ee3d3804557 4
evanso 16:1ee3d3804557 5 }
evanso 16:1ee3d3804557 6
evanso 16:1ee3d3804557 7 Weapons::~Weapons() {
evanso 16:1ee3d3804557 8
evanso 16:1ee3d3804557 9 }
evanso 16:1ee3d3804557 10
evanso 28:a5958497d5ce 11 void Weapons::init(Vector2D sprite_pos, bool sprite_direction,
evanso 28:a5958497d5ce 12 bool bullet_type) {
evanso 28:a5958497d5ce 13 //bullet for alien or for spacship
evanso 28:a5958497d5ce 14 set_direction(sprite_direction);
evanso 28:a5958497d5ce 15 if (bullet_type){
evanso 28:a5958497d5ce 16 calc_bullets_start_pos(sprite_pos, sprite_direction);
evanso 28:a5958497d5ce 17 }else{
evanso 28:a5958497d5ce 18 calc_alien_bullets_start_pos(sprite_pos, sprite_direction);
evanso 28:a5958497d5ce 19 }
evanso 28:a5958497d5ce 20 bullet_delete_counter_ = 0;
evanso 16:1ee3d3804557 21 }
evanso 16:1ee3d3804557 22
evanso 27:8bb2bd97c319 23 void Weapons::calc_bullets_start_pos(Vector2D spaceship_pos,
evanso 27:8bb2bd97c319 24 bool spaceship_sprite_direction_){
evanso 17:25d79cca203a 25 // Sets start position of bullet to spaceships nose
evanso 17:25d79cca203a 26 if (spaceship_sprite_direction_){
evanso 27:8bb2bd97c319 27 position_x_ = spaceship_pos.x + 13;
evanso 27:8bb2bd97c319 28 position_y_ = spaceship_pos.y + 2;
evanso 17:25d79cca203a 29 }else{
evanso 27:8bb2bd97c319 30 position_x_ = spaceship_pos.x;
evanso 27:8bb2bd97c319 31 position_y_ = spaceship_pos.y + 2;
evanso 17:25d79cca203a 32 }
evanso 17:25d79cca203a 33 #ifdef POSITION_BULLET_DEBUG
evanso 27:8bb2bd97c319 34 printf("X = %d\n", position_x_);
evanso 27:8bb2bd97c319 35 printf("Y = %d\n", position_y_);
evanso 17:25d79cca203a 36 #endif
evanso 17:25d79cca203a 37 }
evanso 17:25d79cca203a 38
evanso 18:11068b98e261 39 void Weapons::draw_bullet(N5110 &lcd){
evanso 23:cc44e26c08fa 40 // draws then moves the bullet position
evanso 27:8bb2bd97c319 41 lcd.drawLine(position_x_, position_y_, position_x_+1, position_y_, 1);
evanso 27:8bb2bd97c319 42 if(direction_){
evanso 27:8bb2bd97c319 43 position_x_ += 3;
evanso 19:1bc0a2d22054 44 }else{
evanso 27:8bb2bd97c319 45 position_x_ -= 3;
evanso 19:1bc0a2d22054 46 }
evanso 28:a5958497d5ce 47
evanso 28:a5958497d5ce 48 // increments counter
evanso 28:a5958497d5ce 49 bullet_delete_counter_++;
evanso 28:a5958497d5ce 50 }
evanso 28:a5958497d5ce 51
evanso 28:a5958497d5ce 52 void Weapons::calc_alien_bullets_start_pos(Vector2D alien_pos,
evanso 28:a5958497d5ce 53 bool alien_sprite_direction_){
evanso 28:a5958497d5ce 54 // Sets start position of bullet to middle of alien
evanso 28:a5958497d5ce 55 position_x_ = alien_pos.x + 3;
evanso 28:a5958497d5ce 56 position_y_ = alien_pos.y + 3;
evanso 28:a5958497d5ce 57
evanso 28:a5958497d5ce 58 //gets a random direction for the bullet to travel in
evanso 28:a5958497d5ce 59 set_random_move();
evanso 28:a5958497d5ce 60 }
evanso 28:a5958497d5ce 61
evanso 28:a5958497d5ce 62 void Weapons::draw_alien_bullet(N5110 &lcd,Direction d_){
evanso 28:a5958497d5ce 63 // draws then moves the bullet position
evanso 28:a5958497d5ce 64 lcd.drawLine(position_x_, position_y_, position_x_+1, position_y_, 1);
evanso 28:a5958497d5ce 65
evanso 28:a5958497d5ce 66 //Moves bullet in random direction and accomodates for spaceship movement
evanso 28:a5958497d5ce 67 move_direction();
evanso 28:a5958497d5ce 68 position_x_ += calc_bullet_movement(d_);
evanso 20:febd920ec29e 69
evanso 20:febd920ec29e 70 // increments counter
evanso 28:a5958497d5ce 71 bullet_delete_counter_++;
evanso 28:a5958497d5ce 72 }
evanso 28:a5958497d5ce 73
evanso 28:a5958497d5ce 74 int Weapons::calc_bullet_movement(Direction d_){
evanso 28:a5958497d5ce 75 // moves the bullets in oposite direction to spaceship when it's position is
evanso 28:a5958497d5ce 76 // at min and max x positions and joystick has direction
evanso 28:a5958497d5ce 77 if (d_ == W || d_ == NW || d_ == SW){
evanso 28:a5958497d5ce 78 return 2;
evanso 28:a5958497d5ce 79 }else if (d_ == E || d_ == NE || d_ == SE){
evanso 28:a5958497d5ce 80 return -2;
evanso 28:a5958497d5ce 81 }else {
evanso 28:a5958497d5ce 82 return 0;
evanso 28:a5958497d5ce 83 }
evanso 18:11068b98e261 84 }
evanso 18:11068b98e261 85
evanso 18:11068b98e261 86 Vector2D Weapons::get_pos_one(){
evanso 27:8bb2bd97c319 87 Vector2D pos = {position_x_,position_y_};
evanso 17:25d79cca203a 88 return pos;
evanso 18:11068b98e261 89 }
evanso 18:11068b98e261 90
evanso 22:053c11a202e1 91 void Weapons::set_pos_one(Vector2D pos){
evanso 27:8bb2bd97c319 92 position_x_ = pos.x;
evanso 27:8bb2bd97c319 93 position_y_ = pos.y;
evanso 22:053c11a202e1 94 }
evanso 22:053c11a202e1 95
evanso 18:11068b98e261 96 Vector2D Weapons::get_pos_two(){
evanso 27:8bb2bd97c319 97 Vector2D pos = {position_x_+1,position_y_};
evanso 18:11068b98e261 98 return pos;
evanso 19:1bc0a2d22054 99 }
evanso 19:1bc0a2d22054 100
evanso 28:a5958497d5ce 101 void Weapons::set_direction(bool sprite_direction){
evanso 28:a5958497d5ce 102 direction_ = sprite_direction;
evanso 20:febd920ec29e 103 }
evanso 20:febd920ec29e 104
evanso 20:febd920ec29e 105 int Weapons::get_bullet_delete_counter(){
evanso 28:a5958497d5ce 106 return bullet_delete_counter_;
evanso 20:febd920ec29e 107 }
evanso 20:febd920ec29e 108
evanso 20:febd920ec29e 109 bool Weapons::get_direction(){
evanso 27:8bb2bd97c319 110 return direction_;
evanso 16:1ee3d3804557 111 }