Arturs Kozlovskis 201253737

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Objects.cpp Source File

Objects.cpp

00001 //includes
00002 #include "Objects.h"
00003 #include "Gamepad.h"
00004 #include "N5110.h"
00005 #include "mbed.h"
00006 //sprite for the game cannon
00007 int _cannon[6][6] = {
00008     { 0,0,1,1,0,0 },
00009     { 0,0,1,1,0,0 },
00010     { 0,1,1,1,1,0 },
00011     { 0,1,1,1,1,0 },
00012     { 1,1,1,1,1,1 },
00013     { 1,1,1,1,1,1 },
00014 };
00015 
00016 //initialise the values
00017 Objects::Objects()
00018 {
00019     _cannon_pos = 42; //sets the cannon postion to 0
00020     _initial_shot_pos = 37;// sets the vertical position of the shot which is one above the cannon
00021     _shot_incrementer = 1; // default shot incrementer in y direction
00022     _shot_y_pos.push_back(_initial_shot_pos); // creates the first shot(was needed here for shot function)
00023     _shot_x_pos.push_back(_cannon_pos + 2);
00024     _radiuss = 2; // sets the initial ball radiuss
00025 };
00026 
00027 void Objects::draw_base(N5110 &lcd)
00028 {
00029     lcd.drawRect(0,46,84,2,FILL_BLACK);
00030 };
00031 
00032 void Objects::cannon_position(Gamepad &pad)
00033 {
00034     //reads the sensor value
00035     float joy_mag = pad.get_mag();
00036     float joy_angle = pad.get_angle();
00037 
00038     // checks the joystics position and determines the speed of cannon movement
00039     if(joy_mag < 0.5f && _cannon_pos > 1 && joy_angle > 180.0f) {
00040         _cannon_pos += -1;
00041     } else if(joy_mag < 1.01f && _cannon_pos > 0 && joy_angle > 180.0f) {
00042         _cannon_pos += -2;
00043     } else if(joy_mag < 0.5f && _cannon_pos < 78 && joy_angle > 0.0f) {
00044         _cannon_pos += 1;
00045     } else if (joy_mag <= 1.01f && _cannon_pos < 77 && joy_angle > 0.0f) {
00046         _cannon_pos += 2;
00047     } else {
00048         _cannon_pos = _cannon_pos;
00049     }
00050 }
00051 
00052 void Objects::draw_cannon(N5110 &lcd)
00053 {
00054     lcd.drawSprite(_cannon_pos,40,6,6,(int *)_cannon);
00055 }
00056 
00057 void Objects::draw_shots(N5110 &lcd,Gamepad &pad, bool c)
00058 {
00059     for (int i = 0; i < _shot_y_pos.size(); i++) {
00060         lcd.drawRect(_shot_x_pos[i],_shot_y_pos[i],2,2,FILL_BLACK);
00061         _shot_y_pos[i] -= _shot_incrementer; // moves the shots upwards
00062     }
00063 
00064     //adds another shot if the distance between
00065     //y pos of the initial pos and previous shot pos is more than 7
00066     if(_shot_y_pos[_shot_y_pos.size() - 1 ] + 7 < _initial_shot_pos || _shot_y_pos.size() < 1) {
00067         if(pad.X_held()) {
00068             _shot_y_pos.push_back(_initial_shot_pos);
00069             _shot_x_pos.push_back(_cannon_pos + 2);
00070         }
00071     }
00072     
00073     //erasing a shot if the y position is below 0
00074     if(_shot_y_pos[0] <= 0) {
00075         _shot_y_pos.erase(_shot_y_pos.begin());
00076         _shot_x_pos.erase(_shot_x_pos.begin());
00077     }
00078 
00079 }
00080 
00081 void Objects::draw_ball(N5110 &lcd,int ball_x, int ball_y, int delta_r)
00082 {
00083     lcd.drawCircle(ball_x,ball_y,_radiuss + delta_r,FILL_BLACK);
00084 }
00085 
00086 int Objects::get_size()
00087 {
00088     return _shot_y_pos.size();
00089 }
00090 
00091 int Objects::get_x_value(int i)
00092 {
00093     return _shot_x_pos[i];
00094 }
00095 
00096 int Objects::get_y_value(int i)
00097 {
00098     return _shot_y_pos[i];
00099 }
00100 
00101 void Objects::erase_shot(int i)
00102 {
00103     _shot_y_pos.erase(_shot_y_pos.begin() + i);
00104     _shot_x_pos.erase(_shot_x_pos.begin() + i);
00105 }
00106 
00107 int Objects::get_x_cannon()
00108 {
00109     return _cannon_pos;
00110 }