Initial publish

Dependencies:   mbed

Fork of el17dg by Dmitrijs Griskovs

game/game.cpp

Committer:
Noximilien
Date:
2019-03-05
Revision:
6:100b46be4bea
Parent:
5:2b9181bc5c89
Child:
7:42376925945c

File content as of revision 6:100b46be4bea:



#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;

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;
    }
    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
    if (fire_shot_time_counter == 0){
        lcd.drawSprite((x_ship_pos + spaceship1_width), (y_ship_pos + spaceship1_height/2), 1, 6, simpleLaserShot);
              
    }
    else if (fire_shot_time_counter == 2){
        shot_step_counter += 3;
        fire_shot_traveled = (x_ship_pos + spaceship1_width) + shot_step_counter;
        
        lcd.drawSprite((x_ship_pos + spaceship1_width), (y_ship_pos + spaceship1_height/2), 1, 3, simpleLaserShotDissapear);
        lcd.drawSprite(fire_shot_traveled, (y_ship_pos + spaceship1_height/2), 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; 
        return;  
    }
        
}