C++格斗游戲效果視頻
1.案例簡介
#include "broadSword.h"
//構造函數
BroadSword::BroadSword()
{
?? ?FileManager fm;
?? ?map<string, map<string, string>> mWeapon;
?? ?fm.loadCSVData("Weapons.csv", mWeapon);
?? ?//武器id
?? ?string id = mWeapon["2"]["weaponId"];
?? ?//武器名稱
?? ?this->weaponName = mWeapon[id]["weaponName"];
?? ?//武器攻擊力
?? ?this->baseDamage = atoi(mWeapon[id]["weaponAtk"].c_str());
?? ?//武器暴擊系數
?? ?this->critPlus = atoi(mWeapon[id]["weaponCritPlus"].c_str());
?? ?//武器暴擊率
?? ?this->critRate = atoi(mWeapon[id]["weaponCritRate"].c_str());
?? ?//武器吸血系數
?? ?this->suckPlus = atoi(mWeapon[id]["weaponSuckPlus"].c_str());
?? ?//武器吸血率
?? ?this->suckRate = atoi(mWeapon[id]["weaponSuckRate"].c_str());
?? ?//武器冰凍率
?? ?this->frozenRate = atoi(mWeapon[id]["weaponFrozenRate"].c_str());
}
//獲取基礎傷害
int BroadSword::getBaseDamage()
{
?? ?return this->baseDamage;
}
//暴擊效果 ?返回值大于0 觸發暴擊 否則不觸發
int BroadSword::getCrit()
{
?? ?if (isTrigger(this->critRate))
?? ?{
?? ??? ?return this->baseDamage * this->critPlus;
?? ?}
?? ?else
?? ?{
?? ??? ?return 0;
?? ?}
}
//吸血效果 返回值大于0 觸發吸血 否則不觸發
int BroadSword::getSuckBlood()
{
?? ?if (isTrigger(this->suckRate))
?? ?{
?? ??? ?return this->baseDamage * this->suckPlus;
?? ?}
?? ?else
?? ?{
?? ??? ?return 0;
?? ?}
}
//冰凍效果 返回true代表觸發 否則不觸發
bool BroadSword::getFrozen()
{
?? ?if (isTrigger(this->frozenRate))
?? ?{
?? ??? ?return true;
?? ?}
?? ?else
?? ?{
?? ??? ?return false;
?? ?}
}
//觸發概率的算法
bool BroadSword::isTrigger(int rate)
{
?? ?int num = rand() % 100 + 1; ?// 1 ~ 100
?? ?if (num <= rate)
?? ?{
?? ??? ?return true;
?? ?}
?? ?return false;
}
2.CSV文件制作
3.解析單行CSV數據
#include "fileManager.h"
//加載CSV格式文件
void FileManager::loadCSVData(string path, map<string, map<string, string>>& mData)
{
?? ?//讀文件
?? ?ifstream ifs(path);
?? ?if (!ifs.is_open())
?? ?{
?? ??? ?cout << "文件打開失敗" << endl;
?? ??? ?return;
?? ?}
?? ?//第一個數據
?? ?string firstLine;
?? ?ifs >> firstLine;
?? ?//cout << "第一行數據為: " << firstLine << endl;
?? ?//heroId,heroName,heroHp,heroAtk,heroDef,heroInfo
?? ?vector<string>vFirst; //第一行解析后數據放入的容器
?? ?this->parseLineToVector(firstLine, vFirst);
?? ?//測試
?? ?/*for (vector<string>::iterator it = vFirst.begin(); it != vFirst.end(); it++)
?? ?{
?? ??? ?cout << *it << endl;?
?? ?}*/
?? ?string otherLine;
?? ?while (ifs >> otherLine)
?? ?{
?? ??? ?//cout << "otherLine = " << otherLine << endl;
?? ??? ?vector<string>vOther;
?? ??? ?this->parseLineToVector(otherLine, vOther);
?? ??? ?map<string, string>m;
?? ??? ?for (int i = 0; i < vFirst.size(); i++)
?? ??? ?{
?? ??? ??? ?m.insert(make_pair(vFirst[i], vOther[i]));
?? ??? ?}
?? ??? ?//將小map容器插入到大map容器中
?? ??? ?mData.insert(make_pair(vOther[0], m));
?? ?}
?? ?//cout << "第一個英雄姓名: " << mData["1"]["heroName"] << endl;
?? ?//cout << "第二個英雄血量: " << mData["2"]["heroHp"] << endl;
?? ?//cout << "第三個英雄攻擊力: " << mData["3"]["heroAtk"] << endl;
}
//解析單行數據到vector容器中
void FileManager::parseLineToVector(string line, vector<string>& v)
{
?? ?int pos = -1;
?? ?int start = 0;
?? ?while (true)
?? ?{
?? ??? ?pos = (int)line.find(",", start);
?? ??? ?if (pos == -1)
?? ??? ?{
?? ??? ??? ?//最后一個單詞處理
?? ??? ??? ?string temp = line.substr(start);
?? ??? ??? ?v.push_back(temp);
?? ??? ??? ?break;
?? ??? ?}
?? ??? ?string temp = line.substr(start, pos - start);
?? ??? ?v.push_back(temp);
?? ??? ?start = pos + 1;
?? ?}
}