diff --git a/include/Map.hpp b/include/Map.hpp index db1bf27..9030a4b 100644 --- a/include/Map.hpp +++ b/include/Map.hpp @@ -89,12 +89,12 @@ private: .gen = current_gen_, .pos = { - .x = std::rand() % config_.size.x, - .y = std::rand() % config_.size.y, + .x = utils::rand_to(config_.size.x), + .y = utils::rand_to(config_.size.y), }, .weight = config_.min_food_weight + - std::rand() % std::abs(config_.max_food_weight - - config_.min_food_weight), + utils::rand_to(std::abs(config_.max_food_weight - + config_.min_food_weight)), }); } } diff --git a/include/Snake.hpp b/include/Snake.hpp index 6f3bf98..3d079e1 100644 --- a/include/Snake.hpp +++ b/include/Snake.hpp @@ -11,7 +11,7 @@ class SnakeObject { public: struct Config { Object obj; - size_t initial_length; + uint initial_length; int radius; }; @@ -34,12 +34,14 @@ public: // - size_t get_length() { return length_; } + uint get_length() { return length_; } - void set_length(size_t length) { length_ = length; } + uint get_initial_length() { return canvas_config_.initial_length; } + + void set_length(uint length) { length_ = length; } void inc_length(int inc) { - if (-inc > static_cast(length_)) { + if (-inc > int(length_)) { length_ = 1; } else { length_ += inc; @@ -85,14 +87,7 @@ public: protected: virtual void change_direction(float dt) = 0; - virtual void move(float dt) { - real_pos_ += direction_ * dt * snake_config_.speed; - move_time_delta_ += dt; - if (move_time_delta_ > snake_config_.move_interval) { - move_time_delta_ -= snake_config_.move_interval; - add(Veci(real_pos_)); - } - } + virtual void move(float dt); protected: const Config snake_config_; diff --git a/include/Text.hpp b/include/Text.hpp new file mode 100644 index 0000000..cb51da9 --- /dev/null +++ b/include/Text.hpp @@ -0,0 +1,18 @@ +#pragma once + +#include "Canvas.hpp" + +namespace canvas { + +struct Text : public Object { + uint scale; + uint value; +}; + +} // namespace canvas + +namespace paint { + +void text(const canvas::Text &t); + +} // namespace paint diff --git a/include/Utils.hpp b/include/Utils.hpp index da6229e..d936fb6 100644 --- a/include/Utils.hpp +++ b/include/Utils.hpp @@ -56,6 +56,15 @@ template inline Vec to_world_coord(Vec pos) { }; } +inline int rand_to(int x) { + if (std::abs(x) <= 1) { + return 0; + } + + bool is_negative = x < 0; + return (is_negative ? -1 : 1) + rand() % std::abs(x); +} + } // namespace utils namespace color { diff --git a/src/Canvas.cpp b/src/Canvas.cpp index 8acfee1..aafbd80 100644 --- a/src/Canvas.cpp +++ b/src/Canvas.cpp @@ -2,7 +2,7 @@ #include "Utils.hpp" -using namespace utils; +using utils::screen_at; namespace paint { diff --git a/src/Game.cpp b/src/Game.cpp index 237a23a..8e0c39d 100644 --- a/src/Game.cpp +++ b/src/Game.cpp @@ -9,6 +9,7 @@ #include "Params.hpp" #include "Player.hpp" #include "Snake.hpp" +#include "Text.hpp" #include "Utils.hpp" #include "World.hpp" @@ -90,6 +91,12 @@ void draw() { player.draw(map_offset); map.draw(map_offset); + + paint::text({ + {.pos = {.x = 10, .y = 10}, .color = {color::BLUE}}, + 5, + player.get_length() - player.get_initial_length(), + }); } // free game data in this function diff --git a/src/Snake.cpp b/src/Snake.cpp index 275c1fd..a41c3d0 100644 --- a/src/Snake.cpp +++ b/src/Snake.cpp @@ -27,3 +27,12 @@ bool SnakeObject::touches(const SnakeObject &other) { } } // namespace canvas + +void Snake::move(float dt) { + real_pos_ += direction_ * dt * snake_config_.speed; + move_time_delta_ += dt; + if (move_time_delta_ > snake_config_.move_interval) { + move_time_delta_ -= snake_config_.move_interval; + add(Veci(real_pos_)); + } +} diff --git a/src/Text.cpp b/src/Text.cpp new file mode 100644 index 0000000..78cc310 --- /dev/null +++ b/src/Text.cpp @@ -0,0 +1,161 @@ +#include "Text.hpp" + +#include + +using utils::screen_at; + +constexpr uint LETTER_WIDTH = 4; +constexpr uint LETTER_HEIGHT = 6; + +using Letter = bool[LETTER_WIDTH][LETTER_HEIGHT]; + +namespace utils { + +constexpr Letter letter_0 = { + {1, 1, 1, 1, 1}, + {1, 0, 0, 0, 1}, + {1, 0, 0, 0, 1}, + {1, 1, 1, 1, 1}, +}; + +constexpr Letter letter_1 = { + {0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0}, + {1, 1, 1, 1, 1}, +}; + +constexpr Letter letter_2 = { + {1, 0, 1, 1, 1}, + {1, 0, 1, 0, 1}, + {1, 0, 1, 0, 1}, + {1, 1, 1, 0, 1}, +}; + +constexpr Letter letter_3 = { + {1, 0, 1, 0, 1}, + {1, 0, 1, 0, 1}, + {1, 0, 1, 0, 1}, + {1, 1, 1, 1, 1}, +}; + +constexpr Letter letter_4 = { + {1, 1, 1, 0, 0}, + {0, 0, 1, 0, 0}, + {0, 0, 1, 0, 0}, + {1, 1, 1, 1, 1}, +}; + +constexpr Letter letter_5 = { + {1, 1, 1, 0, 1}, + {1, 0, 1, 0, 1}, + {1, 0, 1, 0, 1}, + {1, 0, 1, 1, 1}, +}; + +constexpr Letter letter_6 = { + {1, 1, 1, 1, 1}, + {1, 0, 1, 0, 1}, + {1, 0, 1, 0, 1}, + {1, 0, 1, 1, 1}, +}; + +constexpr Letter letter_7 = { + {1, 0, 0, 0, 0}, + {1, 0, 0, 0, 0}, + {1, 0, 0, 0, 0}, + {1, 1, 1, 1, 1}, +}; + +constexpr Letter letter_8 = { + {1, 1, 1, 1, 1}, + {1, 0, 1, 0, 1}, + {1, 0, 1, 0, 1}, + {1, 1, 1, 1, 1}, +}; + +constexpr Letter letter_9 = { + {1, 1, 1, 0, 1}, + {1, 0, 1, 0, 1}, + {1, 0, 1, 0, 1}, + {1, 1, 1, 1, 1}, +}; + +constexpr Letter letter_undef = { + {0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0}, +}; + +} // namespace utils + +namespace paint { + +void text(const canvas::Text &t) { + std::string value_str = std::to_string(t.value); + size_t offset = 0; + for (const auto &ch : value_str) { + const Letter *letter = nullptr; + switch (ch) { + case '0': + letter = &utils::letter_0; + break; + case '1': + letter = &utils::letter_1; + break; + case '2': + letter = &utils::letter_2; + break; + case '3': + letter = &utils::letter_3; + break; + case '4': + letter = &utils::letter_4; + break; + case '5': + letter = &utils::letter_5; + break; + case '6': + letter = &utils::letter_6; + break; + case '7': + letter = &utils::letter_7; + break; + case '8': + letter = &utils::letter_8; + break; + case '9': + letter = &utils::letter_9; + break; + default: + letter = &utils::letter_undef; + break; + } + + for (uint i = 0; i < LETTER_WIDTH; ++i) { + for (uint j = 0; j < LETTER_HEIGHT; ++j) { + if ((*letter)[i][j]) { + for (uint k = 0; k < t.scale; ++k) { + int pixel_offset_y = + i * t.scale + offset * (LETTER_WIDTH + 1) * t.scale; + int pixel_offset_x = j * t.scale + k; + std::fill(screen_at({ + .x = t.pos.x + pixel_offset_x, + .y = t.pos.y + pixel_offset_y, + }), + screen_at({ + .x = t.pos.x + pixel_offset_x, + .y = t.pos.y + pixel_offset_y + int(t.scale), + }), + t.color.v); + } + } + } + } + + ++offset; + } +} + +} // namespace paint