Ejemplo n.º 1
0
        public void calcDeersFear() //change all deer fear, if threat is low then dont update fear
        {
            if (game.lion.visible == true)
            {
                for (int i = 0; i < deers.Count; ++i)
                {
                    Deer    d           = (Deer)deers[i];
                    Vector2 dirFromLion = (d.Position - game.lion.Position);
                    float   distance    = dirFromLion.Length();
                    if (distance < 100)
                    {
                        d.addFear(100);
                    }
                    else if (distance < 250)         //if the lion is within a distance ///////////////////
                    {
                        float fear = 250 / distance; //200/dist so 1 to 200 counts (hopefully works right)
                        d.addFear(fear * .6f);       // did this because fear goes up waay to quick
                        //deers[i].addFear(game.lion.Velocity.Length());
                    }
                    else if (distance > 300)//created a deadzone inbetween, like alert zone
                    {
                        deers[i].decayFear();
                    }
                }


                for (int i = 0; i < deers.Count; ++i)
                {
                    if (deers[i].neighbors.Count > 0) //if deer has fear greater than 20, and has neighbors
                    {
                        for (int j = 0; j < deers[i].neighbors.Count; ++j)
                        {
                            avgFear += deers[i].neighbors[j].fear;
                            //loop through them and add deer[i]'s fear to them
                            // deers[j].addFear(fear);
                        }
                        avgFear /= deers[i].neighbors.Count;
                        //avgFear *= .3f;
                        if (deers[i].fear > avgFear)
                        {
                            deers[i].addFear(-avgFear);
                        }
                        else
                        {
                            deers[i].addFear(avgFear);
                        }
                    }
                    avgFear = 0;
                }
            }
            else
            {
                for (int i = 0; i < deers.Count; ++i)
                {
                    deers[i].decayFear();
                }
            }
        }
Ejemplo n.º 2
0
 public void CreateDeer(int deerCt, Texture2D deerImg)
 {
     for (int i = 0; i < deerCt; ++i)
     {
         Deer deerTmp = new Deer(game, deerImg, new Vector2((float)Game.r.NextDouble() * Game.bounds.Width, (float)Game.r.NextDouble() * Game.bounds.Height));
         deers.Add(deerTmp);
         deerCount++;
         AttatchNewDeerFSM(deerTmp, game);
     }
 }
Ejemplo n.º 3
0
        public void Update(SteeringOutput steering, GameTime time, Game game)
        {
            newState = Keyboard.GetState();

            bool keyPressed = false;

            if (game.playerHunter.health > 0 && game.playerHunter.spearJab == false && game.playerHunter.spearThrow == false)
            {
                if (Game.keyboard.IsKeyDown(Keys.A))
                {
                    velocity.X -= maxAcceleration;
                    keyPressed  = true;
                }
                if (Game.keyboard.IsKeyDown(Keys.D))
                {
                    velocity.X += maxAcceleration;
                    keyPressed  = true;
                }
                if (Game.keyboard.IsKeyDown(Keys.W))
                {
                    velocity.Y -= maxAcceleration;
                    keyPressed  = true;
                }
                if (Game.keyboard.IsKeyDown(Keys.S))
                {
                    velocity.Y += maxAcceleration;
                    keyPressed  = true;
                }
            }
            if (game.playerHunter.spearJab == true || game.playerHunter.spearThrow == true)
            {
                for (int i = 0; i < game.deerManager.GetDeerCount(); i++)
                {
                    Deer d = (Deer)game.deerManager.deers[i];
                    dirFromSpear = (d.Position - this.Position);
                    float distance = dirFromSpear.Length();
                    if (distance < 30)
                    {
                        game.deerManager.KillDeer(d);
                        game.playerHunter.spearJab   = false;
                        game.playerHunter.spearThrow = false;
                        break;
                    }
                }

                float distStoL = (game.lion.Position - this.Position).Length();
                if (distStoL < 30)
                {
                    if (game.playerHunter.spearJab)
                    {
                        game.lion.health          -= 2;
                        game.playerHunter.spearJab = false;
                    }
                    if (game.playerHunter.spearThrow)
                    {
                        game.lion.health            -= 1;
                        game.playerHunter.spearThrow = false;
                    }
                }
            }
            if (game.playerHunter.spearJab && game.playerHunter.health > 0)
            {
                keyPressed  = true;
                getPosition = this.position;
                Vector2 movement = game.playerHunter.Velocity;

                movement.X += 12 * (float)Math.Cos(game.playerHunter.orientation);
                movement.Y += (12 * (float)Math.Sin(game.playerHunter.orientation));

                move += 3;
                if (move < jabDistance)
                {
                    this.position    = getPosition;
                    this.position.X += ((int)movement.X);
                    this.position.Y += ((int)movement.Y);
                }

                else
                {
                    this.position              = game.playerHunter.Position;
                    this.position.X           += 40;
                    this.position.Y           -= 20;
                    move                       = 0;
                    game.playerHunter.spearJab = false;
                }
            }

            if (game.playerHunter.spearThrow && game.playerHunter.health > 0)
            {
                //game.playerHunter.spearThrow = true;
                Vector2 movement = Vector2.Zero;
                getPosition = this.position;
                movement    = game.playerHunter.throwVelocity;

                movement.X += 8 * (float)Math.Cos(game.playerHunter.throwOrientation);
                movement.Y += (8 * (float)Math.Sin(game.playerHunter.throwOrientation));
                keyPressed  = true;


                move += 4;
                if (move < throwDistance)
                {
                    this.position    = getPosition;
                    this.position.X += ((int)movement.X);
                    this.position.Y += ((int)movement.Y);
                }

                else
                {
                    this.position    = game.playerHunter.Position;
                    this.position.X += 40;
                    this.position.Y -= 20;
                    move             = 0;
                    game.playerHunter.spearThrow = false;
                }
            }


            if (!keyPressed)
            {
                this.position    = game.playerHunter.Position;
                this.position.X += 40;
                this.position.Y -= 20;
                velocity         = new Vector2();
            }
            if (velocity.Length() > MaxSpeed)
            {
                velocity.Normalize();
                velocity *= maxSpeed;
            }


            base.Update(steering, time);

            //position += velocity;
            //orientation += rotation;

            collide.position.X += (float)((((image.Height - 5) / 2) * Math.Cos(this.orientation)));
            collide.position.Y += (float)(((image.Height - 5) / 2) * Math.Sin(this.orientation));
        }
