예제 #1
0
    public override void Run(IReGoapAction <string, object> previous, IReGoapAction <string, object> next, IReGoapActionSettings <string, object> settings, ReGoapState <string, object> goalState, Action <IReGoapAction <string, object> > done, Action <IReGoapAction <string, object> > fail)
    {
        base.Run(previous, next, settings, goalState, done, fail);
        var workstation = agent.GetMemory().GetWorldState().Get("nearestWorkstation") as Workstation;

        if (workstation != null && workstation.CraftResource(resourcesBag, recipe))
        {
            ReGoapLogger.Log("[CraftRecipeAction] crafted recipe " + recipe.GetCraftedResource());
            done(this);
        }
        else
        {
            fail(this);
        }
    }
예제 #2
0
    public override void Run(IReGoapAction previous, IReGoapAction next, IReGoapActionSettings settings, ReGoapState goalState, Action <IReGoapAction> done, Action <IReGoapAction> fail)
    {
        base.Run(previous, next, settings, goalState, done, fail);
        var workstation = agent.GetMemory().GetWorldState().Get <Workstation>("nearestWorkstation");

        if (workstation.CraftResource(resourcesBag, recipe))
        {
            ReGoapLogger.Log("[CraftRecipeAction] crafted recipe " + recipe.GetCraftedResource());
            done(this);
        }
        else
        {
            fail(this);
        }
    }
예제 #3
0
        protected override void Awake()
        {
            base.Awake();
            recipe = RawRecipe as IRecipe;
            if (recipe == null)
            {
                throw new UnityException("[CraftRecipeAction] The rawRecipe ScriptableObject must implement IRecipe.");
            }
            resourcesBag = GetComponent <ResourcesBag>();

            // could implement a more flexible system that handles dynamic resources's count
            foreach (var pair in recipe.GetNeededResources())
            {
                preconditions.Set("hasResource" + pair.Key, true);   // 添加前置条件
            }

            effects.Set("hasResource" + recipe.GetCraftedResource(), true); // 添加执行结果
        }
예제 #4
0
    public bool CraftResource(ResourcesBag crafterBag, IRecipe recipe, float value = 1f)
    {
        // check Recipe, could be removed since the agent already check for recipe items
        foreach (var pair in recipe.GetNeededResources())
        {
            if (crafterBag.GetResource(pair.Key) < pair.Value * value)
            {
                //throw new UnityException(string.Format("[Workstation] Trying to craft recipe '{0}' without having enough '{1}' resources.", recipe.GetCraftedResource(), pair.Key));
                return(false);
            }
        }
        // if can go loop again and remove needed resources
        foreach (var pair in recipe.GetNeededResources())
        {
            crafterBag.RemoveResource(pair.Key, pair.Value * value);
        }
        var resource = recipe.GetCraftedResource();

        crafterBag.AddResource(resource, value);
        return(true);
    }