瀏覽代碼

Double visitor?

Vincent Stumpf 1 年之前
父節點
當前提交
95ee5d04e6

+ 32 - 6
src/map/skills/main.cpp

@@ -1,15 +1,41 @@
 
 #include "skill.hpp"
 #include "skills.hpp"
-#include "swordsman/bash.hpp"
-#include "swordsman/provoke.hpp"
+// #include "swordsman/bash.hpp"
+// #include "swordsman/provoke.hpp"
+
+#include "skilllist.hpp"
+
+
 
 int main() {
-	Bash bash;
-	Provoke provoke;
 
-	bash.castend_damage_id();
-	provoke.castend_nodamage_id();
+	constexpr int skill_id = SM_BASH;
+
+	const auto &sk = skill_db.at(static_cast<e_skill>(skill_id));
+
+	const SkillImpl sk2 = Bash{};
+
+	std::visit([](auto &skill) {
+		skill.getSkillID();
+
+		skill.castend_damage_id(); // error
+		// skill.hpp:19:26: error: ‘const class Provoke’ has no member named ‘castendDamageId’; did you mean ‘castendNoDamageId’?
+		//    19 |   return as_underlying().castendDamageId();
+		//       |          ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~
+		//       |          castendNoDamageId
+
+	}, sk2);
+
+
+	
+	for (auto &it : skill_db) {
+		std::visit([](auto &skill) {
+			skill.getSkillID();
+
+			// skill.castend_damage_id();
+		}, it.second);
+	}
 
 	return 0;
 }

+ 14 - 6
src/map/skills/skill.hpp

@@ -15,16 +15,20 @@ constexpr int MAX_SKILL_LEVEL = 13;
 template <typename T>
 class Skill {
 public:
-	int castend_damage_id() {
-		return as_underlying().castend_damage_id();
+	int castend_damage_id() const {
+		return as_underlying().castendDamageId();
 	};
-	int castend_nodamage_id() {
-		return as_underlying().castend_nodamage_id();
+	int castend_nodamage_id() const {
+		return as_underlying().castendNoDamageId();
 	};
-	int castend_pos2() {
-		return as_underlying().castend_pos2();
+	int castend_pos2() const {
+		return as_underlying().castendPos2();
 	};
 
+	uint16_t getSkillID() const {
+		return nameid;
+	}
+
 protected:
 	explicit Skill(e_skill skillid) : nameid(static_cast<uint16_t>(skillid)) {};
 private:
@@ -38,6 +42,10 @@ private:
 	inline T& as_underlying() {
 		return static_cast<T&>(*this);
 	}
+
+	inline const T& as_underlying() const {
+		return static_cast<const T&>(*this);
+	}
 };
 
 

+ 15 - 0
src/map/skills/skilllist.hpp

@@ -0,0 +1,15 @@
+#include <variant>
+
+#include "skill.hpp"
+#include "skills.hpp"
+
+#include "swordsman/bash.hpp"
+#include "swordsman/provoke.hpp"
+
+
+using SkillImpl = std::variant<Bash, Provoke>;
+
+std::unordered_map<e_skill, SkillImpl> skill_db = {
+	{ e_skill::SM_BASH, Bash{} },
+	{ e_skill::SM_PROVOKE, Provoke{} }
+};

+ 1 - 7
src/map/skills/skills.hpp

@@ -5,13 +5,7 @@
 #define MAP_SKILLS_HPP
 
 #include <unordered_map>
-
-enum e_skill;
-
-template <typename T>
-class Skill;
-
-inline std::unordered_map<e_skill, Skill> skills;
+#include "skill.hpp"
 
 /// List of Skills
 enum e_skill {

+ 2 - 2
src/map/skills/swordsman/bash.hpp

@@ -5,9 +5,9 @@
 #include "../skills.hpp"
 
 
-class Bash : Skill<Bash> {
+class Bash : public Skill<Bash> {
 public:
-	int castend_damage_id() {
+	int castendDamageId() const {
 		return 0;
 	};
 

+ 2 - 2
src/map/skills/swordsman/provoke.hpp

@@ -4,9 +4,9 @@
 #include "../skill.hpp"
 
 
-class Provoke : Skill<Provoke> {
+class Provoke : public Skill<Provoke> {
 public:
-	int castend_nodamage_id() {
+	int castendNoDamageId() const {
 		return 0;
 	};