Przeglądaj źródła

增加了创建各类节点的工厂

tanghai 14 lat temu
rodzic
commit
9ffda7115d

+ 2 - 1
Src/CMakeLists.txt

@@ -53,4 +53,5 @@ set(ThirdPartyLibs
 	${PERFTOOLS_DEBUG_LIBRARIES}
 )
 
-ADD_SUBDIRECTORY(Egametang)
+ADD_SUBDIRECTORY(Egametang)
+ADD_SUBDIRECTORY(Game)

+ 1 - 0
Src/Game/CMakeLists.txt

@@ -0,0 +1 @@
+ADD_SUBDIRECTORY(Event)

+ 0 - 1
Src/Game/Event/ActionIf.h

@@ -11,5 +11,4 @@ public:
 
 } // namespace Egametang
 
-
 #endif // EVENT_ACTIONIF_H

+ 22 - 12
Src/Game/Event/AndNode.cc

@@ -1,26 +1,36 @@
-#include <boost/foreach.hpp>
-#include "Base/Marcos.h"
 #include "Event/AndNode.h"
 
-namespace Egametang {
+namespace Egametang {
+
 AndNode::~AndNode()
 {
-	foreach(LogicNodeIf* node, nodes)
-	{
-		delete node;
-	}
+	delete left;
+	delete right;
 }
 
 bool AndNode::Check(ContexIf* contex)
 {
-	foreach(LogicNodeIf* node, nodes)
+	if (!left->Check(contex))
 	{
-		if (!node->Check(contex))
-		{
-			return false;
-		}
+		return false;
+	}
+	if (!right->Check(contex))
+	{
+		return false;
 	}
 	return true;
+}
+
+void AndNode::AddChildNode(NodeIf *node, int type)
+{
+	if (type == 0)
+	{
+		left = node;
+	}
+	else
+	{
+		right = node;
+	}
 }
 
 } // namespace Egametang

+ 5 - 3
Src/Game/Event/AndNode.h

@@ -1,18 +1,20 @@
 #ifndef EVENT_ANDNODE_H
 #define EVENT_ANDNODE_H
 
-#include "Event/LogicNodeIf.h"
+#include "Event/NodeIf.h"
 
 namespace Egametang {
 
-class AndNode: public LogicNodeIf
+class AndNode: public NodeIf
 {
 private:
-	std::list<LogicNodeIf*> nodes;
+	NodeIf* left;
+	NodeIf* right;
 
 public:
 	virtual ~AndNode();
 	virtual bool Check(ContexIf* contex);
+	virtual void AddChildNode(NodeIf *node, int type);
 };
 
 } // namespace Egametang

+ 25 - 0
Src/Game/Event/BuffTypeCondition.cc

@@ -0,0 +1,25 @@
+#include <boost/foreach.hpp>
+#include "Base/Marcos.h"
+#include "Event/BuffTypeCondition.h"
+
+namespace Egametang {
+BuffType::BuffType(int buff_type): buff_type(buff_type)
+{
+}
+
+bool BuffType::Check(ContexIf* contex)
+{
+	return true;
+}
+
+Egametang::BuffTypeFactory::~BuffTypeFactory()
+{
+}
+
+NodeIf* BuffTypeFactory::GetInstance(const LogicNode& conf)
+{
+	return new BuffType(conf.args(0));
+}
+
+} // namespace Egametang
+

+ 35 - 0
Src/Game/Event/BuffTypeCondition.h

@@ -0,0 +1,35 @@
+#ifndef EVENT_BUFFTYPECONDITION_H
+#define EVENT_BUFFTYPECONDITION_H
+
+#include "Event/NodeIf.h"
+
+namespace Egametang {
+
+class ContexIf;
+
+// 条件节点还可以预绑定一些配置参数,
+// 例如下面的buff_type字段由策划配置
+// 可配置成dot hot之类的, 由工厂类设置
+class BuffType: public NodeIf
+{
+private:
+	int buff_type;
+
+public:
+	BuffType(int buff_type);
+
+	virtual bool Check(ContexIf* contex);
+};
+
+class BuffTypeFactory: NodeFactoryIf
+{
+public:
+	virtual ~BuffTypeFactory();
+
+	virtual NodeIf* GetInstance(const LogicNode& conf);
+};
+
+} // namespace Egametang
+
+
+#endif // EVENT_BUFFTYPECONDITION_H

+ 3 - 26
Src/Game/Event/CMakeLists.txt

@@ -1,32 +1,9 @@
 PROTOBUF_GENERATE_CPP(proto_srcs proto_hdrs 
-	Echo.proto
+	EventConf.proto
 )
 
-SET(RpcSrc 
-	RpcCommunicator.cc
-	RpcController.cc
-	RequestHandler.cc
-	ResponseHandler.cc
-	RpcChannel.cc
-	RpcServer.cc
-	RpcSession.cc
+SET(EventSrc 
 	${proto_srcs}
 )
 
-
-ADD_LIBRARY(Rpc ${RpcSrc})
-
-ADD_EXECUTABLE(RpcCommunicatorTest RpcCommunicatorTest.cc)
-
-SET(Excutes 
-	RpcCommunicatorTest
-)
-
-FOREACH(Excute ${Excutes})
-	TARGET_LINK_LIBRARIES(${Excute}
-		Rpc
-		Thread
-		${ThirdPartyLibs}
-	)
-	ADD_TEST(${Excute} ${Excute})
-ENDFOREACH()
+ADD_LIBRARY(Event ${EventSrc})

+ 28 - 0
Src/Game/Event/ConditionFactory.cc

@@ -0,0 +1,28 @@
+#include "Event/ConditionFactory.h"
+
+namespace Egametang {
+
+ConditionFactory::ConditionFactory(): factories(100)
+{
+}
+
+ConditionFactory::~ConditionFactory()
+{
+	for (std::size_t i = 0; i < factories.size(); ++i)
+	{
+		delete factories[i];
+	}
+}
+
+void ConditionFactory::Register(int type, NodeFactoryIf* factory)
+{
+	factories[type] = factory;
+}
+
+NodeIf* ConditionFactory::GetInstance(const LogicNode& conf)
+{
+	int32 type = conf.type();
+	return factories[type]->GetInstance(conf);
+}
+
+} // namespace Egametang

+ 26 - 0
Src/Game/Event/ConditionFactory.h

@@ -0,0 +1,26 @@
+#ifndef EVENT_CONDITIONFACTORY_H
+#define EVENT_CONDITIONFACTORY_H
+
+#include <vector>
+#include "Event/NodeIf.h"
+
+namespace Egametang {
+
+class ConditionFactory
+{
+private:
+	std::vector<NodeFactoryIf*> factories;
+
+public:
+	ConditionFactory();
+
+	~ConditionFactory();
+
+	void Register(int type, NodeFactoryIf* factory);
+
+	NodeIf* GetInstance(const LogicNode& conf);
+};
+
+} // namespace Egametang
+
+#endif // EVENT_CONDITIONFACTORY_H

+ 0 - 13
Src/Game/Event/ConditionNode.cc

@@ -1,13 +0,0 @@
-#include <boost/foreach.hpp>
-#include "Base/Marcos.h"
-#include "Event/ConditionNode.h"
-
-namespace Egametang {
-
-bool BuffType::Check(ContexIf* contex)
-{
-	return true;
-}
-
-} // namespace Egametang
-

+ 0 - 22
Src/Game/Event/ConditionNode.h

@@ -1,22 +0,0 @@
-#ifndef EVENT_CONDITIONNODE_H
-#define EVENT_CONDITIONNODE_H
-
-#include "Event/ConditionNode.h"
-
-namespace Egametang {
-
-// 条件节点还可以预绑定一些配置参数,例如下面的type字段由策划配置
-// 可配置成dot hot之类的
-class BuffType: public LogicNodeIf
-{
-public:
-	int type;
-
-public:
-	virtual bool Check(ContexIf* contex);
-};
-
-} // namespace Egametang
-
-
-#endif // EVENT_CONDITIONNODE_H

+ 3 - 3
Src/Game/Event/DotFirstDamage.txt

@@ -9,15 +9,15 @@ EventConf
 		LogicNode 
 		{
 			// 4表示这是一个条件节点
-			type: 4
+			node: 4
 			// 1表示这个节点是用来判断buff类型的条件节点
-			condition: 1
+			type: 1
 			// 2表示dot
 			args:2
 		}
 	}
 	
-	// 
+	// 动作
 	ActionConf
 	{
 		// 扣血

+ 24 - 13
Src/Game/Event/Event.cc

@@ -1,11 +1,32 @@
 #include "Event/Event.h"
+#include "Event/EventConf.pb.h"
 
 namespace Egametang {
 
-Event::Event(EventConf& conf)
+Event::Event(NodeFactories& factories, EventConf& conf):
+		action(NULL), condition(NULL)
 {
-	BuildCondition(conf);
-	BuildAction(conf);
+	const ConditionConf& condition_conf = conf.condition();
+	if (condition_conf.has_node())
+	{
+		const LogicNode& node_conf = condition_conf.node();
+		BuildCondition(factories, node_conf, condition);
+	}
+}
+
+void Event::BuildCondition(
+		NodeFactories& factories, const LogicNode& conf,
+		NodeIf*& node)
+{
+	int32 type = conf.type();
+	node = factories.GetInstance(conf);
+	for (int i = 0; i < conf.nodes_size(); ++i)
+	{
+		const LogicNode& logic_node_conf = conf.nodes(i);
+		NodeIf* logic_node = NULL;
+		BuildCondition(factories, logic_node_conf, logic_node);
+		node->AddChildNode(logic_node, i);
+	}
 }
 
 Event::~Event()
@@ -14,16 +35,6 @@ Event::~Event()
 	delete action;
 }
 
-void Event::BuildCondition(EventConf& conf)
-{
-
-}
-
-void Event::BuildAction(EventConf& conf)
-{
-
-}
-
 void Event::Excute(ContexIf* contex)
 {
 	if(condition->Check(contex))

+ 12 - 7
Src/Game/Event/Event.h

@@ -2,26 +2,31 @@
 #define EVENT_EVENT_H
 
 #include "Event/ActionIf.h"
-#include "Event/LogicNodeIf.h"
+#include "Event/NodeIf.h"
 
 namespace Egametang {
 
-class EventConf;
-
 class Event
 {
 private:
 	int type;
-	LogicNodeIf* condition;
 	ActionIf* action;
+	NodeIf* condition;
+
+	void BuildCondition(
+			NodeFactories& factories, const LogicNode& conf,
+			NodeIf*& condition);
 
-	void BuildCondition(EventConf& conf);
-	void BuildAction(EventConf& conf);
+	void BuildAction(
+			NodeFactories& factories, const ConditionConf& conf);
 
 public:
-	Event(EventConf& conf);
+	Event(NodeFactories& factories, EventConf& conf);
+
 	~Event();
+
 	int Type() const;
+
 	void Excute(ContexIf* contex);
 };
 

+ 7 - 7
Src/Game/Event/EventConf.proto

@@ -3,9 +3,9 @@ package Egametang;
 message LogicNode
 {
 	// and or not condition 4种节点
-	reqired int32 type = 1;
+	required int32 node = 1;
 	// 哪种条件
-	optional int32 condition = 2;
+	optional int32 type = 2;
 	// 条件需要的参数
 	repeated int32 args = 3;
 	// 包含多个子节点
@@ -14,22 +14,22 @@ message LogicNode
 
 message ConditionConf
 {
-	optional LogicNode node = 1
+	optional LogicNode node = 1;
 };
 
 message ActionConf
 {
 	// 行为编号
-	required int32 id = 1;
+	required int32 type = 1;
 	repeated int32 args = 2;
 };
 
 message EventConf
 {
-	// 哪类事件
+	// 事件类型
 	required int32 type = 1;
 	// 条件
-	ConditionConf condition = 2;
+	required ConditionConf condition = 2;
 	// 动作
-	ActionConf action = 3;
+	required ActionConf action = 3;
 };

+ 16 - 2
Src/Game/Event/GameEvents.cc

@@ -6,9 +6,23 @@ GameEvents::GameEvents(): events(100)
 {
 }
 
-void GameEvents::AddEvent(Event& event)
+GameEvents::~GameEvents()
 {
-	events[event.Type()].push_back(event);
+	for (std::size_t i = 0; i < events.size(); ++i)
+	{
+		for (std::list<Event>::iterator iter = events[i].begin();
+			iter != events[i].end(); ++iter)
+		{
+			delete *iter;
+		}
+	}
+}
+
+void GameEvents::AddEvent(EventConf& conf)
+{
+	int32 type = conf.type();
+	Event* event = NULL;
+	events[type].push_back(event);
 }
 
 void GameEvents::Excute(int type, ContexIf* contex)

+ 4 - 1
Src/Game/Event/GameEvents.h

@@ -17,12 +17,15 @@ enum
 class GameEvents
 {
 private:
+	ConditionFactory factories;
 	std::vector<std::list<Event> > events;
 
 public:
 	GameEvents();
 
-	void AddEvent(Event& event);
+	~GameEvents();
+
+	void AddEvent(EventConf& conf);
 
 	void Excute(int type, ContexIf* contex);
 };

+ 0 - 30
Src/Game/Event/LogicNodeIf.h

@@ -1,30 +0,0 @@
-#ifndef EVENT_LOGICNODEIF_H
-#define EVENT_LOGICNODEIF_H
-
-namespace Egametang {
-
-class Spell
-{
-
-};
-
-class Buff
-{
-public:
-	int type;
-};
-
-class ContexIf
-{
-};
-
-class LogicNodeIf
-{
-public:
-	virtual ~LogicNodeIf();
-	virtual bool Check(ContexIf* contex) = 0;
-};
-
-} // namespace Egametang
-
-#endif // EVENT_LOGICNODEIF_H

+ 41 - 0
Src/Game/Event/NodeFactories.cc

@@ -0,0 +1,41 @@
+#include "Event/NodeFactories.h"
+
+namespace Egametang {
+
+NodeIf* AndNodeFactory::GetInstance(const LogicNode& conf)
+{
+	return new AndNode();
+}
+
+NodeIf* AndNodeFactory::GetInstance(const LogicNode& conf)
+{
+	return new OrNode();
+}
+
+NodeIf* AndNodeFactory::GetInstance(const LogicNode& conf)
+{
+	return new NotNode();
+}
+
+NodeFactories::NodeFactories(ConditionFactory* condition_factory):
+		node_factories(4)
+{
+	node_factories[0] = new AndNodeFactory();
+	node_factories[1] = new OrNodeFactory();
+	node_factories[2] = new NotNodeFactory();
+	node_factories[3] = condition_factory;
+}
+
+NodeFactories::~NodeFactories()
+{
+}
+
+NodeIf* NodeFactories::GetInstance(const LogicNode& conf)
+{
+	int32 type = conf.type();
+	return node_factories[type]->GetInstance(conf);
+}
+
+}
+
+

+ 45 - 0
Src/Game/Event/NodeFactories.h

@@ -0,0 +1,45 @@
+#ifndef EVENT_NODEFACTORIES_H
+#define EVENT_NODEFACTORIES_H
+
+#include <vector>
+#include "Event/NodeIf.h"
+
+namespace Egametang {
+
+class AndNodeFactory: public NodeFactoryIf
+{
+public:
+	virtual NodeIf* GetInstance(const LogicNode& conf);
+};
+
+
+class OrNodeFactory: public NodeFactoryIf
+{
+public:
+	virtual NodeIf* GetInstance(const LogicNode& conf);
+};
+
+
+class NotNodeFactory: public NodeFactoryIf
+{
+public:
+	virtual NodeIf* GetInstance(const LogicNode& conf);
+};
+
+
+class NodeFactories
+{
+private:
+	std::vector<NodeFactoryIf*> node_factories;
+
+public:
+	NodeFactories(ConditionFactory* condition_factory);
+
+	virtual ~NodeFactories();
+
+	virtual NodeIf* GetInstance(const LogicNode& conf);
+};
+
+} // namespace Egametang
+
+#endif // EVENT_NODEFACTORIES_H

+ 31 - 0
Src/Game/Event/NodeIf.h

@@ -0,0 +1,31 @@
+#ifndef EVENT_NODEIF_H
+#define EVENT_NODEIF_H
+
+namespace Egametang {
+
+class ContexIf
+{
+};
+
+enum ChildNodeType
+{
+	LEFT  = 0,
+	RIGHT = 1,
+};
+
+class NodeIf
+{
+public:
+	virtual bool Check(ContexIf* contex) = 0;
+	virtual void AddChildNode(NodeIf *node, int type);
+};
+
+class NodeFactoryIf
+{
+public:
+	virtual NodeIf* GetInstance(const LogicNode& conf) = 0;
+};
+
+} // namespace Egametang
+
+#endif // EVENT_NODEIF_H

+ 7 - 2
Src/Game/Event/NotNode.cc

@@ -1,5 +1,4 @@
-#include <boost/foreach.hpp>
-#include "Base/Marcos.h"
+#include <glog/logging.h>
 #include "Event/NotNode.h"
 
 namespace Egametang {
@@ -13,5 +12,11 @@ bool NotNode::Check(ContexIf* contex)
 	return !node->Check(contex);
 }
 
+void NotNode::AddChildNode(NodeIf *node, int type)
+{
+	CHECK_EQ(1, type);
+	this->node = node;
+}
+
 } // namespace Egametang
 

+ 4 - 3
Src/Game/Event/NotNode.h

@@ -1,18 +1,19 @@
 #ifndef EVENT_NOTNODE_H
 #define EVENT_NOTNODE_H
 
-#include "Event/LogicNodeIf.h"
+#include "Event/NodeIf.h"
 
 namespace Egametang {
 
-class NotNode: public LogicNodeIf
+class NotNode: public NodeIf
 {
 private:
-	LogicNodeIf* node;
+	NodeIf* node;
 
 public:
 	virtual ~NotNode();
 	virtual bool Check(ContexIf* contex);
+	virtual void AddChildNode(NodeIf *node, int type);
 };
 
 } // namespace Egametang

+ 20 - 11
Src/Game/Event/OrNode.cc

@@ -1,27 +1,36 @@
-#include <boost/foreach.hpp>
-#include "Base/Marcos.h"
 #include "Event/OrNode.h"
 
 namespace Egametang {
 OrNode::~OrNode()
 {
-	foreach(LogicNodeIf* node, nodes)
-	{
-		delete node;
-	}
+	delete left;
+	delete right;
 }
 
 bool OrNode::Check(ContexIf* contex)
 {
-	foreach(LogicNodeIf* node, nodes)
+	if (left->Check(contex))
+	{
+		return true;
+	}
+	if (right->Check(contex))
 	{
-		if (node->Check(contex))
-		{
-			return true;
-		}
+		return true;
 	}
 	return false;
 }
 
+void OrNode::AddChildNode(NodeIf *node, int type)
+{
+	if (type == 0)
+	{
+		left = node;
+	}
+	else
+	{
+		right = node;
+	}
+}
+
 } // namespace Egametang
 

+ 5 - 3
Src/Game/Event/OrNode.h

@@ -1,18 +1,20 @@
 #ifndef EVENT_ORNODE_H
 #define EVENT_ORNODE_H
 
-#include "Event/LogicNodeIf.h"
+#include "Event/NodeIf.h"
 
 namespace Egametang {
 
-class OrNode: public LogicNodeIf
+class OrNode: public NodeIf
 {
 private:
-	std::list<LogicNodeIf*> nodes;
+	NodeIf* left;
+	NodeIf* right;
 
 public:
 	virtual ~OrNode();
 	virtual bool Check(ContexIf* contex);
+	virtual void AddChildNode(NodeIf *node, int type);
 };
 
 } // namespace Egametang