Ejemplo n.º 1
0
        protected override void Step()
        {
            base.Step();

            if (animTimer % 10 == 0 && ++imageIndex > Sprites.CharacterRects[spriteIndex].Count - 1)
            {
                imageIndex = 0;
                if (currentState == PredState.Emerge)
                {
                    nextState = PredState.Idle;
                }
            }

            if (currentState != nextState)
            {
                stateTimer = 0;
            }
            currentState = nextState;
            int xMove, yMove;

            switch (currentState)
            {
            case PredState.Emerge:
                ChangeSprite("pred_emerge");
                break;

            case PredState.Idle:
                ChangeSprite("pred_idle");
                if (!IsMoving)
                {
                    Dorf foundFood = (Dorf)Characters.FirstOrDefault(x => x is Dorf && Math.Abs(x.GridX - GridX) < 5 && Math.Abs(x.GridY - GridY) < 5);
                    if (foundFood != null)
                    {
                        nextState = PredState.ChaseDorf;
                    }
                    else
                    {
                        bool horizontal = Game.R.Next(2) == 0;
                        Move(horizontal ? Game.R.Next(3) - 1 : 0, horizontal ? 0 : Game.R.Next(3) - 1);
                        nextState = PredState.Wander;
                    }
                }
                break;

            case PredState.Wander:
                ChangeSprite("pred_run");
                if (!IsMoving)
                {
                    nextState = PredState.Idle;
                }
                break;

            case PredState.ChaseDorf:
                ChangeSprite("pred_run");
                if (!IsMoving)
                {
                    Dorf foundFood = (Dorf)Characters.FirstOrDefault(x => x is Dorf && Math.Abs(x.GridX - GridX) + Math.Abs(x.GridY - GridY) == 1);
                    if (foundFood != null && StandingOn != Tile.TileType.Water)
                    {
                        nextState = PredState.Attack;
                    }
                    else
                    {
                        if (localView.MoveToward(localView.GetNearestCharacter(typeof(Dorf)), out xMove, out yMove) && !Game.GridPointOccupied(GridX + xMove, GridY + yMove, true, this))
                        {
                            Move(xMove, yMove);
                        }
                        else
                        {
                            //Move randomly until it can get to the Dorf
                            bool horizontal = Game.R.Next(2) == 0;
                            Move(horizontal ? Game.R.Next(3) - 1 : 0, horizontal ? 0 : Game.R.Next(3) - 1);
                        }
                    }
                }
                break;

            case PredState.Attack:
                ChangeSprite("pred_attack");
                if (stateTimer == 0)
                {
                    Dorf foundFood = (Dorf)Characters.FirstOrDefault(x => x is Dorf && Math.Abs(x.GridX - GridX) + Math.Abs(x.GridY - GridY) == 1);
                    if (foundFood != null)
                    {
                        new SoundPlayer(Properties.Resources.Hit_Hurt2).Play();
                        foundFood.Hit(Strength, this);
                    }
                }
                else if (stateTimer > 40)
                {
                    nextState = PredState.Idle;
                }
                break;

            case PredState.Hurt:
                ChangeSprite("pred_hurt");
                if (stateTimer >= 30)
                {
                    nextState = PredState.Idle;
                }
                break;
            }
        }
