Arturs Kozlovskis / Mbed 2 deprecated ELEC2645_Project_el18ak

Dependencies:   mbed

Revision:
23:cd38e48635e7
Parent:
22:f1811602a817
Child:
24:74a53dd49806
--- a/My_game_clases/Functions.cpp	Fri May 15 17:45:20 2020 +0000
+++ b/My_game_clases/Functions.cpp	Mon May 18 10:54:22 2020 +0000
@@ -0,0 +1,455 @@
+//includes
+#include "mbed.h"
+#include "N5110.h"
+#include "Gamepad.h"
+#include "Objects.h"
+#include "Functions.h"
+#include "math.h"
+
+//creates struct objects
+Ball ball;//
+Ball_linear ball1;
+
+Functions::Functions()
+{
+    //initialises values
+    _initial_radiuss = 2;
+    _cannon_y_pos = 40;
+    _score = 0;
+    _counter = 1;
+    _previous = 0;
+    _now = 0;
+
+    for (int i = 0; i < 10; i++) {
+        ///////////////////////////struct ball parabolic
+        ball.delta_r[i] = i;
+        ball.ball_y_pos[i] = _initial_radiuss + i + 1;//the +1 just makes so that the ball does not hit the ceiling but leaves 1 pixel
+        ball.movement_y_counter[i] = -1;
+        ball.time_incrementer[i] = 1;
+        ball.ball_x_pos[i] = 0;
+        ball.time[i] = 0;
+        ball.ball_x_incrementer[i] = 1;
+        ball.ball_trajectory_width[i] = 1;
+        ///////////////////////////////////struc ball linear
+        ball1.delta_r[i] = i;
+        ball1.ball_y_pos[i] = i + 5;
+        ball1.time_incrementer[i] = 1;
+        ball1.ball_x_pos[i] = 0;
+        ball1.movement_y_counter[i] = 1;
+        ball1.ball_x_incrementer[i] = 1;
+        ///////////////////////////////////
+    }
+}
+
+void Functions::ball_position_parabolic(N5110 &lcd, Objects &objects, int c )
+{
+    //increments the time
+    ball.time[c] += ball.time_incrementer[c];
+    //while the previous and time value is not the same continu until they do
+    //creates parabolic movement for the ball
+    while(_previous != ball.time[c]) {
+        if (_counter == 1) {
+            if(ball.ball_y_pos[c] - _initial_radiuss - ball.delta_r[c] - 2 < 0) {
+                _previous = 0;
+            } else {
+                _previous = round(pow((ball.ball_y_pos[c] - _initial_radiuss - ball.delta_r[c] - 2)/ball.ball_trajectory_width[c],0.5));
+            }
+            _now = round(pow((ball.ball_y_pos[c] - _initial_radiuss - ball.delta_r[c] - 1)/ball.ball_trajectory_width[c],0.5));
+            //checks if x incrementation has to happen
+            if (!(_previous == _now)) {
+                //checks if ball has reached the end of the screen
+                if (ball.ball_x_pos[c] >= (83 - 2 * (_initial_radiuss + ball.delta_r[c])) ) {
+                    ball.ball_x_incrementer[c] = -1 ;
+                }
+                if (ball.ball_x_pos[c] <= 0 ) {
+                    ball.ball_x_incrementer[c] = 1;
+                }
+                ball.ball_x_pos[c] += ball.ball_x_incrementer[c];
+            }
+
+            //lcd,x,y,delta_r;
+            objects.draw_ball(lcd,_initial_radiuss + ball.delta_r[c] + ball.ball_x_pos[c],ball.ball_y_pos[c], ball.delta_r[c]);
+            //checks if ball has to change direction of movment in y direction
+            if(ball.ball_y_pos[c] == 45 - _initial_radiuss - ball.delta_r[c] && _counter == 1) {
+                ball.time_incrementer[c] = -1;
+                _counter = -1;
+                // printf("%d \n", _counter);
+                break;
+            }
+            //increments the balls y position
+            ball.ball_y_pos[c] += 1;
+        }
+        if (_counter == -1) {
+            _previous = round(pow((ball.ball_y_pos[c] - _initial_radiuss - ball.delta_r[c])/ball.ball_trajectory_width[c],0.5));
+            _now = round(pow((ball.ball_y_pos[c] - _initial_radiuss - ball.delta_r[c] - 1)/ball.ball_trajectory_width[c],0.5));
+            //checks if x position has to be incremented
+            if(!(_previous == _now)) {
+                if (ball.ball_x_pos[c] >= (83 - 2 * (_initial_radiuss + ball.delta_r[c])) ) {
+                    ball.ball_x_incrementer[c] = -1 ;
+                }
+                if (ball.ball_x_pos[c] <= 0 ) {
+                    ball.ball_x_incrementer[c] = 1;
+                }
+                ball.ball_x_pos[c] += ball.ball_x_incrementer[c];
+            }
+            //lcd,x,y,delta_r;
+            objects.draw_ball(lcd,_initial_radiuss + ball.delta_r[c] + ball.ball_x_pos[c],ball.ball_y_pos[c], ball.delta_r[c]);
+            //checks if movement in y direction has to be changed
+            if(ball.ball_y_pos[c] == 1 + _initial_radiuss + ball.delta_r[c] && _counter == -1) {
+                ball.time_incrementer[c] = 1;
+                _counter = 1;
+                break;
+            }
+            //decrements y position
+            ball.ball_y_pos[c] += -1;
+        }
+        //checks for ball and shot collisions
+        collision_checker(lcd,objects);
+    }
+    lcd.clear();
+    //lcd,x,y,delta_r;
+    objects.draw_ball(lcd,_initial_radiuss + ball.delta_r[c] + ball.ball_x_pos[c],ball.ball_y_pos[c], ball.delta_r[c]);
+}
+
+void Functions::ball_position_linear(N5110 &lcd, Objects &objects, int c )
+{
+    //draws the ball
+    for (int i = 0; i < ball1.movement_y_counter[c]; i++) {
+        ball1.ball_y_pos[c] =  ball1.ball_y_pos[c] + ball1.time_incrementer[c];
+        objects.draw_ball(lcd,_initial_radiuss + ball1.delta_r[c] + ball1.ball_x_pos[c],ball1.ball_y_pos[c], ball1.delta_r[c]);
+        collision_checker(lcd,objects);
+    }
+    //clears the after image of the ball
+    lcd.clear();
+    //lcd,x,y,delta_r;
+    //creates just the ball at the end position
+    objects.draw_ball(lcd,_initial_radiuss + ball1.delta_r[c] + ball1.ball_x_pos[c],ball1.ball_y_pos[c], ball1.delta_r[c]);
+    //increments in  the x direction
+    ball1.ball_x_pos[c] += ball1.ball_x_incrementer[c];
+}
+
+void Functions::collision_checker(N5110 &lcd,Objects &objects)
+{
+    //checks for all shots if they have collided
+    for (int i = 0; i < objects.get_size(); i++) {
+        //if a pixel in front is black collision has occured
+        //checks for both pixels that are in front if the shot
+        for (int j = 0; j < 2; j++) {
+            //if pixel is black call ball finder function to determine which ball it is
+            if (lcd.getPixel(objects.get_x_value(i) + j,  objects.get_y_value(i)) == 1) {
+                ball1_finder(objects.get_x_value(i) + j,objects.get_y_value(i));
+                ball_finder(objects.get_x_value(i) + j,objects.get_y_value(i));
+                //erase the shot that has collided
+                objects.erase_shot(i);
+                break;
+            }
+        }
+    }
+}
+
+void Functions::ball1_finder(int x, int y)
+{
+    int delta_y;
+    int delta_x;
+    float delta_r;
+    float radiuss;
+    //checks for all the balls if the radiuss of it is in range of the shot
+    for (int i = 0; i < 10; i++) {
+        delta_y = ball1.ball_y_pos[i] - y;
+        delta_x = ball1.ball_x_pos[i] - x + ball1.delta_r[i] + _initial_radiuss;
+        delta_r = pow(delta_x*delta_x + delta_y*delta_y,0.5);
+        radiuss = ball1.delta_r[i] + _initial_radiuss;
+        //comapres the two radius values
+        if ( delta_r > (radiuss - 0.5f) && delta_r < (radiuss + 0.5f)) {
+            if (ball1.delta_r[i] == 0) {
+                ball1.delta_r[i] += -3;// -3 is because even with radiuss of 0 a 1 pixel ball flies around the screen
+                break;
+            } else {
+                ball1.delta_r[i] += -1;
+                break;
+            }
+        }
+    }
+}
+
+void Functions::ball_finder(int x, int y)
+{
+    int delta_y;
+    int delta_x;
+    float delta_r;
+    float radiuss;
+    //checks for all the balls
+    for (int i = 0; i < 10; i++) {
+        delta_y = ball.ball_y_pos[i] - y;
+        delta_x = ball.ball_x_pos[i] - x + ball.delta_r[i] + _initial_radiuss;
+        delta_r = pow(delta_x*delta_x + delta_y*delta_y,0.5);
+        radiuss = ball.delta_r[i] + _initial_radiuss;
+        //compares the two raiduss values
+        if ( delta_r > (radiuss - 0.5f) && delta_r < (radiuss + 0.5f)) {
+            if (ball.delta_r[i] == 0) {
+                ball.delta_r[i] += -3;// -3 is because even with radiuss of 0 a 1 pixel ball flies around
+                break;
+            } else {
+                ball.delta_r[i] += -1;
+                break;
+            }
+        }
+    }
+}
+
+bool Functions::cannon_smash(N5110 &lcd, Objects &objects)
+{
+    //if a pixel infront of the cannon is black a collision has occured
+    //each case check different pixel
+    for (int i = 0; i < 14; i++) {
+        switch(i) {
+            case 0:
+                if (lcd.getPixel(objects.get_x_cannon() - 1, _cannon_y_pos + 5 ) == 1) {
+                    return true;
+                }
+                break;
+            case 1:
+                if (lcd.getPixel(objects.get_x_cannon() - 1, _cannon_y_pos + 4 ) == 1) {
+                    return true;
+                }
+                break;
+            case 2:
+                if (lcd.getPixel(objects.get_x_cannon(), _cannon_y_pos + 3 ) == 1) {
+                    return true;
+                }
+                break;
+            case 3:
+                if (lcd.getPixel(objects.get_x_cannon(), _cannon_y_pos + 2 ) == 1) {
+                    return true;
+                }
+                break;
+            case 4:
+                if (lcd.getPixel(objects.get_x_cannon() + 1, _cannon_y_pos + 1 ) == 1) {
+                    return true;
+                }
+                break;
+            case 5:
+                if (lcd.getPixel(objects.get_x_cannon() + 1, _cannon_y_pos  ) == 1) {
+                    return true;
+                }
+                break;
+            case 6:
+                if (lcd.getPixel(objects.get_x_cannon() + 2, _cannon_y_pos - 1) == 1) {
+                    return true;
+                }
+                break;
+            case 7:
+                if (lcd.getPixel(objects.get_x_cannon() + 3, _cannon_y_pos - 1 ) == 1) {
+                    return true;
+                }
+                break;
+            case 8:
+                if (lcd.getPixel(objects.get_x_cannon() + 4, _cannon_y_pos ) == 1) {
+                    return true;
+                }
+                break;
+            case 9:
+                if (lcd.getPixel(objects.get_x_cannon() + 4, _cannon_y_pos + 1 ) == 1) {
+                    return true;
+                }
+                break;
+            case 10:
+                if (lcd.getPixel(objects.get_x_cannon() + 5, _cannon_y_pos + 2 ) == 1) {
+                    return true;
+                }
+                break;
+            case 11:
+                if (lcd.getPixel(objects.get_x_cannon() + 5, _cannon_y_pos + 3 ) == 1) {
+                    return true;
+                }
+                break;
+            case 12:
+                if (lcd.getPixel(objects.get_x_cannon() + 6, _cannon_y_pos + 4 ) == 1) {
+                    return true;
+                }
+                break;
+            case 13:
+                if (lcd.getPixel(objects.get_x_cannon() + 6, _cannon_y_pos + 5 ) == 1) {
+                    return true;
+                }
+                break;
+        }
+    }
+    return false;
+}
+
+int Functions::random(Gamepad &pad)
+{
+    //reads the two pot values
+    //it uses the thermal nosise as the random generator
+    float pot1 = pad.read_pot1();
+    float pot2 = pad.read_pot2();
+    while(pot1 > 0.1f) {
+        pot1 += -0.1;
+    }
+    while(pot1 > 0.01f) {
+        pot1 += -0.01;
+    }
+    while(pot1 > 0.001f) {
+        pot1 += -0.001;
+    }
+    while(pot1 > 0.0001f) {
+        pot1 += -0.0001;
+    }
+    while(pot1 > 0.00001f) {
+        pot1 += -0.00001;
+    }
+    while(pot1 > 0.000001f) {
+        pot1 += -0.000001;
+    }
+    while(pot2 > 0.1f) {
+        pot2 += -0.1;
+    }
+    while(pot2 > 0.01f) {
+        pot2 += -0.01;
+    }
+    while(pot2 > 0.001f) {
+        pot2 += -0.001;
+    }
+    while(pot2 > 0.0001f) {
+        pot2 += -0.0001;
+    }
+    while(pot2 > 0.00001f) {
+        pot2 += -0.00001;
+    }
+    while(pot2 > 0.000001f) {
+        pot2 += -0.000001;
+    }
+
+    pot1 = pot1 * 100000000;
+    pot2 = pot2 * 100000000;
+
+    int number = 0;
+
+    if (((pot1 + pot2) / 2) >= 100) {
+        number = (int)floor((pot1 + pot2) / 20);
+    } else {
+        number = (int)floor((pot1 + pot2) / 2);
+    }
+    return number;
+}
+
+void Functions::ball_creater_linear(N5110 &lcd, Objects &objects,Gamepad &pad)
+{
+    //creates 3 random values
+    int random0_99 = random(pad);
+    int random0_9 = random(pad)%10;
+    int random0_5 = random(pad)%5;
+
+    //generates a random ball value
+    if (created_balls.size() == 0) {
+        created_balls.push_back(random0_9);
+    } else if (created_balls.size() < 2 ) {
+        if(_score > 20 &&_score < 40) {
+            created_balls.push_back(random0_9);
+        }
+        if(_score > 100){
+            created_balls.push_back(random0_5);
+        }
+    }
+
+    //runs for each created ball
+    for (int i = 0; i < created_balls.size(); i++) {
+        //sets boundries for balls x position so does not go out of screen
+        if (ball1.ball_x_pos[created_balls[i]] >= (82 - 2*(_initial_radiuss + ball1.delta_r[created_balls[i]])) ) {
+            ball1.ball_x_incrementer[created_balls[i]] = -1;
+            //changes the speed ofo the ball
+            ball1.movement_y_counter[created_balls[i]] = random0_5 + 1;
+        }
+        if (ball1.ball_x_pos[created_balls[i]] <= 0 ) {
+            ball1.ball_x_incrementer[created_balls[i]] = 1;
+            //changes the speed of the ball
+            ball1.movement_y_counter[created_balls[i]] = random0_5 + 1;
+        }
+
+        //sets boundries for balls y position so does not go out of screen
+        if((ball1.ball_y_pos[created_balls[i]] + _initial_radiuss + ball1.delta_r[created_balls[i]] ) == 45 ) {
+            ball1.time_incrementer[created_balls[i]] = -1 ;
+        }
+        if((ball1.ball_y_pos[created_balls[i]] + _initial_radiuss + ball1.delta_r[created_balls[i]] + ball1.movement_y_counter[created_balls[i]]) > 45 ) {
+            ball1.ball_y_pos[created_balls[i]]  = 45 - _initial_radiuss - ball1.delta_r[created_balls[i]] - ball1.movement_y_counter[created_balls[i]];
+        }
+        if((ball1.ball_y_pos[created_balls[i]] - _initial_radiuss - ball1.delta_r[created_balls[i]] ) == 0) {
+            ball1.time_incrementer[created_balls[i]] = 1;
+        }
+        if((ball1.ball_y_pos[created_balls[i]] - _initial_radiuss - ball1.delta_r[created_balls[i]] - ball1.movement_y_counter[created_balls[i]] ) < 0) {
+            ball1.ball_y_pos[created_balls[i]] =  _initial_radiuss + ball1.delta_r[created_balls[i]] + ball1.movement_y_counter[created_balls[i]];
+        }
+        //draws the actual ball
+        ball_position_linear(lcd, objects, created_balls[i]);
+    }
+
+    //draws the balls again since in each draw lcd.clear() has been used
+    for(int i = 0; i < created_balls.size(); i++) {
+        objects.draw_ball(lcd,_initial_radiuss + ball1.delta_r[created_balls[i]] + ball1.ball_x_pos[created_balls[i]],ball1.ball_y_pos[created_balls[i]], ball1.delta_r[created_balls[i]]);
+    }
+
+    //erases the balls which have been destroyed
+    for(int i = 0; i < created_balls.size(); i++) {
+        if(ball1.delta_r[created_balls[i]] == -3) {
+            created_balls.erase(created_balls.begin() + i);
+        }
+    }
+
+    //checks if any of the balls have been destroyed and restores their delta_r
+    for (int i = 0; i < 10; i++) {
+        if(ball1.delta_r[i] == -3) {
+            ball1.delta_r[i] = i;
+            ball1.ball_y_pos[i] = _initial_radiuss + i;
+            ball1.ball_x_pos[i] = 0;//_initial_radiuss + i;
+            //scores here
+            _score += i;
+        }
+    }
+}
+void Functions ::ball_creater_parabolic(N5110 &lcd, Objects &objects, Gamepad &pad)
+{
+    //creates a randmo value
+    int random0_9 = random(pad)%10;
+
+    //generates a random ball size
+    if (created_ball.size() == 0 && _score > 40) {
+        created_ball.push_back(random0_9);
+    }
+
+    //creates the ball
+    if(created_ball.size() != 0) {
+        ball_position_parabolic(lcd, objects, created_ball[0]);
+
+        //draws the linear balls again
+        for(int i = 0; i < created_balls.size(); i++) {
+            objects.draw_ball(lcd,_initial_radiuss + ball1.delta_r[created_balls[i]] + ball1.ball_x_pos[created_balls[i]],ball1.ball_y_pos[created_balls[i]], ball1.delta_r[created_balls[i]]);
+        }
+        //resets the destroyed ball
+        if (ball.delta_r[created_ball[0]] == -3) {
+            ball.delta_r[created_ball[0]] = created_ball[0];
+            ball.ball_x_pos[created_ball[0]] = 0;
+            ball.ball_y_pos[created_ball[0]] = _initial_radiuss + created_ball[0] + 1;
+            //scores when a balll is destroyed
+            _score += created_ball[0] * 2;
+            created_ball.erase(created_ball.begin());
+        }
+    }
+}
+int Functions::round(float number)
+{
+    //round the passed value
+    float decimal = number;
+    while(decimal > 1) {
+        decimal += -1;
+    }
+    if (decimal < 0.5f) {
+        return floor(number);
+    } else {
+        return ceil(number);
+    }
+}
+int Functions::get_score()
+{
+    return _score;
+}
\ No newline at end of file