< Summary

Information
Class: Safarimacik.Model.ConsumingAnimalState
Assembly: Safarimacik
File(s): /builds/szofttech-ab-2025/group-03/safarimacik/model/AnimalState.cs
Line coverage
96%
Covered lines: 26
Uncovered lines: 1
Coverable lines: 27
Total lines: 116
Line coverage: 96.2%
Branch coverage
91%
Covered branches: 11
Total branches: 12
Branch coverage: 91.6%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
get_Plant()100%11100%
Tick()91.67%121295.45%

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>
 151public class ConsumingAnimalState(Animal animal, bool isDrinking, Plant? plant, Herbivore? prey) : AnimalState(animal) {
 152  private bool _isDrinking = isDrinking;
 153  private Plant? _plant = plant;
 154  private Herbivore? _prey = prey;
 55
 156  public Plant? Plant => _plant;
 57
 158  public override void Tick() {
 159    _animal.Age();
 60
 161    if (_isDrinking) {
 162      _animal.Drink(Water.Nutrition);
 163    } else {
 164      try {
 165        if (_animal is Herbivore && _plant != null) {
 166          _animal.Eat(_plant.Nutrition);
 167          if (_plant is RegeneratingPlant regenPlant) {
 168            regenPlant.Consume();
 169          }
 170        } else if (_animal is Predator && _prey != null) {
 171          _animal.Eat(_prey.Nutrition);
 172          _prey.Kill();
 173        }
 174      } catch (ObjectDisposedException) {
 075        _animal.State = new IdleAnimalState(_animal);
 176      } finally {
 177        _animal.RemoveFoodFromMemory(_animal.TilePosition); // if food persists on this tile after consumption, it will 
 178      }
 179    }
 180  }
 81}
 82
 83/// <summary>
 84/// Represents a state in which the animal is asleep after consumption.
 85/// </summary>
 86public class SleepingAnimalState(Animal animal, int duration) : AnimalState(animal) {
 87  private int _duration = duration;
 88
 89  public override void Tick() {
 90    _animal.Age();
 91    _animal.CheckVitals();
 92    if (_duration > 0) {
 93      _duration -= 1;
 94    } else {
 95      _animal.State = new IdleAnimalState(_animal);
 96    }
 97  }
 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}