/*
88888888
888888888888
88888888888888
8888888888888888
888888888888888888
888888 8888 888888
88888 88 88888
888888 8888 888888
88888888888888888888
88888888888888888888
8888888888888888888888
8888888888888888888888888888
88888888888888888888888888888888
88888888888888888888
888888888888888888888888
888888 8888888888 888888
888 8888 8888 888
888 888
OCTOBANANA
Licensed under the MIT License
Copyright (c) 2019 Brett Robinson
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#ifndef GAME_WORLD_HH
#define GAME_WORLD_HH
#include "ob/parg.hh"
#include "ob/text.hh"
#include "ob/term.hh"
#include "ob/lispp.hh"
#include "ob/timer.hh"
#include "ob/color.hh"
#include "ob/readline.hh"
#include "ob/ordered_map.hh"
#include "ob/belle/io.hh"
#include "ob/belle/signal.hh"
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
namespace Nyble {
using Read = OB::Belle::IO::Read;
using Key = OB::Belle::IO::Read::Key;
using Mouse = OB::Belle::IO::Read::Mouse;
using Tick = std::chrono::nanoseconds;
using Clock = std::chrono::steady_clock;
using Readline = OB::Readline;
using Timer = OB::Belle::asio::steady_timer;
using error_code = OB::Belle::error_code;
using namespace std::chrono_literals;
using namespace std::string_literals;
namespace fs = std::filesystem;
namespace Text = OB::Text;
namespace Term = OB::Term;
namespace Belle = OB::Belle;
namespace iom = OB::Term::iomanip;
namespace aec = OB::Term::ANSI_Escape_Codes;
struct Pos {
// TODO use signed integer
std::size_t x {0};
std::size_t y {0};
Pos(std::size_t const x, std::size_t const y) : x {x}, y {y} {}
Pos() = default;
Pos(Pos&&) = default;
Pos(Pos const&) = default;
~Pos() = default;
Pos& operator=(Pos&&) = default;
Pos& operator=(Pos const&) = default;
};
struct Size {
std::size_t w {0};
std::size_t h {0};
Size(std::size_t const w, std::size_t const h) : w {w}, h {h} {}
Size() = default;
Size(Size&&) = default;
Size(Size const&) = default;
~Size() = default;
Size& operator=(Size&&) = default;
Size& operator=(Size const&) = default;
};
struct Color {
std::uint8_t r {0};
std::uint8_t g {0};
std::uint8_t b {0};
Color(std::uint8_t const r, std::uint8_t const g, std::uint8_t const b) : r {r}, g {g}, b {b} {}
Color() = default;
Color(Color&&) = default;
Color(Color const&) = default;
~Color() = default;
Color& operator=(Color&&) = default;
Color& operator=(Color const&) = default;
};
Color hex_to_rgb(std::string const& str);
struct Style {
friend std::ostream& operator<<(std::ostream& os, Style const& obj);
enum Type : std::uint8_t {
Bit_2 = 0,
Bit_4,
Bit_8,
Bit_24,
};
enum Attr : std::uint8_t {
Null = 0,
Bold = 1 << 0,
Reverse = 1 << 1,
};
std::uint8_t type {Bit_24};
std::uint8_t attr {Null};
Color fg;
Color bg;
};
class Scene;
struct Cell {
Scene* scene;
int zidx;
Style style;
std::string text;
};
class Buffer {
public:
Buffer() = default;
Buffer(Buffer&&) = default;
Buffer(Buffer const&) = default;
~Buffer() = default;
Buffer& operator=(Buffer&&) = default;
Buffer& operator=(Buffer const&) = default;
void operator()(Cell const& cell);
Cell& at(Pos const& pos);
std::vector& row(std::size_t y);
Cell& col(Pos const& pos);
Pos cursor();
void cursor(Pos const& pos);
Size size();
void size(Size const& size);
bool empty();
void clear();
private:
Pos _pos;
Size _size;
std::vector> _value;
}; // class Buffer
class Engine;
using Ctx = Engine*;
class Scene {
public:
Scene(Ctx ctx) : _ctx {ctx} {}
Scene(Scene&&) = default;
Scene(Scene const&) = default;
virtual ~Scene() = default;
Scene& operator=(Scene&&) = default;
Scene& operator=(Scene const&) = default;
virtual void on_winch(Size const& size) = 0;
virtual bool on_input(Read::Ctx const& ctx) = 0;
virtual bool on_update(Tick const delta) = 0;
virtual bool on_render(Buffer& buf) = 0;
Ctx _ctx;
Pos _pos;
Size _size;
}; // class Scene
using Scenes = OB::Ordered_map>;
class Background : public Scene {
public:
Background(Ctx ctx);
Background(Background&&) = default;
Background(Background const&) = default;
~Background();
Background& operator=(Background&&) = default;
Background& operator=(Background const&) = default;
void on_winch(Size const& size);
bool on_input(Read::Ctx const& ctx);
bool on_update(Tick const delta);
bool on_render(Buffer& buf);
// private:
Cell _cell {this, 0, Style{Style::Bit_24, Style::Null, Color{}, hex_to_rgb("031323")}, " "};
}; // class Background
class Board : public Scene {
public:
Board(Ctx ctx);
Board(Board&&) = default;
Board(Board const&) = default;
~Board();
Board& operator=(Board&&) = default;
Board& operator=(Board const&) = default;
void on_winch(Size const& size);
bool on_input(Read::Ctx const& ctx);
bool on_update(Tick const delta);
bool on_render(Buffer& buf);
// private:
Style _style {Style::Bit_24, Style::Null, hex_to_rgb("0b253d"), hex_to_rgb("031323")};
Style _block1 {Style::Bit_24, Style::Null, Color(), hex_to_rgb("031323")};
Style _block2 {Style::Bit_24, Style::Null, Color(), hex_to_rgb("0b253d")};
bool _init {true};
}; // class Board
class Snake : public Scene {
public:
Snake(Ctx ctx);
Snake(Snake&&) = default;
Snake(Snake const&) = default;
~Snake();
Snake& operator=(Snake&&) = default;
Snake& operator=(Snake const&) = default;
void on_winch(Size const& size);
bool on_input(Read::Ctx const& ctx);
bool on_update(Tick const delta);
bool on_render(Buffer& buf);
// private:
struct {
std::size_t idx {0};
std::vector |