Ahmed Hedait / Mbed 2 deprecated el16ah

Dependencies:   mbed

Ball/Ball.cpp

Committer:
ahmedhedait
Date:
2018-05-08
Revision:
25:28c57be06933
Parent:
22:745b4d352183

File content as of revision 25:28c57be06933:

#include "Ball.h"

// nothing doing in the constructor and destructor
Ball::Ball()
{

}

Ball::~Ball()
{

}

void Ball::init()
{
    _circy = 5;
    _circx = 5;
    _speed = 1;

}

void Ball::draw(N5110 &lcd)
{
    // VERY SIMPLE CODE IN WHCIH I DREW THE BALL OF THE MAZE.
    lcd.drawCircle(_circx,_circy,2,FILL_BLACK);
}

void Ball::update(Direction dir)
{
    // ADJUSTING THE SPEED OF THE BALL FOR THE 4 DIFFERENT DIRECTIONS.
    if (dir == N) {
        _circy -= _speed;
    } else if (dir == S) {
        _circy += _speed;
    }

    if (dir == W) {
        _circx -= _speed;
    } else if (dir == E) {
        _circx += _speed;
    }

    // THIS CODE IS NEEDED TO MAKE SURE THAT THE BALL DOES NOT SURPASS THE DIMENSIONS OF THE LCD SCREEN.
    if (_circy < 3) {
        _circy = 3;
    }

    if (_circy > HEIGHT - 4) {
        _circy = HEIGHT - 4;
    }

    if (_circx < 3) {
        _circx = 3;
    }
}

Vector2D Ball::get_pos() {
    Vector2D p = {_circx,_circy};
    return p;    
}

void Ball::check_wall_collision(Gamepad &pad)
{
    // FOR LOOP TO STOP BALL FROM GOING THROUGH WALLS OF THE MAZE.
    for (int i = 0; i <= 12; i++) {
        //printf(" Stage  %d\n", i);

        if(i == 0) {    // SET VARIABLES FOR EACH WALL
            _a = 10;
            _b = 0;
            _c = 1;
            _d = 39;

        } else if (i == 1) {
            _a = 18;
            _b = 32;
            _c = 1;
            _d = 15;
        }

        else if (i == 2) {
            _a = 36;
            _b = 25;
            _c = 1;
            _d = 25;
        }

        else if(i == 3) {
            _a = 45;
            _b = 0;
            _c = 1;
            _d = 11;
        }

        else if (i == 4) {
            _a = 45;
            _b = 18;
            _c = 1;
            _d = 30;
        }

        else if (i == 5) {
            _a = 55;
            _b = 6;
            _c = 1;
            _d = 45;
        }

        else if (i == 6) {
            _a = 64;
            _b = 0;
            _c = 1;
            _d = 20;
        }

        else if (i == 7) {
            _a = 64;
            _b = 27;
            _c = 1;
            _d = 13;
        }

        else if (i == 8) {
            _a = 72;
            _b = 10;
            _c = 1;
            _d = 30;
        }

        else if (i == 9) {
            _a = 18;
            _b = 25;
            _c = 18;
            _d = 1;
        } else if (i == 10) {
            _a = 18;
            _b = 18;
            _c = 27;
            _d = 1;
        }

        else if (i == 11) {
            _a = 18;
            _b = 10;
            _c = 27;
            _d = 1;
        }

        else if (i == 12) {
            _a = 64;
            _b = 40;
            _c = 20;
            _d = 1;
        }

        // ADDED A PRINT FUNCTION TO DETECT IF COLLISION OCCURED BETWEEN BALL AND WALL OR NOT IN THE GIVEN DIMENSIONS.
        if (
            (_circy >= _b - 2) && //top
            (_circy <= 1 + _b + _d) && //bottom
            (_circx >= _a - 2) && //left
            (_circx <= _a + _c + 1)  //right
        ) {
            // printf("COLLISION");
            
            /* CONDITIONS FOR THE 4 DIRECTIONS FOR EACH RECTANGLE NEEDS TO BE MET SO THAT THE BALL DOES NOT PASS 
            THE DIFFERENT WALLS OF THE MAZE. */
            //left
            if (_circx <= _a - 2) {
                if(_circx >= _a - 3) {
                    _circx = _a - 3;
                }
            }

            //right
            if(_circx >= _a + 2) {
                if(_circx <= _a + 3) {
                    _circx = _a + 3;
                }
            }

            //top
            if(_circy <= _b - 2) {
                if(_circy >= _b - 3) {
                    _circy = _b - 3;
                }
            }

            //bottom
            if(_circy >= _b + _d) {
                if(_circy <= 2 + _b + _d) {
                    (_circy = 2 + _b + _d);
                }
            }
        }
    }
    
    
    /* GAVE THE AXIS NEEEDED FOR THE BALL TO GET OUT AND ONLY OUT OF THE OPENING OF THE MAZE. WEHN THE CONDITIONS ARE
       NOT MEANT, THEN IT RESTRICTS THE BALL FROM MOVING OUTSIDE THE MAZE */  
    
    if (_circy == 27) {
        if (_circx > WIDTH) {
            _circx = WIDTH;
        }
    } else if (_circx > 80) {
        _circx = 80;
    }
}