Example #1
0
        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        void Update(TimeSpan gameTime)
        {
            effects.RemoveAll(e => e.ShouldBeRemoved);

            float secondsSinceLastFrame = (float)gameTime.TotalSeconds;

            plantCounter   += secondsSinceLastFrame;
            runningSeconds += secondsSinceLastFrame;


            List <IEntity> agents = entities.FindAll(e => e is Agent);


            if (!gameRunning)
            {
                return;
            }
            else if (gameRunning && (runningSeconds > AIModifiers.maxRunningTimeInSeconds || agents.Count < 1))
            {
                gameRunning = false;
                agents.ForEach(a => deadAgents.Push((Agent)a));

                Dictionary <AgentFactory, int> endingAgents = new Dictionary <AgentFactory, int>();

                foreach (Agent a in agents)
                {
                    foreach (AgentFactory f in factories)
                    {
                        if (f.ProvidedAgentTypeName == a.EntityType)
                        {
                            if (endingAgents.ContainsKey(f))
                            {
                                endingAgents[f] += 1;
                            }
                            else
                            {
                                endingAgents.Add(f, 1);
                            }
                        }
                    }
                }
                List <Agent> dead                = deadAgents.ToList();
                Agent        winningAgent        = deadAgents.Peek();
                AgentFactory winningAgentFactory = null;
                int          currentCount        = 0;


                foreach (AgentFactory f in factories)
                {
                    try
                    {
                        f.RegisterWinners(dead);
                    }
                    catch
                    {
                    }
                }

                foreach (KeyValuePair <AgentFactory, int> f in endingAgents)
                {
                    if (f.Value > currentCount)
                    {
                        winningAgentFactory = f.Key;
                        currentCount        = f.Value;
                    }
                }

                return;
            }



            //Reset tmp lists
            toBeAdded.Clear();
            toBeRemoved.Clear();

            Random rnd = new Random();

            //Create plant
            if (plantCounter > 1)
            {
                plantCounter = 0;
                for (int i = 0; i < AIModifiers.newPlantsPerSecond; i++)
                {
                    Plant p = new Plant();
                    p.Position = new AIVector(rnd.Next((int)surfaceWidth), rnd.Next((int)surfaceHeight));
                    entities.Add(p);
                }
            }


            int agentCount = 0;

            foreach (IEntity e in agents)
            {
                agentCount++;
                Agent            a = (Agent)e;
                PropertyProvider p = pproviders[a.Id.ToString()];
                //Increment hunger
                p.Hunger += AIModifiers.hungerIncrementPerSecond * secondsSinceLastFrame;

                //Do action
                a.ActionResultCallback(DoAction(a, gameTime));

                //Reduce hitpoints due to hunger
                if (p.Hunger > AIModifiers.maxHungerBeforeHitpointsDamage)
                {
                    p.Hitpoints -= AIModifiers.hungerHitpointsDamagePerSecond * secondsSinceLastFrame;
                }

                //Remove agent if under 0 hitpoints = agent is dead
                if (p.Hitpoints <= 0)
                {
                    toBeRemoved.Add(a);
                }
                else if (p.Hunger <= p.Endurance) //Hitpoint regenration due to endurance higher then hunger
                {
                    p.Hitpoints += AIModifiers.hitpointRegenPerSecond * secondsSinceLastFrame;
                    if (p.Hitpoints > p.Health)
                    {
                        p.Hitpoints = p.Health;
                    }
                }

                //Procreation reduction
                if (p.ProcreationCountDown > 0)
                {
                    p.ProcreationCountDown -= AIModifiers.procreationReductionPerSecond * secondsSinceLastFrame;
                    if (p.ProcreationCountDown < 0)
                    {
                        p.ProcreationCountDown = 0;
                    }
                }
            }


            entities.AddRange(toBeAdded);
            toBeRemoved.ForEach(a => { entities.Remove(a); deadAgents.Push((Agent)a); });
        }