Nemesis game, friendly

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Friendly.cpp Source File

Friendly.cpp

00001 #include "Friendly.h"
00002 
00003 Friendly::Friendly()
00004 {
00005 }
00006 
00007 Friendly::~Friendly()
00008 {
00009 }
00010 
00011 
00012 // Initializion method:
00013 
00014 void Friendly::init()
00015 {
00016     _x = 2;         // Starting x position (fixed).
00017     _y = 18;        // starting y position (fixed, centre).
00018 }
00019 
00020 
00021 // Draws friendly ship onto LCD:
00022 
00023 void Friendly::draw(N5110 &lcd)
00024 {
00025     lcd.drawLine(_x,_y,_x,_y+5,1);
00026     lcd.drawLine(_x+1,_y,_x+3,_y,1);
00027     lcd.drawLine(_x+1,_y+5,_x+3,_y+5,1);
00028     lcd.drawLine(_x+4,_y+1,_x+5,_y+1,1);
00029     lcd.drawLine(_x+4,_y+4,_x+5,_y+4,1);
00030     lcd.drawLine(_x+6,_y+2,_x+6,_y+3,1);
00031 }
00032 
00033 
00034 // Updates friendly ship's position based on basic vertical and horizontal movement:
00035 
00036 void Friendly::update_basic(Direction d,float mag)
00037 {
00038     _speed = int(mag*4.0f);     // Speed of movement depends on analog stick (mag).
00039     
00040     if (d == N) {               // If direction is north, move upwards (y).
00041         _y-=_speed;
00042     } else if (d == S) {        // If direction is south, move downwards (y).
00043         _y+=_speed;
00044     } else if (d == W) {        // If direction is west, move to the left (x).
00045         _x-=_speed;
00046     } else if (d == E) {        // If direction is east, move to the right (x).
00047         _x+=_speed;
00048     }
00049 }
00050 
00051 
00052 // Updates friendly ship's position based on diagonal movement:
00053 
00054 void Friendly::update_diagonal(Direction d,float mag)
00055 {
00056     _speed = int(mag*4.0f);
00057     
00058     if (d == NW) {              // If direction is northwest, move upwards (y) and to the left (x).
00059         _y-=_speed;
00060         _x-=_speed;
00061     } else if (d == NE) {       // If direction is northeast, move upwards (y) and to the right (x).
00062         _y-=_speed;
00063         _x+=_speed;
00064     } else if (d == SW) {       // If direction is southwest, move downwards (y) and to the left (x).
00065         _y+=_speed;
00066         _x-=_speed;
00067     } else if (d == SE) {       // If direction is southeast, move downwards (y) and to the right (x).
00068         _y+=_speed;
00069         _x+=_speed;
00070     }
00071 }
00072 
00073 
00074 // Prevents friendly ship from going off-screen:
00075 
00076 void Friendly::check_pos() 
00077 {    
00078     if (_y < 1) {   // Prevents ship from moving past upper gridline.
00079         _y = 1;
00080     }
00081     
00082     if (_y > 33) {  // Prevents ship from moving past lower gridline.
00083         _y = 33;
00084     }
00085     
00086     if (_x < 1) {   // Prevents ship from moving past left gridline.
00087         _x = 1;
00088     }
00089     
00090     if (_x > 78) {  // Prevents ship from moving past right gridline.
00091         _x = 78;
00092     }
00093 }
00094 
00095 
00096 // Obtains friendly's current position:
00097 
00098 Vector2D Friendly::get_pos() 
00099 {
00100     Vector2D p = {_x,_y};
00101     return p;    
00102 }