summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJoursoir <chat@joursoir.net>2021-03-09 21:15:47 +0000
committerJoursoir <chat@joursoir.net>2021-03-09 21:15:47 +0000
commitd0254f6bb715369b80b4340747e0ce30a009dfe6 (patch)
tree2ced13d48b3595c215713fdc353d9f10af9c5b5c
parentcd445cccc1ccd135d006f6fbb519aeda8f90f133 (diff)
downloadascii-road-d0254f6bb715369b80b4340747e0ce30a009dfe6.tar.gz
ascii-road-d0254f6bb715369b80b4340747e0ce30a009dfe6.tar.bz2
ascii-road-d0254f6bb715369b80b4340747e0ce30a009dfe6.zip
add class GameWorld for generating world
-rw-r--r--GameWorld.cpp85
-rw-r--r--GameWorld.hpp17
2 files changed, 102 insertions, 0 deletions
diff --git a/GameWorld.cpp b/GameWorld.cpp
new file mode 100644
index 0000000..c0f95c7
--- /dev/null
+++ b/GameWorld.cpp
@@ -0,0 +1,85 @@
+#include <ncurses.h>
+#include <stdlib.h>
+
+#include "GameWorld.hpp"
+#include "bg_models.hpp"
+#include "veh_models.hpp"
+
+GameWorld::GameWorld(int y, int x)
+ : w_y(y), w_x(x)
+{
+ int i, j; //, k, h;
+ world = new char*[y];
+
+ for(i = 0; i < y; i++) {
+ world[i] = new char[x+1];
+ for(j = 0; j < x; j++)
+ world[i][j] = ' ';
+ world[i][x] = '\0';
+ }
+
+ // generate road
+ move(y-1, 0);
+ for(i = 0; i < x; i++) {
+ world[y-1][i] = '_';
+ addch('_');
+ }
+
+ // generate bridge
+ for(i = 0; i < x; i += bg_store[bg_bridge].length)
+ Draw(&bg_store[bg_bridge], bg_store[bg_bridge].height-1, i, true);
+
+ // generate water
+
+ // generate trees
+ i = 0;
+ while(i < x) {
+ const struct object_info *tree = &bg_store[rand() % bg_tree_types_max];
+ Draw(tree, y-1, i, true);
+ i += tree->length + rand() % 10;
+ }
+}
+
+GameWorld::~GameWorld()
+{
+ int i;
+ for(i = 0; i < w_y; i++)
+ delete[] world[i];
+ delete[] world;
+}
+
+bool GameWorld::Draw(const struct object_info *obj, int y, int x,
+ bool replace)
+{
+ // draw from down left corner
+ bool ret = false;
+ int i, j, k, h;
+ int length = obj->length;
+ for(i = x, j = 0; (i < w_x) && (j < length); i++, j++) {
+ if(i < 0)
+ continue;
+ ret = true;
+
+ h = obj->height - 1;
+ for(k = 0; k <= h; k++) {
+ char ch = obj->model[k][j];
+ if(ch != SUPPORT_CHAR) {
+ if(replace)
+ world[y - (h - k)][i] = ch;
+ mvaddch(y - (h - k), i, ch);
+ }
+ else RedrawCh(y - (h - k), i);
+ }
+ }
+ return ret;
+}
+
+void GameWorld::RedrawCh(int y, int x)
+{
+ mvaddch(y, x, world[y][x]);
+}
+
+void GameWorld::Update()
+{
+ refresh();
+}
diff --git a/GameWorld.hpp b/GameWorld.hpp
new file mode 100644
index 0000000..c252bf2
--- /dev/null
+++ b/GameWorld.hpp
@@ -0,0 +1,17 @@
+#ifndef ASCIIROAD_GAMEWORLD_H
+#define ASCIIROAD_GAMEWORLD_H
+
+class GameWorld {
+ char **world;
+ int w_y, w_x;
+public:
+ GameWorld(int a_y, int a_x);
+ ~GameWorld();
+
+ bool Draw(const struct object_info *obj, int y, int x,
+ bool replace);
+ void RedrawCh(int y, int x);
+ void Update();
+};
+
+#endif /* ASCIIROAD_GAMEWORLD_H */