ELEC2645 (2019/20) / Mbed 2 deprecated ELEC2645_Project_el19zf

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Collision.cpp Source File

Collision.cpp

00001 #include "Collision.h"
00002 
00003 const int heart_sprite[7][9] = {
00004     {0,1,1,0,0,0,1,1,0},
00005     {1,1,1,1,0,1,1,1,1},
00006     {1,1,1,1,1,1,1,1,1},
00007     {0,1,1,1,1,1,1,1,0},
00008     {0,0,1,1,1,1,1,0,0},
00009     {0,0,0,1,1,1,0,0,0},
00010     {0,0,0,0,1,0,0,0,0},
00011 };
00012 
00013 Collision::Collision() 
00014 {
00015     
00016 }
00017 
00018 Collision::~Collision() 
00019 {
00020 
00021 }
00022 
00023 void Collision::init() 
00024 {
00025     _health = 3;
00026     _check_col_index = 0;
00027     _check_des_index = 0;
00028 }
00029 
00030 void Collision::reset_check_col()
00031 {
00032     _check_col_index = 0;
00033 }
00034 
00035 bool Collision::check(N5110 &lcd) 
00036 {
00037     _check_col_index = 0;
00038     //check main part of people expect hands and foots(x,y+3)(x+4,y+3)(x,y+7)(x+4,y+7)
00039     if(lcd.getPixel(_people_pos.x+1,_people_pos.y)) {  _check_col_index = 1; }
00040     if(lcd.getPixel(_people_pos.x+3,_people_pos.y)) {  _check_col_index = 1; }
00041     if(lcd.getPixel(_people_pos.x+1,_people_pos.y+1)) {  _check_col_index = 1; }
00042     if(lcd.getPixel(_people_pos.x+3,_people_pos.y+1)) {  _check_col_index = 1; }
00043     if(lcd.getPixel(_people_pos.x+1,_people_pos.y+1)) {  _check_col_index = 1; }
00044     if(lcd.getPixel(_people_pos.x+3,_people_pos.y+3)) {  _check_col_index = 1; }
00045     if(lcd.getPixel(_people_pos.x+1,_people_pos.y+5)) {  _check_col_index = 1; }
00046     if(lcd.getPixel(_people_pos.x+3,_people_pos.y+5)) {  _check_col_index = 1; }
00047     if(lcd.getPixel(_people_pos.x,_people_pos.y+6)) {  _check_col_index = 1; }
00048     if(lcd.getPixel(_people_pos.x+4,_people_pos.y+6)) {  _check_col_index = 1; }
00049     if(lcd.getPixel(_people_pos.x+2,_people_pos.y+3)) {  _check_col_index = 1; }
00050     //printf("index = %d\n",_check_col_index);
00051     //printf("people_pos = %f,%f\n",_people_pos.x+2,_people_pos.y+3);
00052     if((_check_col_index == 1)&&(_health > 0)) { 
00053         //_check_col_index = 0;
00054         _health = _health - 1;
00055     }
00056     return _check_col_index;
00057     //printf("health is %d\n",_health);
00058 }
00059 
00060 //use a getPixel function to check whether 'People' reaches the destination
00061 bool Collision::check_des(N5110 &lcd)
00062 {
00063     _check_des_index = 0;
00064     if(lcd.getPixel(82,24)||lcd.getPixel(82,25)||lcd.getPixel(82,26)||
00065     lcd.getPixel(82,27)||lcd.getPixel(82,23)||lcd.getPixel(82,22)||
00066     lcd.getPixel(82,21)||lcd.getPixel(82,20)||lcd.getPixel(82,28)||
00067     lcd.getPixel(81,24)||lcd.getPixel(81,25)||lcd.getPixel(81,23))
00068     {
00069         _check_des_index = 1;
00070         //printf("reach des\n");
00071     }
00072     return _check_des_index;
00073 }
00074 
00075 //draw the health indicator at the top left corner
00076 void Collision::draw(N5110 &lcd) 
00077 {
00078     if(_health > 0)
00079         lcd.drawRect(1,1,_health*2,3,FILL_BLACK);
00080 }
00081 
00082 void Collision::draw_basic(N5110 &lcd)
00083 {   
00084     lcd.printString("    HEALTH   ",0,1);
00085     lcd.drawRect(1,1,WIDTH-2,2,FILL_BLACK);
00086     lcd.drawRect(1,45,WIDTH-2,2,FILL_BLACK);
00087     lcd.drawRect(1,1,3,HEIGHT-2,FILL_BLACK);
00088     lcd.drawRect(80,1,3,HEIGHT-2,FILL_BLACK);
00089 }
00090 
00091 //draw a 1 second collision interface to clearly show the health decreases
00092 void Collision::draw_collision(N5110 &lcd)
00093 {
00094     lcd.clear();
00095     draw_basic(lcd);
00096     for(int i =0; i <get_health()+1;i++)
00097         lcd.drawSprite(18*i+20,24,7,9,(int*)heart_sprite);
00098     lcd.refresh();
00099     wait(1);
00100 
00101     lcd.clear();
00102     draw_basic(lcd);
00103     for(int i =0; i <get_health();i++)
00104         lcd.drawSprite(18*i+20,24,7,9,(int*)heart_sprite);
00105     lcd.refresh();
00106     wait(1);
00107 }
00108 
00109 int Collision::get_health() 
00110 {
00111     return _health;
00112 }
00113 
00114 int Collision::get_des()
00115 {
00116     return _check_des_index;
00117 }
00118 
00119 bool Collision::get_check_col()
00120 {
00121     return _check_col_index;
00122 }
00123 
00124 void Collision::set_pos(Vector2D pos) 
00125 {
00126     _people_pos = pos;
00127 }