public void Init() { mMapMgr = MapMgr.Instance; mOpenList = new List <Node>(); mCloseList = new List <Node>(); mRes = new List <Node>(); HTLogger.Info("AStarMgr init done."); }
protected override BTNodeStatus OnExcute(IAgent agent, Blackboard bb) { AIEntity entity = (AIEntity)agent; bb.SetValue(BlackboardKey.TargetMovePos, entity.Company.position); entity.Model.Thinking.Value = mThinkingArray[UnityEngine.Random.Range(0, mThinkingArray.Length)]; HTLogger.Debug("感觉贫穷,设置目标地点:Company,坐标:" + entity.Company.position); return(BTNodeStatus.Finished); }
private void GenerateRandomMap() { mTimer.Begin(TIMER_KEY_CREATE_MAP); mCurrentSeed = RandomMapCreator.Instance.TryToCreate(Probability, RandomSeed, MaxTimes); mTimer.End(TIMER_KEY_CREATE_MAP); HTLogger.Debug(string.Format("生成随机地图耗时:{0}毫秒", mTimer.GetSumTime(TIMER_KEY_CREATE_MAP))); HTLogger.Debug("随机种子:" + mCurrentSeed); SpawnMap(); }
public void Save <T>(T data, string path) { string json = JsonUtility.ToJson(data); File.WriteAllText(path, json); #if UNITY_EDITOR UnityEditor.AssetDatabase.Refresh(); #endif HTLogger.Debug("Save map done."); }
private void Init() { LoggerService.Instance.Init(); HTLogger.Info("LoggerService init done."); mTxt_Num_Food = GetTextFromSlider(Slider_Food, TXT_NUM); mTxt_Num_Water = GetTextFromSlider(Slider_Water, TXT_NUM); mTxt_Num_Energy = GetTextFromSlider(Slider_Energy, TXT_NUM); mTxt_Num_Mood = GetTextFromSlider(Slider_Mood, TXT_NUM); GameFacade.Instance.Init(this); HTLogger.Info("GameFacade init done."); }
public RunningData GetRunningData(int key) { RunningData data; if (mDataDict.TryGetValue(key, out data) && data != null) { return(data); } HTLogger.Error("Get RunningData fail."); return(null); }
public T Load <T>(string path) { string json = File.ReadAllText(path); T data = JsonUtility.FromJson <T>(json); if (data == null) { HTLogger.Debug("Load map fail."); } HTLogger.Debug("Load map done."); return(data); }
protected override BTNodeStatus OnExcute(IAgent agent, Blackboard bb) { AIEntity entity = (AIEntity)agent; Vector3 target = bb.GetValue(BlackboardKey.TargetMovePos, Vector3.one); entity.Move(target); if (entity.AlreadyReachTarget(target, 1.0f)) { HTLogger.Debug("已经到达目标点:" + target); return(BTNodeStatus.Finished); } //HTLogger.Debug("持续向目标点:" + target + "移动中"); return(BTNodeStatus.Running); }
public void SaveRunningData(int key) { RunningData data; if (mDataDict.TryGetValue(key, out data) && data != null) { mSaveMgr.Save(data, mSaveMgr.PackageSavePath(key + ".json")); HTLogger.Debug("Save RunningData done."); } else { HTLogger.Debug("Save RunningData fail."); } }
public void LoadRunningData(int key) { RunningData data = mSaveMgr.Load <RunningData>(mSaveMgr.PackageSavePath(key + ".json")); if (mDataDict.ContainsKey(key)) { mDataDict[key] = data; } else { mDataDict.Add(key, data); } HTLogger.Debug("Load RunningData done."); }
protected override BTNodeStatus OnExcute(IAgent agent, Blackboard bb) { AIEntity entity = (AIEntity)agent; Vector3 target = bb.GetValue(BlackboardKey.TargetMovePos, Vector3.zero); entity.Turn(target); if (entity.AlreadyTurnToTarget(target, 0.1f)) { HTLogger.Debug("已经转向目标点:" + target + "方向"); return(BTNodeStatus.Finished); } HTLogger.Debug("持续向目标点:" + target + "方向转向中"); return(BTNodeStatus.Running); }
protected override BTNodeStatus OnExcute(IAgent agent, Blackboard bb) { AIModel model = ((AIEntity)agent).Model; if (CheckOver(model)) { HTLogger.Warning("死亡!"); return(BTNodeStatus.Finished); } model.Food.Value += BTExampleConsts.DAILY_FOOD_COST * Time.deltaTime; model.Water.Value += BTExampleConsts.DAILY_WATER_COST * Time.deltaTime; model.Energy.Value += BTExampleConsts.DAILY_ENERGY_COST * Time.deltaTime; model.Mood.Value += BTExampleConsts.DAILY_MOOD_COST * Time.deltaTime; return(BTNodeStatus.Running); }
protected override BTNodeStatus OnExcute(IAgent agent, Blackboard bb) { AIModel model = ((AIEntity)agent).Model; if (CheckOver(model)) { HTLogger.Debug("退出吃东西"); return(BTNodeStatus.Finished); } model.Food.Value += BTExampleConsts.FOOD_TO_FOOD * Time.deltaTime; model.Water.Value += BTExampleConsts.FOOD_TO_WATER * Time.deltaTime; model.Energy.Value += BTExampleConsts.FOOD_TO_ENERGY * Time.deltaTime; model.Mood.Value += BTExampleConsts.FOOD_TO_MOOD * Time.deltaTime; model.Money.Value += BTExampleConsts.FOOD_TO_MONEY * Time.deltaTime; return(BTNodeStatus.Running); }
private void ClickPathFindingBtn(int times, int count, int mapNum) { List <float> resTimesIn = new List <float>(); List <float> resTimesOut = new List <float>(); Vector2 start = new Vector2(0, 0); Vector2 end = new Vector2(mMapMgr.Width - 1, mMapMgr.Height - 1); //mMapMgr.BuildMap(); List <int> seedList = mRandomMapCreator.GetValidSeedList(mapNum, Probability, RandomSeed, MaxTimes); for (int p = 0; p < seedList.Count; p++) { mRandomMapCreator.Create(seedList[p], Probability); mMapMgr.BuildMap(); for (int j = 0; j < count; j++) { mTimer.Begin(TIMER_KEY_ASTAR_PF); for (int i = 0; i < times; i++) { mAStarMgr.FindPath(start, end); } mTimer.End(TIMER_KEY_ASTAR_PF); resTimesIn.Add(mTimer.GetSumTime(TIMER_KEY_ASTAR_PF)); } resTimesIn.Sort(); float sumTimeIn = 0.0f; for (int i = 1; i <= resTimesIn.Count - 2; i++) { sumTimeIn += resTimesIn[i]; } resTimesOut.Add((sumTimeIn / (resTimesIn.Count - 2)) / times); } resTimesOut.Sort(); float sumTimeOut = 0.0f; for (int i = 1; i <= resTimesOut.Count - 2; i++) { sumTimeOut += resTimesOut[i]; } mFindPathTime = sumTimeOut / (resTimesOut.Count - 2); mPath = mAStarMgr.FindPath(start, end); //mFindPathTime = mTimer.GetSumTime(TIMER_KEY_ASTAR_PF); mFindPathCost = CalculatePathConsume(mPath); SpawnMap(); HTLogger.Debug(string.Format("寻路 {0} 次:{1}毫秒", times, mFindPathTime * times)); HTLogger.Debug(string.Format("寻路 1 次:{0}毫秒", mFindPathTime)); HTLogger.Debug("路径消耗:" + mFindPathCost); }
protected override bool InternalCondition(IAgent agent, Blackboard bb) { AIModel model = ((AIEntity)agent).Model; bool ret = model.Food.Value <= 50 && model.Water.Value >= 15 && model.Money.Value >= 20; if (ret) { HTLogger.Debug("通过food检测"); } else { HTLogger.Debug("未通过food检测"); } return(ret); }
private void Init() { CustomLoggerService.Instance.Init(); mTimer = TimerServiceForTest.Instance; mTimer.Init(); SaveMgr.Instance.Init(); mMapMgr = MapMgr.Instance; mMapMgr.Init(MapWidth, MapHeight); mAStarMgr = AStarMgr.Instance; mAStarMgr.Init(); mRandomMapCreator = RandomMapCreator.Instance; mRandomMapCreator.Init(); mRecorder = Recorder.Instance; mRecorder.Init(); mTimer.Subscribe(TIMER_KEY_CREATE_MAP); mTimer.Subscribe(TIMER_KEY_ASTAR_PF); InitDataId(); HTLogger.Info("All init done."); }
private void Start() { UnityDebugListener unityDebugListener = new UnityDebugListener(); UnityWarningListener unityWarningListener = new UnityWarningListener(); UnityErrorListener unityErrorListener = new UnityErrorListener(); HTLogger.Instance.Init(true); HTLogger.Instance.AddListener(HTLogger.Channel.Info, unityDebugListener); HTLogger.Instance.AddListener(HTLogger.Channel.Todo, unityDebugListener); HTLogger.Instance.AddListener(HTLogger.Channel.Debug, unityDebugListener); HTLogger.Instance.AddListener(HTLogger.Channel.Warning, unityWarningListener); HTLogger.Instance.AddListener(HTLogger.Channel.Error, unityErrorListener); HTLogger.Info("测试 Logger 成功!"); HTLogger.Todo("测试 Logger 成功!"); HTLogger.Debug("测试 Logger 成功!"); HTLogger.Warning("测试 Logger 成功!"); HTLogger.Error("测试 Logger 成功!"); }
private void BuildBehaviorTree() { mBlackboard = new Blackboard(); mRootNode = new RepeatNode( new ParallelNode(1) .AddChild(new SelectorNode() .AddChild(new SequenceNode() .AddChild(new FeelBoring(), new WalkTo(), new HaveFun()) , new SequenceNode() .AddChild(new FeelHungry(), new WalkTo(), new EatFood()) , new SequenceNode() .AddChild(new FeelThirsty(), new WalkTo(), new DrinkWater()) , new SequenceNode() .AddChild(new FeelTired(), new WalkTo(), new HaveRest()) , new SequenceNode() .AddChild(new FeelPoor(), new WalkTo(), new Work()) ) , new Alive()) , 0); HTLogger.Info("AI 行为树构建完成!"); }
private void SpawnMap() { mNodeTransArray = new Transform[mMapMgr.Width, mMapMgr.Height]; for (int i = 0; i < mMapMgr.Width; i++) { for (int j = 0; j < mMapMgr.Height; j++) { Node node = mMapMgr.Map[i, j]; mNodeTransArray[i, j] = SpawnNode(node).transform; if (node.IsConnected) { SetColor(node, NormalColor); } else { SetColor(node, ObstacleColor); } } } SetColor(mMapMgr.Map[0, 0], StartColor); SetColor(mMapMgr.Map[mMapMgr.Width - 1, mMapMgr.Height - 1], EndColor); HTLogger.Debug("实例化地图成功"); }
/// <summary> /// 随机生成障碍物 /// </summary> /// <param 障碍物生成概率(0,1)="probability"></param> /// <param 地图宽="width"></param> /// <param 地图高="height"></param> /// <param 随机种子="seed"></param> /// <returns></returns> private List <Node> GenerateObstacleList(float probability, int width, int height, int seed) { if (probability > 1 || probability < 0) { HTLogger.Error("障碍物生成概率不合法!"); return(null); } List <Node> res = new List <Node>(); List <int> randomNumList = GenerateRandomNumList(1, 100, width * height, seed); int threshold = Mathf.FloorToInt(probability * 100.0f); for (int i = 0, k = 0; i < width; i++) { for (int j = 0; j < height; j++, k++) { if (randomNumList[k] <= threshold) { res.Add(mMapMgr.Map[i, j]); } } } return(res); }
/// <summary> /// 尝试创建随机 Map /// </summary> /// <param 障碍物生成概率(0,1)="probability"></param> /// <param 起始随机种子>-1="startSeed"></param> /// <param 尝试最大次数="times"></param> /// <returns></returns> public int TryToCreate(float probability, int startSeed, int times) { Node[,] mapTemp = new Node[mMapMgr.Width, mMapMgr.Height]; mapTemp = CopyMap(); int count = 0; while (count < times) { Create(startSeed, probability); if (CheckMapIsConnected()) { //HTLogger.Debug("随机地图生成成功"); return(startSeed); } else { RecoverMap(mapTemp); } startSeed++; count++; } HTLogger.Debug("随机地图生成失败"); return(-1); }
public void Init() { mMapMgr = MapMgr.Instance; HTLogger.Info("RandomMapCreator init done."); }
protected override bool InternalCondition(IAgent agent, Blackboard bb) { HTLogger.Debug("通过money检测"); return(true); }
public void Init() { mSaveMgr = SaveMgr.Instance; mDataDict = new Dictionary <int, RunningData>(); HTLogger.Info("Recorder init done."); }
private void SimpleAStarPathFinding() { SimpleAStar.Point start = new SimpleAStar.Point(0, 0); SimpleAStar.Point end = new SimpleAStar.Point(MapWidth - 1, MapHeight - 1); List <float> resTimeListIn = new List <float>(); List <float> resTimeListOut = new List <float>(); List <int> seedList = mRandomMapCreator.GetValidSeedList(MapNums, Probability, RandomSeed, MaxTimes); for (int p = 0; p < seedList.Count; p++) { mRandomMapCreator.Create(seedList[p], Probability); List <SimpleAStar.Point> obstacleList = new List <SimpleAStar.Point>(); for (int i = 0; i < MapWidth; i++) { for (int j = 0; j < MapHeight; j++) { if (mMapMgr.Map[i, j].IsConnected == false) { obstacleList.Add(new SimpleAStar.Point(i, j)); } } } SimpleAStar.AStarS aStars = new SimpleAStar.AStarS(MapWidth, MapHeight, obstacleList); for (int j = 0; j < Count; j++) { mTimer.Begin(TIMER_KEY_ASTAR_PF); for (int i = 0; i < LoopTimes; i++) { aStars.FindPath(start, end); } mTimer.End(TIMER_KEY_ASTAR_PF); resTimeListIn.Add(mTimer.GetSumTime(TIMER_KEY_ASTAR_PF)); } resTimeListIn.Sort(); float sumTimeIn = 0.0f; for (int i = 1; i <= resTimeListIn.Count - 2; i++) { sumTimeIn += resTimeListIn[i]; } resTimeListOut.Add((sumTimeIn / (resTimeListIn.Count - 2)) / LoopTimes); mSimplePath = aStars.FindPath(start, end); } resTimeListOut.Sort(); float sumTimeOut = 0.0f; for (int i = 1; i <= resTimeListOut.Count - 2; i++) { sumTimeOut += resTimeListOut[i]; } float time = sumTimeOut / (resTimeListOut.Count - 2); List <Node> pathNodeList = new List <Node>(); for (int i = 0; i < mSimplePath.Count; i++) { SimpleAStar.Point point = mSimplePath[i]; pathNodeList.Add(new Node(point.X, point.Y)); } int cost = CalculatePathConsume(pathNodeList); //float time = mTimer.GetSumTime(TIMER_KEY_ASTAR_PF); //SpawnMap(); HTLogger.Debug(string.Format("(简易 AStar)寻路 {0} 次:{1}毫秒", LoopTimes, time * LoopTimes)); HTLogger.Debug(string.Format("(简易 AStar)寻路 1 次:{0}毫秒", time)); HTLogger.Debug("(简易 AStar)路径消耗:" + cost); }
public void Init() { HTLogger.Info("SaveMgr init done."); }