Helios Lyons 201239214

Dependencies:   mbed

Brief

My aim for this project was to create a FRDM K64F adapted version of the classic Space Invaders by Tomohiro Nishikado. The game itself has a number of clear features to implement;

  • Left to right movement for the player 'canon'
  • A fixed amount of player lives
  • Firing mechanics for both canon and invaders (hence collision systems)
  • Random firing from remaining invaders
  • Wave based combat

My own addition to these established ideas was Boss waves, featuring a single, larger sprite which fires at a faster interval than previous waves. The addition of a movement system using a basic for loop, as opposed to a velocity based system, will enhance the nostalgic feel of the game.

https://os.mbed.com/media/uploads/helioslyons/screenshot_2020-05-27_at_06.12.00.png

Gameplay

Movement is controlled with the joystick, moving the canon left or right. Fire by pressing A. Invaders spawn at set positions, but randomly fire at a set interval, which is higher for boss waves. Time is taken during each wave, and displayed at wave intervals, and if the play wins.

Controls are shown on the Gamepad below: (attribution: Craig A. Evans, ELEC2645 University of Leeds)

https://os.mbed.com/media/uploads/helioslyons/screenshot_2020-05-27_at_06.20.18.png

Battle/Battle.cpp

Committer:
helioslyons
Date:
2020-05-18
Revision:
10:19b250c7de5e
Parent:
9:6a245c8ce08e
Child:
11:1fd8fca23375

File content as of revision 10:19b250c7de5e:

#include "Battle.h"
#include "Bullet.h"
#include "Invader.h"
#include "Army.h"

#include "N5110.h"
#include "Gamepad.h"

#include <vector>
 
Battle::Battle()
{
    
}
 
Battle::~Battle()
{

}
 
void Battle::init(int invaders, int speed, int interval)
{
    _invaders = invaders;
    _speed = speed;
    _interval = interval;
    
    _army.create_army();
    //_army.start_pos(5,5);
    //_canon.init(44);

    //interval.attach(&Battle::player_fire, 8.0);

}

void Battle::read_input(Gamepad &pad)
{
    _d = pad.get_direction();
    _mag = pad.get_mag();
}

void Battle::draw(N5110 &lcd)
{
    //lcd.drawLine(1,1,WIDTH/2,HEIGHT-1,2);
    print_score(lcd); // score
    
    _canon.draw(lcd); // canon
    _army.draw(lcd); // invaders
    
    vector<Bullet>::iterator it1;
    int i;
    vector<Bullet>::iterator it2;
    int n;
    
    for (it1 = invBomb.begin(); it1 != invBomb.end(); ++it1, ++i) {
        invBomb[i].draw(lcd);
        }
    for (it2 = playBul.begin(); it2 != playBul.end(); ++it2, ++n) {
        playBul[i].draw(lcd);
        }
    print_score(lcd);
}

void Battle::update(Gamepad &pad)
{
    // important to update paddles and ball before checking collisions so can
    // correct for it before updating the display
    _canon.update(_d,_mag);
    
    invader_fire();
    canon_fire(pad);
    
    int i, n;
    vector<Bullet>::iterator it1;
    vector<Bullet>::iterator it2;
    
    for (it1 = invBomb.begin(); it1 != invBomb.end(); ++it1, ++i) {
        invBomb[i].update();
        }
    for (it2 = playBul.begin(); it2 != playBul.end(); ++it2, ++n) {
        playBul[i].update();
        }

    bullet_collisions();
    check_invader_hit(pad);
    check_canon_hit(pad);
}

void Battle::invader_fire()
{
    _army.rand_invader();
    
    int x1 = _army._fire_x;
    int y1 = _army._fire_y;
    
    Bullet bomb; // instantiate new bullet object
    bomb.init(false, x1, y1); // initialise
    
    invBomb.push_back(bomb); // add to invader bomb vector
}

void Battle::canon_fire(Gamepad &pad)
{
    bool A = pad.A_pressed();
    if (A) {
            // 
            
            Vector2D canon_pos = _canon.get_pos();
            int x1 = canon_pos.x;
            int y1 = canon_pos.y;
            
            Bullet shot;
            shot.init(true, x1, y1);
            playBul.push_back(shot);
        }
}

void Battle::check_invader_hit(Gamepad &pad)
{
    vector<Bullet>::iterator it1; // Iterate through canon bullet vector
    Vector2D playBulPos; // Use collision function from army to iterate through invaders
    int i; // Match deletes bullet, removes invader 

    for (it1 = playBul.begin(); it1 != playBul.end(); ++it1, ++i) {
        
        playBulPos = playBul[i].get_pos();
        int playBulX = playBulPos.x;
        int playBulY = playBulPos.y;
        
        _army.check_col(playBulX, playBulY);
        _canon.kill(_army._sprite_pass);
        playBul.erase(playBul.begin() + i - 1);

    }
}

void Battle::check_canon_hit(Gamepad &pad)
{
    vector<Bullet>::iterator it2; // Iterate through invader bomb vector
    Vector2D invBombPos; // If positions are equal, delete bullet and remove life
    Vector2D canonPos;
    
    int i;
    
    for (it2 = invBomb.begin(); it2 != invBomb.end(); ++it2, ++i) {
        
        invBombPos = invBomb[i].get_pos();
        canonPos = _canon.get_pos();
        
        if (invBombPos.x == canonPos.x && invBombPos.y == canonPos.y) {
            invBomb.erase(invBomb.begin() + 1 - 1);
            _canon.remove_life();
            }
        } 
}

void Battle::bullet_collisions()
{
    vector<Bullet>::iterator it1;
    vector<Bullet>::iterator it2;
    
    Vector2D invBombCol;
    Vector2D playBulCol;
    int i;
    
    for (it2 = invBomb.begin(); it2 != invBomb.end(); ++it2, ++i) {
        invBombCol = invBomb[i].get_pos();
        
        if (invBombCol.y >= 48){
            invBomb.erase(invBomb.begin() + i - 1);
        }
    }
    
    int n;
    
    for (it1 = playBul.begin(); it1 != playBul.end(); ++it1, ++n) {
        playBulCol = playBul[n].get_pos();
        
        if (playBulCol.x >= 0){
            playBul.erase(playBul.begin() + n - 1);
        }
    }
}

void Battle::print_score(N5110 &lcd)
{
    int total_score = _canon.get_score();
    char buffer[14];
    sprintf(buffer,"%2d",total_score);
    lcd.printString(buffer,42,1);
}