public override void Initialize(EntityProperties properties, JsonObject aiconfig)
        {
            if (!(entity is EntityAgent))
            {
                entity.World.Logger.Error("The task ai currently only works on entities inheriting from. EntityLiving. Will ignore loading tasks for entity {0} ", entity.Code);
                return;
            }

            JsonObject[] tasks = aiconfig["aitasks"]?.AsArray();
            if (tasks == null)
            {
                return;
            }

            foreach (JsonObject taskConfig in tasks)
            {
                string taskCode = taskConfig["code"]?.AsString();
                Type   taskType = null;
                if (!AiTaskManager.TaskTypes.TryGetValue(taskCode, out taskType))
                {
                    entity.World.Logger.Error("Task with code {0} for entity {1} does not exist. Ignoring.", taskCode, entity.Code);
                    continue;
                }

                IAiTask task = (IAiTask)Activator.CreateInstance(taskType, (EntityAgent)entity);
                task.LoadConfig(taskConfig, aiconfig);

                taskManager.AddTask(task);
            }
        }
示例#2
0
        public override void Initialize(EntityProperties properties, JsonObject aiconfig)
        {
            if (!(entity is EntityAgent))
            {
                entity.World.Logger.Error("The task ai currently only works on entities inheriting from EntityAgent. Will ignore loading tasks for entity {0} ", entity.Code);
                return;
            }

            PathTraverser = new WaypointsTraverser(entity as EntityAgent);

            JsonObject[] tasks = aiconfig["aitasks"]?.AsArray();
            if (tasks == null)
            {
                return;
            }

            foreach (JsonObject taskConfig in tasks)
            {
                string taskCode = taskConfig["code"]?.AsString();
                bool   enabled  = taskConfig["enabled"].AsBool(true);
                if (!enabled)
                {
                    continue;
                }

                Type taskType = null;
                if (!AiTaskRegistry.TaskTypes.TryGetValue(taskCode, out taskType))
                {
                    entity.World.Logger.Error("Task with code {0} for entity {1} does not exist. Ignoring.", taskCode, entity.Code);
                    continue;
                }

                IAiTask task = (IAiTask)Activator.CreateInstance(taskType, (EntityAgent)entity);

                try
                {
                    task.LoadConfig(taskConfig, aiconfig);
                } catch (Exception e)
                {
                    entity.World.Logger.Error("Task with code {0} for entity {1}: Unable to load json code.", taskCode, entity.Code);
                    throw e;
                }

                TaskManager.AddTask(task);
            }
        }