mirror of
https://github.com/ProgramSnail/snake_2024.git
synced 2025-12-24 23:28:44 +00:00
some refastoring, cycled map
This commit is contained in:
parent
b4509733da
commit
1e9c70f7d8
5 changed files with 44 additions and 16 deletions
|
|
@ -49,8 +49,8 @@ public:
|
|||
|
||||
void draw(Veci offset) override {
|
||||
for (const auto &food : food_) {
|
||||
Veci food_pos = food.pos - offset;
|
||||
if (utils::is_valid_pos(food_pos) and not food.eaten) {
|
||||
Veci food_pos = utils::to_world_coord(food.pos - offset);
|
||||
if (utils::is_on_screen(food_pos) and not food.eaten) {
|
||||
paint::circle({{
|
||||
.pos = food_pos,
|
||||
.color = color::scale(config_.food_color,
|
||||
|
|
|
|||
5
include/Params.hpp
Normal file
5
include/Params.hpp
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
#pragma once
|
||||
|
||||
#include "Vec.hpp"
|
||||
|
||||
constexpr Veci WORLD_SIZE = {.x = 10000, .y = 10000};
|
||||
|
|
@ -1,8 +1,10 @@
|
|||
#pragma once
|
||||
|
||||
#include <algorithm>
|
||||
#include <cassert>
|
||||
|
||||
#include "Engine.h"
|
||||
#include "Params.hpp"
|
||||
#include "Vec.hpp"
|
||||
|
||||
namespace utils {
|
||||
|
|
@ -21,21 +23,39 @@ inline int x(int x) { return safe::ux(safe::lx(x)); }
|
|||
inline int y(int y) { return safe::uy(safe::ly(y)); }
|
||||
} // namespace safe
|
||||
|
||||
inline uint32_t *screen_at(int x, int y) {
|
||||
return &buffer[safe::x(x)][safe::y(y)];
|
||||
inline uint32_t *screen_at(Veci pos) {
|
||||
return &buffer[safe::x(pos.x)][safe::y(pos.y)];
|
||||
}
|
||||
|
||||
template <typename T> inline bool is_valid_pos(Vec<T> pos) {
|
||||
template <typename T> inline bool is_on_screen(Vec<T> pos) {
|
||||
return pos.x >= 0 and pos.x < SCREEN_HEIGHT and pos.y >= 0 and
|
||||
pos.y < SCREEN_WIDTH;
|
||||
}
|
||||
|
||||
inline Veci get_cursor() { return {.x = get_cursor_y(), .y = get_cursor_x()}; }
|
||||
|
||||
inline constexpr Veci get_center() {
|
||||
inline constexpr Veci get_screen_center() {
|
||||
return {.x = SCREEN_HEIGHT / 2, .y = SCREEN_WIDTH / 2};
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
template <typename T> inline T to_inteval_from_zero_to(uint bound, T val) {
|
||||
assert(bound != 0);
|
||||
|
||||
bool was_negative = (val < 0);
|
||||
val = std::abs(val);
|
||||
val = val - int((int(val) / bound) * bound);
|
||||
return was_negative ? bound - val : val;
|
||||
}
|
||||
|
||||
template <typename T> inline Vec<T> to_world_coord(Vec<T> pos) {
|
||||
return {
|
||||
.x = to_inteval_from_zero_to(WORLD_SIZE.x, pos.x),
|
||||
.y = to_inteval_from_zero_to(WORLD_SIZE.y, pos.y),
|
||||
};
|
||||
}
|
||||
|
||||
} // namespace utils
|
||||
|
||||
namespace color {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue