From d0254f6bb715369b80b4340747e0ce30a009dfe6 Mon Sep 17 00:00:00 2001 From: Joursoir Date: Tue, 9 Mar 2021 21:15:47 +0000 Subject: add class GameWorld for generating world --- GameWorld.cpp | 85 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ GameWorld.hpp | 17 ++++++++++++ 2 files changed, 102 insertions(+) create mode 100644 GameWorld.cpp create mode 100644 GameWorld.hpp 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 +#include + +#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 */ -- cgit v1.2.3-18-g5258