CreateRobotConsoleHandler.cs 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. using System;
  2. using System.Collections.Generic;
  3. using CommandLine;
  4. using NLog;
  5. namespace ET
  6. {
  7. [ConsoleHandler(ConsoleMode.CreateRobot)]
  8. public class CreateRobotConsoleHandler: IConsoleHandler
  9. {
  10. public async ETTask Run(ModeContex contex, string content)
  11. {
  12. switch (content)
  13. {
  14. case ConsoleMode.CreateRobot:
  15. Log.Console("CreateRobot args error!");
  16. break;
  17. default:
  18. CreateRobotArgs options = null;
  19. Parser.Default.ParseArguments<CreateRobotArgs>(content.Split(' '))
  20. .WithNotParsed(error => throw new Exception($"CreateRobotArgs error!"))
  21. .WithParsed(o => { options = o; });
  22. // 获取当前进程的RobotScene
  23. using (ListComponent<StartSceneConfig> thisProcessRobotScenes = ListComponent<StartSceneConfig>.Create())
  24. {
  25. List<StartSceneConfig> robotSceneConfigs = StartSceneConfigCategory.Instance.Robots;
  26. foreach (StartSceneConfig robotSceneConfig in robotSceneConfigs)
  27. {
  28. if (robotSceneConfig.Process != Game.Options.Process)
  29. {
  30. continue;
  31. }
  32. thisProcessRobotScenes.Add(robotSceneConfig);
  33. }
  34. // 创建机器人
  35. for (int i = 0; i < options.Num; ++i)
  36. {
  37. int index = i % thisProcessRobotScenes.Count;
  38. StartSceneConfig robotSceneConfig = thisProcessRobotScenes[index];
  39. Scene robotScene = Game.Scene.Get(robotSceneConfig.Id);
  40. RobotManagerComponent robotManagerComponent = robotScene.GetComponent<RobotManagerComponent>();
  41. Scene robot = await robotManagerComponent.NewRobot(Game.Options.Process * 10000 + i);
  42. robot.AddComponent<AIComponent, int>(1);
  43. Log.Console($"create robot {robot.Zone}");
  44. await TimerComponent.Instance.WaitAsync(2000);
  45. }
  46. }
  47. break;
  48. }
  49. contex.Parent.RemoveComponent<ModeContex>();
  50. await ETTask.CompletedTask;
  51. }
  52. }
  53. }