diff --git a/main.cpp b/main.cpp index d20be0576d89b65e3decd480c7b5cccb1a33be70..57edc66762bf87b2cb9a467b45deb387d44e701c 100644 --- a/main.cpp +++ b/main.cpp @@ -61,14 +61,7 @@ class GameLoop { Score Highscore = {screenWidth,screenHeigth}; public: void run(Platform& platform, FloatSeconds const &frame_time, Level &level, Player &player_) { - for(int y = 0; y < framebuffer.h(); ++y) { - for(int x = 0; x < framebuffer.w(); ++x) { - framebuffer.at({ x, y }) = - (y == 0 || y == (framebuffer.h() - 1)) ? _t : - (x == 0 || x == (framebuffer.w() - 1)) ? _t : - _0; - } - } + framebuffer.clear(_0); if(frame_time.count() != 0.0f) { if(!debug){ @@ -266,7 +259,9 @@ public: framebuffer.blit_topleft(i.ScorePos,i.getScoreSprite()); } } - + framebuffer.blit_topleft({ 3, 3 },player_.getLivesSprite()); + // kruegerzo72182> + // <hoffmanncl72341 platform.render(framebuffer); ++frame; diff --git a/player.cpp b/player.cpp index 700dbbd316d7d2f91e86440ae114441dd38bc48b..9fde6abbc5afe3dc73f7e8eb243e9fad21fc45d7 100644 --- a/player.cpp +++ b/player.cpp @@ -1,7 +1,8 @@ #include "player.hpp" #include "Level.hpp" #include "playerprojectile.hpp" -Player::Player(float PosX, float PosY) +Player::Player(float PosX, float PosY) : + shader(*this) { hitpoints = 5; topleft.X=PosX; @@ -125,23 +126,46 @@ static StaticPixelBuffer<PLAYER_W, PLAYER_H> constexpr left[2] = { }}} }; -PixelBuffer const& Player::getSprite() +Player::Shader::Shader(Player const &player) : + PixelBuffer(PLAYER_W, (PLAYER_H + 2)), player(player) { - switch ( dir ) { +} - case direction::BACK: - return back[animationstep]; - break; - case direction::LEFT: - return left[animationstep]; - break; - case direction::RIGHT: - return right[animationstep]; - break; +Color Player::Shader::peekUnchecked(PixelPoint const &pos) const +{ + auto const &basesprite = [this]() { + switch (player.dir) { + case direction::BACK: + return back[player.animationstep]; + break; + case direction::LEFT: + return left[player.animationstep]; + break; + case direction::RIGHT: + return right[player.animationstep]; + break; - default: - return front[animationstep]; + default: + return front[player.animationstep]; + } + }(); + if(player.timetillreload == 0.0f) { + return basesprite.peek(pos); } + + auto reloadpercent = (player.timetillreload / RELOAD_TIME); + auto reloadwidth = static_cast<int>(reloadpercent * PLAYER_W); + int constexpr BAR_H = 1; + + if((pos.y >= (h() - BAR_H)) && (pos.x <= reloadwidth)) { + return PLAYER_PROJECTILE_COLOR; + } + return basesprite.peek(pos); +} + +PixelBuffer const& Player::getSprite() +{ + return shader; } void Player::move(Platform &p, FloatSeconds const & Frametime, Level &level) { @@ -218,3 +242,64 @@ void Player::shoot(Level &level) } } +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]; +} diff --git a/player.hpp b/player.hpp index c37d0df83481e43c7aab0b8244b0cd6c05490081..aaa14cdc90ced8353a34e791e360a1121040d7d7 100644 --- a/player.hpp +++ b/player.hpp @@ -8,20 +8,34 @@ int constexpr PLAYER_W = 17; int constexpr PLAYER_H = 11; +float constexpr RELOAD_TIME = 1.0f; class Player : public Entity { protected: + // Turned out to be way overkill given that we only draw a bar below the + // sprite... + class Shader : public PixelBuffer { + protected: + virtual Color peekUnchecked(PixelPoint const &pos) const; + public: + Player const &player; + + Shader(Player const &player); + }; + int speed=48; direction dir=direction::BACK; int animationstep=0; float timetillreload=0.0f; float animationtimer=0.0f; + Shader shader; public: Player(float x, float y); PixelBuffer const& getSprite() override; void move(Platform &p, FloatSeconds const & Frametime, Level &level); void shoot(Level &level); + const PixelBuffer& getLivesSprite(); }; #endif // PLAYER_HPP