private void MakeNewDecision() { if (!IsMoving) { if (thirst > 70 - Game.R.Next(20) && localView.InRadius(Tile.TileType.Water, (int)intelligence)) { nextState = DorfState.GoToWater; } else if (hunger > 70 - Game.R.Next(20) && localView.InRadius(typeof(Food), (int)intelligence)) { nextState = DorfState.GoToFood; } else if (StandingOn == Tile.TileType.Water && localView.InRadius(Tile.TileType.Grass, (int)intelligence)) { nextState = DorfState.GoToLand; } else if (Game.R.Next(3) == 0) { bool horizontal = Game.R.Next(2) == 0; Move(horizontal ? Game.R.Next(3) - 1 : 0, horizontal ? 0 : Game.R.Next(3) - 1); nextState = DorfState.Walk; } else { nextState = DorfState.Idle; if (Game.R.Next(1000) == 0 && !Game.GridPointOccupied(GridX, GridY, true, this)) { new Egg(this); } } } }
public void LetGo() { if (IsGrabbed) { IsGrabbed = false; nextState = DorfState.Idle; } }
public bool Grab() { if (IsMoving) { return(false); } nextState = DorfState.Grabbed; IsGrabbed = true; return(true); }
private void UpdateCurrentState() { if (currentState != nextState) { currentState = nextState; switch (currentState) { case DorfState.GoToFood: case DorfState.GoToLand: case DorfState.GoToWater: case DorfState.Walk: ChangeSprite("dorf_run"); break; case DorfState.Idle: ChangeSprite("dorf_idle"); break; case DorfState.Hurt: ChangeSprite("dorf_hurt"); break; case DorfState.RunAway: ChangeSprite("dorf_run_away"); break; } stateTimer = 0; } }
public void Hit(int strength, Character attacker) { this.attacker = attacker; nextState = DorfState.Hurt; Hurt(strength * 10, attacker is Player ? "murder" : "a predator"); }
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; } }