Example #1
0
        public void DoWithSearch(Entity entity, ObjectMesh objectMesh)
        {
            List <GameObject> nearbyObjects = objectMesh.GetObjectsInRange((int)entity.Position.X, (int)entity.Position.Y, 100);

            foreach (GameObject o in nearbyObjects)
            {
                if (o is Person p && p.Sex == Sex.Female)
                {
                    entity.SetColor(Color.Red);
                    return;
                }
            }

            entity.SetBaseColor();
        }
Example #2
0
        // Retrieve GameObject at the specified point and satisfies the given predicate.
        public GameObject GetObjectAtPoint(Point point, Predicate <GameObject> predicate)
        {
            RectangleF pointRectangle = new RectangleF(point, new SizeF(0.1f, 0.1f));

            List <GameObject> gameObjects = ObjectMesh.GetObjectsInRange(point.X, point.Y, 1)
                                            .Where(g => predicate(g) && g.GetRectangleF().IntersectsWith(pointRectangle)).ToList();

            if (gameObjects.Count > 0)
            {
                return(gameObjects[0]);
            }
            else
            {
                return(null);
            }
        }
Example #3
0
        public void DoWithSearchAndTime(Entity entity, ObjectMesh objectMesh, GameTime time)
        {
            if (timerEnabled && stopTime != null)
            {
                if (time >= stopTime)
                {
                    entity.SetCanMove(true);
                    End();
                }
                return;
            }

            if (entity is Person person)
            {
                int range            = Person.mateRange;
                int mateRangeSquared = Person.mateRange * Person.mateRange;
                // Search all nearbyObjects for people to mate with. Then mate with all of them that are in range.
                List <GameObject> nearbyObjects = objectMesh.GetObjectsInRange(person.Position.X, person.Position.Y, range);
                foreach (GameObject gameObject in nearbyObjects)
                {
                    if (gameObject is Person mate && mate.Sex != person.Sex && !person.IsPregnant && !mate.IsPregnant &&
                        Utilities.SquaredDistance(person.Position, mate.Position) < mateRangeSquared)
                    {
                        person.ReproduceWith(mate);
                        // Start timer. Initialize stopTime to the current time plus x number of seconds. Start checking time to allow player to move.
                        if (!timerEnabled)
                        {
                            timerEnabled = true;
                            person.SetCanMove(false);
                            stopTime = time.Copy();
                            stopTime.AddSeconds(Person.mateTime);
                        }
                    }
                }
            }

            if (!timerEnabled)
            {
                End();
            }
        }
Example #4
0
        // Update Cycle.
        public void EntityUpdate(GameTime gameTime, ObjectMesh objectMesh, int mouseX, int mouseY)
        {
            entityCount = Entities.Count;
            UpdateData();

            if (Utilities.Rng.NextDouble() < animalSpawnChance && entityCount < maxEntityCount)
            {
                SpawnRandomAnimal();
            }

            List <Entity> deadEntities = new List <Entity>();

            // Player updates.
            if (Player.BasePerson != null)
            {
                if (Player.BasePerson.Hunger <= 0.0)
                {
                    //Player.BasePerson.TakeDamage(1, null);
                }

                if (Player.IsAlive && Player.BasePerson.Health <= 0)
                {
                    Player.OnCancelData(); // Player is dead and does not have data to display.
                    Player.Kill();
                    deadEntities.Add(Player.BasePerson);
                    Player.Move();
                }
                else
                {
                    Player.CarryOutAction(objectMesh, GameTime);
                    Player.Move();
                    Player.UpdateData();
                }
            }

            // Updates for the entities in the list.
            for (int i = 0; i < Entities.Count; i++)
            {
                Entity e = Entities[i];

                List <GameObject> nearbyObjects = objectMesh
                                                  .GetObjectsInRange(e.Position.X, e.Position.Y, e.VisionRange)
                                                  .Where(o => o != e)
                                                  .ToList();

                if (e.GetHealth() <= 0)
                {
                    deadEntities.Add(e);
                    if (e is Person person)
                    {
                        person.DropAllItems(false);
                        person.OnCancelData();
                    }
                    else if (e is Animal animal)
                    {
                        animal.OnCancelData();
                    }
                }
                else
                {
                    // -- Person Updates --
                    if (e is Person p && personBrainCounter == 0) // Only decide action after a certain period of time. 1 / personBrainPeriod times.
                    {
                        if (e.Hunger <= 0.0)
                        {
                            e.TakeDamage(1, null);
                        }

                        p.UpdateNeeds();
                        p.UpdateData();

                        // People that are close together will decrease their social need.
                        if (nearbyObjects.OneSatisfies(g => g.ObjectID != p.ObjectID && g is Person))
                        {
                            p.DecreaseSocialNeed();
                        }

                        // People close to a campfire will be warmed up.
                        if (nearbyObjects.OneSatisfies(g => g is Campfire))
                        {
                            p.DecreaseWarmthNeed();
                        }

                        if (!e.ActionLocked) // Locked actions will not be overwritten. Used to make entity wait.
                        {
                            personBrain.DecideAction(p, nearbyObjects, PersonCount, GetItemCount(ItemType.Apple), mouseX, mouseY);
                        }
                    }

                    // -- Animal Updates --
                    if (e is Animal a && animalBrainCounter == 0)
                    {
                        if (!a.ActionLocked)
                        {
                            animalBrain.DecideAction(a, nearbyObjects);
                        }
                        a.UpdateNeeds();
                    }
                    e.CarryOutAction(objectMesh, GameTime);
                }
            }

            // Update brain counters after all entities have been updated.
            personBrainCounter = (personBrainCounter + 1) % personBrainPeriod;
            animalBrainCounter = (animalBrainCounter + 1) % animalBrainPeriod;

            foreach (Entity e in deadEntities)
            {
                EntitiesRemove(e);
            }

            GameTime = gameTime;
        }