Пример #1
0
        public override void Initialize(EntityProperties properties, JsonObject aiconfig)
        {
            if (!(entity is EntityAgent))
            {
                entity.World.Logger.Error("The goal ai currently only works on entities inheriting from EntityAgent. Will ignore loading goals for entity {0} ", entity.Code);
                return;
            }

            PathTraverser = new StraightLineTraverser(entity as EntityAgent);

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

            foreach (JsonObject goalConfig in goals)
            {
                string goalCode = goalConfig["code"]?.AsString();
                Type   goalType = null;
                if (!AiGoalRegistry.GoalTypes.TryGetValue(goalCode, out goalType))
                {
                    entity.World.Logger.Error("Goal with code {0} for entity {1} does not exist. Ignoring.", goalCode, entity.Code);
                    continue;
                }

                AiGoalBase goal = (AiGoalBase)Activator.CreateInstance(goalType, (EntityAgent)entity);
                goal.LoadConfig(goalConfig, aiconfig);

                goalManager.AddGoal(goal);
            }
        }
Пример #2
0
        public void OnGameTick(float dt)
        {
            foreach (AiGoalBase newGoal in Goals)
            {
                if ((activeGoal == null || newGoal.Priority > activeGoal.PriorityForCancel) && newGoal.ShouldExecuteAll())
                {
                    activeGoal?.FinishExecuteAll(true);
                    activeGoal = newGoal;
                    newGoal.StartExecuteAll();
                }
            }


            if (activeGoal != null && !activeGoal.ContinueExecuteAll(dt))
            {
                activeGoal.FinishExecuteAll(false);
                activeGoal = null;
            }



            if (entity.World.EntityDebugMode)
            {
                string tasks = "";
                if (activeGoal != null)
                {
                    tasks += AiTaskRegistry.TaskCodes[activeGoal.GetType()] + "(" + activeGoal.Priority + ")";
                }
                entity.DebugAttributes.SetString("AI Goal", tasks.Length > 0 ? tasks : "-");
            }
        }
Пример #3
0
        internal void Notify(string key, object data)
        {
            for (int i = 0; i < Goals.Count; i++)
            {
                AiGoalBase newGoal = Goals[i];

                if (newGoal.Notify(key, data))
                {
                    if ((newGoal == null || newGoal.Priority > activeGoal.PriorityForCancel))
                    {
                        activeGoal?.FinishExecuteAll(true);
                        activeGoal = newGoal;
                        newGoal.StartExecuteAll();
                    }
                }
            }
        }
Пример #4
0
 public void RemoveGoal(AiGoalBase goal)
 {
     Goals.Remove(goal);
 }
Пример #5
0
 public void AddGoal(AiGoalBase goal)
 {
     Goals.Add(goal);
 }