Добавлены локации и перемещение по ним

This commit is contained in:
DeLiss
2026-06-11 11:37:19 +05:00
parent 1e7bac87d3
commit cba2274630
15 changed files with 506 additions and 57 deletions

View File

@@ -1,16 +1,40 @@
#include "../include/game.h"
#include "../include/player.h"
#include <iostream>
#include <string>
void Game::run() {
loadLocations();
showMainMenu();
while (isRunning) {
render();
processCommand();
}
}
void Game::loadLocations() {
locations.clear();
for (int i = 1; i <= 5; i++) {
Location location;
std::string path = "data/locations/location_" + std::to_string(i) + ".txt";
if (location.loadFromFile(path)) {
locations.push_back(location);
}
}
}
Location* Game::getCurrentLocation() {
for (auto& location : locations) {
if (location.id == player.currentLocation) {
return &location;
}
}
return nullptr;
}
void Game::showMainMenu() {
while (true) {
std::cout << "=== ASCII-G - The Cookies Edition ===\n";
@@ -22,47 +46,61 @@ void Game::showMainMenu() {
std::string str;
std::getline(std::cin, str);
if (str.length() != 1) {
std::cout << "It seems you've tried to input more than 1 digit... Don't do this please.\n";
continue;
}
char select = str[0];
if (select == '1') {
if (str == "1") {
std::string name;
std::cout << ">>> Enter your character name: ";
std::getline(std::cin, name);
if (name == "") {
std::cout << ">>> Empty name? Damn... let's call you Zero!\n";
if (name.empty()) {
player = Player("Zero");
}
else {
player = Player(name);
}
std::cout << ">>> Created new character " << player.name << "!";
Location* location = getCurrentLocation();
if (location != nullptr) {
player.position = location->startPosition;
}
std::cout << ">>> Created new character " << player.name << "!\n";
break;
}
else if (select == '2') {
std::cout << ">>> I can't do this... yet.";
else if (str == "2") {
std::cout << ">>> Load system is not ready yet.\n";
}
else if (select == '3') {
else if (str == "3") {
isRunning = false;
break;
}
else {
std::cout << ">>> I can't do this... yet? Let's try again!";
std::cout << ">>> Unknown menu option.\n";
}
}
}
void Game::render() {
std::cout << "\n\n=== ASCII-G - The Cookies Edition ===\n";
std::cout << "Commands: stats, exit\n";
Location* location = getCurrentLocation();
if (location == nullptr) {
std::cout << "Current location not found.\n";
isRunning = false;
return;
}
location->draw(player.position);
std::cout << "\nCommands:\n";
std::cout << "WASD - move\n";
std::cout << "stats - show stats\n";
std::cout << "exit - exit game\n";
}
void Game::processCommand() {
std::string command;
std::cout << "> ";
std::cin >> command;
@@ -72,7 +110,117 @@ void Game::processCommand() {
else if (command == "stats") {
player.showStats();
}
else if (command == "w" || command == "W") {
movePlayer(0, -1);
}
else if (command == "s" || command == "S") {
movePlayer(0, 1);
}
else if (command == "a" || command == "A") {
movePlayer(-1, 0);
}
else if (command == "d" || command == "D") {
movePlayer(1, 0);
}
else {
std::cout << "Unknown command.\n";
}
}
void Game::movePlayer(int dx, int dy) {
Location* location = getCurrentLocation();
if (location == nullptr) {
return;
}
int newX = player.position.x + dx;
int newY = player.position.y + dy;
if (!location->isWalkable(newX, newY)) {
std::cout << "You hit the wall.\n";
return;
}
char tile = location->getTile(newX, newY);
player.position.x = newX;
player.position.y = newY;
handleTile(tile);
}
void Game::handleTile(char tile) {
Location* location = getCurrentLocation();
if (location == nullptr) {
return;
}
if (tile == 'C') {
std::cout << "You found a chest. For now it gives you 25 gold.\n";
player.gold += 25;
}
else if (tile == 'E') {
std::cout << "Enemy encountered. Combat will be added later.\n";
}
else if (tile == 'N') {
std::cout << "NPC: Hello, traveler.\n";
}
else if (tile == 'D') {
if (!location->riddle.isSolved) {
bool solved = solveRiddle(*location);
if (!solved) {
player.hp -= 10;
std::cout << "Wrong answer. You lost 10 HP.\n";
return;
}
}
if (location->south != 0) {
player.currentLocation = location->south;
}
else if (location->east != 0) {
player.currentLocation = location->east;
}
else if (location->north != 0) {
player.currentLocation = location->north;
}
else if (location->west != 0) {
player.currentLocation = location->west;
}
else {
std::cout << "There is no exit from this door.\n";
return;
}
Location* newLocation = getCurrentLocation();
if (newLocation != nullptr) {
player.position = newLocation->startPosition;
std::cout << "You entered: " << newLocation->title << "\n";
}
}
}
bool Game::solveRiddle(Location& location) {
if (location.riddle.text.empty()) {
return true;
}
std::cout << "\nRiddle:\n";
std::cout << location.riddle.text << "\n";
std::cout << "Answer: ";
std::string answer;
std::cin >> answer;
if (answer == location.riddle.answer) {
std::cout << "Correct.\n";
location.riddle.isSolved = true;
return true;
}
return false;
}

127
src/location.cpp Normal file
View File

@@ -0,0 +1,127 @@
#include "../include/location.h"
#include <fstream>
#include <iostream>
#include <sstream>
bool Location::loadFromFile(const std::string& path) {
std::ifstream file(path);
if (!file.is_open()) {
std::cout << "Cannot open location file: " << path << "\n";
return false;
}
std::string line;
bool readingMap = false;
while (std::getline(file, line)) {
if (line.empty()) {
continue;
}
if (line == "MAP_BEGIN") {
readingMap = true;
continue;
}
if (line == "MAP_END") {
readingMap = false;
continue;
}
if (readingMap) {
map.push_back(line);
continue;
}
size_t pos = line.find('=');
if (pos == std::string::npos) {
continue;
}
std::string key = line.substr(0, pos);
std::string value = line.substr(pos + 1);
if (key == "ID") id = std::stoi(value);
else if (key == "TITLE") title = value;
else if (key == "WIDTH") width = std::stoi(value);
else if (key == "HEIGHT") height = std::stoi(value);
else if (key == "START_X") startPosition.x = std::stoi(value);
else if (key == "START_Y") startPosition.y = std::stoi(value);
else if (key == "NORTH") north = std::stoi(value);
else if (key == "SOUTH") south = std::stoi(value);
else if (key == "WEST") west = std::stoi(value);
else if (key == "EAST") east = std::stoi(value);
else if (key == "RIDDLE_TYPE") riddle.type = std::stoi(value);
else if (key == "RIDDLE_TEXT") riddle.text = value;
else if (key == "RIDDLE_ANSWER") riddle.answer = value;
}
for (int y = 0; y < map.size(); y++) {
for (int x = 0; x < map[y].size(); x++) {
char tile = map[y][x];
if (tile == 'C') {
Chest chest;
chest.pos = { x, y };
chest.gold = 25;
chests.push_back(chest);
}
else if (tile == 'E') {
Enemy enemy;
enemy.pos = { x, y };
enemy.type = "Goblin";
enemy.hp = 50;
enemies.push_back(enemy);
}
else if (tile == 'N') {
NPC npc;
npc.pos = { x, y };
npc.name = "Stranger";
npcs.push_back(npc);
}
}
}
return true;
}
void Location::draw(const Point& playerPosition) const {
std::cout << "\n=== " << title << " ===\n";
for (int y = 0; y < map.size(); y++) {
for (int x = 0; x < map[y].size(); x++) {
if (playerPosition.x == x && playerPosition.y == y) {
std::cout << '@';
}
else {
char tile = map[y][x];
if (tile == '@') {
std::cout << '.';
}
else {
std::cout << tile;
}
}
}
std::cout << "\n";
}
}
bool Location::isWalkable(int x, int y) const {
if (y < 0 || y >= map.size()) return false;
if (x < 0 || x >= map[y].size()) return false;
char tile = map[y][x];
return tile != '#';
}
char Location::getTile(int x, int y) const {
if (y < 0 || y >= map.size()) return '#';
if (x < 0 || x >= map[y].size()) return '#';
return map[y][x];
}

View File

@@ -1,17 +1,19 @@
#include "../include/player.h"
#include <iostream>
Player::Player(std::string _name)
{
this->name = _name;
Player::Player(std::string name) {
this->name = name;
hp = 100;
atk = 10;
gold = 0;
currentLocation = 1;
}
void Player::showStats() {
std::cout << "\n === " << this->name << "'s STATS ===";
std::cout << "\nHP : " << this->hp;
std::cout << "\nATK : " << this->atk;
std::cout << "\nGOLD : " << this->gold;
void Player::showStats() const {
std::cout << "\n=== " << name << "'s STATS ===";
std::cout << "\nHP: " << hp;
std::cout << "\nATK: " << atk;
std::cout << "\nGOLD: " << gold;
std::cout << "\nLOCATION: " << currentLocation;
std::cout << "\nPOSITION: " << position.x << ", " << position.y << "\n";
}