Ejemplo n.º 2
0
        protected override void Step()
        {
            base.Step();
            stateTimer++;
            age    += 0.001;
            Thirst += (StandingOn == Tile.TileType.Water) ? -0.4 : 0.02;
            Hunger += 0.01;
            if (Thirst == 100 && Hurt(0.02, "thirst"))
            {
                return;
            }
            if (Hunger == 100 && Hurt(0.02, "hunger"))
            {
                return;
            }
            //if (Stamina == 0 && Hurt(0.005, "exhaustion")) return;

            if (animTimer % 10 == 0 && ++imageIndex > Sprites.CharacterRects[spriteIndex].Count - 1)
            {
                imageIndex = 0;
            }

            int xMove, yMove;

            UpdateCurrentState();
            switch (currentState)
            {
            case DorfState.Idle:
                ChangeSprite("dorf_idle");
                if (++awarenessTimer % 50 == 0)
                {
                    MakeNewDecision();
                }
                break;

            case DorfState.Walk:
                ChangeSprite("dorf_run");
                if (!IsMoving)
                {
                    nextState = DorfState.Idle;
                }
                break;

            case DorfState.Hurt:
                ChangeSprite("dorf_hurt");
                if (stateTimer > 20)
                {
                    nextState = DorfState.RunAway;
                }
                break;

            case DorfState.RunAway:
                ChangeSprite("dorf_run_away");

                if (!IsMoving)
                {
                    if (stateTimer > 400)
                    {
                        nextState = DorfState.Idle;
                    }
                    else
                    {
                        localView.MoveAway(attacker, out xMove, out yMove);
                        if (Game.GridPointOccupied(GridX + xMove, GridY + yMove, true, this))
                        {
                            bool horizontal = Game.R.Next(2) == 0;
                            Move(horizontal ? Game.R.Next(3) - 1 : 0, horizontal ? 0 : Game.R.Next(3) - 1);
                        }
                        else
                        {
                            Move(xMove, yMove);
                        }
                    }
                }
                break;

            case DorfState.GoToFood:
                ChangeSprite("dorf_run");
                if (!IsMoving)
                {
                    Food foundFood = (Food)GameObject.Objects.FirstOrDefault(x => x is Food && x.GridX == GridX && x.GridY == GridY);
                    if (foundFood != null)
                    {
                        nextState = DorfState.StartEat;
                    }

                    if (localView.MoveToward(typeof(Food), out xMove, out yMove) && !Game.GridPointOccupied(GridX + xMove, GridY + yMove, true, this))
                    {
                        Move(xMove, yMove);
                    }
                    else
                    {
                        bool horizontal = Game.R.Next(2) == 0;
                        Move(horizontal ? Game.R.Next(3) - 1 : 0, horizontal ? 0 : Game.R.Next(3) - 1);
                    }
                }
                break;

            case DorfState.GoToWater:
                ChangeSprite("dorf_run");
                if (!IsMoving)
                {
                    if (localView.MoveToward(Tile.TileType.Water, out xMove, out yMove) && !Game.GridPointOccupied(GridX + xMove, GridY + yMove, true, this))
                    {
                        Move(xMove, yMove);
                    }
                    else if (!IsMoving)
                    {
                        nextState = DorfState.Idle;
                    }
                }
                break;

            case DorfState.GoToLand:
                ChangeSprite("dorf_run");
                if (!IsMoving)
                {
                    if (localView.MoveToward(Tile.TileType.Grass, out xMove, out yMove) && !Game.GridPointOccupied(GridX + xMove, GridY + yMove, true, this))
                    {
                        Move(xMove, yMove);
                    }
                    else if (!IsMoving)
                    {
                        nextState = DorfState.Idle;
                    }
                }
                break;

            case DorfState.StartEat:
                ChangeSprite("dorf_eat"); //Temporary, change to eating sprite
                Food food = (Food)GameObject.Objects.FirstOrDefault(x => x is Food && x.GridX == GridX && x.GridY == GridY);
                if (food != null)         //just in case something happened to the food
                {
                    Eat(food);
                    nextState = DorfState.Eat;
                }
                else
                {
                    nextState = DorfState.Idle;
                }
                break;

            case DorfState.Eat:
                ChangeSprite("dorf_eat");     //Temporary, change to eating sprite
                //new SoundPlayer(Properties.Resources.eat).Play();
                if (stateTimer > 50)
                {
                    nextState = DorfState.Idle;
                }
                break;

            case DorfState.Grabbed:
                ChangeSprite("dorf_idle");
                Teleport(Player.Player1.Reticle.GridX, Player.Player1.Reticle.GridY);
                break;

            case DorfState.Die:
                ChangeSprite("dorf_die");
                break;
            }
        }