Initial publish

Dependencies:   mbed

Fork of el17dg by Dmitrijs Griskovs

game/game.cpp

Committer:
Noximilien
Date:
2019-03-11
Revision:
7:42376925945c
Parent:
6:100b46be4bea
Child:
8:c18c240665aa

File content as of revision 7:42376925945c:



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

#include "models.h"
#include "main.h"
#include "game.h"

//int x_ship_origin = 0;
//int y_ship_origin = 24;

int x_ship_pos = 0;
int y_ship_pos = 24;

int fire_shot_time_counter = 0;
int shot_step_counter = 0;
int fire_shot_traveled = 0;
int start_blast_x_position = 0;
int start_blast_y_position = 0;

const int ship_step_incremetion = 2;
bool is_fire_shot = false;


bool Game::updateAndDraw() {

    shipMovment();
    
    if (gamepad.check_event(gamepad.B_PRESSED)){
        is_fire_shot = true;
        start_blast_x_position = x_ship_pos + spaceship1_width;
        start_blast_y_position = y_ship_pos + (spaceship1_height/2);        /////////////////////////////////////////////
    }
    if (is_fire_shot){
        
    shipFire();
    
    }    
    
    
    lcd.drawSprite(x_ship_pos, y_ship_pos, spaceship1_width, spaceship1_height, spaceShip1);
    printf("%i\n", fire_shot_traveled);
    /*char buffer[4];
    sprintf(buffer,"%i\n",(int)(x_dir.read()*84));
    printf(buffer);*/
    
    bool want_to_pause = false;
    if (gamepad.check_event(gamepad.START_PRESSED)){
        
        want_to_pause = true;
    }
    return want_to_pause;
}


void Game::shipMovment(){           // The position of the ship
        
    if(x_ship_pos <= 48 && x_ship_pos >= 0){
        if(x_dir.read() > 0.6f){
           x_ship_pos -= ship_step_incremetion;
        }
        else if(x_dir.read() < 0.4f){
           x_ship_pos += ship_step_incremetion;
        }
    } 
    
    else if (x_ship_pos <= 48){ x_ship_pos = 0;}     //Limits for x direction border IMPROVE IF POSSIBLE.
    else { x_ship_pos = 48;}
    
    
    if (y_ship_pos <= (47 - spaceship1_height) && y_ship_pos >= 0){
        if(y_dir.read() > 0.6f){
           y_ship_pos -= ship_step_incremetion; 
        }
        else if(y_dir.read() < 0.4f){
           y_ship_pos += ship_step_incremetion; 
        }
    }
    else if (y_ship_pos >= (47 - spaceship1_height)){ y_ship_pos = 47 - spaceship1_height;}     //Limits for y direction border IMPROVE IF POSSIBLE.
    else if (y_ship_pos < 0){ y_ship_pos = 0;}
       
}




void Game::shipFire(){                          //IMPROVE OR SIMPLIFY - Make it into a spearate class so that I could do multiple shots at the same time.
    
    //int shoot_begins_x = start_blast_x_position;
    //int shoot_begins_y = start_blast_y_position;
    
    
    if (fire_shot_time_counter == 0){
        lcd.drawSprite(start_blast_x_position, (start_blast_y_position), 1, 6, simpleLaserShot);
              
    }
    else if (fire_shot_time_counter == 1){      ///////////////////////////////////////////////////////////////////////////////////////////////////
        lcd.drawSprite(fire_shot_traveled, (start_blast_y_position), 1, 3, simpleLaserShotDissapear);
        
        shot_step_counter += 3;
        fire_shot_traveled = start_blast_x_position + shot_step_counter;
        
        lcd.drawSprite(fire_shot_traveled, (start_blast_y_position), 1, 3, simpleLaserShot);
        
        fire_shot_time_counter = 0;      
    }
    fire_shot_time_counter += 1;
    
    if ( fire_shot_traveled > 84){
        
        fire_shot_time_counter = 0;
        shot_step_counter = 0;
        fire_shot_traveled = 0;
        
        is_fire_shot = !is_fire_shot;   
    } 
}