Ben Evans University Second Year Project. Game Called Defender.

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Spaceship.cpp Source File

Spaceship.cpp

00001 #include "Spaceship.h"
00002 
00003 // Constant Definitions
00004 #define SPRITE_X_LENGTH 13
00005 #define SPRITE_Y_LENGTH 4
00006 #define STARTING_X_POS 36
00007 #define STARTING_Y_POS 22
00008 #define OFF_SCREEN_Y_ONE 9
00009 #define OFF_SCREEN_Y_TWO 44
00010 #define OFF_SCREEN_X_ONE 56
00011 #define OFF_SCREEN_X_TWO 15
00012 
00013  
00014 const int k_spaceship_sprite_E[SPRITE_Y_LENGTH][SPRITE_X_LENGTH] = {
00015     { 1,1,1,1,1,1,0,0,0,0,0,0,0 },
00016     { 0,1,1,1,1,1,1,1,1,0,0,0,0 },
00017     { 1,1,1,1,1,1,1,1,1,1,1,1,1 },
00018     { 0,0,1,1,1,1,1,0,0,0,0,0,0 },
00019 };
00020 
00021 const int k_spaceship_sprite_W[SPRITE_Y_LENGTH][SPRITE_X_LENGTH] = {
00022     { 0,0,0,0,0,0,0,1,1,1,1,1,1 },
00023     { 0,0,0,0,1,1,1,1,1,1,1,1,0 },
00024     { 1,1,1,1,1,1,1,1,1,1,1,1,1 },
00025     { 0,0,0,0,0,0,1,1,1,1,1,0,0 },
00026 };
00027  
00028 Spaceship::Spaceship() {
00029     
00030 }
00031  
00032 Spaceship::~Spaceship() {
00033     
00034 }
00035  
00036 void Spaceship::init() {
00037     // Assign values to variables
00038     CheckCollision::sprite_x_length = SPRITE_X_LENGTH;
00039     CheckCollision::sprite_y_length = SPRITE_Y_LENGTH;
00040     CheckAlienCollision::sprite_x_length = SPRITE_X_LENGTH;
00041     CheckAlienCollision::sprite_y_length = SPRITE_Y_LENGTH;
00042     position_x_ = STARTING_X_POS;
00043     position_y_ = STARTING_Y_POS;
00044     
00045     // Sets original spaceship direction to facing East
00046     direction_ = true;
00047 }
00048  
00049 void Spaceship::draw(N5110 &lcd) {
00050     off_screen_x_y_checker();
00051     
00052     // Draws spaceships at defined x and y positions with different sprite 
00053     // Direction depending on joystick position 
00054     if (direction_) {
00055         lcd.drawSprite(position_x_, position_y_, SPRITE_Y_LENGTH, 
00056         SPRITE_X_LENGTH,(int*)k_spaceship_sprite_E);
00057     }else if (!direction_) {
00058     lcd.drawSprite(position_x_, position_y_, SPRITE_Y_LENGTH, SPRITE_X_LENGTH, 
00059     (int*)k_spaceship_sprite_W);
00060     }
00061     
00062     // printf("Spaceship Y postion = %d\n",position_y_);
00063 }
00064 
00065 void Spaceship::off_screen_x_y_checker() {
00066     // Checks y position of spaceship and then alters y position if off map
00067     if (position_y_ < OFF_SCREEN_Y_ONE) {
00068         position_y_ = OFF_SCREEN_Y_ONE;
00069     }else if (position_y_ > OFF_SCREEN_Y_TWO) {
00070         position_y_ = OFF_SCREEN_Y_TWO;
00071     }  
00072     
00073     // Checks x position of spaceship and alters x position 
00074     if (position_x_ > OFF_SCREEN_X_ONE) {
00075         position_x_ = OFF_SCREEN_X_ONE;
00076     }else if (position_x_ < OFF_SCREEN_X_TWO) {
00077         position_x_ = OFF_SCREEN_X_TWO;
00078     }
00079 }   
00080  
00081 void Spaceship::movement(Direction d_) {
00082     // Switch statement to change spaceship parameters depending on 
00083     // Joystick direction
00084     switch (d_) {
00085     case CENTRE: set_spaceship_peram(0, 0, false , false); break;
00086     case N: set_spaceship_peram(0, -1, false , false); break;
00087     case NE: set_spaceship_peram(-1, -1, true, true); break;
00088     case E: set_spaceship_peram(-1, 0, true, true); break;
00089     case SE: set_spaceship_peram(-1, 1, true, true); break;
00090     case S: set_spaceship_peram(0, 1, false, false); break;
00091     case SW: set_spaceship_peram(1, 1, true, false); break;
00092     case W: set_spaceship_peram(1, 0, true, false); break;
00093     case NW: set_spaceship_peram(1, -1, true, false); break;
00094     }
00095     
00096     // printf("y= %d\n", position_y_);
00097 }
00098 
00099 void Spaceship::set_spaceship_peram(int x_change,int y_change, 
00100 bool sprite_change, bool sprite_param) {
00101     position_x_ += x_change;
00102     position_y_ += y_change;
00103     
00104     // Only changes sprite direction when joystick direction isn't N or S
00105     if (sprite_change) {
00106         direction_ = sprite_param;
00107     }
00108 }
00109 
00110 bool Spaceship::get_spaceship_sprite_direction() {
00111     return direction_;
00112 }