ELEC2645 (2019/20) / Mbed 2 deprecated ELEC2645_Project_el19zf

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers People.cpp Source File

People.cpp

00001 #include "People.h"
00002 #define INIT_x 1
00003 #define INIT_y 20
00004 
00005 const int people_sprite[8][5] = {
00006         {0,1,1,1,0},
00007         {0,1,1,1,0},
00008         {0,0,1,0,0},
00009         {1,1,1,1,1},
00010         {0,0,1,0,0},
00011         {0,1,0,1,0},
00012         {0,1,0,1,0},
00013         {0,1,0,1,0},
00014 };//try to draw a people, but for the operability of game, it seems not a good implementation..
00015 
00016 const int running_people_sprite[8][5] = {
00017         {0,1,1,1,0},
00018         {0,1,1,1,0},
00019         {0,0,1,0,0},
00020         {1,1,1,1,1},
00021         {0,0,1,0,0},
00022         {0,1,0,1,0},
00023         {1,0,0,0,1},
00024         {1,0,0,0,1},
00025 };
00026 
00027 People::People() 
00028 {
00029     
00030 }
00031 
00032 People::~People() 
00033 {
00034     
00035 }
00036     
00037 void People::init() 
00038 {    
00039     _x = INIT_x;
00040     _y = INIT_y;//Set initial postion of people
00041     _index = 0;
00042 }
00043 
00044 void People::draw(N5110 &lcd,int flag) 
00045 {
00046     if(int(_mag*5)!=0&&flag!=0){
00047         if(!_index){
00048             lcd.drawSprite(_x,_y,8,5,(int*)people_sprite);//Draw sprite to represent a people
00049         // printf("drawSprite");
00050             _index = 1;
00051         }else{
00052             lcd.drawSprite(_x,_y,8,5,(int*)running_people_sprite);
00053             _index = 0;
00054         }
00055     }else{
00056         lcd.drawSprite(_x,_y,8,5,(int*)people_sprite);
00057     }      
00058 }
00059 
00060 /*control people by Joystick in different angle and magnitude
00061     */
00062 void People::update()
00063 {
00064     if(_d == S)        {
00065         _x += 0;            _y += int(_mag*5);
00066     } else if(_d == SE){
00067         _x += int(_mag*5);   _y += int(_mag*5);
00068     } else if(_d == E){
00069         _x += int(_mag*5);   _y += 0;
00070     } else if(_d == NE){
00071         _x += int(_mag*5);   _y -= int(_mag*5);
00072     } else if(_d == N){
00073         _x -= 0;            _y -= int(_mag*5);
00074     } else if(_d == NW){
00075         _x -= int(_mag*5);   _y -= int(_mag*5);
00076     } else if(_d == W){
00077         _x -= int(_mag*5);   _y -= 0;
00078     } else if(_d == SW){
00079         _x -= int(_mag*5);   _y += int(_mag*5);
00080     }
00081     //printf("Mag = %f,Direction = %d,Coordinate = %d, %d\n",_mag,_d,_x,_y);
00082     
00083     control_lim();//without going off screen
00084 }
00085 
00086 void People::control_lim()
00087 {
00088     if (_x < 1) { _x = 1;}//left
00089     else if (_x > 78) { _x = 78;}//right
00090     else if (_y < 1) {_y = 1;}//top
00091     else if (_y > 41) {_y = 41;}//bottom
00092 }
00093 
00094 void People::set_pos(Vector2D p)
00095 {
00096     _x = p.x;
00097     _y = p.y;
00098 }
00099 
00100 void People::set_velocity(Direction d,float mag)
00101 {
00102     _d = d;
00103     _mag = mag;
00104 }
00105 
00106 Vector2D People::get_pos()
00107 {
00108     Vector2D p = {_x,_y};
00109     //printf("Coord = %f,%f\n",p.x,p.y);
00110     return p;
00111 }
00112