Ejemplo n.º 4
0
        private void AttatchNewDeerFSM(Deer deer, Game game)
        {
            //Declaring Actions for states
            ScaredAction scaredAction = new ScaredAction();
            WanderAction wanderAction = new WanderAction();
            GrazeAction  grazeAction  = new GrazeAction();
            //FleeFromLionAction fleeFromLionAction = new FleeFromLionAction();
            //FleeFromHunterAction fleeFromHunterAction = new FleeFromHunterAction();
            DeerFleeAction deerFleeAction = new DeerFleeAction();
            resetWander    resetWander    = new resetWander();
            emptyAction    emptyAction    = new emptyAction();
            FlockAction    flockAction    = new FlockAction();

            //Declaring States for FSM
            State scaredState = new State("scared", scaredAction);
            State wanderState = new State("wander", emptyAction, wanderAction, resetWander);
            State grazeState  = new State("graze", grazeAction);
            State fleeState   = new State("flee", deerFleeAction);
            //fleeState.AddActions();
            State flockState = new State("flock", flockAction);


            //Declaring conditions used for transitions
            FearGreaterThan  fearGreaterThan80                 = new FearGreaterThan(80);
            FearGreaterThan  fearGreaterThan60                 = new FearGreaterThan(60);
            FearLessThan     fearLessThan40                    = new FearLessThan(40);
            FearLessThan     fearLessThan20                    = new FearLessThan(20);
            ThreatLevel      hunterHighThreatLevel             = new ThreatLevel(75f);
            DistanceToHunter distanceToHunter                  = new DistanceToHunter(200f);
            AndCondition     andThreatDistanceHunter           = new AndCondition(hunterHighThreatLevel, distanceToHunter);
            OrCondition      orFearThreat                      = new OrCondition(fearGreaterThan60, andThreatDistanceHunter);
            NeighborCountGreaterCondition neighborCountGreater = new NeighborCountGreaterCondition(5);
            NeighborCountLessCondition    neighborCountLess    = new NeighborCountLessCondition(2);
            RandomTimerCondition          fleetoScaredTimer    = new RandomTimerCondition(initialTime, 600);
            RandomTimerCondition          wandertoGrazeTimer   = new RandomTimerCondition(initialTime, shortTimer);//800 to 1200
            RandomTimerCondition          flocktoGrazeTimer    = new RandomTimerCondition(initialTime, 600);
            RandomTimerCondition          grazetoFlockTimer    = new RandomTimerCondition(initialTime, 600);
            RandomCondition randomCondition                    = new RandomCondition(2, 2);
            //AndCondition andRandomLowFear = new AndCondition(randomCondition, fearLessThan40);
            AndCondition andTimerLowFear = new AndCondition(fleetoScaredTimer, fearLessThan20);
            AndCondition andRandomNeighborCountGreater = new AndCondition(randomCondition, neighborCountGreater);
            AndCondition andRandomNeighborCountLess    = new AndCondition(grazetoFlockTimer, neighborCountLess);
            //AndCondition andRNCRandom = new AndCondition(andRandomNeighborCount, grazetoFlockTimer);
            WanderCondition wanderTrue = new WanderCondition();


            //Declaring Transitions
            Transition gotoWanderFromScared = new Transition(andTimerLowFear, wanderState, 0);
            //Transition gotoWanderFromScared = new Transition(andRandomLowFear, wanderState, 0);
            Transition gotoWanderFromGraze  = new Transition(wanderTrue, wanderState, 0);
            Transition gotoWanderFromGraze2 = new Transition(andRandomNeighborCountLess, wanderState, 0);

            Transition gotoGrazeFromScared = new Transition(andTimerLowFear, grazeState, 0);
            //Transition gotoGrazeFromScared = new Transition(andRandomLowFear, grazeState, 0);
            Transition gotoGrazeFromWander = new Transition(wandertoGrazeTimer, grazeState, 0);

            Transition gotoFlockfromGraze = new Transition(andRandomNeighborCountGreater, flockState, 0);
            Transition gotoGrazefromFlock = new Transition(flocktoGrazeTimer, grazeState, 0);

            Transition gotoScaredFromWander = new Transition(orFearThreat, scaredState, 0); //old cond was feargreaterthan 60
            Transition gotoScaredFromGraze  = new Transition(orFearThreat, scaredState, 0);
            Transition gotoScaredFromFlock  = new Transition(orFearThreat, scaredState, 0);

            Transition gotoFlee = new Transition(fearGreaterThan80, fleeState, 0);
            //Transition gotoScaredFromFlee = new Transition(andTimerLowFear, scaredState, 0);
            Transition gotoScaredFromFlee = new Transition(fearLessThan40, scaredState, 0);

            //Declaring actions for transitions
            gotoWanderFromScared.addActions(wanderAction);
            gotoWanderFromGraze.addActions(wanderAction);
            gotoWanderFromGraze2.addActions(wanderAction);
            gotoGrazeFromScared.addActions(grazeAction);
            gotoGrazeFromWander.addActions(grazeAction);
            gotoGrazefromFlock.addActions(grazeAction);
            gotoScaredFromWander.addActions(scaredAction);
            gotoScaredFromGraze.addActions(scaredAction);
            gotoFlee.addActions(deerFleeAction);
            gotoFlockfromGraze.addActions(flockAction);
            gotoScaredFromFlock.addActions(scaredAction);
            gotoScaredFromFlee.addActions(scaredAction);


            //Hooking up transitions to states
            scaredState.addTransition(gotoWanderFromScared);
            scaredState.addTransition(gotoGrazeFromScared);
            scaredState.addTransition(gotoFlee);

            wanderState.addTransition(gotoScaredFromWander);
            wanderState.addTransition(gotoGrazeFromWander);

            grazeState.addTransition(gotoScaredFromGraze);
            grazeState.addTransition(gotoWanderFromGraze);
            grazeState.addTransition(gotoFlockfromGraze);
            grazeState.addTransition(gotoWanderFromGraze2);

            fleeState.addTransition(gotoScaredFromFlee);

            flockState.addTransition(gotoGrazefromFlock);
            flockState.addTransition(gotoScaredFromFlock);


            FiniteStateMachine newFSM = new FiniteStateMachine(wanderState);

            deer.fsm = newFSM;
        }
