< Summary

Information
Class: Safarimacik.Model.SleepingAnimalState
Assembly: Safarimacik
File(s): /builds/szofttech-ab-2025/group-03/safarimacik/model/AnimalState.cs
Line coverage
100%
Covered lines: 11
Uncovered lines: 0
Coverable lines: 11
Total lines: 116
Line coverage: 100%
Branch coverage
100%
Covered branches: 2
Total branches: 2
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
Tick()100%22100%

File(s)

/builds/szofttech-ab-2025/group-03/safarimacik/model/AnimalState.cs

#LineLine coverage
 1using Godot;
 2
 3
 4namespace Safarimacik.Model;
 5
 6
 7/// <summary>
 8/// Base class for all animal states.
 9/// </summary>
 10public abstract class AnimalState(Animal animal) {
 11  protected Animal _animal = animal;
 12
 13  public abstract void Tick();
 14}
 15
 16/// <summary>
 17/// Represents a state in which the animal has nothing to do.
 18/// </summary>
 19public class IdleAnimalState(Animal animal) : AnimalState(animal) {
 20  public override void Tick() {
 21    _animal.Age();
 22    _animal.Crave();
 23    _animal.CheckVitals();
 24  }
 25}
 26
 27/// <summary>
 28/// Represents a state in which the animal approaches its destination following its given path.
 29/// </summary>
 30public class WalkingAnimalState(Animal animal, List<Vector2> path) : AnimalState(animal) {
 31  private Vector2 _destination = path.Last();
 32  private List<Vector2> _path = path;
 33
 34  public override void Tick() {
 35    _animal.Age();
 36    _animal.Crave();
 37    _animal.CheckVitals();
 38    if (!HasReachedDestination()) {
 39      _animal.Move(_path);
 40    }
 41  }
 42
 43  public bool HasReachedDestination() {
 44    return _destination == _animal.Position;
 45  }
 46}
 47
 48/// <summary>
 49/// Represents a state in which the animal is consuming nearby food or water.
 50/// </summary>
 51public class ConsumingAnimalState(Animal animal, bool isDrinking, Plant? plant, Herbivore? prey) : AnimalState(animal) {
 52  private bool _isDrinking = isDrinking;
 53  private Plant? _plant = plant;
 54  private Herbivore? _prey = prey;
 55
 56  public Plant? Plant => _plant;
 57
 58  public override void Tick() {
 59    _animal.Age();
 60
 61    if (_isDrinking) {
 62      _animal.Drink(Water.Nutrition);
 63    } else {
 64      try {
 65        if (_animal is Herbivore && _plant != null) {
 66          _animal.Eat(_plant.Nutrition);
 67          if (_plant is RegeneratingPlant regenPlant) {
 68            regenPlant.Consume();
 69          }
 70        } else if (_animal is Predator && _prey != null) {
 71          _animal.Eat(_prey.Nutrition);
 72          _prey.Kill();
 73        }
 74      } catch (ObjectDisposedException) {
 75        _animal.State = new IdleAnimalState(_animal);
 76      } finally {
 77        _animal.RemoveFoodFromMemory(_animal.TilePosition); // if food persists on this tile after consumption, it will 
 78      }
 79    }
 80  }
 81}
 82
 83/// <summary>
 84/// Represents a state in which the animal is asleep after consumption.
 85/// </summary>
 186public class SleepingAnimalState(Animal animal, int duration) : AnimalState(animal) {
 187  private int _duration = duration;
 88
 189  public override void Tick() {
 190    _animal.Age();
 191    _animal.CheckVitals();
 192    if (_duration > 0) {
 193      _duration -= 1;
 194    } else {
 195      _animal.State = new IdleAnimalState(_animal);
 196    }
 197  }
 98}
 99
 100/// <summary>
 101/// Represents a state in which the animal is mating with another animal.
 102/// </summary>
 103public class MatingAnimalState(Animal animal) : AnimalState(animal) {
 104  private int _duration = 5;
 105  public override void Tick() {
 106    if (_duration > 0) {
 107      --_duration;
 108      return;
 109    }
 110
 111    _animal.Mate();
 112    _animal.Age();
 113    _animal.CheckVitals();
 114    _animal.State = new IdleAnimalState(_animal);
 115  }
 116}