From a8bcacc95045102e67f2feabbdddf79535837554 Mon Sep 17 00:00:00 2001 From: jacopograndi Date: Thu, 19 Aug 2021 18:46:51 +0200 Subject: forgot to make repo until now --- game/entity.cpp | 3 ++ game/entity.h | 43 ++++++++++++++++++ game/ground.cpp | 82 ++++++++++++++++++++++++++++++++++ game/ground.h | 22 ++++++++++ game/gst.cpp | 1 + game/gst.h | 34 +++++++++++++++ game/menu.cpp | 29 +++++++++++++ game/menu.h | 47 ++++++++++++++++++++ game/player.h | 12 +++++ game/playercontrol.cpp | 116 +++++++++++++++++++++++++++++++++++++++++++++++++ game/playercontrol.h | 87 +++++++++++++++++++++++++++++++++++++ game/tile.cpp | 1 + game/tile.h | 23 ++++++++++ game/view.cpp | 77 ++++++++++++++++++++++++++++++++ game/view.h | 32 ++++++++++++++ 15 files changed, 609 insertions(+) create mode 100644 game/entity.cpp create mode 100644 game/entity.h create mode 100644 game/ground.cpp create mode 100644 game/ground.h create mode 100644 game/gst.cpp create mode 100644 game/gst.h create mode 100644 game/menu.cpp create mode 100644 game/menu.h create mode 100644 game/player.h create mode 100644 game/playercontrol.cpp create mode 100644 game/playercontrol.h create mode 100644 game/tile.cpp create mode 100644 game/tile.h create mode 100644 game/view.cpp create mode 100644 game/view.h (limited to 'game') diff --git a/game/entity.cpp b/game/entity.cpp new file mode 100644 index 0000000..8362c40 --- /dev/null +++ b/game/entity.cpp @@ -0,0 +1,3 @@ +#include "entity.h" +#include +; \ No newline at end of file diff --git a/game/entity.h b/game/entity.h new file mode 100644 index 0000000..1fcf026 --- /dev/null +++ b/game/entity.h @@ -0,0 +1,43 @@ +#ifndef ENTITIES_H +#define ENTITIES_H + +#include +#include + +#include "../umath/vec2.h" + +class Ability { + public: + Ability(); +}; + +class EntityInfo { + public: + EntityInfo() { spritebounds = vec2 { 16*6, 16 }; } + + std::string name; + + float hp; + float attack; + float defence; + int range; + float sight; + int move; + std::vector abilities; + + vec2 spritebounds; +}; + +class Entity { + public: + Entity(int x, int y, EntityInfo &info, int owner) + : x(x), y(y), info(info), owner(owner) {} + + int x, y; + bool done = false; + EntityInfo &info; + + int owner; +}; + +#endif \ No newline at end of file diff --git a/game/ground.cpp b/game/ground.cpp new file mode 100644 index 0000000..c0ba2ca --- /dev/null +++ b/game/ground.cpp @@ -0,0 +1,82 @@ +#include "ground.h" +#include "gst.h" + +#include +#include + +Ground::Ground (int sx, int sy) { + sizex = sx; sizey = sy; + tiles = new int[sx*sy]; + for (int i=0; i Ground::star (int pos) { + std::vector fs; + int x = pos % sizex, y = pos / sizex; + if (x-1 >= 0) { fs.push_back(at(x-1, y)); } + if (x+1 < sizex) { fs.push_back(at(x+1, y)); } + if (y-1 >= 0) { fs.push_back(at(x, y-1)); } + if (y+1 < sizey) { fs.push_back(at(x, y+1)); } + return fs; +} + +class step { public: + step(int pos, int m) : pos(pos), m(m) {}; + bool operator==(step oth) { return pos==oth.pos && m==oth.m; } + bool operator==(int p) { return pos==p; } + int pos, m; +}; + +std::vector Ground::move_area (Gst &gst, Entity ent) { + std::vector moves; + std::vector visited { at(ent.x, ent.y) }; + std::vector frontier { step { at(ent.x, ent.y), ent.info.move } }; + + int iter=0; + for (; iter<10000; iter++) { + + if (frontier.size() == 0) break; + step maxf {-1, -1}; + for (step t : frontier) { + if (t.m > maxf.m) { + maxf.pos = t.pos; + maxf.m = t.m; + } + } + frontier.erase(std::remove(frontier.begin(), frontier.end(), maxf), + frontier.end()); + auto forward_star = star(maxf.pos); + for (int t : forward_star) { + if (!(std::find(visited.begin(), visited.end(), t) != visited.end()) + && !(std::find(frontier.begin(), frontier.end(), t) != frontier.end())) { + int walkedm = maxf.m - gst.tiles[gst.ground.tiles[t]].move_cost; + bool obstructed = false; + for (Entity &e : gst.entities) { + if (e.owner != ent.owner && at(e.x, e.y) == t) { + obstructed = true; + break; + } + } + if (walkedm >= 0 && !obstructed) { + frontier.emplace_back(t, walkedm); + moves.push_back(t); + } + } + } + visited.push_back(maxf.pos); + } + + std::cout << "iters: " << iter; + + return moves; +} \ No newline at end of file diff --git a/game/ground.h b/game/ground.h new file mode 100644 index 0000000..545deb9 --- /dev/null +++ b/game/ground.h @@ -0,0 +1,22 @@ +#ifndef GROUND_H +#define GROUND_H + +#include "entity.h" + +class Gst; + +class Ground { + public: + Ground (int sx, int sy); + ~Ground (); + + int *tiles; + + int sizex; + int sizey; + int at (int x, int y); + std::vector star (int pos); + std::vector move_area (Gst &gst, Entity ent); +}; + +#endif \ No newline at end of file diff --git a/game/gst.cpp b/game/gst.cpp new file mode 100644 index 0000000..5c195c8 --- /dev/null +++ b/game/gst.cpp @@ -0,0 +1 @@ +#include "gst.h" \ No newline at end of file diff --git a/game/gst.h b/game/gst.h new file mode 100644 index 0000000..340d596 --- /dev/null +++ b/game/gst.h @@ -0,0 +1,34 @@ +#ifndef GST_H +#define GST_H + +#include + +#include "ground.h" +#include "entity.h" +#include "tile.h" +#include "player.h" + +class Gst { + public: + Gst(int sx, int sy) : ground(sx, sy) {} + + std::vector infos; + std::vector tiles; + std::vector entities; + Ground ground; + + std::vector players; + + int turn { 0 }; + int day { 0 }; + + void end_day () { + day++; + if (day >= players.size()) { + day = 0; + turn++; + } + } +}; + +#endif \ No newline at end of file diff --git a/game/menu.cpp b/game/menu.cpp new file mode 100644 index 0000000..76913b5 --- /dev/null +++ b/game/menu.cpp @@ -0,0 +1,29 @@ +#include "menu.h" + +void Menu::close () { + active = false; +} + +void Menu::open (vec2 res) { + active = true; + pos = vec2 { (float)res.x, (float)res.y }; + float height = options.size() * 20; + size = vec2 { 120, height+10 }; + pos *= 0.5f; + pos -= size/2; +} + +int Menu::mouse_option (vec2 mouse) { + int i=0; + for (Option opt : options) { + vec2 off { 10, 10.0f + i*20 }; + vec2 sizeopt { 100, 20 }; + off += pos; + if (off.x < mouse.x && mouse.x < off.x+sizeopt.x + && off.y < mouse.y && mouse.y < off.y+sizeopt.y ) { + return opt.id; + } + i++; + } + return -1; +} \ No newline at end of file diff --git a/game/menu.h b/game/menu.h new file mode 100644 index 0000000..95be683 --- /dev/null +++ b/game/menu.h @@ -0,0 +1,47 @@ +#ifndef MENU_H +#define MENU_H + +#include +#include + +#include "../umath/vec2.h" + +class Option { + public: + Option(std::string name, int id) : name(name), id(id) {} + + std::string name; + int id; +}; + + +class Menu { + public: + bool active { false }; + std::vector