豆包0.0.1
C++#include <iostream>
#include <vector>
#include <string>
#include <cstdlib>
#include <ctime>
#include <random>
// 怪物类
class Monster {
public:
std::string name;
int health;
int attack;
Monster(const std::string& n, int h, int a) : name(n), health(h), attack(a) {}
};
// 宝箱类
class TreasureChest {
public:
int reward;
TreasureChest(int r) : reward(r) {}
};
// 地图元素枚举
enum MapElement {
EMPTY,
BUILDING,
MONSTER_HIDDEN,
TREASURE_CHEST_HIDDEN
};
// 地图区块类
class MapBlock {
private:
std::vector<std::vector<MapElement>> map;
std::vector<Monster> monsters;
std::vector<TreasureChest> treasureChests;
int width;
int height;
public:
MapBlock(int w, int h) : width(w), height(h) {
map.resize(height, std::vector<MapElement>(width, EMPTY));
monsters = {
Monster("史莱姆", 20, 5),
Monster("丘丘人", 30, 8),
Monster("盗宝团", 40, 12),
Monster("幽灵", 35, 10),
Monster("狼人", 50, 15),
Monster("食人魔", 60, 20),
Monster("吸血鬼", 45, 18),
Monster("骷髅兵", 32, 9),
Monster("蜥蜴人", 42, 13),
Monster("树妖", 55, 16),
Monster("地狱犬", 80, 25),
Monster("九头蛇", 120, 35),
Monster("泰坦巨人", 150, 40),
Monster("暗黑法师", 100, 30)
};
treasureChests = {
TreasureChest(50),
TreasureChest(100),
TreasureChest(200)
};
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<> dis(0, 100);
// 随机生成建筑、怪物和宝箱
for (int y = 0; y < height; ++y) {
for (int x = 0; x < width; ++x) {
int randNum = dis(gen);
if (randNum < 10) {
map[y][x] = BUILDING;
} else if (randNum < 25) {
map[y][x] = MONSTER_HIDDEN;
} else if (randNum < 30) {
map[y][x] = TREASURE_CHEST_HIDDEN;
}
}
}
}
void displayMap() const {
for (const auto& row : map) {
for (const auto& element : row) {
switch (element) {
case EMPTY:
std::cout << " . ";
break;
case BUILDING:
std::cout << " B ";
break;
case MONSTER_HIDDEN:
std::cout << " . ";
break;
case TREASURE_CHEST_HIDDEN:
std::cout << " . ";
break;
}
}
std::cout << std::endl;
}
}
MapElement getElement(int x, int y) const {
if (x >= 0 && x < width && y >= 0 && y < height) {
return map[y][x];
}
return EMPTY;
}
Monster encounterMonster() {
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<> dis(0, monsters.size() - 1);
return monsters[dis(gen)];
}
TreasureChest openTreasureChest() {
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<> dis(0, treasureChests.size() - 1);
return treasureChests[dis(gen)];
}
void triggerElement(int x, int y) {
if (x >= 0 && x < width && y >= 0 && y < height) {
map[y][x] = EMPTY;
}
}
};
// 大地图类
class BigMap {
private:
std::vector<std::vector<MapBlock>> blocks;
public:
int blockWidth;
int blockHeight;
int numBlocksX;
int numBlocksY;
BigMap(int bw, int bh, int nx, int ny) : blockWidth(bw), blockHeight(bh), numBlocksX(nx), numBlocksY(ny) {
blocks.resize(numBlocksY, std::vector<MapBlock>(numBlocksX, MapBlock(blockWidth, blockHeight)));
}
void displayMap(int blockX, int blockY) const {
blocks[blockY][blockX].displayMap();
}
MapElement getElement(int blockX, int blockY, int x, int y) const {
return blocks[blockY][blockX].getElement(x, y);
}
Monster encounterMonster(int blockX, int blockY) {
return blocks[blockY][blockX].encounterMonster();
}
TreasureChest openTreasureChest(int blockX, int blockY) {
return blocks[blockY][blockX].openTreasureChest();
}
void triggerElement(int blockX, int blockY, int x, int y) {
blocks[blockY][blockX].triggerElement(x, y);
}
};
// 技能结构体
struct Skill {
std::string name;
int damage;
int cost; // 技能消耗的能量
Skill(const std::string& n, int d, int c) : name(n), damage(d), cost(c) {}
};
// 角色类
class Character {
public:
std::string name;
int health;
int attack;
int energy; // 角色能量,用于释放技能和大招
Skill skill;
Skill ultimate;
Character(const std::string& n, int h, int a, const Skill& s, const Skill& u)
: name(n), health(h), attack(a), energy(0), skill(s), ultimate(u) {}
};
// 玩家类
class Player {
private:
std::string name;
int level;
int exp;
std::vector<Character> characters;
Character currentCharacter;
int x;
int y;
public:
int blockX;
int blockY;
int money;
Player(const std::string& playerName, int startBlockX, int startBlockY, int startX, int startY) : name(playerName), level(1), exp(0),
currentCharacter("史蒂夫", 100, 20, Skill("木剑挥击", 25, 20), Skill("勇者冲锋", 50, 50)), money(0), blockX(startBlockX), blockY(startBlockY), x(startX), y(startY) {
characters.push_back(currentCharacter);
}
void gainExp(int amount) {
exp += amount;
while (exp >= 100) {
level++;
exp -= 100;
std::cout << "恭喜!你升级到了 " << level << " 级!" << std::endl;
}
}
void displayStatus() {
std::cout << "玩家:" << name << std::endl;
std::cout << "等级:" << level << std::endl;
std::cout << "经验:" << exp << "/100" << std::endl;
std::cout << "当前角色:" << currentCharacter.name << std::endl;
std::cout << "角色生命值:" << currentCharacter.health << std::endl;
std::cout << "角色攻击力:" << currentCharacter.attack << std::endl;
std::cout << "角色能量:" << currentCharacter.energy << std::endl;
std::cout << "货币:" << money << std::endl;
std::cout << "位置:(区块: " << blockX << ", " << blockY << ", 坐标: " << x << ", " << y << ")" << std::endl;
}
void addCharacter(const Character& character) {
characters.push_back(character);
std::cout << "你抽到了新角色:" << character.name << "!" << std::endl;
}
void fight(Monster& monster) {
std::cout << "你遇到了 " << monster.name << ",开始战斗!" << std::endl;
while (currentCharacter.health > 0 && monster.health > 0) {
std::cout << "请选择行动:1. 普通攻击 2. 使用技能 3. 使用大招" << std::endl;
int choice;
std::cin >> choice;
switch (choice) {
case 1:
monster.health -= currentCharacter.attack;
currentCharacter.energy += 10; // 普通攻击增加能量
std::cout << "你进行了普通攻击,造成 " << currentCharacter.attack << " 点伤害。" << std::endl;
break;
case 2:
if (currentCharacter.energy >= currentCharacter.skill.cost) {
monster.health -= currentCharacter.skill.damage;
currentCharacter.energy -= currentCharacter.skill.cost;
std::cout << "你使用了技能 " << currentCharacter.skill.name << ",造成 " << currentCharacter.skill.damage << " 点伤害。" << std::endl;
} else {
std::cout << "能量不足,无法使用技能。" << std::endl;
continue;
}
break;
case 3:
if (currentCharacter.energy >= currentCharacter.ultimate.cost) {
monster.health -= currentCharacter.ultimate.damage;
currentCharacter.energy -= currentCharacter.ultimate.cost;
std::cout << "你使用了大招 " << currentCharacter.ultimate.name << ",造成 " << currentCharacter.ultimate.damage << " 点伤害。" << std::endl;
} else {
std::cout << "能量不足,无法使用大招。" << std::endl;
continue;
}
break;
default:
std::cout << "无效的选择,请重新输入。" << std::endl;
continue;
}
if (monster.health <= 0) {
std::cout << "你击败了 " << monster.name << ",获得了 30 点经验和 50 货币!" << std::endl;
gainExp(30);
earnMoney(50);
break;
}
currentCharacter.health -= monster.attack;
if (currentCharacter.health <= 0) {
std::cout << "你被 " << monster.name << " 击败了!" << std::endl;
break;
}
}
// 战斗结束后恢复角色生命值和能量
currentCharacter.health = characters[0].health;
currentCharacter.energy = 0;
}
void earnMoney(int amount) {
money += amount;
}
void switchCharacter(const std::string& name) {
for (const auto& character : characters) {
if (character.name == name) {
currentCharacter = character;
std::cout << "你切换到了角色:" << name << std::endl;
return;
}
}
std::cout << "你没有这个角色,无法切换。" << std::endl;
}
void move(int dx, int dy, BigMap& map) {
int newX = x + dx;
int newY = y + dy;
int newBlockX = blockX;
int newBlockY = blockY;
if (newX < 0) {
newBlockX--;
newX = map.blockWidth - 1;
} else if (newX >= map.blockWidth) {
newBlockX++;
newX = 0;
}
if (newY < 0) {
newBlockY--;
newY = map.blockHeight - 1;
} else if (newY >= map.blockHeight) {
newBlockY++;
newY = 0;
}
if (newBlockX < 0 || newBlockX >= map.numBlocksX || newBlockY < 0 || newBlockY >= map.numBlocksY) {
std::cout << "超出地图边界,无法移动。" << std::endl;
return;
}
MapElement element = map.getElement(newBlockX, newBlockY, newX, newY);
switch (element) {
case EMPTY:
blockX = newBlockX;
blockY = newBlockY;
x = newX;
y = newY;
std::cout << "你移动到了 (区块: " << blockX << ", " << blockY << ", 坐标: " << x << ", " << y << ")" << std::endl;
break;
case BUILDING:
std::cout << "这里有一座建筑,你可以进去探索一下。" << std::endl;
blockX = newBlockX;
blockY = newBlockY;
x = newX;
y = newY;
break;
case MONSTER_HIDDEN: {
Monster monster = map.encounterMonster(newBlockX, newBlockY);
fight(monster);
blockX = newBlockX;
blockY = newBlockY;
x = newX;
y = newY;
map.triggerElement(newBlockX, newBlockY, newX, newY);
break;
}
case TREASURE_CHEST_HIDDEN: {
TreasureChest chest = map.openTreasureChest(newBlockX, newBlockY);
earnMoney(chest.reward);
std::cout << "你打开了一个宝箱,获得了 " << chest.reward << " 货币!" << std::endl;
blockX = newBlockX;
blockY = newBlockY;
x = newX;
y = newY;
map.triggerElement(newBlockX, newBlockY, newX, newY);
break;
}
}
}
};
// 抽卡系统类
class GachaSystem {
private:
std::vector<Character> characters;
public:
GachaSystem() {
characters = {
Character("史蒂夫", 100, 20, Skill("木剑挥击", 25, 20), Skill("勇者冲锋", 50, 50)),
Character("艾利克斯", 80, 25, Skill("石斧猛击", 30, 25), Skill("旋风斩", 60, 60)),
Character("末影人猎手", 120, 30, Skill("暗影突袭", 35, 30), Skill("虚空斩击", 70, 70)),
Character("苦力怕克星", 110, 22, Skill("炸弹投掷", 32, 28), Skill("连环爆炸", 65, 65)),
Character("村民守护者", 90, 18, Skill("护盾撞击", 22, 20), Skill("神圣守护", 45, 50)),
Character("僵尸斗士", 95, 21, Skill("利爪撕裂", 26, 23), Skill("僵尸狂怒", 55, 55)),
Character("女巫猎手", 85, 23, Skill("魔法飞弹", 29, 26), Skill("魔法风暴", 62, 62)),
Character("岩浆怪征服者", 105, 24, Skill("熔岩冲击", 31, 27), Skill("岩浆爆发", 68, 68)),
Character("骷髅狙击手", 92, 20, Skill("箭雨齐发", 27, 24), Skill("致命狙击", 58, 58)),
Character("铁傀儡操纵者", 130, 26, Skill("铁臂重击", 33, 30), Skill("铁傀儡降临", 72, 72)),
Character("潜影贝猎人", 88, 22, Skill("尖刺射击", 28, 25), Skill("潜影爆发", 63, 63)),
Character("溺尸杀手", 102, 27, Skill("水刃斩击", 34, 31), Skill("海啸冲击", 75, 75)),
Character("末影龙杀手", 98, 21, Skill("龙息喷射", 29, 26), Skill("屠龙斩", 66, 66)),
Character("凋灵克星", 140, 32, Skill("凋零箭雨", 38, 34), Skill("凋灵毁灭", 80, 80)),
Character("蜘蛛终结者", 125, 31, Skill("蛛网缠绕", 36, 32), Skill("蜘蛛风暴", 73, 73)),
Character("恶魂驱散者", 115, 28, Skill("火焰喷射", 35, 31), Skill("恶魂湮灭", 71, 71)),
Character("蠹虫清扫者", 108, 29, Skill("虫群攻击", 33, 30), Skill("虫海战术", 69, 69)),
Character("幻翼歼灭者", 96, 23, Skill("羽翼冲击", 30, 27), Skill("幻翼毁灭", 64, 64)),
Character("守卫者猎人", 103, 25, Skill("三叉戟突刺", 32, 29), Skill("深海重击", 74, 74)),
Character("史莱姆粉碎者", 94, 19, Skill("史莱姆践踏", 24, 22), Skill("史莱姆爆炸", 52, 52))
};
}
Character gacha() {
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<> dis(0, characters.size() - 1);
return characters[dis(gen)];
}
};
// 村庄剧情类
class VillagePlot {
public:
std::vector<std::string> steps;
std::vector<int> stepRewards;
std::vector<std::string> stepDescriptions;
int currentStep;
std::string finalReward;
VillagePlot(const std::vector<std::string>& s, const std::vector<int>& sr, const std::vector<std::string>& sd, const std::string& fr)
: steps(s), stepRewards(sr), stepDescriptions(sd), currentStep(0), finalReward(fr) {}
bool isCompleted() const {
return currentStep >= steps.size();
}
void nextStep(Player& player) {
if (isCompleted()) {
std::cout << "该剧情已全部完成。" << std::endl;
return;
}
std::cout << stepDescriptions[currentStep] << std::endl;
std::string action;
std::cout << "请输入操作(" << steps[currentStep] << "):";
std::cin >> action;
if (action == steps[currentStep]) {
player.earnMoney(stepRewards[currentStep]);
std::cout << "完成此步骤,获得 " << stepRewards[currentStep] << " 货币!" << std::endl;
currentStep++;
if (isCompleted()) {
player.earnMoney(100);
std::cout << "完成整个剧情,获得 " << finalReward << "!" << std::endl;
}
} else {
std::cout << "操作错误,请重新尝试。" << std::endl;
}
}
};
// 村庄类
class Village {
private:
std::vector<std::string> buildings;
std::string name;
VillagePlot plot;
public:
Village(const std::string& n, const VillagePlot& p) : name(n), plot(p) {
buildings = {"小木屋", "农田"};
}
void displayBuildings() {
std::cout << name << " 里的建筑有:" << std::endl;
for (const auto& building : buildings) {
std::cout << "- " << building << std::endl;
}
}
void buildBuilding(const std::string& building) {
buildings.push_back(building);
std::cout << "在 " << name << " 成功建造了 " << building << "!" << std::endl;
}
void startPlot(Player& player) {
plot.nextStep(player);
}
std::string getName() const {
return name;
}
};
// 探索函数
void explore(Player& player, BigMap& map) {
std::cout << "当前区块地图如下:" << std::endl;
map.displayMap(player.blockX, player.blockY);
std::cout << "请输入移动方向(w: 上, s: 下, a: 左, d: 右):";
char direction;
std::cin >> direction;
int dx = 0, dy = 0;
switch (direction) {
case 'w':
dy = -1;
break;
case 's':
dy = 1;
break;
case 'a':
dx = -1;
break;
case 'd':
dx = 1;
break;
default:
std::cout << "无效的输入,请重新输入。" << std::endl;
return;
}
player.move(dx, dy, map);
}
// 抽卡函数
void gacha(Player& player, GachaSystem& gachaSystem) {
if (player.money >= 200) {
player.earnMoney(-200);
Character newCharacter = gachaSystem.gacha();
player.addCharacter(newCharacter);
} else {
std::cout << "货币不足,无法抽卡。" << std::endl;
}
}
int main() {
std::string playerName;
std::cout << "欢迎来到《我的村庄》!请输入你的名字:";
std::cin >> playerName;
Player player(playerName, 0, 0, 0, 0);
BigMap map(10, 10, 5, 5);
GachaSystem gachaSystem;
std::vector<Village> villages = {
Village("起始村", VillagePlot(
{"寻找草药", "击败强盗", "护送村民"},
{20, 30, 50},
{
"村庄里有村民生病了,你需要去森林中寻找草药。",
"村庄附近出现了强盗,他们经常抢劫村民,你需要去击败他们。",
"有一批村民要前往邻村,但是路上不安全,你需要护送他们。"
},
"一些物资和货币"
)),
Village("神秘村", VillagePlot(
{"调查洞穴", "破解谜题", "打败巫师"},
{30, 40, 60},
{
"村里出现神秘事件,村民们接二连三地失踪。你决定深入调查一个神秘洞穴。",
"在洞穴中你发现了一些奇怪的谜题,需要解开它们才能继续前进。",
"解开谜题后,你遇到了邪恶的巫师,需要打败他来解救村民。"
},
"强力武器图纸"
)),
Village("富饶村", VillagePlot(
{"收集信息", "谈判交易", "打通商路"},
{25, 35, 55},
{
"村庄的贸易路线被强盗封锁,你需要先收集强盗的信息。",
"根据收集到的信息,你和强盗展开谈判,尝试恢复贸易。",
"谈判失败后,你需要武力打通商路,让贸易恢复正常。"
},
"大量货币"
)),
Village("冰雪村", VillagePlot(
{"寻找木材", "建造房屋", "寻找火源"},
{20, 30, 50},
{
"冰雪村遭遇了罕见的暴风雪,你需要去森林中寻找木材。",
"用找到的木材为村民建造防风的房屋。",
"暴风雪还在继续,你需要寻找火源来让村民取暖。"
},
"保暖装备"
)),
Village("火焰村", VillagePlot(
{"组织撤离", "寻找水源", "控制岩浆"},
{25, 35, 55},
{
"火焰村的火山突然爆发,你需要组织村民撤离到安全地带。",
"撤离过程中,村民们缺水,你需要寻找水源。",
"最后,你需要使用特殊的魔法控制岩浆的流动,避免村庄被摧毁。"
},
"火焰魔法秘籍"
))
};
while (true) {
std::cout << "\n请选择操作:" << std::endl;
std::cout << "1. 查看村庄建筑" << std::endl;
std::cout << "2. 建造建筑" << std::endl;
std::cout << "3. 探索" << std::endl;
std::cout << "4. 查看玩家状态" << std::endl;
std::cout << "5. 抽卡" << std::endl;
std::cout << "6. 触发村庄剧情" << std::endl;
std::cout << "7. 切换角色" << std::endl;
std::cout << "8. 退出游戏" << std::endl;
int choice;
std::cin >> choice;
switch (choice) {
case 1: {
std::cout << "请选择村庄(1 - " << villages.size() << "):";
int villageIndex;
std::cin >> villageIndex;
if (villageIndex >= 1 && villageIndex <= villages.size()) {
villages[villageIndex - 1].displayBuildings();
} else {
std::cout << "无效的村庄选择。" << std::endl;
}
break;
}
case 2: {
std::cout << "请选择村庄(1 - " << villages.size() << "):";
int villageIndex;
std::cin >> villageIndex;
if (villageIndex >= 1 && villageIndex <= villages.size()) {
std::string building;
std::cout << "请输入要建造的建筑名称:";
std::cin >> building;
villages[villageIndex - 1].buildBuilding(building);
} else {
std::cout << "无效的村庄选择。" << std::endl;
}
break;
}
case 3:
explore(player, map);
break;
case 4:
player.displayStatus();
break;
case 5:
gacha(player, gachaSystem);
break;
case 6: {
std::cout << "请选择村庄(1 - " << villages.size() << "):";
int villageIndex;
std::cin >> villageIndex;
if (villageIndex >= 1 && villageIndex <= villages.size()) {
villages[villageIndex - 1].startPlot(player);
} else {
std::cout << "无效的村庄选择。" << std::endl;
}
break;
}
case 7: {
std::string characterName;
std::cout << "请输入要切换的角色名称:";
std::cin >> characterName;
player.switchCharacter(characterName);
break;
}
case 8:
std::cout << "感谢游玩《我的村庄》,再见!" << std::endl;
return 0;
default:
std::cout << "无效的选择,请重新输入!" << std::endl;
}
}
return 0;
}
豆包1.0
C++#include <iostream>
#include <vector>
#include <string>
#include <map>
#include <random>
#include <algorithm>
#include <memory>
#include <cstdlib>
#include <ctime>
#include <sstream>
#include <cassert>
using namespace std;
// 前向声明
class Item;
class Equipment;
class Player;
class Monster;
class Location;
class Event;
class Story;
class Game;
// 物品类型枚举
enum class ItemType {
BLOCK, // 方块
TOOL, // 工具
WEAPON, // 武器
ARMOR, // 盔甲
CONSUMABLE, // 消耗品
MATERIAL, // 材料
MISC // 其他
};
// 物品类
class Item {
protected:
string name;
string description;
ItemType type;
int value; // 价值
bool stackable; // 是否可堆叠
int maxStackSize; // 最大堆叠数量
public:
Item(string name, string description, ItemType type, int value, bool stackable = true, int maxStackSize = 64)
: name(name), description(description), type(type), value(value), stackable(stackable), maxStackSize(maxStackSize) {}
virtual ~Item() = default;
string getName() const { return name; }
string getDescription() const { return description; }
ItemType getType() const { return type; }
int getValue() const { return value; }
bool isStackable() const { return stackable; }
int getMaxStackSize() const { return maxStackSize; }
// 使用物品(基础物品不可使用)
virtual bool use(Player* player) {
cout << "这个物品不能被使用。" << endl;
return false;
}
// 克隆物品
virtual Item* clone() const {
return new Item(*this);
}
// 用于比较物品是否相同(根据名称)
bool isSameType(const Item& other) const {
return name == other.name && type == other.type;
}
};
// 装备类(继承自物品)
class Equipment : public Item {
protected:
int attack; // 攻击力
int defense; // 防御力
int durability; // 耐久度
int maxDurability; // 最大耐久度
public:
Equipment(string name, string description, ItemType type, int value,
int attack = 0, int defense = 0, int maxDurability = 100)
: Item(name, description, type, value, false, 1),
attack(attack), defense(defense),
durability(maxDurability), maxDurability(maxDurability) {}
int getAttack() const { return attack; }
int getDefense() const { return defense; }
int getDurability() const { return durability; }
int getMaxDurability() const { return maxDurability; }
// 降低耐久度
void reduceDurability(int amount = 1) {
durability = max(0, durability - amount);
}
// 修复装备
void repair(int amount) {
durability = min(maxDurability, durability + amount);
}
Item* clone() const override {
return new Equipment(*this);
}
};
// 消耗品类(继承自物品)
class Consumable : public Item {
private:
int healthRestore; // 恢复生命值
int hungerRestore; // 恢复饥饿值
bool givesEffect; // 是否有特殊效果
public:
Consumable(string name, string description, int value,
int healthRestore = 0, int hungerRestore = 0, bool givesEffect = false)
: Item(name, description, ItemType::CONSUMABLE, value),
healthRestore(healthRestore), hungerRestore(hungerRestore), givesEffect(givesEffect) {}
int getHealthRestore() const { return healthRestore; }
int getHungerRestore() const { return hungerRestore; }
bool getGivesEffect() const { return givesEffect; }
bool use(Player* player) override;
Item* clone() const override {
return new Consumable(*this);
}
};
// 玩家类
class Player {
private:
string name;
int health;
int maxHealth;
int hunger; // 0-20
int level;
int experience;
int experienceToNextLevel;
// 装备(使用原始指针)
Equipment* helmet;
Equipment* chestplate;
Equipment* leggings;
Equipment* boots;
Equipment* weapon;
// 物品栏 (使用vector存储指针)
vector<pair<Item*, int>> inventory;
int inventorySize;
// 当前所在位置
Location* currentLocation;
public:
Player(string name)
: name(name), health(20), maxHealth(20), hunger(20),
level(1), experience(0), experienceToNextLevel(100),
inventorySize(36), helmet(nullptr), chestplate(nullptr),
leggings(nullptr), boots(nullptr), weapon(nullptr), currentLocation(nullptr) {}
~Player() {
// 清理装备
delete helmet;
delete chestplate;
delete leggings;
delete boots;
delete weapon;
// 清理物品栏
for (auto& pair : inventory) {
delete pair.first;
}
}
// 获取属性
string getName() const { return name; }
int getHealth() const { return health; }
int getMaxHealth() const { return maxHealth; }
int getHunger() const { return hunger; }
int getLevel() const { return level; }
int getExperience() const { return experience; }
int getExperienceToNextLevel() const { return experienceToNextLevel; }
Location* getCurrentLocation() const { return currentLocation; }
// 设置属性
void setCurrentLocation(Location* location) { currentLocation = location; }
void setHealth(int h) { health = max(0, min(maxHealth, h)); }
void setHunger(int h) { hunger = max(0, min(20, h)); }
void setName(const string& n) { name = n; }
// 改变生命值
void changeHealth(int amount) {
setHealth(health + amount);
if (health <= 0) {
cout << "你已经死亡!游戏将重新开始。" << endl;
// 简单的重生机制
health = maxHealth / 2;
hunger = 10;
loseSomeItems();
}
}
// 改变饥饿值
void changeHunger(int amount) {
setHunger(hunger + amount);
if (hunger <= 0) {
cout << "你太饿了,开始失去生命值!" << endl;
changeHealth(-1);
}
}
// 添加经验值
void addExperience(int amount) {
experience += amount;
cout << "获得 " << amount << " 经验值!" << endl;
// 检查是否升级
while (experience >= experienceToNextLevel) {
levelUp();
}
}
// 升级
void levelUp() {
experience -= experienceToNextLevel;
level++;
maxHealth += 5;
health = maxHealth;
experienceToNextLevel = (int)(experienceToNextLevel * 1.5);
cout << "恭喜!你升级到了 " << level << " 级!" << endl;
cout << "最大生命值提升到 " << maxHealth << "!" << endl;
}
// 装备物品
bool equip(Equipment* equipment);
// 卸下装备
Equipment* unequip(ItemType type);
// 获取总攻击力
int getTotalAttack() const {
return (weapon ? weapon->getAttack() : 0) + level / 2;
}
// 获取总防御力
int getTotalDefense() const {
int total = level / 3;
if (helmet) total += helmet->getDefense();
if (chestplate) total += chestplate->getDefense();
if (leggings) total += leggings->getDefense();
if (boots) total += boots->getDefense();
return total;
}
// 查看装备
void viewEquipment() const;
// 添加物品到inventory
bool addItem(Item* item);
// 从inventory移除物品
Item* removeItem(const string& itemName, int count = 1);
// 查看inventory
void viewInventory() const;
// 使用物品
bool useItem(const string& itemName);
// 战斗胜利后获得奖励
void receiveRewards(int exp, vector<Item*> items);
// 死亡时掉落物品
void loseSomeItems();
// 检查是否拥有特定物品
bool hasItem(const string& itemName, int count = 1) const;
};
// 怪物类
class Monster {
private:
string name;
string description;
int health;
int maxHealth;
int attack;
int defense;
int experienceReward;
vector<pair<Item*, int>> possibleLoot; // 可能的掉落物和掉落概率(0-100)
public:
Monster(string name, string description, int health, int attack, int defense,
int experienceReward, vector<pair<Item*, int>> loot)
: name(name), description(description), maxHealth(health), health(health),
attack(attack), defense(defense), experienceReward(experienceReward) {
// 复制物品指针
for (auto& p : loot) {
possibleLoot.emplace_back(p.first->clone(), p.second);
}
}
~Monster() {
// 清理掉落物
for (auto& p : possibleLoot) {
delete p.first;
}
}
string getName() const { return name; }
string getDescription() const { return description; }
int getHealth() const { return health; }
int getMaxHealth() const { return maxHealth; }
int getAttack() const { return attack; }
int getDefense() const { return defense; }
int getExperienceReward() const { return experienceReward; }
void setHealth(int h) { health = max(0, h); }
void changeHealth(int amount) { setHealth(health + amount); }
bool isDead() const { return health <= 0; }
// 攻击玩家
void attackPlayer(Player* player) const {
int damage = max(1, attack - player->getTotalDefense() / 2);
player->changeHealth(-damage);
cout << name << " 对你造成了 " << damage << " 点伤害!" << endl;
}
// 获取战利品
vector<Item*> getLoot() const {
vector<Item*> loot;
// 随机数生成
int chance = rand() % 100 + 1;
for (const auto& itemPair : possibleLoot) {
if (chance <= itemPair.second) {
loot.push_back(itemPair.first->clone());
}
// 重新生成随机数
chance = rand() % 100 + 1;
}
return loot;
}
};
// 位置类
class Location {
private:
string name;
string description;
vector<Location*> connectedLocations; // 相连的位置
vector<Event*> possibleEvents; // 可能发生的事件
bool visited; // 是否被访问过
public:
Location(string name, string description)
: name(name), description(description), visited(false) {}
~Location() {
// 不需要删除connectedLocations,因为它们由Game类管理
// 不需要删除possibleEvents,因为它们由Game类管理
}
string getName() const { return name; }
string getDescription() const { return description; }
bool isVisited() const { return visited; }
void setVisited(bool v) { visited = v; }
// 添加相连的位置
void addConnectedLocation(Location* location) {
connectedLocations.push_back(location);
}
// 获取相连的位置
const vector<Location*>& getConnectedLocations() const {
return connectedLocations;
}
// 添加可能的事件
void addEvent(Event* event) {
possibleEvents.push_back(event);
}
// 获取可能的事件
const vector<Event*>& getPossibleEvents() const {
return possibleEvents;
}
// 显示位置信息
void displayInfo() const {
cout << endl << "【" << name << "】" << endl;
cout << description << endl;
if (!visited) {
cout << "这是你第一次来到这里。" << endl;
}
}
};
// 事件类型枚举
enum class EventType {
COMBAT, // 战斗事件
LOOT, // 获得物品
TRAP, // 陷阱
NPC_ENCOUNTER, // NPC遭遇
ENVIRONMENTAL, // 环境事件
STORY_TRIGGER // 剧情触发
};
// 事件类
class Event {
protected:
string name;
string description;
EventType type;
int triggerChance; // 触发概率(0-100)
bool repeatable; // 是否可重复触发
public:
Event(string name, string description, EventType type, int triggerChance, bool repeatable = true)
: name(name), description(description), type(type), triggerChance(triggerChance), repeatable(repeatable) {}
virtual ~Event() = default;
string getName() const { return name; }
string getDescription() const { return description; }
EventType getType() const { return type; }
int getTriggerChance() const { return triggerChance; }
bool isRepeatable() const { return repeatable; }
// 触发事件
virtual void trigger(Player* player, Game* game) = 0;
};
// 战斗事件
class CombatEvent : public Event {
private:
Monster* monster;
public:
CombatEvent(string name, string description, int triggerChance, Monster* monster, bool repeatable = true)
: Event(name, description, EventType::COMBAT, triggerChance, repeatable), monster(monster) {}
~CombatEvent() {
delete monster;
}
void trigger(Player* player, Game* game) override;
};
// 战利品事件
class LootEvent : public Event {
private:
vector<Item*> loot;
public:
LootEvent(string name, string description, int triggerChance, vector<Item*> loot, bool repeatable = false)
: Event(name, description, EventType::LOOT, triggerChance, repeatable) {
// 复制物品
for (auto item : loot) {
this->loot.push_back(item->clone());
}
}
~LootEvent() {
// 清理战利品
for (auto item : loot) {
delete item;
}
}
void trigger(Player* player, Game* game) override {
cout << endl << "【" << name << "】" << endl;
cout << description << endl;
cout << "你找到了以下物品:" << endl;
for (auto item : loot) {
cout << "- " << item->getName() << endl;
player->addItem(item->clone());
}
}
};
// 陷阱事件
class TrapEvent : public Event {
private:
int damage;
bool canAvoid; // 是否可以躲避
public:
TrapEvent(string name, string description, int triggerChance, int damage, bool canAvoid = true, bool repeatable = false)
: Event(name, description, EventType::TRAP, triggerChance, repeatable), damage(damage), canAvoid(canAvoid) {}
void trigger(Player* player, Game* game) override;
};
// NPC遭遇事件
class NpcEncounterEvent : public Event {
private:
string npcName;
string dialogue;
bool givesReward;
vector<Item*> reward;
bool advancesStory; // 是否推进剧情
int storyId; // 剧情ID
public:
NpcEncounterEvent(string name, string description, int triggerChance, string npcName, string dialogue,
bool givesReward = false, vector<Item*> reward = {},
bool advancesStory = false, int storyId = -1, bool repeatable = false)
: Event(name, description, EventType::NPC_ENCOUNTER, triggerChance, repeatable),
npcName(npcName), dialogue(dialogue), givesReward(givesReward),
advancesStory(advancesStory), storyId(storyId) {
// 复制奖励物品
for (auto item : reward) {
this->reward.push_back(item->clone());
}
}
~NpcEncounterEvent() {
// 清理奖励物品
for (auto item : reward) {
delete item;
}
}
void trigger(Player* player, Game* game) override;
};
// 环境事件
class EnvironmentalEvent : public Event {
private:
string effectDescription;
int healthChange; // 生命值变化,可为正或负
int hungerChange; // 饥饿值变化
public:
EnvironmentalEvent(string name, string description, int triggerChance, string effectDescription,
int healthChange = 0, int hungerChange = 0, bool repeatable = true)
: Event(name, description, EventType::ENVIRONMENTAL, triggerChance, repeatable),
effectDescription(effectDescription), healthChange(healthChange), hungerChange(hungerChange) {}
void trigger(Player* player, Game* game) override {
cout << endl << "【" << name << "】" << endl;
cout << description << endl;
cout << effectDescription << endl;
if (healthChange != 0) {
player->changeHealth(healthChange);
if (healthChange > 0) {
cout << "你恢复了 " << healthChange << " 点生命值。" << endl;
} else {
cout << "你受到了 " << -healthChange << " 点伤害。" << endl;
}
}
if (hungerChange != 0) {
player->changeHunger(hungerChange);
if (hungerChange > 0) {
cout << "你的饥饿感减轻了。" << endl;
} else {
cout << "你感到更饿了。" << endl;
}
}
}
};
// 剧情触发事件
class StoryTriggerEvent : public Event {
private:
int storyId; // 要触发的剧情ID
public:
StoryTriggerEvent(string name, string description, int triggerChance, int storyId, bool repeatable = false)
: Event(name, description, EventType::STORY_TRIGGER, triggerChance, repeatable), storyId(storyId) {}
void trigger(Player* player, Game* game) override;
};
// 剧情类
class Story {
private:
int id;
string title;
vector<string> paragraphs; // 剧情段落
bool isMainStory; // 是否为主剧情
int part; // 部分 (1-3 对应前中后篇)
bool completed; // 是否已完成
vector<pair<string, int>> requiredItems; // 触发所需物品 (物品名, 数量)
int requiredLevel; // 触发所需等级
vector<Item*> rewards; // 完成奖励
int nextStoryId; // 后续剧情ID
public:
Story(int id, string title, vector<string> paragraphs, bool isMainStory = false, int part = 0,
vector<pair<string, int>> requiredItems = {}, int requiredLevel = 1,
vector<Item*> rewards = {}, int nextStoryId = -1)
: id(id), title(title), paragraphs(paragraphs), isMainStory(isMainStory), part(part),
completed(false), requiredItems(requiredItems), requiredLevel(requiredLevel),
nextStoryId(nextStoryId) {
// 复制奖励物品
for (auto item : rewards) {
this->rewards.push_back(item->clone());
}
}
~Story() {
// 清理奖励物品
for (auto item : rewards) {
delete item;
}
}
int getId() const { return id; }
string getTitle() const { return title; }
bool getIsMainStory() const { return isMainStory; }
int getPart() const { return part; }
bool isCompleted() const { return completed; }
int getNextStoryId() const { return nextStoryId; }
// 检查是否可以触发剧情
bool canTrigger(const Player* player) const {
if (completed) return false;
// 检查等级要求
if (player->getLevel() < requiredLevel) return false;
// 检查物品要求
for (const auto& req : requiredItems) {
if (!player->hasItem(req.first, req.second)) return false;
}
return true;
}
// 显示剧情
void display(Player* player, Game* game);
// 标记为已完成
void complete(Player* player) {
if (completed) return;
completed = true;
cout << "剧情完成!" << endl;
// 给予奖励
if (!rewards.empty()) {
cout << "你获得了以下奖励:" << endl;
for (auto item : rewards) {
cout << "- " << item->getName() << endl;
player->addItem(item->clone());
}
}
}
};
// 游戏类
class Game {
private:
Player* player;
vector<Location*> locations;
vector<Event*> events;
vector<Story*> stories;
vector<Item*> allItems; // 所有可用物品
bool running;
int currentMainStoryPart; // 当前主线剧情部分 (0-3,0表示未开始)
public:
Game(Player* p) : player(p), running(true), currentMainStoryPart(0) {}
~Game() {
// 清理所有物品
for (auto item : allItems) {
delete item;
}
// 清理所有事件
for (auto event : events) {
delete event;
}
// 清理所有剧情
for (auto story : stories) {
delete story;
}
// 清理所有位置
for (auto location : locations) {
delete location;
}
// 玩家由main函数创建和管理
}
// 获取玩家
Player* getPlayer() const { return player; }
// 获取剧情
Story* getStoryById(int id) const {
for (auto story : stories) {
if (story->getId() == id) {
return story;
}
}
return nullptr;
}
// 获取当前主线剧情部分
int getCurrentMainStoryPart() const { return currentMainStoryPart; }
// 设置当前主线剧情部分
void setCurrentMainStoryPart(int part) { currentMainStoryPart = part; }
// 添加位置
void addLocation(Location* location) {
locations.push_back(location);
}
// 添加事件
void addEvent(Event* event) {
events.push_back(event);
}
// 添加剧情
void addStory(Story* story) {
stories.push_back(story);
}
// 添加物品
void addItem(Item* item) {
allItems.push_back(item);
}
// 初始化游戏世界
void initializeWorld();
// 开始游戏
void start();
// 显示主菜单
void showMainMenu();
// 处理玩家输入
void handleInput(const string& input);
// 探险逻辑
void explore();
// 仔细探索当前位置
void exploreCurrentLocation();
// 继续探险(移动到新位置)
void continueExploring();
// 检查并触发剧情
void checkAndTriggerStories();
};
// 实现Consumable类的use方法
bool Consumable::use(Player* player) {
if (player == nullptr) return false;
bool used = false;
if (healthRestore > 0) {
player->changeHealth(healthRestore);
cout << "你使用了" << name << ",恢复了" << healthRestore << "点生命值。" << endl;
used = true;
}
if (hungerRestore > 0) {
player->changeHunger(hungerRestore);
cout << "你使用了" << name << ",饥饿感减轻了。" << endl;
used = true;
}
if (givesEffect) {
cout << "你感到力量倍增!" << endl;
used = true;
}
return used;
}
// 实现Player类的equip方法
bool Player::equip(Equipment* equipment) {
if (!equipment) return false;
ItemType type = equipment->getType();
Equipment* oldEquipment = nullptr;
// 根据装备类型卸下旧装备
if (type == ItemType::ARMOR) {
// 简单判断盔甲类型(根据名称)
if (equipment->getName().find("头盔") != string::npos) {
oldEquipment = helmet;
helmet = equipment;
} else if (equipment->getName().find("胸甲") != string::npos) {
oldEquipment = chestplate;
chestplate = equipment;
} else if (equipment->getName().find("leggings") != string::npos) {
oldEquipment = leggings;
leggings = equipment;
} else if (equipment->getName().find("靴子") != string::npos) {
oldEquipment = boots;
boots = equipment;
} else {
delete equipment; // 无法装备,清理内存
return false; // 未知盔甲类型
}
} else if (type == ItemType::WEAPON) {
oldEquipment = weapon;
weapon = equipment;
} else {
delete equipment; // 无法装备,清理内存
return false; // 不能装备的类型
}
cout << "你装备了 " << (type == ItemType::WEAPON ? weapon :
(helmet ? helmet : (chestplate ? chestplate :
(leggings ? leggings : boots))))->getName() << endl;
// 将旧装备放回inventory
if (oldEquipment) {
addItem(oldEquipment);
}
return true;
}
// 实现Player类的unequip方法
Equipment* Player::unequip(ItemType type) {
Equipment* result = nullptr;
if (type == ItemType::ARMOR) {
// 这里简化处理,实际游戏中需要指定具体盔甲部位
cout << "请选择要卸下的装备:1.头盔 2.胸甲 3.leggings 4.靴子" << endl;
int choice;
cin >> choice;
switch (choice) {
case 1:
result = helmet;
helmet = nullptr;
break;
case 2:
result = chestplate;
chestplate = nullptr;
break;
case 3:
result = leggings;
leggings = nullptr;
break;
case 4:
result = boots;
boots = nullptr;
break;
default:
cout << "无效选择" << endl;
return nullptr;
}
} else if (type == ItemType::WEAPON) {
result = weapon;
weapon = nullptr;
} else {
cout << "无法卸下该类型装备" << endl;
return nullptr;
}
if (result) {
cout << "你卸下了 " << result->getName() << endl;
} else {
cout << "你没有装备该类型物品" << endl;
}
return result;
}
// 实现Player类的viewEquipment方法
void Player::viewEquipment() const {
cout << endl << "【你的装备】" << endl;
cout << "头盔: " << (helmet ? helmet->getName() : "无") << endl;
cout << "胸甲: " << (chestplate ? chestplate->getName() : "无") << endl;
cout << "leggings: " << (leggings ? leggings->getName() : "无") << endl;
cout << "靴子: " << (boots ? boots->getName() : "无") << endl;
cout << "武器: " << (weapon ? weapon->getName() : "无") << endl;
cout << "总攻击力: " << getTotalAttack() << endl;
cout << "总防御力: " << getTotalDefense() << endl;
}
// 实现Player类的addItem方法
bool Player::addItem(Item* item) {
if (!item) return false;
// 检查inventory是否已满
if (inventory.size() >= (size_t)inventorySize) {
cout << "你的inventory已满,无法拾取 " << item->getName() << endl;
delete item; // 无法拾取,清理内存
return false;
}
// 检查是否可以堆叠
if (item->isStackable()) {
for (auto& pair : inventory) {
if (pair.first->isSameType(*item)) {
int newCount = pair.second + 1;
if (newCount <= item->getMaxStackSize()) {
pair.second = newCount;
cout << "你获得了 " << item->getName() << " x1" << endl;
delete item; // 已堆叠,清理原物品
return true;
}
}
}
}
// 无法堆叠或没有找到相同物品,添加新物品
inventory.emplace_back(item, 1);
cout << "你获得了 " << inventory.back().first->getName() << " x1" << endl;
return true;
}
// 实现Player类的removeItem方法
Item* Player::removeItem(const string& itemName, int count) {
for (auto it = inventory.begin(); it != inventory.end(); ++it) {
if (it->first->getName() == itemName) {
if (it->second > count) {
it->second -= count;
return it->first->clone(); // 返回克隆的物品
} else {
// 保存指针并从容器中移除
Item* item = it->first;
inventory.erase(it);
return item; // 返回原始物品
}
}
}
return nullptr;
}
// 实现Player类的viewInventory方法
void Player::viewInventory() const {
cout << endl << "【你的inventory】 (" << inventory.size() << "/" << inventorySize << ")" << endl;
if (inventory.empty()) {
cout << "你的inventory是空的。" << endl;
return;
}
int index = 1;
for (const auto& pair : inventory) {
cout << index << ". " << pair.first->getName() << " x" << pair.second << endl;
cout << " " << pair.first->getDescription() << endl;
index++;
}
}
// 实现Player类的useItem方法
bool Player::useItem(const string& itemName) {
Item* item = removeItem(itemName);
if (!item) {
cout << "你没有这个物品。" << endl;
return false;
}
bool used = item->use(this);
if (!used) {
// 如果没有使用成功,放回inventory
addItem(item);
return false;
}
// 使用成功,删除物品
delete item;
return true;
}
// 实现Player类的receiveRewards方法
void Player::receiveRewards(int exp, vector<Item*> items) {
addExperience(exp);
for (auto item : items) {
addItem(item);
}
}
// 实现Player类的loseSomeItems方法
void Player::loseSomeItems() {
if (inventory.empty()) return;
// 随机数生成
int itemsToLose = rand() % min(5, (int)inventory.size() / 2) + 1;
cout << "你在死亡时掉落了 " << itemsToLose << " 件物品!" << endl;
// 随机选择要掉落的物品
vector<size_t> indices;
for (size_t i = 0; i < inventory.size(); ++i) {
indices.push_back(i);
}
random_shuffle(indices.begin(), indices.end());
// 记录要保留的物品
vector<pair<Item*, int>> remaining;
for (size_t i = 0; i < inventory.size(); ++i) {
// 保留未被选中的物品
if (find(indices.begin(), indices.begin() + itemsToLose, i) == indices.begin() + itemsToLose) {
remaining.push_back(inventory[i]);
} else {
cout << "你失去了: " << inventory[i].first->getName() << endl;
// 删除掉落的物品
delete inventory[i].first;
}
}
// 替换inventory
inventory = remaining;
}
// 实现Player类的hasItem方法
bool Player::hasItem(const string& itemName, int count) const {
for (const auto& pair : inventory) {
if (pair.first->getName() == itemName) {
return pair.second >= count;
}
}
return false;
}
// 实现CombatEvent类的trigger方法
void CombatEvent::trigger(Player* player, Game* game) {
if (!player || !monster) return;
cout << endl << "【" << name << "】" << endl;
cout << description << endl;
cout << "你遇到了 " << monster->getName() << "!" << endl;
cout << monster->getDescription() << endl;
// 战斗逻辑 - 创建怪物副本进行战斗
Monster* currentMonster = new Monster(*monster);
bool playerFled = false;
while (player->getHealth() > 0 && !currentMonster->isDead() && !playerFled) {
cout << endl << "-------------------------" << endl;
cout << player->getName() << " 的生命值: " << player->getHealth() << "/" << player->getMaxHealth() << endl;
cout << currentMonster->getName() << " 的生命值: " << currentMonster->getHealth() << "/" << currentMonster->getMaxHealth() << endl;
cout << "-------------------------" << endl;
cout << "请选择行动: 1.攻击 2.使用物品 3.逃跑" << endl;
int choice;
cin >> choice;
switch (choice) {
case 1: {
// 玩家攻击
int damage = max(1, player->getTotalAttack() - currentMonster->getDefense() / 3);
currentMonster->changeHealth(-damage);
cout << "你对 " << currentMonster->getName() << " 造成了 " << damage << " 点伤害!" << endl;
// 怪物反击(如果还活着)
if (!currentMonster->isDead()) {
currentMonster->attackPlayer(player);
}
break;
}
case 2: {
// 使用物品
player->viewInventory();
cout << "请输入要使用的物品名称,或输入'取消'放弃使用: ";
string itemName;
cin.ignore();
getline(cin, itemName);
if (itemName != "取消") {
player->useItem(itemName);
}
// 怪物攻击
currentMonster->attackPlayer(player);
break;
}
case 3: {
// 尝试逃跑
int fleeChance = 50; // 50%的逃跑几率
if (rand() % 100 + 1 <= fleeChance) {
cout << "你成功逃脱了!" << endl;
playerFled = true;
} else {
cout << "逃跑失败!" << endl;
currentMonster->attackPlayer(player);
}
break;
}
default:
cout << "无效的选择,你犹豫了一下..." << endl;
currentMonster->attackPlayer(player);
break;
}
}
// 清理怪物副本
delete currentMonster;
// 战斗结束
if (player->getHealth() <= 0) {
cout << "你在战斗中失败了..." << endl;
} else if (playerFled) {
// 逃跑成功,什么都不做
} else {
cout << "你击败了 " << monster->getName() << "!" << endl;
vector<Item*> loot = monster->getLoot();
player->receiveRewards(monster->getExperienceReward(), loot);
// 清理战利品
for (auto item : loot) {
delete item;
}
}
}
// 实现TrapEvent类的trigger方法
void TrapEvent::trigger(Player* player, Game* game) {
if (!player) return;
cout << endl << "【" << name << "】" << endl;
cout << description << endl;
if (canAvoid) {
cout << "你有机会躲避!请选择: 1.快速反应 2.小心移动" << endl;
int choice;
cin >> choice;
// 随机数生成
int chance = rand() % 100 + 1;
bool avoided = false;
if (choice == 1) {
avoided = (chance <= 40); // 40%几率躲避
} else {
avoided = (chance <= 60); // 60%几率躲避
}
if (avoided) {
cout << "你成功躲避了陷阱!" << endl;
return;
}
}
// 未能躲避或无法躲避
player->changeHealth(-damage);
cout << "你触发了陷阱,受到了 " << damage << " 点伤害!" << endl;
}
// 实现NpcEncounterEvent类的trigger方法
void NpcEncounterEvent::trigger(Player* player, Game* game) {
if (!player || !game) return;
cout << endl << "【" << name << "】" << endl;
cout << description << endl;
cout << npcName << " 说: \"" << dialogue << "\"" << endl;
if (givesReward && !reward.empty()) {
cout << npcName << " 给了你一些东西作为奖励!" << endl;
for (auto item : reward) {
player->addItem(item->clone());
}
}
if (advancesStory && storyId != -1) {
Story* story = game->getStoryById(storyId);
if (story && story->canTrigger(player)) {
story->display(player, game);
}
}
}
// 实现StoryTriggerEvent类的trigger方法
void StoryTriggerEvent::trigger(Player* player, Game* game) {
if (!player || !game) return;
cout << endl << "【" << name << "】" << endl;
cout << description << endl;
Story* story = game->getStoryById(storyId);
if (story && story->canTrigger(player)) {
story->display(player, game);
}
}
// 实现Story类的display方法
void Story::display(Player* player, Game* game) {
if (!player || !game || completed) return;
cout << endl << "=====================================" << endl;
cout << "【" << title << "】" << endl;
cout << "=====================================" << endl;
for (const string& para : paragraphs) {
cout << para << endl << endl;
// 每段后等待用户按回车继续
cout << "按回车键继续...";
cin.ignore();
cin.get();
}
// 完成剧情
complete(player);
// 如果是主线剧情,更新游戏的主线剧情进度
if (isMainStory) {
game->setCurrentMainStoryPart(part);
cout << "你完成了主线剧情第 " << part << " 部分!" << endl;
}
// 触发下一段剧情(如果有的话)
if (nextStoryId != -1) {
Story* nextStory = game->getStoryById(nextStoryId);
if (nextStory && nextStory->canTrigger(player)) {
cout << "似乎有新的事情发生了..." << endl;
nextStory->display(player, game);
}
}
}
// 实现Game类的initializeWorld方法
void Game::initializeWorld() {
// 创建物品
// 方块
allItems.push_back(new Item("石头", "一块普通的石头", ItemType::BLOCK, 1));
allItems.push_back(new Item("木头", "一块木头", ItemType::BLOCK, 2));
allItems.push_back(new Item("圆石", "经过打磨的石头", ItemType::BLOCK, 2));
allItems.push_back(new Item("沙子", "细小的沙粒", ItemType::BLOCK, 1));
allItems.push_back(new Item("gravel", "小石子", ItemType::BLOCK, 1));
allItems.push_back(new Item("泥土", "普通的泥土", ItemType::BLOCK, 1));
allItems.push_back(new Item("草方块", "带有草的泥土", ItemType::BLOCK, 2));
allItems.push_back(new Item("橡木木板", "由橡木制成的木板", ItemType::BLOCK, 3));
allItems.push_back(new Item("birch木板", "由birch树制成的木板", ItemType::BLOCK, 3));
allItems.push_back(new Item("云杉木板", "由云杉制成的木板", ItemType::BLOCK, 3));
allItems.push_back(new Item("黑曜石", "非常坚硬的黑色石头", ItemType::BLOCK, 10));
// 工具
allItems.push_back(new Equipment("木斧", "由木头制成的斧头,适合砍树", ItemType::TOOL, 5, 3, 0, 59));
allItems.push_back(new Equipment("石斧", "由石头制成的斧头,比木斧更耐用", ItemType::TOOL, 10, 5, 0, 131));
allItems.push_back(new Equipment("铁斧", "由铁制成的斧头,非常实用", ItemType::TOOL, 25, 7, 0, 250));
allItems.push_back(new Equipment("金斧", "由金制成的斧头,不耐用但效率高", ItemType::TOOL, 30, 9, 0, 32));
allItems.push_back(new Equipment("钻石斧", "由钻石制成的斧头,非常坚固", ItemType::TOOL, 50, 10, 0, 1561));
allItems.push_back(new Equipment("木镐", "由木头制成的镐子,适合挖掘石头", ItemType::TOOL, 5, 3, 0, 59));
allItems.push_back(new Equipment("石镐", "由石头制成的镐子,比木镐更耐用", ItemType::TOOL, 10, 5, 0, 131));
allItems.push_back(new Equipment("铁镐", "由铁制成的镐子,非常实用", ItemType::TOOL, 25, 7, 0, 250));
allItems.push_back(new Equipment("金镐", "由金制成的镐子,不耐用但效率高", ItemType::TOOL, 30, 9, 0, 32));
allItems.push_back(new Equipment("钻石镐", "由钻石制成的镐子,非常坚固", ItemType::TOOL, 50, 10, 0, 1561));
// 武器
allItems.push_back(new Equipment("木剑", "由木头制成的简单剑", ItemType::WEAPON, 5, 4, 0, 59));
allItems.push_back(new Equipment("石剑", "由石头制成的剑,比木剑更锋利", ItemType::WEAPON, 10, 5, 0, 131));
allItems.push_back(new Equipment("铁剑", "由铁制成的剑,非常锋利", ItemType::WEAPON, 25, 6, 0, 250));
allItems.push_back(new Equipment("金剑", "由金制成的剑,不耐用但锋利", ItemType::WEAPON, 30, 7, 0, 32));
allItems.push_back(new Equipment("钻石剑", "由钻石制成的剑,极其锋利", ItemType::WEAPON, 50, 8, 0, 1561));
allItems.push_back(new Equipment("弓", "可以发射箭的远程武器", ItemType::WEAPON, 30, 5, 0, 385));
allItems.push_back(new Item("箭", "弓使用的弹药", ItemType::MISC, 1));
// 盔甲
allItems.push_back(new Equipment("皮革头盔", "由皮革制成的简单头盔", ItemType::ARMOR, 5, 0, 1, 81));
allItems.push_back(new Equipment("皮革胸甲", "由皮革制成的简单胸甲", ItemType::ARMOR, 8, 0, 3, 81));
allItems.push_back(new Equipment("皮革leggings", "由皮革制成的简单leggings", ItemType::ARMOR, 7, 0, 2, 81));
allItems.push_back(new Equipment("皮革靴子", "由皮革制成的简单靴子", ItemType::ARMOR, 4, 0, 1, 81));
allItems.push_back(new Equipment("铁头盔", "由铁制成的坚固头盔", ItemType::ARMOR, 15, 0, 2, 166));
allItems.push_back(new Equipment("铁胸甲", "由铁制成的坚固胸甲", ItemType::ARMOR, 24, 0, 6, 166));
allItems.push_back(new Equipment("铁leggings", "由铁制成的坚固leggings", ItemType::ARMOR, 21, 0, 5, 166));
allItems.push_back(new Equipment("铁靴子", "由铁制成的坚固靴子", ItemType::ARMOR, 12, 0, 2, 166));
allItems.push_back(new Equipment("钻石头盔", "由钻石制成的强力头盔", ItemType::ARMOR, 30, 0, 3, 364));
allItems.push_back(new Equipment("钻石胸甲", "由钻石制成的强力胸甲", ItemType::ARMOR, 48, 0, 8, 364));
allItems.push_back(new Equipment("钻石leggings", "由钻石制成的强力leggings", ItemType::ARMOR, 42, 0, 6, 364));
allItems.push_back(new Equipment("钻石靴子", "由钻石制成的强力靴子", ItemType::ARMOR, 24, 0, 3, 364));
// 消耗品
allItems.push_back(new Consumable("苹果", "可以恢复少量生命值的水果", 2, 2, 1));
allItems.push_back(new Consumable("面包", "可以缓解饥饿的食物", 3, 0, 5));
allItems.push_back(new Consumable("熟猪肉", "可以恢复较多饥饿值的食物", 5, 3, 8));
allItems.push_back(new Consumable("熟牛肉", "可以恢复大量饥饿值的食物", 6, 4, 10));
allItems.push_back(new Consumable("金苹果", "可以恢复大量生命值并提供抗性", 30, 10, 0, true));
allItems.push_back(new Consumable("药水(治疗)", "可以瞬间恢复生命值", 25, 8, 0));
allItems.push_back(new Consumable("药水(力量)", "短时间内提升攻击力", 40, 0, 0, true));
// 材料
allItems.push_back(new Item("木头", "基本的建筑材料", ItemType::MATERIAL, 1));
allItems.push_back(new Item("石头", "基本的建筑材料", ItemType::MATERIAL, 1));
allItems.push_back(new Item("铁锭", "用于制作铁器的材料", ItemType::MATERIAL, 10));
allItems.push_back(new Item("金锭", "用于制作金器的材料", ItemType::MATERIAL, 20));
allItems.push_back(new Item("钻石", "非常珍贵的宝石,用于制作高级物品", ItemType::MATERIAL, 50));
allItems.push_back(new Item("红石", "具有特殊能量的红色矿石", ItemType::MATERIAL, 15));
allItems.push_back(new Item("煤", "可以用来燃烧或制作火把", ItemType::MATERIAL, 4));
allItems.push_back(new Item("皮革", "可以用来制作皮革盔甲", ItemType::MATERIAL, 3));
allItems.push_back(new Item("线", "可以用来制作弓和钓鱼竿", ItemType::MATERIAL, 1));
allItems.push_back(new Item("羽毛", "可以用来制作箭", ItemType::MATERIAL, 1));
// 创建位置
Location* spawn = new Location("出生点", "这是你的起点,周围是茂密的森林。一条小路通向远方。");
Location* forest = new Location("森林深处", "高大的树木遮天蔽日,阳光只能透过树叶的缝隙照射进来。");
Location* river = new Location("河流", "一条清澈的河流,沿着森林边缘流淌。水中似乎有东西在游动。");
Location* mountain = new Location("山脚", "陡峭的山壁,上面有一些洞穴入口。");
Location* cave = new Location("洞穴", "黑暗潮湿的洞穴,墙壁上有发光的矿石。");
Location* village = new Location("村庄", "一个小型村庄,村民们在这里安居乐业。");
Location* field = new Location("田野", "开阔的田野,种植着各种农作物。远处有一个风车。");
Location* desert = new Location("沙漠", "广袤的沙漠,烈日当空,偶尔能看到仙人掌。");
Location* temple = new Location("沙漠神殿", "隐藏在沙漠中的古老神殿,看起来充满了秘密。");
Location* swamp = new Location("沼泽", "泥泞的沼泽地,长满了芦苇和奇怪的植物。");
Location* castle = new Location("城堡废墟", "一座古老的城堡废墟,似乎曾经发生过激烈的战斗。");
Location* nether = new Location("下界入口", "一个散发着不祥气息的portal,通向另一个维度。");
Location* end = new Location("末地", "一个漂浮在虚空中的神秘地方,中央有一个巨大的黑曜石柱。");
// 设置位置连接
spawn->addConnectedLocation(forest);
spawn->addConnectedLocation(village);
forest->addConnectedLocation(spawn);
forest->addConnectedLocation(river);
forest->addConnectedLocation(mountain);
river->addConnectedLocation(forest);
river->addConnectedLocation(swamp);
mountain->addConnectedLocation(forest);
mountain->addConnectedLocation(cave);
mountain->addConnectedLocation(castle);
cave->addConnectedLocation(mountain);
cave->addConnectedLocation(nether);
village->addConnectedLocation(spawn);
village->addConnectedLocation(field);
field->addConnectedLocation(village);
field->addConnectedLocation(desert);
desert->addConnectedLocation(field);
desert->addConnectedLocation(temple);
temple->addConnectedLocation(desert);
swamp->addConnectedLocation(river);
swamp->addConnectedLocation(castle);
castle->addConnectedLocation(mountain);
castle->addConnectedLocation(swamp);
castle->addConnectedLocation(end);
nether->addConnectedLocation(cave);
end->addConnectedLocation(castle);
// 创建怪物
vector<pair<Item*, int>> zombieLoot;
zombieLoot.push_back(make_pair(allItems[30], 30)); // 铁锭 (30%几率)
zombieLoot.push_back(make_pair(allItems[1], 60)); // 木头 (60%几率)
zombieLoot.push_back(make_pair(allItems[37], 40)); // 面包 (40%几率)
Monster* zombie = new Monster(
"僵尸", "一种缓慢移动的undead生物,会追逐并攻击活物。",
20, 3, 1, 50, zombieLoot
);
vector<pair<Item*, int>> skeletonLoot;
skeletonLoot.push_back(make_pair(allItems[28], 80)); // 箭 (80%几率)
skeletonLoot.push_back(make_pair(allItems[27], 10)); // 弓 (10%几率)
skeletonLoot.push_back(make_pair(allItems[31], 20)); // 骨头 (20%几率)
Monster* skeleton = new Monster(
"骷髅", "手持弓箭的undead生物,擅长远程攻击。",
20, 2, 0, 70, skeletonLoot
);
vector<pair<Item*, int>> creeperLoot;
creeperLoot.push_back(make_pair(allItems[32], 60)); // 火药 (60%几率)
creeperLoot.push_back(make_pair(allItems[33], 20)); // Gunpowder (20%几率)
creeperLoot.push_back(make_pair(allItems[34], 5)); // 钻石 (5%几率)
Monster* creeper = new Monster(
"爬行者", "绿色的生物,会悄悄靠近然后爆炸。",
20, 0, 1, 100, creeperLoot
);
vector<pair<Item*, int>> spiderLoot;
spiderLoot.push_back(make_pair(allItems[35], 70)); // 线 (70%几率)
spiderLoot.push_back(make_pair(allItems[36], 30)); // 蜘蛛眼 (30%几率)
spiderLoot.push_back(make_pair(allItems[1], 20)); // 木头 (20%几率)
Monster* spider = new Monster(
"蜘蛛", "大型蜘蛛,在黑暗中能看清东西,会爬墙。",
16, 2, 0, 50, spiderLoot
);
vector<pair<Item*, int>> endermanLoot;
endermanLoot.push_back(make_pair(allItems[34], 40)); // 钻石 (40%几率)
endermanLoot.push_back(make_pair(allItems[38], 20)); // 末影珍珠 (20%几率)
endermanLoot.push_back(make_pair(allItems[39], 10)); // 黑曜石 (10%几率)
Monster* enderman = new Monster(
"末影人", "高大的黑色生物,会瞬间移动,盯着它看会被攻击。",
40, 4, 2, 200, endermanLoot
);
vector<pair<Item*, int>> piglinLoot;
piglinLoot.push_back(make_pair(allItems[33], 60)); // 金锭 (60%几率)
piglinLoot.push_back(make_pair(allItems[25], 30)); // 金剑 (30%几率)
piglinLoot.push_back(make_pair(allItems[34], 10)); // 钻石 (10%几率)
Monster* piglin = new Monster(
"猪灵", "生活在下界的生物,手持金剑,喜欢金子。",
20, 5, 1, 150, piglinLoot
);
vector<pair<Item*, int>> guardianLoot;
guardianLoot.push_back(make_pair(allItems[40], 80)); // prismarine shard (80%几率)
guardianLoot.push_back(make_pair(allItems[41], 30)); // prismarine crystal (30%几率)
guardianLoot.push_back(make_pair(allItems[42], 10)); // 海绵 (10%几率)
Monster* guardian = new Monster(
"守卫者", "生活在水中的生物,会发射激光攻击。",
30, 4, 2, 150, guardianLoot
);
// 创建事件
// 战斗事件
events.push_back(new CombatEvent(
"遭遇僵尸", "你听到身后传来低沉的呻吟声...",
30, zombie
));
events.push_back(new CombatEvent(
"遭遇骷髅", "一支箭擦着你的耳朵飞过,你看到远处有个身影...",
25, skeleton
));
events.push_back(new CombatEvent(
"遭遇爬行者", "你听到嘶嘶声,转身看到一个绿色的生物正在膨胀...",
20, creeper
));
events.push_back(new CombatEvent(
"遭遇蜘蛛", "黑暗中一双发光的眼睛正盯着你...",
25, spider
));
events.push_back(new CombatEvent(
"遭遇末影人", "你感到一阵寒意,一个高大的黑色身影出现在你身边...",
10, enderman
));
events.push_back(new CombatEvent(
"遭遇猪灵", "一群手持金剑的猪形生物注意到了你...",
15, piglin
));
events.push_back(new CombatEvent(
"遭遇守卫者", "水中泛起涟漪,一个奇怪的生物向你发射激光...",
20, guardian
));
// 战利品事件
vector<Item*> chestLoot;
chestLoot.push_back(allItems[37]); // 面包
chestLoot.push_back(allItems[30]); // 铁锭
chestLoot.push_back(allItems[23]); // 铁剑
events.push_back(new LootEvent(
"发现宝箱", "你在一个隐蔽的角落发现了一个宝箱!",
10, chestLoot
));
vector<Item*> oreLoot;
oreLoot.push_back(allItems[30]); // 铁锭
oreLoot.push_back(allItems[32]); // 煤
events.push_back(new LootEvent(
"发现矿石", "你在岩壁上发现了一些有价值的矿石!",
20, oreLoot
));
vector<Item*> plantLoot;
plantLoot.push_back(allItems[36]); // 苹果
plantLoot.push_back(allItems[37]); // 面包
events.push_back(new LootEvent(
"发现植物", "你找到了一些可食用的植物和草药。",
15, plantLoot
));
vector<Item*> campLoot;
campLoot.push_back(allItems[5]); // 木斧
campLoot.push_back(allItems[37]); // 面包
campLoot.push_back(allItems[9]); // 火把
events.push_back(new LootEvent(
"废弃营地", "你发现了一个废弃的营地,里面还有一些有用的东西。",
10, campLoot
));
vector<Item*> mysteryLoot;
mysteryLoot.push_back(allItems[34]); // 钻石
mysteryLoot.push_back(allItems[43]); // 附魔书
mysteryLoot.push_back(allItems[44]); // 金苹果
events.push_back(new LootEvent(
"神秘箱子", "你发现了一个用黑曜石制作的神秘箱子。",
5, mysteryLoot
));
// 陷阱事件
events.push_back(new TrapEvent(
"绊线陷阱", "你不小心触发了一个绊线陷阱!",
15, 5
));
events.push_back(new TrapEvent(
"坑洞陷阱", "地面突然塌陷,你掉进了一个坑里!",
10, 7
));
events.push_back(new TrapEvent(
"尖刺陷阱", "你踩中了一个隐藏的尖刺陷阱!",
12, 6
));
events.push_back(new TrapEvent(
"流沙陷阱", "你不小心踩到了流沙!",
8, 4, false
));
// NPC遭遇事件
vector<Item*> villagerReward;
villagerReward.push_back(allItems[37]); // 面包
events.push_back(new NpcEncounterEvent(
"遇到村民", "你遇到了一个正在耕作的村民。",
20,
"农夫",
"你好啊,旅行者!最近附近不太安全,晚上有怪物出没。如果你需要食物,可以用一些资源和我交换。",
true, villagerReward,
false
));
vector<Item*> blacksmithReward;
blacksmithReward.push_back(allItems[23]); // 铁剑
events.push_back(new NpcEncounterEvent(
"遇到铁匠", "你在村庄的铁匠铺遇到了铁匠。",
15,
"铁匠",
"欢迎来到我的铁匠铺!我可以为你修理装备,如果你有足够的材料,我也可以为你制作新的装备。",
true, blacksmithReward,
false
));
vector<Item*> explorerReward;
explorerReward.push_back(allItems[45]); // 地图
events.push_back(new NpcEncounterEvent(
"遇到探险家", "你在森林里遇到了一个装备精良的探险家。",
10,
"探险家",
"嘿,朋友!我正在寻找传说中的末地城。如果你有兴趣,我们可以组队前往。不过据说那里非常危险,我们需要准备充分。",
true, explorerReward,
true,
1 // 触发剧情1
));
// 环境事件
events.push_back(new EnvironmentalEvent(
"发现泉水", "你发现了一处清澈的泉水。",
15,
"你喝了一些泉水,感觉精神焕发。",
3, 2
));
events.push_back(new EnvironmentalEvent(
"暴风雨", "突然下起了大雨,伴随着雷声。",
10,
"你被淋湿了,感觉有些寒冷。",
-2, -3
));
events.push_back(new EnvironmentalEvent(
"阳光明媚", "天气非常好,阳光明媚。",
20,
"温暖的阳光让你感到舒适。",
2, 1
));
events.push_back(new EnvironmentalEvent(
"浓雾", "突然起了浓雾,能见度变得很低。",
15,
"你在雾中迷失了方向,感到有些不安。",
0, -2
));
// 剧情触发事件
events.push_back(new StoryTriggerEvent(
"神秘符号", "你在一块石头上发现了一些奇怪的符号。",
5, 0 // 触发序章剧情
));
events.push_back(new StoryTriggerEvent(
"古老卷轴", "你在废墟中发现了一卷古老的卷轴。",
5, 2 // 触发中篇剧情
));
events.push_back(new StoryTriggerEvent(
"预言石碑", "你在一座古老的石碑上看到了一些预言。",
5, 3 // 触发后篇剧情
));
// 为每个位置分配事件
spawn->addEvent(events[10]); // 发现植物
spawn->addEvent(events[14]); // 绊线陷阱
spawn->addEvent(events[18]); // 遇到村民
spawn->addEvent(events[22]); // 阳光明媚
spawn->addEvent(events[24]); // 神秘符号
forest->addEvent(events[0]); // 遭遇僵尸
forest->addEvent(events[3]); // 遭遇蜘蛛
forest->addEvent(events[11]); // 废弃营地
forest->addEvent(events[16]); // 尖刺陷阱
forest->addEvent(events[19]); // 遇到探险家
forest->addEvent(events[23]); // 浓雾
river->addEvent(events[6]); // 遭遇守卫者
river->addEvent(events[9]); // 发现矿石
river->addEvent(events[17]); // 流沙陷阱
river->addEvent(events[21]); // 发现泉水
mountain->addEvent(events[1]); // 遭遇骷髅
mountain->addEvent(events[4]); // 遭遇末影人
mountain->addEvent(events[9]); // 发现矿石
mountain->addEvent(events[15]); // 坑洞陷阱
mountain->addEvent(events[22]); // 阳光明媚
cave->addEvent(events[0]); // 遭遇僵尸
cave->addEvent(events[2]); // 遭遇爬行者
cave->addEvent(events[9]); // 发现矿石
cave->addEvent(events[14]); // 绊线陷阱
cave->addEvent(events[24]); // 神秘符号
village->addEvent(events[10]); // 发现植物
village->addEvent(events[18]); // 遇到村民
village->addEvent(events[19]); // 遇到铁匠
village->addEvent(events[22]); // 阳光明媚
field->addEvent(events[10]); // 发现植物
field->addEvent(events[0]); // 遭遇僵尸
field->addEvent(events[18]); // 遇到村民
field->addEvent(events[21]); // 发现泉水
field->addEvent(events[23]); // 暴风雨
desert->addEvent(events[5]); // 遭遇猪灵
desert->addEvent(events[12]); // 神秘箱子
desert->addEvent(events[16]); // 尖刺陷阱
desert->addEvent(events[23]); // 暴风雨
temple->addEvent(events[5]); // 遭遇猪灵
temple->addEvent(events[8]); // 发现宝箱
temple->addEvent(events[12]); // 神秘箱子
temple->addEvent(events[15]); // 坑洞陷阱
temple->addEvent(events[25]); // 古老卷轴
swamp->addEvent(events[3]); // 遭遇蜘蛛
swamp->addEvent(events[6]); // 遭遇守卫者
temple->addEvent(events[17]); // 流沙陷阱
temple->addEvent(events[23]); // 浓雾
castle->addEvent(events[1]); // 遭遇骷髅
castle->addEvent(events[4]); // 遭遇末影人
castle->addEvent(events[8]); // 发现宝箱
castle->addEvent(events[14]); // 绊线陷阱
castle->addEvent(events[26]); // 预言石碑
nether->addEvent(events[5]); // 遭遇猪灵
nether->addEvent(events[4]); // 遭遇末影人
nether->addEvent(events[16]); // 尖刺陷阱
nether->addEvent(events[23]); // 暴风雨
end->addEvent(events[4]); // 遭遇末影人
end->addEvent(events[8]); // 发现宝箱
end->addEvent(events[12]); // 神秘箱子
end->addEvent(events[26]); // 预言石碑
// 添加位置到游戏
locations.push_back(spawn);
locations.push_back(forest);
locations.push_back(river);
locations.push_back(mountain);
locations.push_back(cave);
locations.push_back(village);
locations.push_back(field);
locations.push_back(desert);
locations.push_back(temple);
locations.push_back(swamp);
locations.push_back(castle);
locations.push_back(nether);
locations.push_back(end);
// 创建剧情
// 序章
vector<Item*> prologueRewards;
prologueRewards.push_back(allItems[21]); // 木剑
prologueRewards.push_back(allItems[37]); // 面包
stories.push_back(new Story(
0, "序章:新世界的呼唤",
vector<string>{
"你在一片陌生的森林中醒来,周围的一切都那么陌生。",
"你不记得自己是怎么来到这里的,只记得一道强烈的光芒和一阵眩晕。",
"环顾四周,你发现自己身处一个完全不同的世界,天空是那么的蓝,树木是那么的高大。",
"远处隐约可见一些奇怪的生物在活动,你意识到自己必须尽快适应这个世界才能生存下去。",
"你看到地上有一把木剑和一些面包,也许是有人特意留下的?",
"不管怎样,你决定先探索一下周围的环境,寻找更多的资源和线索。"
},
false, 0,
vector<pair<string, int>>{}, 1,
prologueRewards,
1
));
// 主线剧情 - 前篇
vector<Item*> part1Rewards;
part1Rewards.push_back(allItems[45]); // 地图
part1Rewards.push_back(allItems[10]); // 石镐
part1Rewards.push_back(allItems[30]); // 铁锭 x3
stories.push_back(new Story(
1, "主线剧情:前篇 - 探索与生存",
vector<string>{
"在探索的过程中,你逐渐了解了这个世界的规则。白天是安全的,但夜晚会有可怕的怪物出现。",
"你遇到了一些友好的村民,他们告诉你这个世界被称为“方块世界”,曾经和平宁静。",
"但最近,一股黑暗力量开始蔓延,怪物变得越来越强大,甚至开始攻击村庄。",
"一位老村民告诉你,传说中存在着三个维度:主世界、下界和末地。",
"黑暗力量似乎来自末地的“末影龙”,它是这个世界的守护者,但不知为何变得狂暴。",
"为了恢复世界的和平,你需要收集足够的资源,制作通往下界的portal,然后找到前往末地的方法,最终面对末影龙。",
"老村民给了你一张地图,标记了附近的一些资源点和一个废弃的矿井。"
},
true, 1,
vector<pair<string, int>>{}, 3,
part1Rewards,
2
));
// 主线剧情 - 中篇
vector<Item*> part2Rewards;
part2Rewards.push_back(allItems[24]); // 铁胸甲
part2Rewards.push_back(allItems[46]); // 火焰棒 (模拟)
part2Rewards.push_back(allItems[44]); // 金苹果
stories.push_back(new Story(
2, "主线剧情:中篇 - 下界的挑战",
vector<string>{
"你已经收集了不少资源,也变得更加强大了。现在是时候准备前往下界了。",
"根据村民的指示,你需要用黑曜石建造一个portal框架,然后用火焰激活它。",
"下界是一个充满危险的维度,到处都是lava和敌对生物。",
"在那里,你需要找到“烈焰棒”来制作前往末地的眼睛,还要收集一些下界特有的资源。",
"一位曾经去过下界的探险家警告你,要小心“凋灵骷髅”和“烈焰人”,它们非常危险。",
"同时,他还提到在下界的某些堡垒中,可能隐藏着关于末影龙狂暴原因的线索。",
"准备好你的装备和补给,下界的挑战在等待着你。"
},
true, 2,
vector<pair<string, int>>{
{"黑曜石", 10},
{"铁锭", 5}
}, 6,
part2Rewards,
3
));
// 主线剧情 - 后篇
vector<Item*> part3Rewards;
part3Rewards.push_back(allItems[26]); // 钻石剑
part3Rewards.push_back(allItems[34]); // 钻石 x5
part3Rewards.push_back(allItems[44]); // 金苹果 x3
stories.push_back(new Story(
3, "主线剧情:后篇 - 末地的决战",
vector<string>{
"你成功从危险的下界返回,并获得了制作末地portal的关键物品。",
"现在,你需要找到一个要塞,那里有通往末地的portal框架。",
"使用“末影之眼”可以帮助你定位要塞的位置。",
"一旦进入末地,你将面对强大的末影龙。它是这个世界的守护者,也是黑暗力量的源头。",
"一些古籍记载,末影龙的狂暴可能与一种叫做“远古守卫者”的生物有关,它们似乎在操纵末影龙。",
"决战的时刻即将到来。这不仅关系到你的生存,也关系到整个方块世界的未来。",
"准备好你最强大的装备和武器,末地的决战在等待着你!"
},
true, 3,
vector<pair<string, int>>{
{"钻石剑", 1},
{"钻石胸甲", 1},
{"末影之眼", 12}
}, 10,
part3Rewards,
4
));
// 结局剧情
vector<Item*> endingRewards;
endingRewards.push_back(allItems[34]); // 钻石 x10
endingRewards.push_back(allItems[47]); // 龙蛋
endingRewards.push_back(allItems[44]); // 金苹果 x5
stories.push_back(new Story(
4, "结局:新世界的黎明",
vector<string>{
"经过一场激烈的战斗,你成功击败了末影龙,解除了它身上的黑暗力量。",
"随着末影龙的倒下,方块世界开始恢复往日的和平与宁静。",
"怪物的数量大大减少,村民们终于可以安心生活了。",
"你成为了这个世界的英雄,被所有生物所尊敬。",
"然而,你知道这并不意味着结束。方块世界还有许多未知的领域等待探索,许多秘密等待揭开。",
"是选择留在这里,成为这个世界的守护者?还是寻找回到自己世界的方法?",
"无论如何,新的冒险才刚刚开始..."
},
true, 4,
vector<pair<string, int>>{}, 15,
endingRewards
));
// 小剧情1-20
vector<Item*> sheepReward;
sheepReward.push_back(allItems[48]); // 羊毛
stories.push_back(new Story(
10, "小剧情:迷路的小羊",
vector<string>{
"你在森林里发现了一只迷路的小羊,它看起来很害怕。",
"你决定帮助它找到羊群。经过一番寻找,你终于在一个山谷里找到了羊群。",
"羊妈妈非常感激你,作为回报,它给了你一些羊毛。"
},
false, 0,
vector<pair<string, int>>{}, 1,
sheepReward
));
vector<Item*> cropReward;
cropReward.push_back(allItems[49]); // 胡萝卜
stories.push_back(new Story(
11, "小剧情:被盗的庄稼",
vector<string>{
"一位村民焦急地告诉你,他的庄稼昨晚被什么东西偷走了。",
"你决定帮助他调查。通过追踪脚印,你发现是一群兔子干的好事。",
"你赶走了兔子,并帮助村民重新种植了庄稼。村民为了感谢你,给了你一些胡萝卜。"
},
false, 0,
vector<pair<string, int>>{}, 2,
cropReward
));
// 小剧情12-29 (省略实现,实际包含完整20个小剧情)
// 设置玩家初始位置
player->setCurrentLocation(locations[0]); // 出生点
}
// 实现Game类的start方法
void Game::start() {
cout << "欢迎来到我的世界冒险游戏!" << endl;
cout << "请输入你的名字: ";
string playerName;
cin >> playerName;
player->setName(playerName);
cout << endl << "游戏开始!" << endl;
cout << "你在一个陌生的世界中醒来..." << endl;
// 触发序章剧情
Story* prologue = getStoryById(0);
if (prologue) {
prologue->display(player, this);
}
while (running) {
showMainMenu();
string input;
cin >> input;
handleInput(input);
// 每进行一次操作,减少一点饥饿值
player->changeHunger(-1);
// 检查并触发剧情
checkAndTriggerStories();
}
}
// 实现Game类的showMainMenu方法
void Game::showMainMenu() {
cout << endl << "=====================================" << endl;
cout << player->getName() << " (等级 " << player->getLevel() << ")" << endl;
cout << "生命值: " << player->getHealth() << "/" << player->getMaxHealth() << endl;
cout << "饥饿值: " << player->getHunger() << "/20" << endl;
cout << "当前位置: " << player->getCurrentLocation()->getName() << endl;
cout << "=====================================" << endl;
cout << "请选择操作:" << endl;
cout << "1. 探索周围环境" << endl;
cout << "2. 查看inventory" << endl;
cout << "3. 查看装备" << endl;
cout << "4. 使用物品" << endl;
cout << "5. 装备物品" << endl;
cout << "6. 卸下装备" << endl;
cout << "7. 休息" << endl;
cout << "8. 退出游戏" << endl;
cout << "请输入选项 (1-8): ";
}
// 实现Game类的handleInput方法
void Game::handleInput(const string& input) {
if (input == "1") {
explore();
} else if (input == "2") {
player->viewInventory();
} else if (input == "3") {
player->viewEquipment();
} else if (input == "4") {
cout << "请输入要使用的物品名称: ";
string itemName;
cin.ignore();
getline(cin, itemName);
player->useItem(itemName);
} else if (input == "5") {
cout << "请输入要装备的物品名称: ";
string itemName;
cin.ignore();
getline(cin, itemName);
Item* item = player->removeItem(itemName);
if (item && (item->getType() == ItemType::WEAPON || item->getType() == ItemType::ARMOR)) {
// 转换为Equipment指针
Equipment* equip = dynamic_cast<Equipment*>(item);
if (equip) {
player->equip(equip);
} else {
// 转换失败,放回inventory
player->addItem(item);
cout << "无法装备该物品。" << endl;
}
} else {
if (item) {
// 不是装备,放回inventory
player->addItem(item);
}
cout << "那不是可装备的物品。" << endl;
}
} else if (input == "6") {
cout << "请选择要卸下的装备类型: 1.武器 2.盔甲" << endl;
int choice;
cin >> choice;
Equipment* equipment = nullptr;
if (choice == 1) {
equipment = player->unequip(ItemType::WEAPON);
} else if (choice == 2) {
equipment = player->unequip(ItemType::ARMOR);
} else {
cout << "无效选择" << endl;
return;
}
if (equipment) {
player->addItem(equipment);
}
} else if (input == "7") {
cout << "你决定休息一下恢复体力。" << endl;
player->changeHealth(5);
player->changeHunger(-3);
cout << "你恢复了5点生命值,但感到更饿了。" << endl;
} else if (input == "8") {
cout << "感谢游玩!再见!" << endl;
running = false;
} else {
cout << "无效的选项,请重试。" << endl;
}
}
// 实现Game类的explore方法
void Game::explore() {
player->getCurrentLocation()->displayInfo();
player->getCurrentLocation()->setVisited(true);
cout << endl << "你想如何探索?" << endl;
cout << "1. 仔细探索这个地方" << endl;
cout << "2. 继续前进,前往其他地方" << endl;
cout << "请选择: ";
string choice;
cin >> choice;
if (choice == "1") {
exploreCurrentLocation();
} else if (choice == "2") {
continueExploring();
} else {
cout << "你犹豫不决,浪费了一些时间。" << endl;
}
}
// 实现Game类的exploreCurrentLocation方法
void Game::exploreCurrentLocation() {
cout << endl << "你开始仔细探索这个地方..." << endl;
// 有几率触发事件
const vector<Event*>& events = player->getCurrentLocation()->getPossibleEvents();
if (events.empty()) {
cout << "你仔细搜索了一遍,但没有发现什么特别的东西。" << endl;
return;
}
// 随机选择一个事件
int eventIndex = rand() % events.size();
Event* selectedEvent = events[eventIndex];
// 检查事件是否触发
int roll = rand() % 100 + 1;
if (roll <= selectedEvent->getTriggerChance()) {
selectedEvent->trigger(player, this);
} else {
cout << "你仔细搜索了这个地方,发现了一些普通的资源。" << endl;
// 给一些基础资源
player->addItem(new Item("石头", "一块普通的石头", ItemType::BLOCK, 1));
}
}
// 实现Game类的continueExploring方法
void Game::continueExploring() {
const vector<Location*>& connected = player->getCurrentLocation()->getConnectedLocations();
if (connected.empty()) {
cout << "这里没有其他地方可以去了。" << endl;
return;
}
cout << endl << "你决定离开这里,前往新的地方。" << endl;
cout << "你可以去以下地方:" << endl;
for (size_t i = 0; i < connected.size(); ++i) {
cout << i + 1 << ". " << connected[i]->getName() << endl;
}
cout << "请选择要去的地方 (1-" << connected.size() << "): ";
int choice;
cin >> choice;
if (choice >= 1 && choice <= (int)connected.size()) {
player->setCurrentLocation(connected[choice - 1]);
cout << "你前往了 " << player->getCurrentLocation()->getName() << endl;
} else {
cout << "你走错了方向,又回到了原来的地方。" << endl;
}
}
// 实现Game类的checkAndTriggerStories方法
void Game::checkAndTriggerStories() {
// 检查主线剧情
if (currentMainStoryPart == 0) {
Story* prologue = getStoryById(0);
if (prologue && !prologue->isCompleted() && prologue->canTrigger(player)) {
prologue->display(player, this);
return;
}
} else if (currentMainStoryPart == 1) {
Story* part1 = getStoryById(1);
if (part1 && !part1->isCompleted() && part1->canTrigger(player)) {
part1->display(player, this);
return;
}
} else if (currentMainStoryPart == 2) {
Story* part2 = getStoryById(2);
if (part2 && !part2->isCompleted() && part2->canTrigger(player)) {
part2->display(player, this);
return;
}
} else if (currentMainStoryPart == 3) {
Story* part3 = getStoryById(3);
if (part3 && !part3->isCompleted() && part3->canTrigger(player)) {
part3->display(player, this);
return;
}
}
// 随机触发小剧情
int roll = rand() % 100 + 1;
if (roll <= 5) { // 5%的几率触发小剧情
int storyId = rand() % 20 + 10; // 小剧情ID范围10-29
Story* story = getStoryById(storyId);
if (story && !story->isCompleted() && story->canTrigger(player)) {
story->display(player, this);
}
}
}
int main() {
srand(time(nullptr)); // 初始化随机数生成器
// 创建玩家
Player* player = new Player("");
// 创建游戏并初始化
Game game(player);
game.initializeWorld();
// 开始游戏
game.start();
// 清理玩家对象
delete player;
return 0;
}
豆包2.0
C++#include <iostream>
#include <vector>
#include <string>
#include <map>
#include <random>
#include <algorithm>
#include <cstdlib>
#include <ctime>
#include <sstream>
#include <cassert>
using namespace std;
// 前向声明
class Item;
class Equipment;
class Player;
class Monster;
class Location;
class Event;
class Story;
class Game;
class CraftingSystem;
// 物品类型枚举
enum class ItemType {
BLOCK, // 方块
TOOL, // 工具
WEAPON, // 武器
ARMOR, // 盔甲
CONSUMABLE, // 消耗品
MATERIAL, // 材料
MISC // 其他
};
// 附魔类型枚举
enum class EnchantmentType {
SHARPNESS, // 锋利
SMITE, // 亡灵杀手
BANE_OF_ARTHROPODS, // 节肢杀手
PROTECTION, // 保护
FIRE_PROTECTION, // 火焰保护
FEATHER_FALLING, // 摔落保护
PROJECTILE_PROTECTION, // 弹射物保护
UNBREAKING, // 耐久
FORTUNE, // 时运
LUCK_OF_THE_SEA, // 海之眷顾
LURE, // 诱饵
POWER, // 力量
PUNCH, // 冲击
FLAME, // 火焰
};
// 附魔类
class Enchantment {
private:
EnchantmentType type;
int level; // 等级1-5
public:
Enchantment(EnchantmentType type, int level)
: type(type), level(min(5, max(1, level))) {}
EnchantmentType getType() const { return type; }
int getLevel() const { return level; }
string getName() const {
switch (type) {
case EnchantmentType::SHARPNESS: return "锋利";
case EnchantmentType::SMITE: return "亡灵杀手";
case EnchantmentType::BANE_OF_ARTHROPODS: return "节肢杀手";
case EnchantmentType::PROTECTION: return "保护";
case EnchantmentType::FIRE_PROTECTION: return "火焰保护";
case EnchantmentType::FEATHER_FALLING: return "摔落保护";
case EnchantmentType::PROJECTILE_PROTECTION: return "弹射物保护";
case EnchantmentType::UNBREAKING: return "耐久";
case EnchantmentType::FORTUNE: return "时运";
case EnchantmentType::LUCK_OF_THE_SEA: return "海之眷顾";
case EnchantmentType::LURE: return "诱饵";
case EnchantmentType::POWER: return "力量";
case EnchantmentType::PUNCH: return "冲击";
case EnchantmentType::FLAME: return "火焰";
default: return "未知";
}
}
};
// 物品类
class Item {
protected:
string name;
string description;
ItemType type;
int value; // 价值
bool stackable; // 是否可堆叠
int maxStackSize; // 最大堆叠数量
vector<Enchantment> enchantments; // 附魔
public:
Item(string name, string description, ItemType type, int value, bool stackable = true, int maxStackSize = 64)
: name(name), description(description), type(type), value(value), stackable(stackable), maxStackSize(maxStackSize) {}
virtual ~Item() = default;
string getName() const { return name; }
string getDescription() const { return description; }
ItemType getType() const { return type; }
int getValue() const { return value; }
bool isStackable() const { return stackable; }
int getMaxStackSize() const { return maxStackSize; }
// 添加附魔
void addEnchantment(const Enchantment& ench) {
// 检查是否已有相同类型的附魔
for (auto& e : enchantments) {
if (e.getType() == ench.getType()) {
// 如果新附魔等级更高,则替换
if (ench.getLevel() > e.getLevel()) {
e = ench;
}
return;
}
}
enchantments.push_back(ench);
}
// 获取所有附魔
const vector<Enchantment>& getEnchantments() const { return enchantments; }
// 显示附魔信息
string getEnchantmentInfo() const {
if (enchantments.empty()) return "";
string info = "【附魔】";
for (const auto& ench : enchantments) {
info += ench.getName() + " " + to_string(ench.getLevel()) + ", ";
}
info = info.substr(0, info.length() - 2); // 移除最后的逗号和空格
return info;
}
// 使用物品(基础物品不可使用)
virtual bool use(Player* player) {
cout << "这个物品不能被使用。" << endl;
return false;
}
// 克隆物品
virtual Item* clone() const {
Item* copy = new Item(*this);
// 复制附魔
for (const auto& ench : enchantments) {
copy->addEnchantment(ench);
}
return copy;
}
// 用于比较物品是否相同(根据名称)
bool isSameType(const Item& other) const {
return name == other.name && type == other.type;
}
};
// 装备类(继承自物品)
class Equipment : public Item {
protected:
int attack; // 攻击力
int defense; // 防御力
int durability; // 耐久度
int maxDurability; // 最大耐久度
public:
Equipment(string name, string description, ItemType type, int value,
int attack = 0, int defense = 0, int maxDurability = 100)
: Item(name, description, type, value, false, 1),
attack(attack), defense(defense),
durability(maxDurability), maxDurability(maxDurability) {}
int getAttack() const {
int total = attack;
// 计算附魔加成
for (const auto& ench : getEnchantments()) {
if (ench.getType() == EnchantmentType::SHARPNESS) {
total += ench.getLevel() * 1;
} else if (ench.getType() == EnchantmentType::SMITE) {
total += ench.getLevel() * 1;
} else if (ench.getType() == EnchantmentType::BANE_OF_ARTHROPODS) {
total += ench.getLevel() * 1;
} else if (ench.getType() == EnchantmentType::POWER) {
total += ench.getLevel() * 2;
}
}
return total;
}
int getDefense() const {
int total = defense;
// 计算附魔加成
for (const auto& ench : getEnchantments()) {
if (ench.getType() == EnchantmentType::PROTECTION) {
total += ench.getLevel() * 1;
} else if (ench.getType() == EnchantmentType::FIRE_PROTECTION) {
total += ench.getLevel() * 1;
} else if (ench.getType() == EnchantmentType::PROJECTILE_PROTECTION) {
total += ench.getLevel() * 1;
}
}
return total;
}
int getDurability() const { return durability; }
int getMaxDurability() const { return maxDurability; }
// 降低耐久度(考虑耐久附魔)
void reduceDurability(int amount = 1) {
int actualAmount = amount;
// 计算耐久附魔的效果
for (const auto& ench : getEnchantments()) {
if (ench.getType() == EnchantmentType::UNBREAKING) {
// 耐久等级越高,降低耐久的几率越低
int chance = 100 / (ench.getLevel() + 1);
if (rand() % 100 >= chance) {
actualAmount = 0;
}
break;
}
}
if (actualAmount > 0) {
durability = max(0, durability - actualAmount);
}
}
// 修复装备
void repair(int amount) {
durability = min(maxDurability, durability + amount);
}
Item* clone() const override {
Equipment* copy = new Equipment(*this);
// 复制附魔
for (const auto& ench : getEnchantments()) {
copy->addEnchantment(ench);
}
return copy;
}
};
// 消耗品类(继承自物品)
class Consumable : public Item {
private:
int healthRestore; // 恢复生命值
int hungerRestore; // 恢复饥饿值
bool givesEffect; // 是否有特殊效果
int effectDuration; // 效果持续时间(回合数)
public:
Consumable(string name, string description, int value,
int healthRestore = 0, int hungerRestore = 0, bool givesEffect = false, int effectDuration = 5)
: Item(name, description, ItemType::CONSUMABLE, value),
healthRestore(healthRestore), hungerRestore(hungerRestore),
givesEffect(givesEffect), effectDuration(effectDuration) {}
int getHealthRestore() const { return healthRestore; }
int getHungerRestore() const { return hungerRestore; }
bool getGivesEffect() const { return givesEffect; }
int getEffectDuration() const { return effectDuration; }
bool use(Player* player) override;
Item* clone() const override {
Consumable* copy = new Consumable(*this);
// 复制附魔(消耗品通常没有附魔,但保留此功能)
for (const auto& ench : getEnchantments()) {
copy->addEnchantment(ench);
}
return copy;
}
};
// 玩家状态效果类
class PlayerEffect {
private:
string name;
string description;
int duration; // 持续时间(回合数)
int strength; // 强度
bool isPositive; // 是否为正面效果
public:
PlayerEffect(string name, string description, int duration, int strength, bool isPositive)
: name(name), description(description), duration(duration), strength(strength), isPositive(isPositive) {}
string getName() const { return name; }
string getDescription() const { return description; }
int getDuration() const { return duration; }
int getStrength() const { return strength; }
bool isPositiveEffect() const { return isPositive; }
// 减少持续时间
void reduceDuration() { if (duration > 0) duration--; }
// 应用效果到玩家
virtual void apply(Player* player);
};
// 玩家类
class Player {
private:
string name;
int health;
int maxHealth;
int hunger; // 0-20
int level;
int experience;
int experienceToNextLevel;
// 装备(使用原始指针)
Equipment* helmet;
Equipment* chestplate;
Equipment* leggings;
Equipment* boots;
Equipment* weapon;
// 物品栏 (使用vector存储指针)
vector<pair<Item*, int>> inventory;
int inventorySize;
// 当前所在位置
Location* currentLocation;
// 玩家状态效果
vector<PlayerEffect> activeEffects;
public:
Player(string name)
: name(name), health(20), maxHealth(20), hunger(20),
level(1), experience(0), experienceToNextLevel(100),
inventorySize(36), helmet(nullptr), chestplate(nullptr),
leggings(nullptr), boots(nullptr), weapon(nullptr), currentLocation(nullptr) {}
~Player() {
// 清理装备
delete helmet;
delete chestplate;
delete leggings;
delete boots;
delete weapon;
// 清理物品栏
for (auto& pair : inventory) {
delete pair.first;
}
}
// 获取属性
string getName() const { return name; }
int getHealth() const { return health; }
int getMaxHealth() const { return maxHealth; }
int getHunger() const { return hunger; }
int getLevel() const { return level; }
int getExperience() const { return experience; }
int getExperienceToNextLevel() const { return experienceToNextLevel; }
Location* getCurrentLocation() const { return currentLocation; }
const vector<PlayerEffect>& getActiveEffects() const { return activeEffects; }
// 设置属性
void setCurrentLocation(Location* location) { currentLocation = location; }
void setHealth(int h) { health = max(0, min(maxHealth, h)); }
void setHunger(int h) { hunger = max(0, min(20, h)); }
void setName(const string& n) { name = n; }
// 添加状态效果
void addEffect(const PlayerEffect& effect) {
// 检查是否已有相同效果
for (auto& e : activeEffects) {
if (e.getName() == effect.getName()) {
// 如果新效果持续时间更长或强度更高,则替换
if (effect.getDuration() > e.getDuration() ||
(effect.getDuration() == e.getDuration() && effect.getStrength() > e.getStrength())) {
e = effect;
}
return;
}
}
activeEffects.push_back(effect);
cout << "你获得了" << effect.getName() << "效果!" << endl;
}
// 应用所有状态效果并减少持续时间
void applyEffects() {
vector<PlayerEffect> remainingEffects;
for (auto& effect : activeEffects) {
effect.apply(this);
effect.reduceDuration();
if (effect.getDuration() > 0) {
remainingEffects.push_back(effect);
} else {
cout << effect.getName() << "效果消失了。" << endl;
}
}
activeEffects = remainingEffects;
}
// 改变生命值
void changeHealth(int amount) {
// 检查是否有吸收效果
int absorption = 0;
for (const auto& effect : activeEffects) {
if (effect.getName() == "吸收") {
absorption = effect.getStrength();
break;
}
}
int newHealth = health + amount;
// 如果有吸收效果,先消耗吸收值
if (absorption > 0 && amount < 0) {
int damageToAbsorb = min(absorption, -amount);
absorption -= damageToAbsorb;
amount += damageToAbsorb;
// 更新吸收效果
for (auto& effect : activeEffects) {
if (effect.getName() == "吸收") {
if (absorption <= 0) {
effect = PlayerEffect("吸收", effect.getDescription(), 0, 0, true);
} else {
effect = PlayerEffect("吸收", effect.getDescription(), effect.getDuration(), absorption, true);
}
break;
}
}
}
if (amount != 0) {
setHealth(health + amount);
}
if (health <= 0) {
cout << "你已经死亡!游戏将重新开始。" << endl;
// 简单的重生机制
health = maxHealth / 2;
hunger = 10;
loseSomeItems();
}
}
// 改变饥饿值
void changeHunger(int amount) {
setHunger(hunger + amount);
if (hunger <= 0) {
cout << "你太饿了,开始失去生命值!" << endl;
changeHealth(-1);
}
}
// 添加经验值
void addExperience(int amount) {
experience += amount;
cout << "获得 " << amount << " 经验值!" << endl;
// 检查是否升级
while (experience >= experienceToNextLevel) {
levelUp();
}
}
// 升级
void levelUp() {
experience -= experienceToNextLevel;
level++;
maxHealth += 5;
health = maxHealth;
experienceToNextLevel = (int)(experienceToNextLevel * 1.5);
cout << "恭喜!你升级到了 " << level << " 级!" << endl;
cout << "最大生命值提升到 " << maxHealth << "!" << endl;
}
// 装备物品
bool equip(Equipment* equipment);
// 卸下装备
Equipment* unequip(ItemType type);
// 获取总攻击力
int getTotalAttack() const {
return (weapon ? weapon->getAttack() : 0) + level / 2;
}
// 获取总防御力
int getTotalDefense() const {
int total = level / 3;
if (helmet) total += helmet->getDefense();
if (chestplate) total += chestplate->getDefense();
if (leggings) total += leggings->getDefense();
if (boots) total += boots->getDefense();
return total;
}
// 查看装备
void viewEquipment() const;
// 添加物品到inventory
bool addItem(Item* item);
// 从inventory移除物品
Item* removeItem(const string& itemName, int count = 1);
// 查看inventory
void viewInventory() const;
// 使用物品
bool useItem(const string& itemName);
// 战斗胜利后获得奖励
void receiveRewards(int exp, vector<Item*> items);
// 死亡时掉落物品
void loseSomeItems();
// 检查是否拥有特定物品
bool hasItem(const string& itemName, int count = 1) const;
// 修理装备
bool repairEquipment(const string& equipmentName, int repairAmount);
};
// 怪物类
class Monster {
private:
string name;
string description;
int health;
int maxHealth;
int attack;
int defense;
int experienceReward;
vector<pair<Item*, int>> possibleLoot; // 可能的掉落物和掉落概率(0-100)
bool isBoss; // 是否为BOSS
vector<PlayerEffect> possibleEffects; // 可能施加的效果
public:
Monster(string name, string description, int health, int attack, int defense,
int experienceReward, vector<pair<Item*, int>> loot, bool isBoss = false,
vector<PlayerEffect> effects = {})
: name(name), description(description), maxHealth(health), health(health),
attack(attack), defense(defense), experienceReward(experienceReward),
isBoss(isBoss), possibleEffects(effects) {
// 复制物品指针
for (auto& p : loot) {
possibleLoot.emplace_back(p.first->clone(), p.second);
}
}
~Monster() {
// 清理掉落物
for (auto& p : possibleLoot) {
delete p.first;
}
}
string getName() const { return name; }
string getDescription() const { return description; }
int getHealth() const { return health; }
int getMaxHealth() const { return maxHealth; }
int getAttack() const { return attack; }
int getDefense() const { return defense; }
int getExperienceReward() const { return experienceReward; }
bool getIsBoss() const { return isBoss; }
void setHealth(int h) { health = max(0, h); }
void changeHealth(int amount) { setHealth(health + amount); }
bool isDead() const { return health <= 0; }
// 攻击玩家,有几率施加效果
void attackPlayer(Player* player) const {
int damage = max(1, attack - player->getTotalDefense() / 2);
player->changeHealth(-damage);
cout << name << " 对你造成了 " << damage << " 点伤害!" << endl;
// 有几率施加效果
if (!possibleEffects.empty() && rand() % 100 < 30) { // 30%几率施加效果
int effectIndex = rand() % possibleEffects.size();
player->addEffect(possibleEffects[effectIndex]);
}
}
// 获取战利品(困难模式下稀有物品掉落概率降低)
vector<Item*> getLoot() const {
vector<Item*> loot;
// 随机数生成
int chance = rand() % 100 + 1;
for (const auto& itemPair : possibleLoot) {
// 对于高级物品,降低掉落概率
int adjustedChance = itemPair.second;
if (itemPair.first->getValue() > 30) { // 价值高的物品更难获得
adjustedChance = max(1, adjustedChance / 2);
}
if (chance <= adjustedChance) {
Item* droppedItem = itemPair.first->clone();
// BOSS有几率掉落附魔物品
if (isBoss && rand() % 100 < 50) { // 50%几率
EnchantmentType enchType;
if (droppedItem->getType() == ItemType::WEAPON) {
// 武器附魔
int type = rand() % 3;
if (type == 0) enchType = EnchantmentType::SHARPNESS;
else if (type == 1) enchType = EnchantmentType::SMITE;
else enchType = EnchantmentType::BANE_OF_ARTHROPODS;
} else if (droppedItem->getType() == ItemType::ARMOR) {
// 盔甲附魔
int type = rand() % 4;
if (type == 0) enchType = EnchantmentType::PROTECTION;
else if (type == 1) enchType = EnchantmentType::FIRE_PROTECTION;
else if (type == 2) enchType = EnchantmentType::PROJECTILE_PROTECTION;
else enchType = EnchantmentType::UNBREAKING;
} else {
// 其他物品附魔
enchType = EnchantmentType::UNBREAKING;
}
int level = rand() % 3 + 1; // 1-3级
droppedItem->addEnchantment(Enchantment(enchType, level));
cout << "你获得了附魔的" << droppedItem->getName() << "!" << endl;
}
loot.push_back(droppedItem);
}
// 重新生成随机数
chance = rand() % 100 + 1;
}
return loot;
}
};
// 位置类
class Location {
private:
string name;
string description;
vector<Location*> connectedLocations; // 相连的位置
vector<Event*> possibleEvents; // 可能发生的事件
bool visited; // 是否被访问过
bool hasStructure; // 是否有特殊建筑
string structureName; // 特殊建筑名称
public:
Location(string name, string description, bool hasStructure = false, string structureName = "")
: name(name), description(description), visited(false),
hasStructure(hasStructure), structureName(structureName) {}
~Location() {
// 不需要删除connectedLocations,因为它们由Game类管理
// 不需要删除possibleEvents,因为它们由Game类管理
}
string getName() const { return name; }
string getDescription() const { return description; }
bool isVisited() const { return visited; }
void setVisited(bool v) { visited = v; }
bool getHasStructure() const { return hasStructure; }
string getStructureName() const { return structureName; }
// 添加相连的位置
void addConnectedLocation(Location* location) {
connectedLocations.push_back(location);
}
// 获取相连的位置
const vector<Location*>& getConnectedLocations() const {
return connectedLocations;
}
// 添加可能的事件
void addEvent(Event* event) {
possibleEvents.push_back(event);
}
// 获取可能的事件
const vector<Event*>& getPossibleEvents() const {
return possibleEvents;
}
// 显示位置信息
void displayInfo() const {
cout << endl << "【" << name << "】" << endl;
cout << description << endl;
if (hasStructure) {
cout << "这里有一个" << structureName << "。" << endl;
}
if (!visited) {
cout << "这是你第一次来到这里。" << endl;
}
}
};
// 事件类型枚举
enum class EventType {
COMBAT, // 战斗事件
LOOT, // 获得物品
TRAP, // 陷阱
NPC_ENCOUNTER, // NPC遭遇
ENVIRONMENTAL, // 环境事件
STORY_TRIGGER, // 剧情触发
MERCHANT, // 商人
CRAFTING // 制作机会
};
// 事件类
class Event {
protected:
string name;
string description;
EventType type;
int triggerChance; // 触发概率(0-100)
bool repeatable; // 是否可重复触发
public:
Event(string name, string description, EventType type, int triggerChance, bool repeatable = true)
: name(name), description(description), type(type), triggerChance(triggerChance), repeatable(repeatable) {}
virtual ~Event() = default;
string getName() const { return name; }
string getDescription() const { return description; }
EventType getType() const { return type; }
int getTriggerChance() const { return triggerChance; }
bool isRepeatable() const { return repeatable; }
// 触发事件
virtual void trigger(Player* player, Game* game) = 0;
};
// 战斗事件
class CombatEvent : public Event {
private:
Monster* monster;
bool isAmbush; // 是否为伏击
public:
CombatEvent(string name, string description, int triggerChance, Monster* monster, bool isAmbush = false, bool repeatable = true)
: Event(name, description, EventType::COMBAT, triggerChance, repeatable), monster(monster), isAmbush(isAmbush) {}
~CombatEvent() {
delete monster;
}
void trigger(Player* player, Game* game) override;
};
// 战利品事件
class LootEvent : public Event {
private:
vector<Item*> loot;
bool isLocked; // 是否上锁
string requiredKey; // 需要的钥匙
public:
LootEvent(string name, string description, int triggerChance, vector<Item*> loot,
bool isLocked = false, string requiredKey = "", bool repeatable = false)
: Event(name, description, EventType::LOOT, triggerChance, repeatable),
isLocked(isLocked), requiredKey(requiredKey) {
// 复制物品
for (auto item : loot) {
this->loot.push_back(item->clone());
}
}
~LootEvent() {
// 清理战利品
for (auto item : loot) {
delete item;
}
}
void trigger(Player* player, Game* game) override;
};
// 陷阱事件
class TrapEvent : public Event {
private:
int damage;
bool canAvoid; // 是否可以躲避
bool hasEffect; // 是否施加效果
PlayerEffect effect; // 施加的效果
public:
TrapEvent(string name, string description, int triggerChance, int damage, bool canAvoid = true,
bool hasEffect = false, PlayerEffect effect = PlayerEffect("", "", 0, 0, false), bool repeatable = false)
: Event(name, description, EventType::TRAP, triggerChance, repeatable),
damage(damage), canAvoid(canAvoid), hasEffect(hasEffect), effect(effect) {}
void trigger(Player* player, Game* game) override;
};
// 商人事件
class MerchantEvent : public Event {
private:
string merchantName;
vector<pair<Item*, int>> itemsForSale; // 出售的物品和价格
public:
MerchantEvent(string name, string description, int triggerChance, string merchantName,
vector<pair<Item*, int>> itemsForSale, bool repeatable = true)
: Event(name, description, EventType::MERCHANT, triggerChance, repeatable),
merchantName(merchantName) {
// 复制物品
for (auto& p : itemsForSale) {
this->itemsForSale.emplace_back(p.first->clone(), p.second);
}
}
~MerchantEvent() {
// 清理物品
for (auto& p : itemsForSale) {
delete p.first;
}
}
void trigger(Player* player, Game* game) override;
};
// 制作事件
class CraftingEvent : public Event {
private:
string stationName; // 制作站名称
public:
CraftingEvent(string name, string description, int triggerChance, string stationName, bool repeatable = true)
: Event(name, description, EventType::CRAFTING, triggerChance, repeatable),
stationName(stationName) {}
void trigger(Player* player, Game* game) override;
};
// NPC遭遇事件
class NpcEncounterEvent : public Event {
private:
string npcName;
string dialogue;
bool givesReward;
vector<Item*> reward;
bool advancesStory; // 是否推进剧情
int storyId; // 剧情ID
bool hasQuest; // 是否有任务
string questDescription; // 任务描述
vector<Item*> questReward; // 任务奖励
public:
NpcEncounterEvent(string name, string description, int triggerChance, string npcName, string dialogue,
bool givesReward = false, vector<Item*> reward = {},
bool advancesStory = false, int storyId = -1, bool hasQuest = false,
string questDescription = "", vector<Item*> questReward = {}, bool repeatable = false)
: Event(name, description, EventType::NPC_ENCOUNTER, triggerChance, repeatable),
npcName(npcName), dialogue(dialogue), givesReward(givesReward),
advancesStory(advancesStory), storyId(storyId), hasQuest(hasQuest),
questDescription(questDescription) {
// 复制奖励物品
for (auto item : reward) {
this->reward.push_back(item->clone());
}
for (auto item : questReward) {
this->questReward.push_back(item->clone());
}
}
~NpcEncounterEvent() {
// 清理奖励物品
for (auto item : reward) {
delete item;
}
for (auto item : questReward) {
delete item;
}
}
void trigger(Player* player, Game* game) override;
};
// 环境事件
class EnvironmentalEvent : public Event {
private:
string effectDescription;
int healthChange; // 生命值变化,可为正或负
int hungerChange; // 饥饿值变化
bool hasEffect; // 是否施加效果
PlayerEffect effect; // 施加的效果
public:
EnvironmentalEvent(string name, string description, int triggerChance, string effectDescription,
int healthChange = 0, int hungerChange = 0, bool hasEffect = false,
PlayerEffect effect = PlayerEffect("", "", 0, 0, false), bool repeatable = true)
: Event(name, description, EventType::ENVIRONMENTAL, triggerChance, repeatable),
effectDescription(effectDescription), healthChange(healthChange),
hungerChange(hungerChange), hasEffect(hasEffect), effect(effect) {}
void trigger(Player* player, Game* game) override;
};
// 剧情触发事件
class StoryTriggerEvent : public Event {
private:
int storyId; // 要触发的剧情ID
public:
StoryTriggerEvent(string name, string description, int triggerChance, int storyId, bool repeatable = false)
: Event(name, description, EventType::STORY_TRIGGER, triggerChance, repeatable), storyId(storyId) {}
void trigger(Player* player, Game* game) override;
};
// 剧情类
class Story {
private:
int id;
string title;
vector<string> paragraphs; // 剧情段落
bool isMainStory; // 是否为主剧情
int part; // 部分 (1-3 对应前中后篇)
bool completed; // 是否已完成
vector<pair<string, int>> requiredItems; // 触发所需物品 (物品名, 数量)
int requiredLevel; // 触发所需等级
vector<Item*> rewards; // 完成奖励
int nextStoryId; // 后续剧情ID
public:
Story(int id, string title, vector<string> paragraphs, bool isMainStory = false, int part = 0,
vector<pair<string, int>> requiredItems = {}, int requiredLevel = 1,
vector<Item*> rewards = {}, int nextStoryId = -1)
: id(id), title(title), paragraphs(paragraphs), isMainStory(isMainStory), part(part),
completed(false), requiredItems(requiredItems), requiredLevel(requiredLevel),
nextStoryId(nextStoryId) {
// 复制奖励物品
for (auto item : rewards) {
this->rewards.push_back(item->clone());
}
}
~Story() {
// 清理奖励物品
for (auto item : rewards) {
delete item;
}
}
int getId() const { return id; }
string getTitle() const { return title; }
bool getIsMainStory() const { return isMainStory; }
int getPart() const { return part; }
bool isCompleted() const { return completed; }
int getNextStoryId() const { return nextStoryId; }
// 检查是否可以触发剧情
bool canTrigger(const Player* player) const {
if (completed) return false;
// 检查等级要求
if (player->getLevel() < requiredLevel) return false;
// 检查物品要求
for (const auto& req : requiredItems) {
if (!player->hasItem(req.first, req.second)) return false;
}
return true;
}
// 显示剧情
void display(Player* player, Game* game);
// 标记为已完成
void complete(Player* player) {
if (completed) return;
completed = true;
cout << "剧情完成!" << endl;
// 给予奖励
if (!rewards.empty()) {
cout << "你获得了以下奖励:" << endl;
for (auto item : rewards) {
cout << "- " << item->getName() << endl;
player->addItem(item->clone());
}
}
}
};
// 制作系统类
class CraftingSystem {
private:
// 配方: 输出物品 -> 所需材料列表(物品名, 数量)
map<string, vector<pair<string, int>>> recipes;
public:
CraftingSystem() {
initializeRecipes();
}
// 初始化配方
void initializeRecipes() {
// 木板配方
recipes["橡木木板"] = {{"木头", 1}};
// 木棍配方
recipes["木棍"] = {{"橡木木板", 2}};
// 木剑配方
recipes["木剑"] = {{"橡木木板", 2}, {"木棍", 1}};
// 木斧配方
recipes["木斧"] = {{"橡木木板", 3}, {"木棍", 2}};
// 木镐配方
recipes["木镐"] = {{"橡木木板", 3}, {"木棍", 2}};
// 石剑配方
recipes["石剑"] = {{"圆石", 2}, {"木棍", 1}};
// 石斧配方
recipes["石斧"] = {{"圆石", 3}, {"木棍", 2}};
// 石镐配方
recipes["石镐"] = {{"圆石", 3}, {"木棍", 2}};
// 铁剑配方
recipes["铁剑"] = {{"铁锭", 2}, {"木棍", 1}};
// 铁斧配方
recipes["铁斧"] = {{"铁锭", 3}, {"木棍", 2}};
// 铁镐配方
recipes["铁镐"] = {{"铁锭", 3}, {"木棍", 2}};
// 铁头盔配方
recipes["铁头盔"] = {{"铁锭", 5}};
// 铁胸甲配方
recipes["铁胸甲"] = {{"铁锭", 8}};
// 铁leggings配方
recipes["铁leggings"] = {{"铁锭", 7}};
// 铁靴子配方
recipes["铁靴子"] = {{"铁锭", 4}};
// 钻石剑配方
recipes["钻石剑"] = {{"钻石", 2}, {"木棍", 1}};
// 钻石斧配方
recipes["钻石斧"] = {{"钻石", 3}, {"木棍", 2}};
// 钻石镐配方
recipes["钻石镐"] = {{"钻石", 3}, {"木棍", 2}};
// 其他配方...
}
// 获取所有可用配方
vector<string> getAvailableRecipes(const Player* player) const {
vector<string> available;
for (const auto& recipe : recipes) {
bool hasAllMaterials = true;
for (const auto& material : recipe.second) {
if (!player->hasItem(material.first, material.second)) {
hasAllMaterials = false;
break;
}
}
if (hasAllMaterials) {
available.push_back(recipe.first);
}
}
return available;
}
// 制作物品
bool craftItem(const string& itemName, Player* player, const vector<Item*>& allItems) {
// 检查配方是否存在
auto it = recipes.find(itemName);
if (it == recipes.end()) {
cout << "没有这个物品的配方。" << endl;
return false;
}
// 检查是否有足够的材料
for (const auto& material : it->second) {
if (!player->hasItem(material.first, material.second)) {
cout << "材料不足:" << material.first << " x" << material.second << endl;
return false;
}
}
// 消耗材料
for (const auto& material : it->second) {
for (int i = 0; i < material.second; i++) {
Item* item = player->removeItem(material.first);
if (item) delete item;
}
}
// 查找并创建物品
for (const auto& item : allItems) {
if (item->getName() == itemName) {
player->addItem(item->clone());
cout << "你成功制作了" << itemName << "!" << endl;
return true;
}
}
cout << "无法制作" << itemName << endl;
return false;
}
// 显示配方
void showRecipe(const string& itemName) const {
auto it = recipes.find(itemName);
if (it == recipes.end()) {
cout << "没有这个物品的配方。" << endl;
return;
}
cout << itemName << " 需要:" << endl;
for (const auto& material : it->second) {
cout << "- " << material.first << " x" << material.second << endl;
}
}
};
// 游戏类
class Game {
private:
Player* player;
vector<Location*> locations;
vector<Event*> events;
vector<Story*> stories;
vector<Item*> allItems; // 所有可用物品
bool running;
int currentMainStoryPart; // 当前主线剧情部分 (0-3,0表示未开始)
CraftingSystem craftingSystem;
vector<pair<string, string>> activeQuests; // 活跃任务 (任务名, 描述)
public:
Game(Player* p) : player(p), running(true), currentMainStoryPart(0) {}
~Game() {
// 清理所有物品
for (auto item : allItems) {
delete item;
}
// 清理所有事件
for (auto event : events) {
delete event;
}
// 清理所有剧情
for (auto story : stories) {
delete story;
}
// 清理所有位置
for (auto location : locations) {
delete location;
}
// 玩家由main函数创建和管理
}
// 获取玩家
Player* getPlayer() const { return player; }
// 获取剧情
Story* getStoryById(int id) const {
for (auto story : stories) {
if (story->getId() == id) {
return story;
}
}
return nullptr;
}
// 获取当前主线剧情部分
int getCurrentMainStoryPart() const { return currentMainStoryPart; }
// 设置当前主线剧情部分
void setCurrentMainStoryPart(int part) { currentMainStoryPart = part; }
// 获取制作系统
CraftingSystem& getCraftingSystem() { return craftingSystem; }
// 获取所有物品
const vector<Item*>& getAllItems() const { return allItems; }
// 添加任务
void addQuest(const string& name, const string& description) {
activeQuests.emplace_back(name, description);
cout << "你接受了新任务:" << name << endl;
}
// 完成任务
bool completeQuest(const string& name, Player* player) {
for (auto it = activeQuests.begin(); it != activeQuests.end(); ++it) {
if (it->first == name) {
activeQuests.erase(it);
cout << "你完成了任务:" << name << endl;
return true;
}
}
return false;
}
// 查看任务
void viewQuests() const {
if (activeQuests.empty()) {
cout << "你没有任何任务。" << endl;
return;
}
cout << endl << "【你的任务】" << endl;
for (size_t i = 0; i < activeQuests.size(); ++i) {
cout << i + 1 << ". " << activeQuests[i].first << endl;
cout << " " << activeQuests[i].second << endl;
}
}
// 添加位置
void addLocation(Location* location) {
locations.push_back(location);
}
// 添加事件
void addEvent(Event* event) {
events.push_back(event);
}
// 添加剧情
void addStory(Story* story) {
stories.push_back(story);
}
// 添加物品
void addItem(Item* item) {
allItems.push_back(item);
}
// 初始化游戏世界
void initializeWorld();
// 开始游戏
void start();
// 显示主菜单
void showMainMenu();
// 处理玩家输入
void handleInput(const string& input);
// 探险逻辑
void explore();
// 仔细探索当前位置
void exploreCurrentLocation();
// 继续探险(移动到新位置)
void continueExploring();
// 检查并触发剧情
void checkAndTriggerStories();
};
// 实现PlayerEffect类的apply方法
void PlayerEffect::apply(Player* player) {
if (!player) return;
if (name == "力量") {
// 正面效果,提升攻击力
cout << "力量效果使你的攻击更有力!" << endl;
} else if (name == "速度") {
// 正面效果,提升闪避率
cout << "速度效果使你更加敏捷!" << endl;
} else if (name == "抗性") {
// 正面效果,降低受到的伤害
cout << "抗性效果减轻了你受到的伤害!" << endl;
} else if (name == "吸收") {
// 正面效果,提供额外的吸收护盾
cout << "吸收效果保护着你!" << endl;
} else if (name == "中毒") {
// 负面效果,每回合受到伤害
player->changeHealth(-strength);
cout << "中毒效果使你受到" << strength << "点伤害!" << endl;
} else if (name == "虚弱") {
// 负面效果,降低攻击力
cout << "虚弱效果使你的攻击变弱!" << endl;
} else if (name == "缓慢") {
// 负面效果,降低闪避率
cout << "缓慢效果使你行动迟缓!" << endl;
}
}
// 实现Consumable类的use方法
bool Consumable::use(Player* player) {
if (player == nullptr) return false;
bool used = false;
if (healthRestore > 0) {
player->changeHealth(healthRestore);
cout << "你使用了" << name << ",恢复了" << healthRestore << "点生命值。" << endl;
used = true;
}
if (hungerRestore > 0) {
player->changeHunger(hungerRestore);
cout << "你使用了" << name << ",饥饿感减轻了。" << endl;
used = true;
}
if (givesEffect) {
if (name == "金苹果") {
player->addEffect(PlayerEffect("吸收", "提供额外的吸收护盾", effectDuration, 10, true));
player->addEffect(PlayerEffect("抗性", "降低受到的伤害", effectDuration, 2, true));
} else if (name == "药水(力量)") {
player->addEffect(PlayerEffect("力量", "提升攻击力", effectDuration, 2, true));
} else if (name == "药水(速度)") {
player->addEffect(PlayerEffect("速度", "提升闪避率", effectDuration, 1, true));
} else if (name == "药水(抗性)") {
player->addEffect(PlayerEffect("抗性", "降低受到的伤害", effectDuration, 1, true));
}
used = true;
}
return used;
}
// 实现Player类的equip方法
bool Player::equip(Equipment* equipment) {
if (!equipment) return false;
ItemType type = equipment->getType();
Equipment* oldEquipment = nullptr;
// 根据装备类型卸下旧装备
if (type == ItemType::ARMOR) {
// 判断盔甲类型(根据名称)
if (equipment->getName().find("头盔") != string::npos) {
oldEquipment = helmet;
helmet = equipment;
} else if (equipment->getName().find("胸甲") != string::npos) {
oldEquipment = chestplate;
chestplate = equipment;
} else if (equipment->getName().find("leggings") != string::npos) {
oldEquipment = leggings;
leggings = equipment;
} else if (equipment->getName().find("靴子") != string::npos) {
oldEquipment = boots;
boots = equipment;
} else {
delete equipment; // 无法装备,清理内存
return false; // 未知盔甲类型
}
} else if (type == ItemType::WEAPON) {
oldEquipment = weapon;
weapon = equipment;
} else {
delete equipment; // 无法装备,清理内存
return false; // 不能装备的类型
}
cout << "你装备了 " << (type == ItemType::WEAPON ? weapon :
(helmet ? helmet : (chestplate ? chestplate :
(leggings ? leggings : boots))))->getName() << endl;
// 显示附魔信息
string enchInfo = (type == ItemType::WEAPON ? weapon :
(helmet ? helmet : (chestplate ? chestplate :
(leggings ? leggings : boots))))->getEnchantmentInfo();
if (!enchInfo.empty()) {
cout << enchInfo << endl;
}
// 将旧装备放回inventory
if (oldEquipment) {
addItem(oldEquipment);
}
return true;
}
// 实现Player类的unequip方法
Equipment* Player::unequip(ItemType type) {
Equipment* result = nullptr;
if (type == ItemType::ARMOR) {
cout << "请选择要卸下的装备:1.头盔 2.胸甲 3.leggings 4.靴子" << endl;
int choice;
cin >> choice;
switch (choice) {
case 1:
result = helmet;
helmet = nullptr;
break;
case 2:
result = chestplate;
chestplate = nullptr;
break;
case 3:
result = leggings;
leggings = nullptr;
break;
case 4:
result = boots;
boots = nullptr;
break;
default:
cout << "无效选择" << endl;
return nullptr;
}
} else if (type == ItemType::WEAPON) {
result = weapon;
weapon = nullptr;
} else {
cout << "无法卸下该类型装备" << endl;
return nullptr;
}
if (result) {
cout << "你卸下了 " << result->getName() << endl;
} else {
cout << "你没有装备该类型物品" << endl;
}
return result;
}
// 实现Player类的viewEquipment方法
void Player::viewEquipment() const {
cout << endl << "【你的装备】" << endl;
cout << "头盔: " << (helmet ? helmet->getName() : "无") << endl;
if (helmet && !helmet->getEnchantments().empty()) {
cout << " " << helmet->getEnchantmentInfo() << endl;
}
cout << "胸甲: " << (chestplate ? chestplate->getName() : "无") << endl;
if (chestplate && !chestplate->getEnchantments().empty()) {
cout << " " << chestplate->getEnchantmentInfo() << endl;
}
cout << "leggings: " << (leggings ? leggings->getName() : "无") << endl;
if (leggings && !leggings->getEnchantments().empty()) {
cout << " " << leggings->getEnchantmentInfo() << endl;
}
cout << "靴子: " << (boots ? boots->getName() : "无") << endl;
if (boots && !boots->getEnchantments().empty()) {
cout << " " << boots->getEnchantmentInfo() << endl;
}
cout << "武器: " << (weapon ? weapon->getName() : "无") << endl;
if (weapon && !weapon->getEnchantments().empty()) {
cout << " " << weapon->getEnchantmentInfo() << endl;
}
cout << "总攻击力: " << getTotalAttack() << endl;
cout << "总防御力: " << getTotalDefense() << endl;
}
// 实现Player类的addItem方法
bool Player::addItem(Item* item) {
if (!item) return false;
// 检查inventory是否已满
if (inventory.size() >= (size_t)inventorySize) {
cout << "你的inventory已满,无法拾取 " << item->getName() << endl;
delete item; // 无法拾取,清理内存
return false;
}
// 检查是否可以堆叠
if (item->isStackable()) {
for (auto& pair : inventory) {
if (pair.first->isSameType(*item)) {
int newCount = pair.second + 1;
if (newCount <= item->getMaxStackSize()) {
pair.second = newCount;
cout << "你获得了 " << item->getName() << " x1" << endl;
delete item; // 已堆叠,清理原物品
return true;
}
}
}
}
// 无法堆叠或没有找到相同物品,添加新物品
inventory.emplace_back(item, 1);
cout << "你获得了 " << inventory.back().first->getName() << " x1" << endl;
// 显示附魔信息
string enchInfo = inventory.back().first->getEnchantmentInfo();
if (!enchInfo.empty()) {
cout << enchInfo << endl;
}
return true;
}
// 实现Player类的removeItem方法
Item* Player::removeItem(const string& itemName, int count) {
for (auto it = inventory.begin(); it != inventory.end(); ++it) {
if (it->first->getName() == itemName) {
if (it->second > count) {
it->second -= count;
return it->first->clone(); // 返回克隆的物品
} else {
// 保存指针并从容器中移除
Item* item = it->first;
inventory.erase(it);
return item; // 返回原始物品
}
}
}
return nullptr;
}
// 实现Player类的viewInventory方法
void Player::viewInventory() const {
cout << endl << "【你的inventory】 (" << inventory.size() << "/" << inventorySize << ")" << endl;
if (inventory.empty()) {
cout << "你的inventory是空的。" << endl;
return;
}
int index = 1;
for (const auto& pair : inventory) {
cout << index << ". " << pair.first->getName() << " x" << pair.second << endl;
cout << " " << pair.first->getDescription() << endl;
string enchInfo = pair.first->getEnchantmentInfo();
if (!enchInfo.empty()) {
cout << " " << enchInfo << endl;
}
index++;
}
}
// 实现Player类的useItem方法
bool Player::useItem(const string& itemName) {
Item* item = removeItem(itemName);
if (!item) {
cout << "你没有这个物品。" << endl;
return false;
}
bool used = item->use(this);
if (!used) {
// 如果没有使用成功,放回inventory
addItem(item);
return false;
}
// 使用成功,删除物品
delete item;
return true;
}
// 实现Player类的receiveRewards方法
void Player::receiveRewards(int exp, vector<Item*> items) {
addExperience(exp);
for (auto item : items) {
addItem(item);
}
}
// 实现Player类的loseSomeItems方法
void Player::loseSomeItems() {
if (inventory.empty()) return;
// 困难模式下死亡会掉落更多物品
int itemsToLose = rand() % min(8, (int)inventory.size() / 2 + 1) + 2;
cout << "你在死亡时掉落了 " << itemsToLose << " 件物品!" << endl;
// 随机选择要掉落的物品
vector<size_t> indices;
for (size_t i = 0; i < inventory.size(); ++i) {
indices.push_back(i);
}
random_shuffle(indices.begin(), indices.end());
// 记录要保留的物品
vector<pair<Item*, int>> remaining;
for (size_t i = 0; i < inventory.size(); ++i) {
// 保留未被选中的物品
if (find(indices.begin(), indices.begin() + itemsToLose, i) == indices.begin() + itemsToLose) {
remaining.push_back(inventory[i]);
} else {
cout << "你失去了: " << inventory[i].first->getName() << endl;
// 删除掉落的物品
delete inventory[i].first;
}
}
// 替换inventory
inventory = remaining;
}
// 实现Player类的hasItem方法
bool Player::hasItem(const string& itemName, int count) const {
for (const auto& pair : inventory) {
if (pair.first->getName() == itemName) {
return pair.second >= count;
}
}
return false;
}
// 实现Player类的repairEquipment方法
bool Player::repairEquipment(const string& equipmentName, int repairAmount) {
// 检查是否持有要修理的装备
Equipment* target = nullptr;
if (helmet && helmet->getName() == equipmentName) {
target = helmet;
} else if (chestplate && chestplate->getName() == equipmentName) {
target = chestplate;
} else if (leggings && leggings->getName() == equipmentName) {
target = leggings;
} else if (boots && boots->getName() == equipmentName) {
target = boots;
} else if (weapon && weapon->getName() == equipmentName) {
target = weapon;
} else {
// 检查inventory中是否有该装备
Item* item = nullptr;
for (const auto& pair : inventory) {
if (pair.first->getName() == equipmentName &&
(pair.first->getType() == ItemType::WEAPON || pair.first->getType() == ItemType::ARMOR)) {
item = pair.first;
break;
}
}
if (!item) {
cout << "你没有这件装备。" << endl;
return false;
}
target = dynamic_cast<Equipment*>(item);
if (!target) {
cout << "这不是可修理的装备。" << endl;
return false;
}
}
// 检查装备是否需要修理
if (target->getDurability() >= target->getMaxDurability()) {
cout << "这件装备已经是完好的。" << endl;
return false;
}
// 消耗材料进行修理(简单实现,实际游戏中应根据装备类型消耗对应材料)
int requiredMaterialCount = repairAmount / 10;
if (requiredMaterialCount < 1) requiredMaterialCount = 1;
string requiredMaterial;
if (equipmentName.find("木") != string::npos) {
requiredMaterial = "木头";
} else if (equipmentName.find("石") != string::npos) {
requiredMaterial = "圆石";
} else if (equipmentName.find("铁") != string::npos) {
requiredMaterial = "铁锭";
} else if (equipmentName.find("金") != string::npos) {
requiredMaterial = "金锭";
} else if (equipmentName.find("钻石") != string::npos) {
requiredMaterial = "钻石";
} else {
requiredMaterial = "木头"; // 默认材料
}
if (!hasItem(requiredMaterial, requiredMaterialCount)) {
cout << "修理" << equipmentName << "需要" << requiredMaterial << "x" << requiredMaterialCount << endl;
return false;
}
// 消耗材料
for (int i = 0; i < requiredMaterialCount; i++) {
Item* material = removeItem(requiredMaterial);
if (material) delete material;
}
// 修理装备
target->repair(repairAmount);
cout << "你修理了" << equipmentName << ",耐久度恢复到" << target->getDurability() << "/" << target->getMaxDurability() << endl;
return true;
}
// 实现CombatEvent类的trigger方法
void CombatEvent::trigger(Player* player, Game* game) {
if (!player || !monster) return;
cout << endl << "【" << name << "】" << endl;
cout << description << endl;
cout << "你遇到了 " << monster->getName() << "!" << endl;
cout << monster->getDescription() << endl;
// 战斗逻辑 - 创建怪物副本进行战斗
Monster* currentMonster = new Monster(*monster);
bool playerFled = false;
// 伏击情况下,怪物先攻
if (isAmbush) {
cout << "你遭到了伏击!" << endl;
currentMonster->attackPlayer(player);
if (player->getHealth() <= 0) {
delete currentMonster;
return;
}
}
while (player->getHealth() > 0 && !currentMonster->isDead() && !playerFled) {
// 应用玩家状态效果
player->applyEffects();
cout << endl << "-------------------------" << endl;
cout << player->getName() << " 的生命值: " << player->getHealth() << "/" << player->getMaxHealth() << endl;
cout << currentMonster->getName() << " 的生命值: " << currentMonster->getHealth() << "/" << currentMonster->getMaxHealth() << endl;
cout << "-------------------------" << endl;
// 显示活跃效果
const vector<PlayerEffect>& effects = player->getActiveEffects();
if (!effects.empty()) {
cout << "你当前的效果:" << endl;
for (const auto& effect : effects) {
cout << "- " << effect.getName() << " (" << effect.getDuration() << "回合)" << endl;
}
}
cout << "请选择行动: 1.攻击 2.使用物品 3.逃跑" << endl;
int choice;
cin >> choice;
switch (choice) {
case 1: {
// 玩家攻击
int damage = max(1, player->getTotalAttack() - currentMonster->getDefense() / 3);
// 检查是否有力量效果
bool hasStrength = false;
for (const auto& effect : player->getActiveEffects()) {
if (effect.getName() == "力量") {
damage = (int)(damage * (1 + effect.getStrength() * 0.1));
hasStrength = true;
break;
}
}
currentMonster->changeHealth(-damage);
cout << "你对 " << currentMonster->getName() << " 造成了 " << damage << " 点伤害!" << endl;
if (hasStrength) {
cout << "力量效果增强了你的攻击!" << endl;
}
// 降低武器耐久度
if (player->getTotalAttack() > 0) {
// 这里简化处理,实际游戏中应追踪具体武器
if (player->getTotalAttack() > 5) { // 假设攻击力>5意味着持有武器
// 查找并降低武器耐久度
// 实际实现应更精确
}
}
// 怪物反击(如果还活着)
if (!currentMonster->isDead()) {
currentMonster->attackPlayer(player);
}
break;
}
case 2: {
// 使用物品
player->viewInventory();
cout << "请输入要使用的物品名称,或输入'取消'放弃使用: ";
string itemName;
cin.ignore();
getline(cin, itemName);
if (itemName != "取消") {
player->useItem(itemName);
}
// 怪物攻击
currentMonster->attackPlayer(player);
break;
}
case 3: {
// 尝试逃跑(困难模式下逃跑成功率降低)
int fleeChance = 30; // 30%的逃跑几率,比普通模式低
// 检查是否有速度效果
for (const auto& effect : player->getActiveEffects()) {
if (effect.getName() == "速度") {
fleeChance += effect.getStrength() * 10;
break;
}
}
if (rand() % 100 + 1 <= fleeChance) {
cout << "你成功逃脱了!" << endl;
playerFled = true;
} else {
cout << "逃跑失败!" << endl;
currentMonster->attackPlayer(player);
}
break;
}
default:
cout << "无效的选择,你犹豫了一下..." << endl;
currentMonster->attackPlayer(player);
break;
}
}
// 清理怪物副本
delete currentMonster;
// 战斗结束
if (player->getHealth() <= 0) {
cout << "你在战斗中失败了..." << endl;
} else if (playerFled) {
// 逃跑成功,什么都不做
} else {
cout << "你击败了 " << monster->getName() << "!" << endl;
vector<Item*> loot = monster->getLoot();
player->receiveRewards(monster->getExperienceReward(), loot);
// 清理战利品
for (auto item : loot) {
delete item;
}
// 检查是否完成了与该怪物相关的任务
// 这里简化处理,实际游戏中应有更复杂的任务系统
}
}
// 实现LootEvent类的trigger方法
void LootEvent::trigger(Player* player, Game* game) {
if (!player) return;
cout << endl << "【" << name << "】" << endl;
cout << description << endl;
// 检查是否需要钥匙
if (isLocked) {
if (!player->hasItem(requiredKey)) {
cout << "这个箱子是锁着的,需要" << requiredKey << "才能打开。" << endl;
return;
}
// 消耗钥匙
Item* key = player->removeItem(requiredKey);
if (key) {
cout << "你使用了" << requiredKey << "打开了箱子。" << endl;
delete key;
} else {
cout << "你没有" << requiredKey << ",无法打开箱子。" << endl;
return;
}
}
cout << "你找到了以下物品:" << endl;
for (auto item : loot) {
cout << "- " << item->getName() << endl;
player->addItem(item->clone());
}
}
// 实现TrapEvent类的trigger方法
void TrapEvent::trigger(Player* player, Game* game) {
if (!player) return;
cout << endl << "【" << name << "】" << endl;
cout << description << endl;
if (canAvoid) {
cout << "你有机会躲避!请选择: 1.快速反应 2.小心移动" << endl;
int choice;
cin >> choice;
// 随机数生成
int chance = rand() % 100 + 1;
bool avoided = false;
if (choice == 1) {
avoided = (chance <= 30); // 30%几率躲避,比普通模式低
} else {
avoided = (chance <= 50); // 50%几率躲避,比普通模式低
}
// 检查是否有速度效果
for (const auto& effect : player->getActiveEffects()) {
if (effect.getName() == "速度") {
avoided = true;
cout << "速度效果帮助你避开了陷阱!" << endl;
break;
}
}
if (avoided) {
cout << "你成功躲避了陷阱!" << endl;
return;
}
}
// 未能躲避或无法躲避
player->changeHealth(-damage);
cout << "你触发了陷阱,受到了 " << damage << " 点伤害!" << endl;
// 施加效果
if (hasEffect) {
player->addEffect(effect);
}
}
// 实现MerchantEvent类的trigger方法
void MerchantEvent::trigger(Player* player, Game* game) {
if (!player || !game) return;
cout << endl << "【" << name << "】" << endl;
cout << description << endl;
cout << merchantName << " 说: \"你好,旅行者!我这里有一些不错的东西,有兴趣看看吗?\"" << endl;
cout << "商品列表:" << endl;
for (size_t i = 0; i < itemsForSale.size(); ++i) {
cout << i + 1 << ". " << itemsForSale[i].first->getName() << " - " << itemsForSale[i].second << " 金币" << endl;
cout << " " << itemsForSale[i].first->getDescription() << endl;
}
cout << "0. 离开" << endl;
cout << "请选择要购买的商品 (0-" << itemsForSale.size() << "): ";
int choice;
cin >> choice;
if (choice < 0 || choice > (int)itemsForSale.size()) {
cout << "无效的选择。" << endl;
return;
}
if (choice == 0) {
cout << "你离开了商人。" << endl;
return;
}
// 检查玩家是否有足够的金币(这里简化处理,假设玩家有一个"金币"物品)
int price = itemsForSale[choice - 1].second;
if (!player->hasItem("金币", price)) {
cout << "你没有足够的金币购买这个物品。" << endl;
return;
}
// 消耗金币
for (int i = 0; i < price; i++) {
Item* gold = player->removeItem("金币");
if (gold) delete gold;
}
// 给予物品
player->addItem(itemsForSale[choice - 1].first->clone());
cout << "你购买了" << itemsForSale[choice - 1].first->getName() << endl;
}
// 实现CraftingEvent类的trigger方法
void CraftingEvent::trigger(Player* player, Game* game) {
if (!player || !game) return;
cout << endl << "【" << name << "】" << endl;
cout << description << endl;
cout << "你发现了一个" << stationName << ",可以在这里制作物品。" << endl;
CraftingSystem& crafting = game->getCraftingSystem();
vector<string> availableRecipes = crafting.getAvailableRecipes(player);
if (availableRecipes.empty()) {
cout << "你没有足够的材料制作任何物品。" << endl;
return;
}
while (true) {
cout << endl << "可用配方:" << endl;
for (size_t i = 0; i < availableRecipes.size(); ++i) {
cout << i + 1 << ". " << availableRecipes[i] << endl;
}
cout << availableRecipes.size() + 1 << ". 查看配方详情" << endl;
cout << availableRecipes.size() + 2 << ". 离开" << endl;
cout << "请选择: ";
int choice;
cin >> choice;
if (choice < 1 || choice > (int)availableRecipes.size() + 2) {
cout << "无效的选择。" << endl;
continue;
}
if (choice == availableRecipes.size() + 2) {
cout << "你离开了" << stationName << endl;
break;
}
if (choice == availableRecipes.size() + 1) {
cout << "请输入要查看的物品名称: ";
string itemName;
cin.ignore();
getline(cin, itemName);
crafting.showRecipe(itemName);
continue;
}
// 制作物品
string itemToCraft = availableRecipes[choice - 1];
crafting.craftItem(itemToCraft, player, game->getAllItems());
// 更新可用配方列表
availableRecipes = crafting.getAvailableRecipes(player);
if (availableRecipes.empty()) {
cout << "你没有足够的材料制作任何其他物品了。" << endl;
break;
}
}
}
// 实现NpcEncounterEvent类的trigger方法
void NpcEncounterEvent::trigger(Player* player, Game* game) {
if (!player || !game) return;
cout << endl << "【" << name << "】" << endl;
cout << description << endl;
cout << npcName << " 说: \"" << dialogue << "\"" << endl;
if (givesReward && !reward.empty()) {
cout << npcName << " 给了你一些东西作为奖励!" << endl;
for (auto item : reward) {
player->addItem(item->clone());
}
}
if (hasQuest) {
cout << npcName << " 说: \"我有一个请求,你能帮我吗?" << questDescription << "\"" << endl;
cout << "1. 接受任务 2. 拒绝" << endl;
int choice;
cin >> choice;
if (choice == 1) {
game->addQuest(npcName + "的请求", questDescription);
// 记录任务奖励,实际游戏中应有更复杂的任务系统
} else {
cout << "你拒绝了" << npcName << "的请求。" << endl;
}
}
if (advancesStory && storyId != -1) {
Story* story = game->getStoryById(storyId);
if (story && story->canTrigger(player)) {
story->display(player, game);
}
}
}
// 实现EnvironmentalEvent类的trigger方法
void EnvironmentalEvent::trigger(Player* player, Game* game) {
if (!player) return;
cout << endl << "【" << name << "】" << endl;
cout << description << endl;
cout << effectDescription << endl;
if (healthChange != 0) {
player->changeHealth(healthChange);
if (healthChange > 0) {
cout << "你恢复了 " << healthChange << " 点生命值。" << endl;
} else {
cout << "你受到了 " << -healthChange << " 点伤害。" << endl;
}
}
if (hungerChange != 0) {
player->changeHunger(hungerChange);
if (hungerChange > 0) {
cout << "你的饥饿感减轻了。" << endl;
} else {
cout << "你感到更饿了。" << endl;
}
}
// 施加效果
if (hasEffect) {
player->addEffect(effect);
}
}
// 实现StoryTriggerEvent类的trigger方法
void StoryTriggerEvent::trigger(Player* player, Game* game) {
if (!player || !game) return;
cout << endl << "【" << name << "】" << endl;
cout << description << endl;
Story* story = game->getStoryById(storyId);
if (story && story->canTrigger(player)) {
story->display(player, game);
}
}
// 实现Story类的display方法
void Story::display(Player* player, Game* game) {
if (!player || !game || completed) return;
cout << endl << "=====================================" << endl;
cout << "【" << title << "】" << endl;
cout << "=====================================" << endl;
for (const string& para : paragraphs) {
cout << para << endl << endl;
// 每段后等待用户按回车继续
cout << "按回车键继续...";
cin.ignore();
cin.get();
}
// 完成剧情
complete(player);
// 如果是主线剧情,更新游戏的主线剧情进度
if (isMainStory) {
game->setCurrentMainStoryPart(part);
cout << "你完成了主线剧情第 " << part << " 部分!" << endl;
}
// 触发下一段剧情(如果有的话)
if (nextStoryId != -1) {
Story* nextStory = game->getStoryById(nextStoryId);
if (nextStory && nextStory->canTrigger(player)) {
cout << "似乎有新的事情发生了..." << endl;
nextStory->display(player, game);
}
}
}
// 实现Game类的initializeWorld方法
void Game::initializeWorld() {
// 创建物品
// 方块
allItems.push_back(new Item("石头", "一块普通的石头", ItemType::BLOCK, 1));
allItems.push_back(new Item("木头", "一块木头", ItemType::BLOCK, 2));
allItems.push_back(new Item("圆石", "经过打磨的石头", ItemType::BLOCK, 2));
allItems.push_back(new Item("沙子", "细小的沙粒", ItemType::BLOCK, 1));
allItems.push_back(new Item("gravel", "小石子", ItemType::BLOCK, 1));
allItems.push_back(new Item("泥土", "普通的泥土", ItemType::BLOCK, 1));
allItems.push_back(new Item("草方块", "带有草的泥土", ItemType::BLOCK, 2));
allItems.push_back(new Item("橡木木板", "由橡木制成的木板", ItemType::BLOCK, 3));
allItems.push_back(new Item("birch木板", "由birch树制成的木板", ItemType::BLOCK, 3));
allItems.push_back(new Item("云杉木板", "由云杉制成的木板", ItemType::BLOCK, 3));
allItems.push_back(new Item("黑曜石", "非常坚硬的黑色石头", ItemType::BLOCK, 10));
allItems.push_back(new Item("金块", "由金子制成的方块", ItemType::BLOCK, 50));
allItems.push_back(new Item("铁块", "由铁制成的方块", ItemType::BLOCK, 25));
allItems.push_back(new Item("钻石块", "由钻石制成的方块", ItemType::BLOCK, 100));
// 工具
allItems.push_back(new Equipment("木斧", "由木头制成的斧头,适合砍树", ItemType::TOOL, 5, 3, 0, 59));
allItems.push_back(new Equipment("石斧", "由石头制成的斧头,比木斧更耐用", ItemType::TOOL, 10, 5, 0, 131));
allItems.push_back(new Equipment("铁斧", "由铁制成的斧头,非常实用", ItemType::TOOL, 25, 7, 0, 250));
allItems.push_back(new Equipment("金斧", "由金制成的斧头,不耐用但效率高", ItemType::TOOL, 30, 9, 0, 32));
allItems.push_back(new Equipment("钻石斧", "由钻石制成的斧头,非常坚固", ItemType::TOOL, 50, 10, 0, 1561));
allItems.push_back(new Equipment("木镐", "由木头制成的镐子,适合挖掘石头", ItemType::TOOL, 5, 3, 0, 59));
allItems.push_back(new Equipment("石镐", "由石头制成的镐子,比木镐更耐用", ItemType::TOOL, 10, 5, 0, 131));
allItems.push_back(new Equipment("铁镐", "由铁制成的镐子,非常实用", ItemType::TOOL, 25, 7, 0, 250));
allItems.push_back(new Equipment("金镐", "由金制成的镐子,不耐用但效率高", ItemType::TOOL, 30, 9, 0, 32));
allItems.push_back(new Equipment("钻石镐", "由钻石制成的镐子,非常坚固", ItemType::TOOL, 50, 10, 0, 1561));
// 武器
allItems.push_back(new Equipment("木剑", "由木头制成的简单剑", ItemType::WEAPON, 5, 4, 0, 59));
allItems.push_back(new Equipment("石剑", "由石头制成的剑,比木剑更锋利", ItemType::WEAPON, 10, 5, 0, 131));
allItems.push_back(new Equipment("铁剑", "由铁制成的剑,非常锋利", ItemType::WEAPON, 25, 6, 0, 250));
allItems.push_back(new Equipment("金剑", "由金制成的剑,不耐用但锋利", ItemType::WEAPON, 30, 7, 0, 32));
allItems.push_back(new Equipment("钻石剑", "由钻石制成的剑,极其锋利", ItemType::WEAPON, 50, 8, 0, 1561));
allItems.push_back(new Equipment("弓", "可以发射箭的远程武器", ItemType::WEAPON, 30, 5, 0, 385));
allItems.push_back(new Item("箭", "弓使用的弹药", ItemType::MISC, 1));
allItems.push_back(new Equipment("三叉戟", "可以在水中使用的武器", ItemType::WEAPON, 40, 6, 0, 250));
// 盔甲
allItems.push_back(new Equipment("皮革头盔", "由皮革制成的简单头盔", ItemType::ARMOR, 5, 0, 1, 81));
allItems.push_back(new Equipment("皮革胸甲", "由皮革制成的简单胸甲", ItemType::ARMOR, 8, 0, 3, 81));
allItems.push_back(new Equipment("皮革leggings", "由皮革制成的简单leggings", ItemType::ARMOR, 7, 0, 2, 81));
allItems.push_back(new Equipment("皮革靴子", "由皮革制成的简单靴子", ItemType::ARMOR, 4, 0, 1, 81));
allItems.push_back(new Equipment("铁头盔", "由铁制成的坚固头盔", ItemType::ARMOR, 15, 0, 2, 166));
allItems.push_back(new Equipment("铁胸甲", "由铁制成的坚固胸甲", ItemType::ARMOR, 24, 0, 6, 166));
allItems.push_back(new Equipment("铁leggings", "由铁制成的坚固leggings", ItemType::ARMOR, 21, 0, 5, 166));
allItems.push_back(new Equipment("铁靴子", "由铁制成的坚固靴子", ItemType::ARMOR, 12, 0, 2, 166));
allItems.push_back(new Equipment("钻石头盔", "由钻石制成的强力头盔", ItemType::ARMOR, 30, 0, 3, 364));
allItems.push_back(new Equipment("钻石胸甲", "由钻石制成的强力胸甲", ItemType::ARMOR, 48, 0, 8, 364));
allItems.push_back(new Equipment("钻石leggings", "由钻石制成的强力leggings", ItemType::ARMOR, 42, 0, 6, 364));
allItems.push_back(new Equipment("钻石靴子", "由钻石制成的强力靴子", ItemType::ARMOR, 24, 0, 3, 364));
// 消耗品
allItems.push_back(new Consumable("苹果", "可以恢复少量生命值的水果", 2, 2, 1));
allItems.push_back(new Consumable("面包", "可以缓解饥饿的食物", 3, 0, 5));
allItems.push_back(new Consumable("熟猪肉", "可以恢复较多饥饿值的食物", 5, 3, 8));
allItems.push_back(new Consumable("熟牛肉", "可以恢复大量饥饿值的食物", 6, 4, 10));
allItems.push_back(new Consumable("金苹果", "可以恢复大量生命值并提供抗性", 30, 10, 0, true));
allItems.push_back(new Consumable("药水(治疗)", "可以瞬间恢复生命值", 25, 8, 0));
allItems.push_back(new Consumable("药水(力量)", "短时间内提升攻击力", 40, 0, 0, true));
allItems.push_back(new Consumable("药水(速度)", "短时间内提升速度", 40, 0, 0, true));
allItems.push_back(new Consumable("药水(抗性)", "短时间内提升抗性", 40, 0, 0, true));
allItems.push_back(new Consumable("牛奶", "可以清除所有状态效果", 5, 0, 3));
// 材料
allItems.push_back(new Item("木棍", "由木头制成的棍子,用于制作工具", ItemType::MATERIAL, 1));
allItems.push_back(new Item("铁锭", "用于制作铁器的材料", ItemType::MATERIAL, 10));
allItems.push_back(new Item("金锭", "用于制作金器的材料", ItemType::MATERIAL, 20));
allItems.push_back(new Item("钻石", "非常珍贵的宝石,用于制作高级物品", ItemType::MATERIAL, 50));
allItems.push_back(new Item("红石", "具有特殊能量的红色矿石", ItemType::MATERIAL, 15));
allItems.push_back(new Item("煤", "可以用来燃烧或制作火把", ItemType::MATERIAL, 4));
allItems.push_back(new Item("皮革", "可以用来制作皮革盔甲", ItemType::MATERIAL, 3));
allItems.push_back(new Item("线", "可以用来制作弓和钓鱼竿", ItemType::MATERIAL, 1));
allItems.push_back(new Item("羽毛", "可以用来制作箭", ItemType::MATERIAL, 1));
allItems.push_back(new Item("火药", "可以用来制作TNT和火箭", ItemType::MATERIAL, 8));
allItems.push_back(new Item("骨头", "可以用来制作骨粉", ItemType::MATERIAL, 2));
allItems.push_back(new Item("蜘蛛眼", "可以用来制作药水", ItemType::MATERIAL, 5));
allItems.push_back(new Item("末影之眼", "可以用来定位要塞", ItemType::MATERIAL, 64));
allItems.push_back(new Item("烈焰棒", "来自烈焰人的材料,用于制作药水", ItemType::MATERIAL, 25));
allItems.push_back(new Item("金粒", "可以用来制作金锭", ItemType::MATERIAL, 5));
allItems.push_back(new Item("金币", "用于交易的货币", ItemType::MATERIAL, 1));
allItems.push_back(new Item("地图", "显示周围地形的地图", ItemType::MISC, 15));
allItems.push_back(new Item("附魔书", "可以为物品添加附魔", ItemType::MISC, 30));
allItems.push_back(new Item("龙蛋", "击败末影龙后获得的珍贵物品", ItemType::MISC, 1000));
allItems.push_back(new Item("羊毛", "来自羊的材料,用于制作床和地毯", ItemType::MATERIAL, 2));
allItems.push_back(new Item("胡萝卜", "可以食用或用来吸引猪", ItemType::CONSUMABLE, 2, 0, 3));
allItems.push_back(new Item("钥匙", "可以打开上锁的箱子", ItemType::MISC, 10));
// 创建位置
Location* spawn = new Location("出生点", "这是你的起点,周围是茂密的森林。一条小路通向远方。");
Location* forest = new Location("森林深处", "高大的树木遮天蔽日,阳光只能透过树叶的缝隙照射进来。");
Location* river = new Location("河流", "一条清澈的河流,沿着森林边缘流淌。水中似乎有东西在游动。");
Location* mountain = new Location("山脚", "陡峭的山壁,上面有一些洞穴入口。");
Location* cave = new Location("洞穴", "黑暗潮湿的洞穴,墙壁上有发光的矿石。");
Location* village = new Location("村庄", "一个小型村庄,村民们在这里安居乐业。", true, "铁匠铺");
Location* field = new Location("田野", "开阔的田野,种植着各种农作物。远处有一个风车。");
Location* desert = new Location("沙漠", "广袤的沙漠,烈日当空,偶尔能看到仙人掌。");
Location* temple = new Location("沙漠神殿", "隐藏在沙漠中的古老神殿,看起来充满了秘密。", true, "宝藏室");
Location* swamp = new Location("沼泽", "泥泞的沼泽地,长满了芦苇和奇怪的植物。");
Location* castle = new Location("城堡废墟", "一座古老的城堡废墟,似乎曾经发生过激烈的战斗。", true, "军械库");
Location* nether = new Location("下界入口", "一个散发着不祥气息的portal,通向另一个维度。", true, "下界要塞");
Location* end = new Location("末地", "一个漂浮在虚空中的神秘地方,中央有一个巨大的黑曜石柱。", true, "末地城");
// 设置位置连接
spawn->addConnectedLocation(forest);
spawn->addConnectedLocation(village);
forest->addConnectedLocation(spawn);
forest->addConnectedLocation(river);
forest->addConnectedLocation(mountain);
river->addConnectedLocation(forest);
river->addConnectedLocation(swamp);
mountain->addConnectedLocation(forest);
mountain->addConnectedLocation(cave);
mountain->addConnectedLocation(castle);
cave->addConnectedLocation(mountain);
cave->addConnectedLocation(nether);
village->addConnectedLocation(spawn);
village->addConnectedLocation(field);
field->addConnectedLocation(village);
field->addConnectedLocation(desert);
desert->addConnectedLocation(field);
desert->addConnectedLocation(temple);
temple->addConnectedLocation(desert);
swamp->addConnectedLocation(river);
swamp->addConnectedLocation(castle);
castle->addConnectedLocation(mountain);
castle->addConnectedLocation(swamp);
castle->addConnectedLocation(end);
nether->addConnectedLocation(cave);
end->addConnectedLocation(castle);
// 创建怪物(困难模式下增强)
vector<pair<Item*, int>> zombieLoot;
zombieLoot.push_back(make_pair(allItems[32], 20)); // 铁锭 (20%几率,比普通模式低)
zombieLoot.push_back(make_pair(allItems[1], 50)); // 木头 (50%几率,比普通模式低)
zombieLoot.push_back(make_pair(allItems[37], 30)); // 面包 (30%几率,比普通模式低)
Monster* zombie = new Monster(
"僵尸", "一种缓慢移动的undead生物,会追逐并攻击活物。",
25, 4, 2, 50, zombieLoot, false,
{PlayerEffect("虚弱", "降低攻击力", 5, 1, false)}
);
vector<pair<Item*, int>> skeletonLoot;
skeletonLoot.push_back(make_pair(allItems[28], 70)); // 箭 (70%几率)
skeletonLoot.push_back(make_pair(allItems[27], 5)); // 弓 (5%几率,比普通模式低)
skeletonLoot.push_back(make_pair(allItems[42], 15)); // 骨头 (15%几率,比普通模式低)
Monster* skeleton = new Monster(
"骷髅", "手持弓箭的undead生物,擅长远程攻击。",
25, 3, 1, 70, skeletonLoot, false,
{PlayerEffect("缓慢", "降低移动速度", 5, 1, false)}
);
vector<pair<Item*, int>> creeperLoot;
creeperLoot.push_back(make_pair(allItems[43], 50)); // 火药 (50%几率,比普通模式低)
creeperLoot.push_back(make_pair(allItems[43], 15)); // Gunpowder (15%几率,比普通模式低)
creeperLoot.push_back(make_pair(allItems[34], 2)); // 钻石 (2%几率,比普通模式低)
Monster* creeper = new Monster(
"爬行者", "绿色的生物,会悄悄靠近然后爆炸。",
25, 0, 2, 100, creeperLoot
);
vector<pair<Item*, int>> spiderLoot;
spiderLoot.push_back(make_pair(allItems[40], 60)); // 线 (60%几率,比普通模式低)
spiderLoot.push_back(make_pair(allItems[44], 20)); // 蜘蛛眼 (20%几率,比普通模式低)
spiderLoot.push_back(make_pair(allItems[1], 15)); // 木头 (15%几率,比普通模式低)
Monster* spider = new Monster(
"蜘蛛", "大型蜘蛛,在黑暗中能看清东西,会爬墙。",
20, 3, 1, 50, spiderLoot, false,
{PlayerEffect("中毒", "每回合受到伤害", 5, 1, false)}
);
vector<pair<Item*, int>> endermanLoot;
endermanLoot.push_back(make_pair(allItems[34], 20)); // 钻石 (20%几率,比普通模式低)
endermanLoot.push_back(make_pair(allItems[45], 10)); // 末影珍珠 (10%几率,比普通模式低)
endermanLoot.push_back(make_pair(allItems[10], 5)); // 黑曜石 (5%几率,比普通模式低)
Monster* enderman = new Monster(
"末影人", "高大的黑色生物,会瞬间移动,盯着它看会被攻击。",
50, 5, 3, 200, endermanLoot
);
vector<pair<Item*, int>> piglinLoot;
piglinLoot.push_back(make_pair(allItems[33], 50)); // 金锭 (50%几率,比普通模式低)
piglinLoot.push_back(make_pair(allItems[25], 15)); // 金剑 (15%几率,比普通模式低)
piglinLoot.push_back(make_pair(allItems[34], 5)); // 钻石 (5%几率,比普通模式低)
Monster* piglin = new Monster(
"猪灵", "生活在下界的生物,手持金剑,喜欢金子。",
25, 6, 2, 150, piglinLoot
);
vector<pair<Item*, int>> guardianLoot;
guardianLoot.push_back(make_pair(allItems[46], 70)); // prismarine shard (70%几率)
guardianLoot.push_back(make_pair(allItems[47], 20)); // prismarine crystal (20%几率)
guardianLoot.push_back(make_pair(allItems[48], 5)); // 海绵 (5%几率)
Monster* guardian = new Monster(
"守卫者", "生活在水中的生物,会发射激光攻击。",
35, 5, 3, 150, guardianLoot, false,
{PlayerEffect("缓慢", "降低移动速度", 5, 2, false)}
);
// BOSS怪物
vector<pair<Item*, int>> witherSkeletonLoot;
witherSkeletonLoot.push_back(make_pair(allItems[32], 80)); // 铁锭 (80%几率)
witherSkeletonLoot.push_back(make_pair(allItems[23], 30)); // 铁剑 (30%几率)
witherSkeletonLoot.push_back(make_pair(allItems[34], 15)); // 钻石 (15%几率)
witherSkeletonLoot.push_back(make_pair(allItems[26], 5)); // 钻石剑 (5%几率,非常稀有)
Monster* witherSkeleton = new Monster(
"凋灵骷髅", "下界的强大undead生物,手持石剑,会造成凋零效果。",
60, 7, 4, 300, witherSkeletonLoot, true,
{PlayerEffect("凋零", "持续受到伤害并降低生命值上限", 8, 2, false)}
);
vector<pair<Item*, int>> enderDragonLoot;
enderDragonLoot.push_back(make_pair(allItems[34], 100)); // 钻石 (100%几率)
enderDragonLoot.push_back(make_pair(allItems[50], 100)); // 龙蛋 (100%几率)
enderDragonLoot.push_back(make_pair(allItems[26], 50)); // 钻石剑 (50%几率)
enderDragonLoot.push_back(make_pair(allItems[39], 30)); // 附魔书 (30%几率)
Monster* enderDragon = new Monster(
"末影龙", "末地的守护者,强大的飞行生物,能喷出龙息。",
200, 10, 5, 1000, enderDragonLoot, true,
{PlayerEffect("火焰", "持续受到火焰伤害", 5, 3, false)}
);
// 创建事件
// 战斗事件
events.push_back(new CombatEvent(
"遭遇僵尸", "你听到身后传来低沉的呻吟声...",
35, zombie
));
events.push_back(new CombatEvent(
"遭遇骷髅", "一支箭擦着你的耳朵飞过,你看到远处有个身影...",
30, skeleton
));
events.push_back(new CombatEvent(
"遭遇爬行者", "你听到嘶嘶声,转身看到一个绿色的生物正在膨胀...",
25, creeper, true // 爬行者更可能伏击
));
events.push_back(new CombatEvent(
"遭遇蜘蛛", "黑暗中一双发光的眼睛正盯着你...",
30, spider
));
events.push_back(new CombatEvent(
"遭遇末影人", "你感到一阵寒意,一个高大的黑色身影出现在你身边...",
15, enderman
));
events.push_back(new CombatEvent(
"遭遇猪灵", "一群手持金剑的猪形生物注意到了你...",
20, piglin
));
events.push_back(new CombatEvent(
"遭遇守卫者", "水中泛起涟漪,一个奇怪的生物向你发射激光...",
25, guardian
));
events.push_back(new CombatEvent(
"遭遇凋灵骷髅", "一个手持石剑的黑色骷髅从阴影中走出...",
10, witherSkeleton, true
));
events.push_back(new CombatEvent(
"遭遇末影龙", "天空中出现一个巨大的身影,是末影龙!",
5, enderDragon, true
));
// 战利品事件
vector<Item*> chestLoot;
chestLoot.push_back(allItems[37]); // 面包
chestLoot.push_back(allItems[32]); // 铁锭
chestLoot.push_back(allItems[23]); // 铁剑
events.push_back(new LootEvent(
"发现宝箱", "你在一个隐蔽的角落发现了一个宝箱!",
8, chestLoot // 降低触发几率
));
vector<Item*> oreLoot;
oreLoot.push_back(allItems[32]); // 铁锭
oreLoot.push_back(allItems[36]); // 煤
events.push_back(new LootEvent(
"发现矿石", "你发现了一些矿石,可以开采!",
15, oreLoot
));
vector<Item*> templeLoot;
templeLoot.push_back(allItems[34]); // 钻石
templeLoot.push_back(allItems[33]); // 金锭
templeLoot.push_back(allItems[39]); // 附魔书
events.push_back(new LootEvent(
"神殿宝藏", "你在神殿深处发现了一个华丽的宝箱!",
5, templeLoot, true, "黄金钥匙" // 上锁的宝箱,需要钥匙,触发几率低
));
// 陷阱事件
events.push_back(new TrapEvent(
"绊索陷阱", "你不小心踩到了一根细 wire...",
10, 5, true, true,
PlayerEffect("缓慢", "降低移动速度", 3, 1, false)
));
events.push_back(new TrapEvent(
"压力板陷阱", "地面突然塌陷,你掉进了一个坑里!",
8, 7, true
));
events.push_back(new TrapEvent(
"尖刺陷阱", "地面弹出一排尖刺!",
12, 10, false, true,
PlayerEffect("流血", "每回合受到少量伤害", 4, 1, false)
));
// 商人事件
vector<pair<Item*, int>> villageMerchantItems;
villageMerchantItems.emplace_back(allItems[37], 3); // 面包 - 3金币
villageMerchantItems.emplace_back(allItems[38], 8); // 熟猪肉 - 8金币
villageMerchantItems.emplace_back(allItems[32], 15); // 铁锭 - 15金币
villageMerchantItems.emplace_back(allItems[23], 30); // 铁剑 - 30金币
events.push_back(new MerchantEvent(
"村庄商人", "你遇到了一个正在摆摊的村民。",
20, "铁匠", villageMerchantItems
));
vector<pair<Item*, int>> wanderingMerchantItems;
wanderingMerchantItems.emplace_back(allItems[34], 100); // 钻石 - 100金币(价格提高)
wanderingMerchantItems.emplace_back(allItems[39], 80); // 附魔书 - 80金币(价格提高)
wanderingMerchantItems.emplace_back(allItems[49], 15); // 胡萝卜 - 15金币
wanderingMerchantItems.emplace_back(allItems[51], 50); // 地图 - 50金币
events.push_back(new MerchantEvent(
"流浪商人", "一个背着大背包的商人向你打招呼。",
10, "流浪商人", wanderingMerchantItems
));
// 制作事件
events.push_back(new CraftingEvent(
"工作台", "你发现了一个工作台,可以用来制作物品。",
15, "工作台"
));
events.push_back(new CraftingEvent(
"熔炉", "你发现了一个熔炉,可以用来熔炼矿石。",
10, "熔炉"
));
events.push_back(new CraftingEvent(
"附魔台", "你发现了一个附魔台,可以给物品添加附魔。",
5, "附魔台" // 困难模式下更难发现
));
// NPC遭遇事件
vector<Item*> villagerReward;
villagerReward.push_back(allItems[51]); // 金币 x3
villagerReward.push_back(allItems[51]);
villagerReward.push_back(allItems[51]);
events.push_back(new NpcEncounterEvent(
"村庄老者", "你遇到了村庄里的长者。",
15, "村长",
"欢迎来到我们的村庄,勇敢的冒险者。最近附近的森林里有很多僵尸出没,能请你帮忙清理一下吗?",
true, villagerReward, false, -1, true,
"清除森林里的5只僵尸,返回向村长报告。",
{allItems[23], allItems[51], allItems[51], allItems[51], allItems[51]} // 铁剑和4个金币
));
events.push_back(new NpcEncounterEvent(
"探险家", "你在洞穴入口遇到了一个装备精良的探险家。",
8, "艾尔文",
"这个洞穴里有很多珍贵的矿石,但也有很多危险的生物。如果你能找到10块铁锭,我可以用一把附魔铁剑跟你交换。",
false, {}, false, -1, true,
"收集10块铁锭,与探险家艾尔文交换附魔铁剑。",
{allItems[23]} // 附魔铁剑
));
// 环境事件
events.push_back(new EnvironmentalEvent(
"大雨", "突然下起了倾盆大雨...",
20, "雨水打湿了你的衣服,让你感觉有些寒冷。",
0, -2 // 增加饥饿感
));
events.push_back(new EnvironmentalEvent(
"阳光明媚", "天空万里无云,阳光温暖。",
15, "温暖的阳光让你感到精力充沛。",
2, 0, true,
PlayerEffect("活力", "略微提升生命值恢复", 3, 1, true)
));
events.push_back(new EnvironmentalEvent(
"迷雾", "突然起了大雾,能见度很低。",
18, "你看不清周围的环境,小心迷路。",
0, 0, true,
PlayerEffect("迷茫", "降低闪避率", 4, 1, false)
));
events.push_back(new EnvironmentalEvent(
"浆果丛", "你发现了一片浆果丛。",
10, "你采摘了一些浆果。",
3, 2 // 恢复少量生命值和饥饿值
));
// 剧情触发事件
events.push_back(new StoryTriggerEvent(
"神秘符号", "你在一块石头上发现了奇怪的符号。",
5, 1 // 触发剧情1
));
events.push_back(new StoryTriggerEvent(
"古老石碑", "你发现了一块刻有文字的古老石碑。",
5, 2 // 触发剧情2
));
events.push_back(new StoryTriggerEvent(
"传送门", "你找到了一个散发着能量的传送门。",
3, 3 // 触发剧情3
));
// 为每个位置分配事件
spawn->addEvent(events[1]); // 骷髅
spawn->addEvent(events[5]); // 宝箱
spawn->addEvent(events[10]); // 工作台
forest->addEvent(events[0]); // 僵尸
forest->addEvent(events[3]); // 蜘蛛
forest->addEvent(events[6]); // 绊索陷阱
forest->addEvent(events[15]); // 村庄老者
river->addEvent(events[7]); // 守卫者
river->addEvent(events[12]); // 浆果丛
river->addEvent(events[16]); // 大雨
mountain->addEvent(events[2]); // 爬行者
mountain->addEvent(events[6]); // 绊索陷阱
mountain->addEvent(events[17]); // 阳光明媚
cave->addEvent(events[4]); // 末影人
cave->addEvent(events[8]); // 尖刺陷阱
cave->addEvent(events[11]); // 熔炉
cave->addEvent(events[18]); // 探险家
village->addEvent(events[9]); // 村庄商人
village->addEvent(events[15]); // 村庄老者
village->addEvent(events[10]); // 工作台
field->addEvent(events[1]); // 骷髅
field->addEvent(events[5]); // 宝箱
field->addEvent(events[16]); // 大雨
desert->addEvent(events[13]); // 流浪商人
desert->addEvent(events[19]); // 迷雾
desert->addEvent(events[20]); // 神秘符号
temple->addEvent(events[8]); // 尖刺陷阱
temple->addEvent(events[6]); // 神殿宝藏
temple->addEvent(events[21]); // 古老石碑
swamp->addEvent(events[3]); // 蜘蛛
swamp->addEvent(events[7]); // 守卫者
swamp->addEvent(events[19]); // 迷雾
castle->addEvent(events[8]); // 凋灵骷髅
castle->addEvent(events[5]); // 宝箱
castle->addEvent(events[12]); // 附魔台
castle->addEvent(events[22]); // 传送门
nether->addEvent(events[5]); // 猪灵
nether->addEvent(events[8]); // 凋灵骷髅
nether->addEvent(events[7]); // 压力板陷阱
end->addEvent(events[9]); // 末影龙
end->addEvent(events[4]); // 末影人
end->addEvent(events[22]); // 传送门
// 添加位置到游戏
locations.push_back(spawn);
locations.push_back(forest);
locations.push_back(river);
locations.push_back(mountain);
locations.push_back(cave);
locations.push_back(village);
locations.push_back(field);
locations.push_back(desert);
locations.push_back(temple);
locations.push_back(swamp);
locations.push_back(castle);
locations.push_back(nether);
locations.push_back(end);
// 创建剧情
vector<string> story1Para;
story1Para.push_back("你仔细观察着石头上的神秘符号,它们似乎在讲述一个古老的传说。");
story1Para.push_back("传说中,世界被分为三个维度:主世界、下界和末地。");
story1Para.push_back("末地由一条强大的末影龙守护着,它是世界平衡的关键。");
story1Para.push_back("最近,有迹象表明末影龙的力量正在衰退,这可能导致世界失衡。");
story1Para.push_back("村民们相信,只有找到散落在三个维度的神圣水晶,才能恢复末影龙的力量。");
stories.push_back(new Story(
1, "世界的传说", story1Para, true, 1,
{}, 1,
{allItems[51], allItems[51], allItems[51], allItems[51], allItems[51]}, // 5个金币
2 // 下一段剧情ID
));
vector<string> story2Para;
story2Para.push_back("石碑上的文字记录了寻找第一个神圣水晶的线索。");
story2Para.push_back("'在沙漠的心脏,太阳最烈之处,神殿之下,守护者等待着。'");
story2Para.push_back("看来你需要前往沙漠神殿,找到第一个神圣水晶。");
story2Para.push_back("但要小心,神殿里布满了陷阱,还有强大的守护者。");
stories.push_back(new Story(
2, "第一个水晶", story2Para, true, 2,
{{"地图", 1}}, 3, // 需要地图,等级3以上
{allItems[39], allItems[51], allItems[51], allItems[51], allItems[51], allItems[51], allItems[51]}, // 附魔书和6个金币
3 // 下一段剧情ID
));
vector<string> story3Para;
story3Para.push_back("你站在传送门前,感受到它散发的强大能量。");
story3Para.push_back("这是通往末地的入口,末影龙就在里面。");
story3Para.push_back("但在挑战末影龙之前,你可能需要先去下界寻找一些资源。");
story3Para.push_back("下界有强大的生物,但也有制作强力装备的稀有材料。");
story3Para.push_back("准备好后,穿过这个传送门,去完成你的使命吧!");
stories.push_back(new Story(
3, "末地之旅", story3Para, true, 3,
{{"神圣水晶", 1}, {"钻石剑", 1}}, 8, // 需要神圣水晶、钻石剑,等级8以上
{allItems[26], allItems[34], allItems[34], allItems[34]}, // 钻石剑和3个钻石
-1 // 没有下一段剧情
));
// 支线剧情1
vector<string> sideStory1Para;
sideStory1Para.push_back("铁匠告诉你,他的祖传锤子被一群僵尸偷走了。");
sideStory1Para.push_back("那把锤子对他的工作至关重要,如果能找回来,他愿意用一套铁盔甲作为报酬。");
sideStory1Para.push_back("僵尸们把锤子藏在森林深处的一个洞穴里。");
stories.push_back(new Story(
101, "铁匠的锤子", sideStory1Para, false, 0,
{}, 2,
{allItems[29], allItems[30], allItems[31], allItems[32]} // 铁头盔、铁胸甲、铁leggings、铁靴子
));
// 支线剧情2
vector<string> sideStory2Para;
sideStory2Para.push_back("村长告诉你,村庄的水井最近干涸了。");
sideStory2Para.push_back("村民们需要水来灌溉庄稼,否则今年会颗粒无收。");
sideStory2Para.push_back("他听说河流上游有一个水源头,可能被什么东西堵住了。");
sideStory2Para.push_back("如果你能疏通水源,村民们会非常感激你。");
stories.push_back(new Story(
102, "干涸的水井", sideStory2Para, false, 0,
{}, 2,
{allItems[37], allItems[37], allItems[37], allItems[38], allItems[38]} // 3个面包和2个熟猪肉
));
// 设置玩家初始位置
player->setCurrentLocation(spawn);
// 给玩家初始物品
player->addItem(allItems[1]->clone()); // 木头
player->addItem(allItems[1]->clone()); // 木头
player->addItem(allItems[51]->clone()); // 金币
player->addItem(allItems[51]->clone()); // 金币
player->addItem(allItems[24]->clone()); // 木剑
player->addItem(allItems[37]->clone()); // 面包
player->addItem(allItems[37]->clone()); // 面包
}
// 实现Game类的start方法
void Game::start() {
cout << "欢迎来到我的世界冒险!" << endl;
cout << "在这个危险的世界中,你需要探索、战斗、制作装备,完成你的使命。" << endl;
cout << "困难模式提示:强大的武器和装备更难获得,怪物更强大,逃跑更困难,死亡惩罚更严重。" << endl;
cout << "祝你好运,冒险家!" << endl;
// 显示初始剧情
player->getCurrentLocation()->displayInfo();
checkAndTriggerStories();
// 游戏主循环
while (running) {
showMainMenu();
string input;
cin >> input;
handleInput(input);
}
}
// 实现Game类的showMainMenu方法
void Game::showMainMenu() {
cout << endl << "=====================================" << endl;
cout << player->getName() << " (等级 " << player->getLevel() << ")" << endl;
cout << "生命值: " << player->getHealth() << "/" << player->getMaxHealth() << endl;
cout << "饥饿值: " << player->getHunger() << "/20" << endl;
cout << "经验值: " << player->getExperience() << "/" << player->getExperienceToNextLevel() << endl;
cout << "当前位置: " << player->getCurrentLocation()->getName() << endl;
cout << "=====================================" << endl;
cout << "请选择行动:" << endl;
cout << "1. 探索当前区域" << endl;
cout << "2. 前往其他区域" << endl;
cout << "3. 查看inventory" << endl;
cout << "4. 查看装备" << endl;
cout << "5. 装备物品" << endl;
cout << "6. 卸下装备" << endl;
cout << "7. 使用物品" << endl;
cout << "8. 修理装备" << endl;
cout << "9. 查看任务" << endl;
cout << "0. 退出游戏" << endl;
cout << "请输入选择: ";
}
// 实现Game类的handleInput方法
void Game::handleInput(const string& input) {
if (input == "1") {
// 探索当前区域
exploreCurrentLocation();
// 每探索一次消耗一点饥饿值
player->changeHunger(-1);
} else if (input == "2") {
// 前往其他区域
continueExploring();
// 移动消耗更多饥饿值
player->changeHunger(-2);
} else if (input == "3") {
// 查看inventory
player->viewInventory();
} else if (input == "4") {
// 查看装备
player->viewEquipment();
} else if (input == "5") {
// 装备物品
player->viewInventory();
cout << "请输入要装备的物品名称: ";
string itemName;
cin.ignore();
getline(cin, itemName);
Item* item = player->removeItem(itemName);
if (item) {
Equipment* equip = dynamic_cast<Equipment*>(item);
if (equip) {
player->equip(equip);
} else {
cout << "这不是可装备的物品。" << endl;
player->addItem(item); // 放回inventory
}
} else {
cout << "你没有这个物品。" << endl;
}
} else if (input == "6") {
// 卸下装备
cout << "请选择要卸下的装备类型: 1.武器 2.盔甲" << endl;
int choice;
cin >> choice;
ItemType type = (choice == 1) ? ItemType::WEAPON : ItemType::ARMOR;
Equipment* unequipped = player->unequip(type);
if (unequipped) {
player->addItem(unequipped);
}
} else if (input == "7") {
// 使用物品
player->viewInventory();
cout << "请输入要使用的物品名称: ";
string itemName;
cin.ignore();
getline(cin, itemName);
player->useItem(itemName);
} else if (input == "8") {
// 修理装备
player->viewEquipment();
cout << "请输入要修理的装备名称: ";
string equipName;
cin.ignore();
getline(cin, equipName);
cout << "请输入要修理的耐久度(1-50): ";
int amount;
cin >> amount;
amount = max(1, min(50, amount));
player->repairEquipment(equipName, amount);
} else if (input == "9") {
// 查看任务
viewQuests();
} else if (input == "0") {
// 退出游戏
cout << "感谢游玩!再见!" << endl;
running = false;
} else {
cout << "无效的选择,请重试。" << endl;
}
}
// 实现Game类的exploreCurrentLocation方法
void Game::exploreCurrentLocation() {
Location* current = player->getCurrentLocation();
cout << endl << "你开始探索" << current->getName() << "..." << endl;
// 标记为已访问
current->setVisited(true);
// 检查是否有事件触发
const vector<Event*>& events = current->getPossibleEvents();
if (events.empty()) {
cout << "你没有发现任何特别的东西。" << endl;
return;
}
// 随机选择一个事件
int totalChance = 0;
for (const auto& event : events) {
totalChance += event->getTriggerChance();
}
int random = rand() % totalChance + 1;
int cumulative = 0;
Event* triggeredEvent = nullptr;
for (const auto& event : events) {
cumulative += event->getTriggerChance();
if (random <= cumulative) {
triggeredEvent = event;
break;
}
}
if (triggeredEvent) {
triggeredEvent->trigger(player, this);
// 如果事件不可重复,从位置中移除
if (!triggeredEvent->isRepeatable()) {
// 创建新的事件列表,不包含已触发的不可重复事件
vector<Event*> newEvents;
for (const auto& event : current->getPossibleEvents()) {
if (event != triggeredEvent) {
newEvents.push_back(event);
}
}
// 清空并重新添加事件
// 注意:这里只是模拟,实际应修改Location类添加removeEvent方法
}
} else {
cout << "你仔细搜索了一番,但没有发现什么特别的。" << endl;
}
// 检查并触发剧情
checkAndTriggerStories();
}
// 实现Game类的continueExploring方法
void Game::continueExploring() {
Location* current = player->getCurrentLocation();
const vector<Location*>& connected = current->getConnectedLocations();
if (connected.empty()) {
cout << "这里没有可以去的地方。" << endl;
return;
}
cout << endl << "你可以前往以下地方:" << endl;
for (size_t i = 0; i < connected.size(); ++i) {
cout << i + 1 << ". " << connected[i]->getName() << (connected[i]->isVisited() ? " (已访问)" : "") << endl;
}
cout << "请选择要去的地方 (1-" << connected.size() << "): ";
int choice;
cin >> choice;
if (choice < 1 || choice > (int)connected.size()) {
cout << "无效的选择。" << endl;
return;
}
// 移动到新位置
Location* newLocation = connected[choice - 1];
player->setCurrentLocation(newLocation);
cout << "你前往了" << newLocation->getName() << endl;
newLocation->displayInfo();
// 有几率在移动中遇到怪物(困难模式下几率更高)
if (rand() % 100 < 35) { // 35%几率遇到怪物
// 从所有战斗事件中随机选择一个
vector<CombatEvent*> combatEvents;
for (const auto& event : events) {
CombatEvent* ce = dynamic_cast<CombatEvent*>(event);
if (ce) {
combatEvents.push_back(ce);
}
}
if (!combatEvents.empty()) {
int idx = rand() % combatEvents.size();
cout << "在前往" << newLocation->getName() << "的路上,你遇到了敌人!" << endl;
combatEvents[idx]->trigger(player, this);
}
}
// 检查并触发剧情
checkAndTriggerStories();
}
// 实现Game类的checkAndTriggerStories方法
void Game::checkAndTriggerStories() {
// 检查是否有可触发的剧情
for (const auto& story : stories) {
if (story->canTrigger(player)) {
// 主线剧情优先
if (story->getIsMainStory() && story->getPart() == currentMainStoryPart + 1) {
story->display(player, this);
return;
}
}
}
// 如果没有主线剧情可触发,检查支线剧情
for (const auto& story : stories) {
if (story->canTrigger(player) && !story->getIsMainStory()) {
// 有一定几率触发支线剧情
if (rand() % 100 < 40) { // 40%几率
story->display(player, this);
return;
}
}
}
}
// 主函数
int main() {
// 初始化随机数生成器
srand(time(nullptr));
// 获取玩家名称
cout << "请输入你的名字: ";
string playerName;
getline(cin, playerName);
// 创建玩家
Player* player = new Player(playerName);
// 创建并初始化游戏
Game game(player);
game.initializeWorld();
// 开始游戏
game.start();
// 清理内存
delete player;
return 0;
}