コード例 #1
0
            public PMGEvent(PMGMethod method, PMGActor calling,
                            EventTriggerBehavior behavior = EventTriggerBehavior.ALWAYS)
            {
                _method       = method;
                _behavior     = behavior;
                _callingActor = calling;

                _hasBeenFalse  = false;
                _methodRunning = false;
            }
コード例 #2
0
            public PMGActor GetActorCopy()
            {
                PMGActor copy = new PMGActor(Core);

                copy.IsPlayer = IsPlayer;
                copy.position = new List <int>(position);

                foreach (PMGEvent e in Events)
                {
                    // Make method copy
                    PMGMethod mcopy = new PMGMethod();

                    List <PMGExecuteList> ml = new List <PMGExecuteList>();

                    // Make execute list copies
                    foreach (PMGExecuteList l in e._method._steps)
                    {
                        if (l == null)
                        {
                            ml.Add(null);
                        }
                        else
                        {
                            ml.Add(new PMGExecuteList(mcopy, FunctionOwnerType.METHOD, copy));
                            ml.Last()._functions = l._functions;
                        }
                    }

                    mcopy._steps = ml;

                    PMGEvent ecopy = new PMGEvent(mcopy, copy, e._behavior);

                    PMGExecuteList elist = new PMGExecuteList(ecopy, FunctionOwnerType.EVENT, copy);
                    elist._functions  = e._conditions._functions;
                    ecopy._conditions = elist;

                    copy.Events.Add(ecopy);
                }

                return(copy);
            }
            /*
             * USER DEFINED FUNCTIONS
             */
            public void UF_DebugWriteToConsole(PMGActor actor, object owner, FunctionOwnerType ownerType)
            {
                // Write a message to console for testing purposes.
                Console.WriteLine("Utility functions called. ");


                // Get owner as method (this is for testing so we know it's a method)
                PMGMethod ownerM = owner as PMGMethod;

                // if an int is in stack that is over 1k (we push 1337 by default)
                // Then we change the value function to #1 (instead of #0)
                // In this case pushing 42 instead
                int intFromStack = System.Convert.ToInt32(ownerM._valueStack.GetValueOfType(ValueType.INT));


                if (intFromStack > 1000)
                {
                    Console.WriteLine("Changing value function in owner method steps!");
                    ownerM.CurrentStep._functions[0] = new PMGValueFunction(1);
                    ownerM.Reset();
                }
            }
コード例 #4
0
        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();
        }
コード例 #5
0
 public PMGEventDynamic(PMGMethod method, PMGActor calling,
                        EventTriggerBehavior behavior
                        = EventTriggerBehavior.ALWAYS)
     : base(method, calling, behavior)
 {
 }
コード例 #6
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);
            }
コード例 #7
0
        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);//*/


            //as generator does it
            //get core
            PMGCore core = new PMGCore();
            //make actor
            PMGActor actor = new PMGActor(core);
            //make method and almost event
            PMGMethod eventMethod = new PMGMethod();
            PMGEvent  Event;
            //make list of execute lists
            List <PMGExecuteList> methodListOfExelist = new List <PMGExecuteList>();
            //make execute lists for method
            PMGExecuteList methList = new PMGExecuteList(eventMethod as object, FunctionOwnerType.METHOD, actor);

            methList._functions.Add(new PMGChangeFunction(0));
            methodListOfExelist.Add(methList);

            PMGExecuteList methList2 = new PMGExecuteList(eventMethod as object, FunctionOwnerType.METHOD, actor);

            //add functions to method exe list
            methList2._functions.Add(new PMGChangeFunction(1));
            //add executelists to the  list of exelist

            methodListOfExelist.Add(methList2);
            //add execute list to methods
            eventMethod._steps = methodListOfExelist;
            //now make event
            Event = new PMGEvent(eventMethod, actor);
            //make exe list for event
            PMGExecuteList EventList = new PMGExecuteList(Event as object, FunctionOwnerType.EVENT, actor);

            //add function to exe list
            EventList._functions.Add(new PMGConditionFunction(1));
            //EventList._functions.Add(new PMGConditionFunction(3));
            //add list to event conditions
            Event._conditions = EventList;
            //finish actor by adding event to it
            actor.Events.Add(Event);

            // 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();
        }
コード例 #8
0
            PMGMethod Method;             // or method id

            public PMGEventFixed(PMGMethod method, PMGActor actor,
                                 EventTriggerBehavior behavior = EventTriggerBehavior.ALWAYS)
                : base(method, actor, behavior)
            {
            }