Skip to content
Snippets Groups Projects
Select Git revision
  • cbbf1a0b15316d61cefb90a2a2d513ff783cfd4c
  • master default
  • Huaer
  • krueger
4 results

Scoreb.cpp

Blame
  • Tank.cpp 5.97 KiB
    #include "Tank.hpp"
    Tank::Tank(float x, float y, enum direction dir, int animationstep)
     : Enemy(3,x,y,dir,animationstep)
    {
    }
    
    int Tank::getScore()
    {
        return 500;
    }
    static StaticPixelBuffer<13,11> constexpr front[2] = {
        {{{
            {"      1      "},
            {"      1      "},
            {"    22222    "},
            {"   2223222   "},
            {"  122222221  "},
            {"  151155115  "},
            {" 15151115511 "},
            {"2222255522222"},
            {"2225115151222"},
            {"1111515155111"},
            {"222       222"}
        }}},
        {{{
             {"      1      "},
             {"      1      "},
             {"    22222    "},
             {"   2223222   "},
             {"  122222221  "},
             {"  151155115  "},
             {" 15151115511 "},
             {"2222255522222"},
             {"1115115151111"},
             {"2221515155222"},
             {"111       111"}
         }}}
        };
    static StaticPixelBuffer<13,11> constexpr back[2] = {
        {{{
            {"      1      "},
            {"      1      "},
            {"    22222    "},
            {"   2222222   "},
            {"  112222211  "},
            {"  151155115  "},
            {" 15151115511 "},
            {"2222255522222"},
            {"2225115151222"},
            {"1111515155111"},
            {"222       222"}
        }}},
        {{{
             {"      1      "},
             {"      1      "},
             {"    22222    "},
             {"   2222222   "},
             {"  112222211  "},
             {"  151155115  "},
             {" 15151115511 "},
             {"2222255522222"},
             {"1115115151111"},
             {"2221515155222"},
             {"111       111"}
         }}}
        };
    static StaticPixelBuffer<17,11> constexpr right[2] = {
        {{{
             {"      1          "},
             {"       1         "},
             {"      22222      "},
             {"     222222222223"},
             {" 15111511511     "},
             {"1515551515551151 "},
             {"55511155511555151"},
             {"22222222222222222"},
             {"10101010101010101"},
             {"01010101010101010"},
             {" 010101010101010 "}
    
        }}},
        {{{
             {"      1          "},
             {"       1         "},
             {"      22222      "},
             {"     222222222223"},
             {" 15111511511     "},
             {"1515551515551151 "},
             {"55511155511555151"},
             {"22222222222222222"},
             {"01010101010101010"},
             {"01010101010101010"},
             {" 101010101010101 "}
         }}}
        };
    
    static StaticPixelBuffer<17,11> constexpr left[2] = {
        {{{
             {"          1      "},
             {"         1       "},
             {"      22222      "},
             {"322222222222     "},
             {"     11115511151 "},
             {" 1155515115155151"},
             {"15511155511111555"},
             {"22222222222222222"},
             {"10101010101010101"},
             {"01010101010101010"},
             {" 010101010101010 "}
    
    
        }}},
        {{{
             {"          1      "},
             {"         1       "},
             {"      22222      "},
             {"322222222222     "},
             {"     11115511151 "},
             {" 1155515115155151"},
             {"15511155511111555"},
             {"22222222222222222"},
             {"01010101010101010"},
             {"01010101010101010"},
             {" 101010101010101 "}
         }}}
        };
    const PixelBuffer& Tank::getSprite()
    {
        switch (dir) {
    
        case direction::BACK:
            return back[animationstep];
            break;
        case direction::LEFT:
            return left[animationstep];
            break;
        case direction::RIGHT:
            return right[animationstep];
            break;
    
        default:
            return front[animationstep];
        }
    };
    /*void Tank::shoot()
    {
      //TODO how do Projectile?
    };*/
    
    void Tank::changeDirection()
    {
        dir=direction(rand()%4);
    };
    
    bool Tank::checkmove(FloatSeconds const & Frametime, Level &level)
    {
        switch (this->dir)
        {
            case direction::BACK: //down
                if(!level.checkCollision(this->topleft.X, this->topleft.Y - 3*Frametime.count(), this->getSprite().w(), this->getSprite().h()))
                    {this->topleft.Y -= 3*Frametime.count(); return false;}
                else return true;
                break;
            case direction::LEFT:
            if(!level.checkCollision(this->topleft.X - 3*Frametime.count(), this->topleft.Y, this->getSprite().w(), this->getSprite().h()))
                 {this->topleft.X -= 3*Frametime.count(); return false;}
            else return true;
                break;
        case direction::RIGHT:
                if(!level.checkCollision(this->topleft.X + 3*Frametime.count(),this->topleft.Y, this->getSprite().w(), this->getSprite().h()))
                     {this->topleft.X += 3*Frametime.count(); return false;}
                else return true;
                break;
            default:
                if(!level.checkCollision(this->topleft.X, this->topleft.Y + 3*Frametime.count(), this->getSprite().w(), this->getSprite().h()))
                    {this->topleft.Y += 3*Frametime.count(); return false;}
                else return true;
                break;
        }
    };
    
    void Tank::move(FloatSeconds const & Frametime, Level &level)
    {
        if(animationtimer>3*Frametime.count())
        {
            animationstep = 1-animationstep;
            animationtimer = 0;
        }
        else
        {
            animationtimer += Frametime.count();
        }
        if(checkmove(Frametime, level)) //Also moved when false;
        {
            changeDirection();
        }
    };
    
    void Tank::turnToPlayer(float PlayerX, float PlayerY)
    {
        if(abs(this->topleft.X - PlayerX) < 5) //buffer of 5
        {
            if(this->topleft.Y > PlayerY)
            {
                dir=direction::FRONT; //up
            }
            else
            {
                dir=direction::BACK; //down
            }
        }
        else
        {
            if(this->topleft.X > PlayerX)
            {
                dir=direction::RIGHT;
            }
            else
            {
                dir=direction::LEFT;
            }
        }
    };
    
    void Tank::doStep(float PlayerX, float PlayerY, FloatSeconds const & Frametime, Level &level) //Pathfinding easier with full level, not used currently.
    {
        timetillreload -= Frametime.count();
        if(playerdetected(PlayerX,PlayerY, level))
        { //Look into each direction, if true turns and shoot!
            shoot(level);
        }
        else
        {
            move(Frametime, level);
        }
    };