Ejemplo n.º 1
0
 private void HandleCollision()
 {
     for (int i = 0; i < AmountOfCores; i++)
     {
         int upperBound = Creatures.Count * (i + 1) / AmountOfCores;
         if (upperBound > Creatures.Count)
         {
             upperBound = Creatures.Count;
         }
         int lowerBound = Creatures.Count * i / AmountOfCores;
         MultithreadingHelper.StartWork((object state) => {
             for (int k = lowerBound; k < upperBound; k++)
             {
                 Creatures[k].HandleCollisions();
             }
             MultithreadingHelper.PulseAndFinish();
         });
     }
     MultithreadingHelper.WaitForEmptyThreadPool();
     ResetCollisionGrid();
 }
Ejemplo n.º 2
0
        protected override void Update(GameTime deltaTime)
        {
            while (creatures.Count < 50)
            {
                Creature justSpawned = new Creature(
                    new Vector2(
                        Simulation.RandomFloat() * simulation.TileMap.GetWorldWidth(),
                        Simulation.RandomFloat() * simulation.TileMap.GetWorldHeight()),
                    Simulation.RandomFloat() * Mathf.PI * 2,
                    this);
                creatures.Add(justSpawned);
            }

            for (int i = 0; i < AmountOfCores; i++)
            {
                int upperBound = creatures.Count * (i + 1) / AmountOfCores;
                if (upperBound > creatures.Count)
                {
                    upperBound = creatures.Count;
                }
                int lowerBound = creatures.Count * i / AmountOfCores;
                MultithreadingHelper.StartWork((object state) => {
                    for (int k = lowerBound; k < upperBound; k++)
                    {
                        creatures[k].ReadSensors();
                    }
                    MultithreadingHelper.PulseAndFinish();
                });
            }
            MultithreadingHelper.WaitForEmptyThreadPool();
            for (int i = 0; i < AmountOfCores; i++)
            {
                int upperBound = creatures.Count * (i + 1) / AmountOfCores;
                if (upperBound > creatures.Count)
                {
                    upperBound = creatures.Count;
                }
                int lowerBound = creatures.Count * i / AmountOfCores;
                MultithreadingHelper.StartWork((object state) => {
                    for (int k = lowerBound; k < upperBound; k++)
                    {
                        creatures[k].Act(deltaTime);
                    }
                    MultithreadingHelper.PulseAndFinish();
                });
            }
            MultithreadingHelper.WaitForEmptyThreadPool();
            numberOfDeaths += CreaturesToKill.Count;

            RemoveCreaturesFromDeathList();
            MergeCreaturesAndSpawnCreatures();

            year += (float)deltaTime.ElapsedGameTime.TotalSeconds;

            HandleCollision();

            if (creatures.Count > 0)
            {
                OldestCreatureAlive = creatures[0];
                foreach (Creature c in creatures)
                {
                    if (c.Age > OldestCreatureAlive.Age)
                    {
                        OldestCreatureAlive = c;
                    }
                }
            }

            AliveCreaturesRecord.Add(creatures.Count);
        }