Skip to content
Snippets Groups Projects
Select Git revision
1 result Searching

Button.cs

Blame
  • player.cpp 8.53 KiB
    #include "player.hpp"
    #include "Level.hpp"
    #include "playerprojectile.hpp"
    Player::Player(float PosX, float PosY)
    {
        hitpoints = 5;
        topleft.X=PosX;
        topleft.Y=PosY;
    
    }
    static StaticPixelBuffer<PLAYER_W, PLAYER_H> 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<PLAYER_W, PLAYER_H> 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<PLAYER_W, PLAYER_H> 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<PLAYER_W, PLAYER_H> 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 "}
         }}}
        };
    
    PixelBuffer const& Player::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 Player::move(Platform &p, FloatSeconds const & Frametime, Level &level)
    {
        timetillreload-=Frametime.count();
        float deltaX = 0.0f;
            float deltaY = 0.0f;
            auto input = p.get_input();
            for(char const c : input) {
                // c = eine der gedrückten Tasten
                switch(c)
                {
                case 'W': case 'w':
                {
                    dir=direction::BACK;
                    deltaY-= speed*Frametime.count(); break;
                }
                case 'A': case 'a':
                {
                    dir=direction::LEFT;
                    deltaX-= speed*Frametime.count(); break;
                }
                case 'S': case 's':
                {
                    dir=direction::FRONT;
                    deltaY+= speed*Frametime.count(); break;
                }
                case 'D': case 'd':
                {
                    dir=direction::RIGHT;
                    deltaX+= speed*Frametime.count(); break;
                }
                case ' ':
                {
                    shoot(level);
                }
                }
            }
            auto const &sprite = this->getSprite();
            if(!level.checkCollision(topleft.X + deltaX, topleft.Y + deltaY, sprite.w(), sprite.h())) {
                topleft.X+=deltaX;
                topleft.Y+=deltaY;
    
                if(deltaX!=0||deltaY!=0)
                {
                    int constexpr ANIMATION_FRAMES = 2;
                    float constexpr ANIMATION_FRAME_TIME = 0.1f;
                    animationtimer += Frametime.count();
                    animationstep = static_cast<int>(animationtimer / ANIMATION_FRAME_TIME) % ANIMATION_FRAMES;
                }
            }
    }
    void Player::shoot(Level &level)
    {
        if(timetillreload<0)
        {
            auto const &sprite = this->getSprite();
            switch ( this->dir ) {
            case direction::FRONT: //Up
            {
                level.addPlayerProjectile({this->topleft.X+sprite.w()/2-1,this->topleft.Y+sprite.h()/2,dir});
                break;
            }
            case direction::LEFT: //left
                level.addPlayerProjectile({this->topleft.X,this->topleft.Y+sprite.h()/2,dir});
                break;
            case direction::RIGHT: //right
                level.addPlayerProjectile({this->topleft.X+sprite.w(),this->topleft.Y+sprite.h()/2,dir});
                break;
            default: //Down
                level.addPlayerProjectile({this->topleft.X+sprite.w()/2-1,this->topleft.Y,dir});
                break;
            }
            timetillreload = 1;
        }
    
    }
    static StaticPixelBuffer<39,7> constexpr lives[6]={
        {{{
              {"                                       "},
              {"                                       "},
              {"                                       "},
              {"                                       "},
              {"                                       "},
              {"                                       "},
              {"                                       "}
          }}},
        {{{
              {" 22 22                                 "},
              {"2222222                                "},
              {"2222222                                "},
              {"2222222                                "},
              {" 22222                                 "},
              {"  222                                  "},
              {"   2                                   "}
          }}},
        {{{
              {" 22 22   22 22                         "},
              {"2222222 2222222                        "},
              {"2222222 2222222                        "},
              {"2222222 2222222                        "},
              {" 22222   22222                         "},
              {"  222     222                          "},
              {"   2       2                           "}
          }}},
        {{{
              {" 22 22   22 22   22 22                 "},
              {"2222222 2222222 2222222                "},
              {"2222222 2222222 2222222                "},
              {"2222222 2222222 2222222                "},
              {" 22222   22222   22222                 "},
              {"  222     222     222                  "},
              {"   2       2       2                   "}
          }}},
        {{{
              {" 22 22   22 22   22 22   22 22         "},
              {"2222222 2222222 2222222 2222222        "},
              {"2222222 2222222 2222222 2222222        "},
              {"2222222 2222222 2222222 2222222        "},
              {" 22222   22222   22222   22222         "},
              {"  222     222     222     222          "},
              {"   2       2       2       2           "}
          }}},
        {{{
              {" 22 22   22 22   22 22   22 22   22 22 "},
              {"2222222 2222222 2222222 2222222 2222222"},
              {"2222222 2222222 2222222 2222222 2222222"},
              {"2222222 2222222 2222222 2222222 2222222"},
              {" 22222   22222   22222   22222   22222 "},
              {"  222     222     222     222     222  "},
              {"   2       2       2       2       2   "}
          }}}
    };
    
    const PixelBuffer& Player::getLivesSprite()
    {
        return lives[hitpoints];
    }