Skip to content

Commit

Permalink
BoardView completed without vision system
Browse files Browse the repository at this point in the history
  • Loading branch information
AppOfficer committed Dec 8, 2024
1 parent 89bf4e0 commit 85ac27b
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 11 deletions.
6 changes: 2 additions & 4 deletions drafts/GameEngine.draft.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* @file GameEngine.h
*
* LocalGen Module: GameEngine (Draft)
* LocalGen Module: GameEngine (Overall Draft)
*
* The GameEngine module is the core of the LocalGen game framework. It
* declares and defines the core functions and data structures required
Expand All @@ -11,14 +11,12 @@
#ifndef LGEN_MODULE_GAMEENGINE_DRAFT
#define LGEN_MODULE_GAMEENGINE_DRAFT 1

#include <cstdint>

#include "coord.h" // lib coord

#include "player.h" // players

#include "tile.h" // tiles

#include "board.h"
#include "board.h" // boards

#endif // LGEN_MODULE_GAMEENGINE_DRAFT
52 changes: 48 additions & 4 deletions drafts/board.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,67 @@
#include "player.h"
#include "tile.h"

using BoardView = std::vector<std::vector<Tile>>;

/// Game map board. Zoomable.
/// The %Board is a complex thing. Many systems are part of it (e.g. vision system).
class Board {
private:
pos_t row, col;
std::vector<std::vector<Tile>> tiles;

public:
Board() :
row(0), col(0) {}
Board(pos_t _row, pos_t _col) :
row(_row), col(_col) {
// board index starts at 1.
// give 1 space at borders for safety and convenience issues.
tiles.resize(row + 2, std::vector<Tile>(col + 2));
tiles.resize(_row + 2, std::vector<Tile>(_col + 2));
}

public:
// Here should lie the vision system. Someone come and write it for me!

/// Check whether the %Tile at (x,y) is visible to a %Player.
bool visible(pos_t x, pos_t y, Player* player) const {
// TODO))
}
/// Same as above, but using %Coord.
bool visible(Coord coo, Player* player) { return visible(SEPA(coo), player); }

public:
/// A view for a %Board, accessible to a player.
/// Declared as class for it may access direct information of the %Board.
class BoardView {
public:
/// Here these are declared as public as this is only a view and contains no content associated to the original %Board directly.
pos_t row, col;
std::vector<std::vector<TileView>> tiles;

public:
BoardView() :
row(0), col(0) {}
/// Constructor for generating a %BoardView using a %Board and a %Player.
/// Leaving this function public is safe, for a %Player cannot get another %Player's address.
BoardView(const Board& board, Player* player) :
row(board.row), col(board.col) {
tiles.resize(board.row + 2, std::vector<TileView>(board.col + 2));
for(pos_t i = 1; i <= row; ++i) {
for(pos_t j = 1; j <= col; ++j) {
tiles[i][j] = TileView(board.tiles[i][j], board.visible(i, j, player));
}
}
}
};

public:
/// Give a player a view of the %Board.
/// Returns a %BoardView.
/// Leaving this function public is safe, for a %Player cannot get another %Player's address.
BoardView view(Player* player) const {
// TODO
// Simply use the constructor to generate.
return BoardView(*this, player);
}
};

/// Declare Alias for convenience.
using BoardView = Board::BoardView;
2 changes: 1 addition & 1 deletion drafts/coord.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ struct Coord {
// Sometimes we need to pass a %Coord through two parameters or something.
// This macro is used to make it available.
// windows.h `COORD` have uppercase members X and Y, so it's not compatible.
#define SEPARATE(coo) (coo).x, (coo).y
#define SEPA(coo) (coo).x, (coo).y

/// Comparison operators for %Coord.
/// `operator==` and (before C++2a) `operator!=` are defined as usual.
Expand Down
2 changes: 1 addition & 1 deletion drafts/player.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class Player {
Player(const std::string& name) :
name(name) {}
virtual ~Player() {}
virtual Move step(const BoardView& view) {}
// virtual Move step(const BoardView& view) {}
};

#endif // LGEN_MODULE_GE_PLAYER
40 changes: 39 additions & 1 deletion drafts/tile.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ enum tile_type_e {
TILE_OBSTACLE = -1,
};

// Some tiles have some properties. Some tiles have aliases. Make them macros.
// Some tiles types have same properties. Some tiles have aliases. Make them macros.
#define TILE_GENERAL TILE_SPAWN
#define TILE_PLAIN TILE_BLANK
#define TILE_NEUTRAL TILE_BLANK
Expand All @@ -49,4 +49,42 @@ struct Tile {
bool light;
};

/// View of a %Tile.
/// Why do we need this? In game, players need more information of a tile than just the tile itself.
struct TileView {
/// For a player in game, whether a tile is visible or not is very important.
bool visible;
Player* occupier;
tile_type_e type;
army_t army;
/// Light has no importance in game, and will not be displayed.
// bool light;

TileView() {}
TileView(const Tile& tile, bool vis) :
visible(vis) {
if(vis) {
occupier = tile.occupier;
type = tile.type;
army = tile.army;
} else {
occupier = nullptr;
switch(tile.type) {
case TILE_SPAWN:
type = TILE_BLANK;
break;
case TILE_MOUNTAIN:
case TILE_CITY:
case TILE_LOOKOUT:
case TILE_OBSERVATORY:
type = TILE_OBSTACLE;
break;
default:
type = tile.type;
}
army = 0;
}
}
};

#endif // LGEN_MODULE_GE_TILE

0 comments on commit 85ac27b

Please sign in to comment.