public static void Main(string[] args)
        {
            Console.WriteLine("Test program starting.");


            // Create a new core (one is needed per game)
            PMGCore core = new PMGCore();

            // Create actor
            PMGActor actor = new PMGActor(core);        // actor used for test

            // Create methods
            // ...
            PMGMethod testMethod  = new PMGMethod();    // method with no event
            PMGMethod eventMethod = new PMGMethod();    // method for event

            // Create functions for the methods
            PMGValueFunction     testValF  = new PMGValueFunction(0);
            PMGConditionFunction testCondF = new PMGConditionFunction(0);
            PMGUtilityFunction   testUtilF = new PMGUtilityFunction(0);
            PMGChangeFunction    testChgF  = new PMGChangeFunction(0);



            // Create events
            PMGEvent testEvent = new PMGEvent(eventMethod, actor);

            // Create execution lists
            PMGExecuteList methList1   = new PMGExecuteList(testMethod as object, FunctionOwnerType.METHOD, actor);
            PMGExecuteList methList1_2 = new PMGExecuteList(testMethod as object, FunctionOwnerType.METHOD, actor);
            PMGExecuteList methList2   = new PMGExecuteList(eventMethod as object, FunctionOwnerType.METHOD, actor);
            PMGExecuteList evList      = new PMGExecuteList(testEvent as object, FunctionOwnerType.EVENT, actor);

            // Add functions to lists
            evList._functions.Add(testCondF);           // Conditions for event
            methList1._functions.Add(testValF);
            methList1._functions.Add(testChgF);

            methList1_2._functions.Add(testValF);
            methList1_2._functions.Add(testChgF);


            methList2._functions.Add(testValF);
            methList2._functions.Add(testUtilF);
            methList2._functions.Add(testChgF);


            // Add execution lists to events/methods
            testEvent._conditions = evList;
            testMethod._steps.Add(methList1);
            eventMethod._steps.Add(methList2);

            // Add event to actor (as dynamic for now)
            actor.Events.Add(testEvent);


            // Run some timesteps in the TODO GAMEMANAGER or whatever
            //
            // In each timestep we: test conditions for events. Run timesteps in methods
            testMethod.Call(); // we want to call method 1 from the beginniing
            for (int i = 0; i < 15; i++)
            {
                core.WorldTimeSteps = i;

                Console.WriteLine("-- T = {0} --", i);

                // test method 1
                testMethod.TimeStep();

                // Check events on actor (testEvent with eventMethod)
                foreach (PMGEvent E in actor.Events)
                {
                    E.EvaluateConditions();
                    E._method.TimeStep();
                }
            }



            Console.ReadKey();
        }
Example #2
0
            // Run all functions in list (bool because condition functions)
            public bool Execute()
            {
                // Get the local stack ready (need proper casting)
                PMGValueStack localStack = null;

                _exing = true;

                switch (_ownerType)
                {
                case FunctionOwnerType.EVENT:
                    PMGEvent E = _owner as PMGEvent;

                    if (E == null)
                    {
                        throw new System.InvalidCastException("Casting of owner as PMGEvent failed.");
                    }

                    localStack = E._valueStack;
                    break;


                case FunctionOwnerType.METHOD:
                    PMGMethod M = _owner as PMGMethod;

                    if (M == null)
                    {
                        throw new System.InvalidCastException("Casting of owner as PMGMethod failed.");
                    }

                    localStack = M._valueStack;
                    break;
                }

                if (localStack == null)
                {
                    throw new System.InvalidOperationException("localStack is still null. Could not be set to event or method stack.");
                }

                // Prepare bool for condition functions
                bool AllTrue = true;

                // Execute all functions in list at once
                for (int i = 0; i < _functions.Count; i++)
                {
                    if (!_exing)
                    {
                        // something stopped the execution of the list
                        _exing = true;
                        return(false);
                    }

                    if (i < 0 || i > _functions.Count)
                    {
                        throw new System.InvalidOperationException("Tried to execute functions outside of list. Did a utility function change it?");
                    }

                    PMGFunction f = _functions[i];

                    switch (f.Type)
                    {
                    case FunctionType.VALUE:
                        PMGValueFunction vf = f as PMGValueFunction;

                        if (vf == null)
                        {
                            throw new System.InvalidCastException("Casting of function as 2PMGValueFunction failed.");
                        }

                        vf.Do(_actor, localStack);
                        break;

                    case FunctionType.UTILITY:
                        PMGUtilityFunction uf = f as PMGUtilityFunction;

                        if (uf == null)
                        {
                            throw new System.InvalidCastException("Casting of function as PMGUtilityFunction failed.");
                        }

                        uf.Do(_actor, _owner, _ownerType);
                        break;

                    case FunctionType.CONDITION:
                        PMGConditionFunction cf = f as PMGConditionFunction;

                        if (cf == null)
                        {
                            throw new System.InvalidCastException("Casting of function as PMGConditionFunction failed.");
                        }

                        AllTrue &= cf.Do(_actor, localStack);
                        break;

                    case FunctionType.CHANGE:
                        PMGChangeFunction chf = f as PMGChangeFunction;

                        if (chf == null)
                        {
                            throw new System.InvalidCastException("Casting of function as PMGChangeFunction failed.");
                        }

                        chf.Do(_actor, localStack);
                        break;
                    }
                }

                return(AllTrue);
            }