added a few more pieces to the framework, overhauled main.cc

This commit is contained in:
2024-07-03 11:08:55 -04:00
parent 97ff14cd26
commit 6cbeb42144
17 changed files with 416 additions and 61 deletions

View File

@ -154,7 +154,7 @@ position_list remove_from_list(const position_list &sorted_positions,
// IMPORTANT: remember to check if player is on the stairs // IMPORTANT: remember to check if player is on the stairs
result character::move(const direction dir, result character::move(const direction dir,
const position_list &available_positions) { const position_list &available_positions) {
if (find(available_positions, pos + MOVE[dir]) if (find(available_positions, pos + MOVE[dir])
!= available_positions.size()) { != available_positions.size()) {
pos += MOVE[dir]; pos += MOVE[dir];
@ -165,12 +165,12 @@ result character::move(const direction dir,
} }
result character::move_or_attack(const direction dir, result character::move_or_attack(const direction dir,
const position_list &available_positions, const position_list &available_positions,
const character_list &chlist) { const character_list &chlist) {
auto res = this->move(dir,available_positions); auto res = this->move(dir, available_positions);
if(res != result::fine) if (res != result::fine)
return res; return res;
return this->attack(dir,chlist); return this->attack(dir, chlist);
} }

176
src/characters.cc.orig Normal file
View File

@ -0,0 +1,176 @@
#include "characters.h"
#include <algorithm>
character::character(const enum race &nrace):
race{nrace}, HP{STARTING_HP[race]},
ATK{STARTING_ATK[race]}, DEF{STARTING_DEF[race]} {}
character::~character() {}
enum race character::get_race() const {
return race;
}
position character::get_position() const {
return pos;
}
int character::get_HP() const {
return HP;
}
int character::get_ATK() const {
return ATK;
}
int character::get_DEF() const {
return DEF;
}
int character::get_gold() const {
return gold;
}
float character::get_hitrate() const {
return base_hitrate;
}
bool character::is_hostile() const {
return hostile;
}
void character::set_position(const position &npos) {
pos = npos;
}
void character::set_HP(const int nHP) {
HP = nHP;
}
void character::set_ATK(const int nATK) {
ATK = nATK;
}
void character::set_DEF(const int nDEF) {
DEF = nDEF;
}
void character::set_gold(const int ngold) {
gold = ngold;
}
void character::set_hitrate(const float nhitrate) {
base_hitrate = nhitrate;
}
void character::set_hostile(const bool is_hostile) {
hostile = is_hostile;
}
void character::apply_buff(const stat_name statn, const int amount) {
// TODO: add checks for bounds
switch (statn) {
case stat_name::HP:
HP += amount;
break;
case stat_name::ATK:
ATK += amount;
break;
case stat_name::DEF:
DEF += amount;
break;
case stat_name::hostile: {
if (amount > 0)
hostile = true;
else
hostile = false;
break;
}
}
}
character_list::character_list():
layer{layer_num::characters} {}
void character_list::print() const {
// TODO: implement it using ncurses
}
void character_list::print(display &display) const {
for (auto &ch : characters)
display.print_position(ch->get_position(),
CHARACTER_REP[ch->get_race()]);
}
std::vector<std::unique_ptr<character>>::const_iterator character_list::begin()
const {
return characters.begin();
}
std::vector<std::unique_ptr<character>>::const_iterator character_list::end()
const {
return characters.end();
}
direction_list character::moveable(const position_list &available_positions)
const {
direction_list result;
for (int i = 0; i < DIRECTION_CNT; ++i)
if (find(available_positions, pos + MOVE[i])
!= available_positions.size())
result.push_back((direction)i);
return result;
}
result character::apply(direction &dir, const potion_list &potions) {
// TODO: implement this after implementing potions
return result::fine;
}
position_list remove_from_list(const position_list &sorted_positions,
position_list excluded) {
std::sort(excluded.begin(), excluded.end());
position_list result{sorted_positions.size() - excluded.size()};
auto exc = excluded.begin();
for (auto src : sorted_positions) {
if (exc != excluded.end() && src == *exc)
++exc;
else
result.push_back(src);
}
return result;
}
// IMPORTANT: remember to check if player is on the stairs
result character::move(const direction dir,
const position_list &available_positions) {
if (find(available_positions, pos + MOVE[dir])
!= available_positions.size()) {
pos += MOVE[dir];
return result::moved;
}
return result::fine;
}
result character::move_or_attack(const direction dir,
const position_list &available_positions,
const character_list &chlist) {
auto res = this->move(dir,available_positions);
if(res != result::fine)
return res;
return this->attack(dir,chlist);
}

View File

@ -14,6 +14,8 @@ enum error {none};
// TODO: update result to include subject // TODO: update result to include subject
enum result {fine, died, go_down, hit, moved}; enum result {fine, died, go_down, hit, moved};
enum game_status {terminated, main_menu, in_game, options};
enum stat_name {HP, ATK, DEF, hostile}; enum stat_name {HP, ATK, DEF, hostile};
const int LAYER_CNT = 4; // TODO: update as you go const int LAYER_CNT = 4; // TODO: update as you go
@ -60,6 +62,8 @@ const feature FEATURE_ENEMIES_CHASE = 1 << 2;
const feature FEATURE_INVENTORY = 1 << 3; const feature FEATURE_INVENTORY = 1 << 3;
const feature FEATURE_THROW = 1 << 4; const feature FEATURE_THROW = 1 << 4;
const feature FEATURE_REVISIT = 1 << 5; const feature FEATURE_REVISIT = 1 << 5;
const feature FEATURE_LOG = 1 << 6;
const feature FEATURE_SAVE = 1 << 7;
typedef std::vector<position> position_list; typedef std::vector<position> position_list;
typedef std::vector<direction> direction_list; typedef std::vector<direction> direction_list;

71
src/constants.h.orig Normal file
View File

@ -0,0 +1,71 @@
// TODO: Consider moving the contents of this header to their relevant
// headers
#ifndef __CONSTANTS_H__
#define __CONSTANTS_H__
#include <vector>
#include "position.h"
// IMPORTANT: added END to the end of all valued enums so that you can
// iterate over them
enum error {none};
// TODO: update result to include subject
enum result {fine, died, go_down, hit, moved};
enum game_status{terminated, main_menu, in_game, options};
enum stat_name {HP, ATK, DEF, hostile};
const int LAYER_CNT = 4; // TODO: update as you go
enum layer_num {map = 0, objects, characters, shop};
const int RACE_CNT = 2; // TODO: update as you go
enum race {unknown = 0, rshade /* TODO: fill out the other races (including enemies) */};
// TODO: fill out the other races (including enemies)
const int MAX_HP[RACE_CNT] = {0, 125};
const int STARTING_HP[RACE_CNT] = {0, 125};
const int STARTING_ATK[RACE_CNT] = {0, 25};
const int STARTING_DEF[RACE_CNT] = {0, 25};
const char CHARACTER_REP[RACE_CNT] = {'@', 'S'};
const int DIRECTION_CNT = 8;
// IMPORTANT: east is positive for x and SOUTH is positive for y
// initializes all directions to an int
enum direction {east = 0, west, north, south, northeast,
northwest, southeast, southest
};
const position MOVE[DIRECTION_CNT] = {
{1, 0}, {-1, 0}, {0, -1}, {0, 1},
{1, -1}, {-1, -1}, {1, 1}, {-1, 1}
};
const int MAP_HEIGHT = 25;
const int MAP_WIDTH = 79;
const int DISPLAY_HEIGHT = 30;
const int DISPLAY_WIDTH = 79;
// TODO: list all extra features
// using constants to keep track of features
// method: features: FEATURE_XXX | FEATURE_YYY
// That is: use bit manipulation
// check if feature is enabled:
// features & FEATURE_XXX
typedef unsigned int feature; // can extend to unsigned long (long) if needed
const feature FEATURE_NCURSES = 1 << 0;
const feature FEATURE_RAND_MAP = 1 << 1;
const feature FEATURE_ENEMIES_CHASE = 1 << 2;
const feature FEATURE_INVENTORY = 1 << 3;
const feature FEATURE_THROW = 1 << 4;
const feature FEATURE_REVISIT = 1 << 5;
const feature FEATURE_LOG = 1 << 6;
const feature FEATURE_SAVE = 1 << 7;
typedef std::vector<position> position_list;
typedef std::vector<direction> direction_list;
#endif

7
src/cursor.h Normal file
View File

@ -0,0 +1,7 @@
#ifndef __CURSOR_H__
#define __CURSOR_H__
#include <ncurses.h>
class cursor;
#endif

View File

@ -11,18 +11,23 @@
#include <vector> #include <vector>
#include "position.h" #include "position.h"
#include "constants.h" #include "constants.h"
#include "cursor.h"
class display final { class display final {
private: private:
std::vector<std::string> contents; std::vector<std::string> contents;
cursor &curs;
public: public:
display(); display();
display(cursor &s);
void clear(); void clear();
// use this instead of overloading for clarity // use this instead of overloading for clarity
void print(std::ostream &out) const; void print(std::ostream &out) const;
void print_line(const std::string &line, const int linenum); void print_line(const std::string &line, const int linenum);
void render() const ;
// will override any character that was there // will override any character that was there
void print_position(const position &pos, const char ch); void print_position(const position &pos, const char ch);
}; };

35
src/display.h.orig Normal file
View File

@ -0,0 +1,35 @@
/*
* CS 246 Final Project
* File: display.h
* Purpose: handles map functionality
*/
#ifndef __DISPLAY_H__
#define __DISPLAY_H__
#include <iostream>
#include <string>
#include <vector>
#include "position.h"
#include "constants.h"
#include "cursor.h"
class display final {
private:
std::vector<std::string> contents;
cursor &curs;
public:
display();
display(cursor &s);
void clear();
// use this instead of overloading for clarity
void print(std::ostream &out) const;
void print_line(const std::string &line, const int linenum);
void render() const ;
// will override any character that was there
void print_position(const position &pos, const char ch);
};
#endif

View File

@ -3,21 +3,22 @@
#include <memory> #include <memory>
#include <vector> #include <vector>
#include "constants.h" #include "constants.h"
#include "display.h"
#include "rng.h" #include "rng.h"
#include "spell.h" // this is for ncurses
#include "characters.h" #include "characters.h"
#include "map.h" #include "map.h"
#include "log.h"
class game final { class game final {
private: private:
display &d;
feature features; feature features;
std::unique_ptr<RNG> rng; std::unique_ptr<RNG> rng;
std::unique_ptr<spell> spell;
std::unique_ptr<character> player; std::unique_ptr<character> player;
std::vector<game_map> levels; std::vector<level> levels;
public: public:
game(int argc = 0, char **argv = nullptr); game(display &new_display, int argc, char **argv);
void run(); game_status run();
}; };
#endif #endif

24
src/game.h.orig Normal file
View File

@ -0,0 +1,24 @@
#ifndef __GAME_H__
#define __GAME_H__
#include <memory>
#include <vector>
#include "constants.h"
#include "display.h"
#include "rng.h"
#include "characters.h"
#include "map.h"
#include "log.h"
class game final {
private:
display &d;
feature features;
std::unique_ptr<RNG> rng;
std::unique_ptr<character> player;
std::vector<level> levels;
public:
game(display &new_display, int argc, char **argv);
game_status run();
};
#endif

6
src/level.h Normal file
View File

@ -0,0 +1,6 @@
#ifndef __LEVEL_H__
#define __LEVEL_H__
class level;
#endif

6
src/log.h Normal file
View File

@ -0,0 +1,6 @@
#ifndef __LOG_H__
#define __LOG_H__
class log;
#endif

View File

@ -1,52 +1,23 @@
#include <iostream> #include "game.h"
#include <cstdlib> #include "cursor.h"
#include <memory>
#include <string>
#include <stdio.h>
#include <time.h>
#include <ncurses.h>
#include "map.h"
#include "races.h"
#include "display.h" #include "display.h"
#include "rng.h"
using namespace std; // The way things are designed to work:
// 1. initializes a new ncurses wrapper c
// Note: This is done the first thing in main because ncurses
// are from procedural programming and may break otherwise (unlikely)
// 2. bind c to a new display c (that handles all output)
// 3. bind d to g for graphics
RNG rng;//{(unsigned int)time(0)}; int main(int argc, char **argv) {
cursor c;
// binds a ncurses cursor to display
display d(c);
// binds display to the game
game g(d, argc, argv);
int main() { while (g.run() != game_status::terminated)
display display; d.render();
unique_ptr<game_map> mmap(new game_map{default_map, 0});
unique_ptr<class shade>
player(new shade{mmap->get_available_positions()});
for (char ch = ' '; ch != 'q'; ch = getchar()) { return 0;
//system("CLS");
cout << "\033[2J";
display.clear();
mmap->print(display);
display.print_position(player->get_position(), '@');
display.print(cout);
cout << "Command: " << ch << endl;
auto available_positions = mmap->get_available_positions();
switch (ch) {
case 'w':
player->move(direction::north, available_positions);
break;
case 'a':
player->move(direction::west, available_positions);
break;
case 's':
player->move(direction::south, available_positions);
break;
case 'd':
player->move(direction::east, available_positions);
break;
}
}
} }

21
src/main.cc.orig Normal file
View File

@ -0,0 +1,21 @@
#include "game.h"
#include "cursor.h"
#include "display.h"
// The way things are designed to work:
// 1. initializes a new ncurses wrapper c
// Note: This is done the first thing in main because ncurses
// are from procedural programming and may break otherwise (unlikely)
// 2. bind c to a new display c (that handles all output)
// 3. bind d to g for graphics
int main(int argc, char **argv) {
cursor c;
// binds a ncurses cursor to display
display d(c);
// binds display to the game
game g(d,argc, argv);
while(g.run()!=game_status::terminated)
d.render();
return 0;
}

View File

@ -23,7 +23,7 @@ typedef struct position {
bool operator<(const position &other) const; bool operator<(const position &other) const;
} position; } position;
size_t find(const std::vector<position> &sorted_list, std::size_t find(const std::vector<position> &sorted_list,
const position &target); const position &target);
#endif #endif

29
src/position.h.orig Normal file
View File

@ -0,0 +1,29 @@
/*
* CS 246 Final Project
* File: map.h
* Purpose: handles map functionality
*/
#ifndef __POSITION_H__
#define __POSITION_H__
#include <vector>
typedef struct position {
int x;
int y;
position();
position(int nx, int ny);
position operator+(const position &other) const;
position &operator=(const position &other);
position &operator+=(const position &other);
bool operator==(const position &other) const;
bool operator!=(const position &other) const;
bool operator<(const position &other) const;
} position;
std::size_t find(const std::vector<position> &sorted_list,
const position &target);
#endif

View File

@ -1 +0,0 @@
test.o: test.cc

Binary file not shown.