Ejemplo n.º 5
0
        public void Update(GameTime gameTime)
        {
            if (deerRemoval.Count > 0)
            {
                foreach (Entity d in deerRemoval)
                {
                    deers.Remove(d);
                    deerDead.Add(d);
                }
                deerRemoval.Clear();
            }
            if (deerDeadRemoval.Count > 0)
            {
                foreach (Entity d in deerDeadRemoval)
                {
                    deerDead.Remove(d);
                }
                deerDeadRemoval.Clear();
            }
            foreach (Entity d in deerDead)
            {
                Vector2 distanceHtoD = d.Position - game.playerHunter.Position;
                float   distHtoD     = distanceHtoD.Length();
                if (distHtoD < 30)
                {
                    if (game.playerHunter.health <= 4)
                    {
                        game.playerHunter.health += 2;
                    }
                    else
                    {
                        game.playerHunter.health = 4;
                    }
                    deerDeadRemoval.Add(d);
                }
                Vector2 distanceLtoD = d.Position - game.lion.Position;
                float   distLtoD     = distanceLtoD.Length();
                if (distLtoD < 30)
                {
                    if (game.lion.health <= 6)
                    {
                        game.lion.health += 2;
                    }
                    else
                    {
                        game.lion.health = 6;
                    }
                    deerDeadRemoval.Add(d);
                }
            }
            UpdateDeerNeighbors();
            calcDeersFear();
            foreach (Entity d in deers)
            {
                d.updateFear();
            }
            if (deerCount > 0)
            {
                deerWander = random.Next(1, deerCount);
            }
            else
            {
                deerWander = -1;
            }
            for (int i = 0; i < deers.Count; ++i)
            {
                Deer d = (Deer)deers[i];
                //check what state and timer cooldown
                --wanderTimer;
                if (wanderTimer == 0)
                {
                    if (i == deerWander)
                    {
                        d.wander = true;
                    }
                    wanderTimer = random.Next(600, 1500);
                }
                foreach (Bush b in game.gameWorld.getBushes())
                {
                    if ((d.Position - b.posit).Length() < 33)
                    {
                        if (!b.occupied)
                        {
                            d.Velocity *= .55f;
                            b.occupied  = true;
                            continue;
                        }
                    }
                }

                d.Update(new SteeringOutput(), gameTime);
            }
        }