mirror of
https://github.com/ProgramSnail/snake_2024.git
synced 2025-12-14 18:38:43 +00:00
initial game
This commit is contained in:
parent
4570d6e593
commit
adabb50a9e
10 changed files with 200 additions and 132 deletions
|
|
@ -1,12 +1,9 @@
|
|||
#pragma once
|
||||
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
#include <vector>
|
||||
#
|
||||
|
||||
#include "Engine.h"
|
||||
#include "Utils.hpp"
|
||||
#include "Vec.hpp"
|
||||
|
||||
enum class Color : uint32_t {
|
||||
BLACK = 0x000000ff,
|
||||
|
|
@ -25,99 +22,27 @@ constexpr Color CL_BG = Color::BLACK;
|
|||
|
||||
using Screen = uint32_t[SCREEN_HEIGHT][SCREEN_WIDTH];
|
||||
|
||||
class CanvasObject {
|
||||
public:
|
||||
struct Config {
|
||||
Veci pos;
|
||||
int size;
|
||||
Color color = Color ::WHITE;
|
||||
};
|
||||
CanvasObject(Config config)
|
||||
: pos_(config.pos), size_(config.size), color_(config.color),
|
||||
shown_(false) {}
|
||||
namespace canvas {
|
||||
|
||||
virtual void move_to(Veci) = 0;
|
||||
virtual void show() = 0;
|
||||
virtual void hide() = 0;
|
||||
|
||||
virtual ~CanvasObject() {
|
||||
if (shown_) {
|
||||
// hide(); // TODO
|
||||
}
|
||||
}
|
||||
|
||||
protected:
|
||||
Veci pos_;
|
||||
int size_;
|
||||
Color color_;
|
||||
bool shown_;
|
||||
struct Object {
|
||||
Veci pos;
|
||||
Color color;
|
||||
};
|
||||
|
||||
class StatelessCanvasObject : public CanvasObject {
|
||||
public:
|
||||
using CanvasObject::CanvasObject;
|
||||
|
||||
void move_to(Veci pos) override {
|
||||
if (not shown_) {
|
||||
return;
|
||||
}
|
||||
hide();
|
||||
pos_ = pos;
|
||||
show();
|
||||
}
|
||||
|
||||
void show() override { fill(color_); }
|
||||
|
||||
void hide() override { fill(CL_BG); }
|
||||
|
||||
protected:
|
||||
virtual void fill(Color color) = 0;
|
||||
struct Square : public Object {
|
||||
int side;
|
||||
};
|
||||
|
||||
class Square : public StatelessCanvasObject {
|
||||
public:
|
||||
using StatelessCanvasObject::StatelessCanvasObject;
|
||||
|
||||
protected:
|
||||
void fill(Color color) override {
|
||||
for (int x = pos_.x; x < pos_.x + size_; ++x) {
|
||||
std::fill(screen_at(x, pos_.y), screen_at(x, pos_.y + size_),
|
||||
static_cast<uint32_t>(color));
|
||||
}
|
||||
}
|
||||
struct Circle : public Object {
|
||||
int radius;
|
||||
};
|
||||
|
||||
class Circle : public StatelessCanvasObject {
|
||||
public:
|
||||
using StatelessCanvasObject::StatelessCanvasObject;
|
||||
} // namespace canvas
|
||||
|
||||
protected:
|
||||
void fill(Color color) override {
|
||||
for (int x = -size_; x < size_; ++x) {
|
||||
int size_y = std::sqrt(size_ * size_ - std::abs(x) * std::abs(x));
|
||||
std::fill(screen_at(pos_.x + x, pos_.y - size_y),
|
||||
screen_at(pos_.x + x, pos_.y + size_y),
|
||||
static_cast<uint32_t>(color));
|
||||
}
|
||||
}
|
||||
};
|
||||
namespace paint {
|
||||
|
||||
class Worm : public CanvasObject {
|
||||
Worm(CanvasObject::Config config, size_t max_track_size)
|
||||
: CanvasObject(config), max_track_size_(max_track_size) {}
|
||||
void square(const canvas::Square &s);
|
||||
|
||||
protected:
|
||||
const size_t max_track_size_;
|
||||
std::vector<Circle> track_;
|
||||
};
|
||||
void circle(const canvas::Circle &c);
|
||||
|
||||
// class Canvas {
|
||||
// public:
|
||||
// Canvas(Screen &screen) : screen_(screen) {}
|
||||
|
||||
// size_t insert(CanvasObject obj) {}
|
||||
// void erase(size_t id) {}
|
||||
|
||||
// private:
|
||||
// Screen &screen_;
|
||||
// };
|
||||
} // namespace paint
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue