//读取配置文件 反射创建对象 //配置状态机 //private void ConfigFSM() //{ // //创建列表 // states = new List<FSMState>(); // //创建状态 // IdleState idle = new IdleState(); // //添加到列表中 // states.Add(idle); // //配置状态(添加映射) // idle.AddMap(FSMTriggerID.NoHealth, FSMStateID.Dead); // idle.AddMap(FSMTriggerID.SawTarget, FSMStateID.Pursuit); // DeadState dead = new DeadState(); // states.Add(dead); // PursuitState pursuit = new PursuitState(); // pursuit.AddMap(FSMTriggerID.NoHealth, FSMStateID.Dead); // pursuit.AddMap(FSMTriggerID.ReachTarget, FSMStateID.Attacking); // pursuit.AddMap(FSMTriggerID.LoseTarget, FSMStateID.Default); // states.Add(pursuit); // AttackingState attack = new AttackingState(); // attack.AddMap(FSMTriggerID.NoHealth, FSMStateID.Dead); // attack.AddMap(FSMTriggerID.WithoutAttackRange, FSMStateID.Pursuit); // attack.AddMap(FSMTriggerID.KilledTarget, FSMStateID.Default); // states.Add(attack); // PatrollingState patrolling = new PatrollingState(); // patrolling.AddMap(FSMTriggerID.NoHealth, FSMStateID.Dead); // patrolling.AddMap(FSMTriggerID.SawTarget, FSMStateID.Pursuit); // patrolling.AddMap(FSMTriggerID.CompletePatrol, FSMStateID.Idle); // states.Add(patrolling); //} //当前状态 private void ConfigFSM() { //var map = AIConfigurationReader.map; //var map = new AIConfigurationReader(configFile).map; var map = AIConfigurationReaderFactory.GetConfigMap(configFile); //作业: //读取配置文件 //形成数据结构 Dictionary<状态, Dictionary<条件编号,状态编号>> //注意:行数据的可能性[Idle] NoHealth>Dead 空 states = new List <FSMState>(); //遍历大字典 状态 //遍历小字典 映射 foreach (string stateName in map.Keys) { Type type = Type.GetType("AI.FSM." + stateName + "State"); FSMState stateObj = Activator.CreateInstance(type) as FSMState; states.Add(stateObj); foreach (var item in map[stateName]) { //item.Key //item.Value var triggerID = (FSMTriggerID)Enum.Parse(typeof(FSMTriggerID), item.Key); var stateID = (FSMStateID)Enum.Parse(typeof(FSMStateID), item.Value); stateObj.AddMap(triggerID, stateID); } } }
private void ConfigFSM() { stateList = new List <FSMState>(); var configMap = AIConfigurationReaderFactory.GetConfigDic(aiConfigFile); foreach (var stateName in configMap.Keys) { //创建状态对象 Type type = Type.GetType("AI.FSM." + stateName + "State"); FSMState stateObject = Activator.CreateInstance(type) as FSMState; //配置状态映射 foreach (var map in configMap[stateName]) { //map.Key : 条件编号 //map.Value:状态编号 FSMTriggerID triggerID = (FSMTriggerID)Enum.Parse(typeof(FSMTriggerID), map.Key); FSMStateID stateID = (FSMStateID)Enum.Parse(typeof(FSMStateID), map.Value); stateObject.AddMap(triggerID, stateID); } //加入集合 stateList.Add(stateObject); } }
//--通过配置文件 private void ConfigFSM() { states = new List <FSMState>(); var Map = AIConfigReaderFactory.GetMap(fileName); foreach (var state in Map) { //state.Key 状态名称 //state.Value 映射 Type type = Type.GetType("AI.FSM." + state.Key + "State"); FSMState stateObj = Activator.CreateInstance(type) as FSMState; states.Add(stateObj); foreach (var map in state.Value) { //map.Key 条件编号 //map.Value 状态编号 FSMTriggerID triggerID = (FSMTriggerID)Enum.Parse(typeof(FSMTriggerID), map.Key); FSMStateID stateID = (FSMStateID)Enum.Parse(typeof(FSMStateID), map.Value); //添加映射 stateObj.AddMap(triggerID, stateID); } } }