Ben Evans / Mbed 2 deprecated Defender_Game

Dependencies:   mbed

Committer:
evanso
Date:
Fri May 15 12:05:33 2020 +0000
Revision:
34:85ccc16f24d2
Parent:
31:6015e8ed859c
Child:
36:27aa597db3d2
Added when aliens collide with people, the people are abducted to the top of the screen. Then that alien tracks the spaceship and moves towards it rather than randomly moving.

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 31:6015e8ed859c 23 void Weapons::smart_bomb(N5110 &lcd){
evanso 31:6015e8ed859c 24 lcd.backLightOff();
evanso 31:6015e8ed859c 25 wait(0.02);
evanso 31:6015e8ed859c 26 lcd.backLightOn();
evanso 31:6015e8ed859c 27 wait(0.02);
evanso 31:6015e8ed859c 28 lcd.backLightOff();
evanso 31:6015e8ed859c 29 wait(0.02);
evanso 31:6015e8ed859c 30 lcd.backLightOn();
evanso 31:6015e8ed859c 31 }
evanso 31:6015e8ed859c 32
evanso 31:6015e8ed859c 33
evanso 27:8bb2bd97c319 34 void Weapons::calc_bullets_start_pos(Vector2D spaceship_pos,
evanso 27:8bb2bd97c319 35 bool spaceship_sprite_direction_){
evanso 17:25d79cca203a 36 // Sets start position of bullet to spaceships nose
evanso 17:25d79cca203a 37 if (spaceship_sprite_direction_){
evanso 27:8bb2bd97c319 38 position_x_ = spaceship_pos.x + 13;
evanso 27:8bb2bd97c319 39 position_y_ = spaceship_pos.y + 2;
evanso 17:25d79cca203a 40 }else{
evanso 27:8bb2bd97c319 41 position_x_ = spaceship_pos.x;
evanso 27:8bb2bd97c319 42 position_y_ = spaceship_pos.y + 2;
evanso 17:25d79cca203a 43 }
evanso 17:25d79cca203a 44 #ifdef POSITION_BULLET_DEBUG
evanso 27:8bb2bd97c319 45 printf("X = %d\n", position_x_);
evanso 27:8bb2bd97c319 46 printf("Y = %d\n", position_y_);
evanso 17:25d79cca203a 47 #endif
evanso 17:25d79cca203a 48 }
evanso 17:25d79cca203a 49
evanso 18:11068b98e261 50 void Weapons::draw_bullet(N5110 &lcd){
evanso 23:cc44e26c08fa 51 // draws then moves the bullet position
evanso 27:8bb2bd97c319 52 lcd.drawLine(position_x_, position_y_, position_x_+1, position_y_, 1);
evanso 27:8bb2bd97c319 53 if(direction_){
evanso 27:8bb2bd97c319 54 position_x_ += 3;
evanso 19:1bc0a2d22054 55 }else{
evanso 27:8bb2bd97c319 56 position_x_ -= 3;
evanso 19:1bc0a2d22054 57 }
evanso 28:a5958497d5ce 58
evanso 28:a5958497d5ce 59 // increments counter
evanso 28:a5958497d5ce 60 bullet_delete_counter_++;
evanso 28:a5958497d5ce 61 }
evanso 28:a5958497d5ce 62
evanso 28:a5958497d5ce 63 void Weapons::calc_alien_bullets_start_pos(Vector2D alien_pos,
evanso 28:a5958497d5ce 64 bool alien_sprite_direction_){
evanso 28:a5958497d5ce 65 // Sets start position of bullet to middle of alien
evanso 28:a5958497d5ce 66 position_x_ = alien_pos.x + 3;
evanso 28:a5958497d5ce 67 position_y_ = alien_pos.y + 3;
evanso 28:a5958497d5ce 68
evanso 28:a5958497d5ce 69 //gets a random direction for the bullet to travel in
evanso 28:a5958497d5ce 70 set_random_move();
evanso 28:a5958497d5ce 71 }
evanso 28:a5958497d5ce 72
evanso 28:a5958497d5ce 73 void Weapons::draw_alien_bullet(N5110 &lcd,Direction d_){
evanso 28:a5958497d5ce 74 // draws then moves the bullet position
evanso 28:a5958497d5ce 75 lcd.drawLine(position_x_, position_y_, position_x_+1, position_y_, 1);
evanso 28:a5958497d5ce 76
evanso 28:a5958497d5ce 77 //Moves bullet in random direction and accomodates for spaceship movement
evanso 28:a5958497d5ce 78 move_direction();
evanso 34:85ccc16f24d2 79 position_x_ += calc_sprite_movement(d_);
evanso 20:febd920ec29e 80
evanso 20:febd920ec29e 81 // increments counter
evanso 28:a5958497d5ce 82 bullet_delete_counter_++;
evanso 28:a5958497d5ce 83 }
evanso 28:a5958497d5ce 84
evanso 22:053c11a202e1 85 void Weapons::set_pos_one(Vector2D pos){
evanso 27:8bb2bd97c319 86 position_x_ = pos.x;
evanso 27:8bb2bd97c319 87 position_y_ = pos.y;
evanso 22:053c11a202e1 88 }
evanso 22:053c11a202e1 89
evanso 28:a5958497d5ce 90 void Weapons::set_direction(bool sprite_direction){
evanso 28:a5958497d5ce 91 direction_ = sprite_direction;
evanso 20:febd920ec29e 92 }
evanso 20:febd920ec29e 93
evanso 20:febd920ec29e 94 int Weapons::get_bullet_delete_counter(){
evanso 28:a5958497d5ce 95 return bullet_delete_counter_;
evanso 20:febd920ec29e 96 }
evanso 20:febd920ec29e 97
evanso 20:febd920ec29e 98 bool Weapons::get_direction(){
evanso 27:8bb2bd97c319 99 return direction_;
evanso 16:1ee3d3804557 100 }