< Summary

Information
Class: Safarimacik.Model.Ranger
Assembly: Safarimacik
File(s): /builds/szofttech-ab-2025/group-03/safarimacik/model/Ranger.cs
Line coverage
75%
Covered lines: 43
Uncovered lines: 14
Coverable lines: 57
Total lines: 105
Line coverage: 75.4%
Branch coverage
62%
Covered branches: 10
Total branches: 16
Branch coverage: 62.5%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%22100%
get_LastKilled()100%11100%
get_Target()100%11100%
Safarimacik.Model.Purchasable.get_Price()100%11100%
get_Position()100%11100%
get_TilePosition()100%11100%
set_TileSpeed(...)100%11100%
get_State()100%11100%
set_State(...)100%11100%
Move(...)33.33%18631.58%
SelectTarget(...)100%22100%
CancelTargetSelection()100%11100%
KillTarget()50%22100%
OnStepsTaken(...)50%22100%
OnStateChanged()100%22100%

File(s)

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

#LineLine coverage
 1using Godot;
 2
 3
 4namespace Safarimacik.Model;
 5
 6
 7/// <summary>
 8/// A ranger that regulates predators.
 9/// </summary>
 10/// Its purpose is to be able to regulate the number of predators.
 11/// It needs to be paid a wage every day.
 12public class Ranger : Purchasable {
 13  public const int Wage = 30;
 14  public const int Price = 95;
 15
 16  private Predator? _target;
 17  private Predator? _lastKilled;
 118  private float _moveSpeed = 25f;
 019  private Func<Vector2, float> _tileSpeed = _ => 0.0f;
 20  private Vector2 _position;
 121  private RangerState _state = null!;
 22
 23  public event Action<List<(Vector2, float)>>? StepsTaken;
 24  public event Action? StateChanged;
 25
 126  public Predator? LastKilled => _lastKilled;
 127  public Predator? Target => _target;
 128  int Purchasable.Price => Price;
 129  public Vector2 Position => _position;
 130  public Vector2I TilePosition => new((int)_position.X / 16, (int)_position.Y / 16);
 31  public Func<Vector2, float> TileSpeed {
 132    set { _tileSpeed = value; }
 33  }
 34  public RangerState State {
 135    get => _state;
 136    set {
 137      _state = value;
 138      OnStateChanged();
 139    }
 40  }
 41
 142  public Ranger(Vector2 position) {
 143    _position = position;
 144    State = new IdleRangerState(this); // this sets _state
 145  }
 46
 47  /// <summary>
 48  /// Moves the ranger through the given route, taking the tilespeed into account. (Should be called each tick.)
 49  /// </summary>
 50  /// <param name="path">List of points the ranger moves through</param>
 51  /// <remarks>
 52  /// The method removes points from 'path' that have been reached by the ranger during it's movement.
 53  /// </remarks>
 154  public void Move(List<Vector2> path) {
 155    float distanceRemaining = _moveSpeed;
 56
 157    List<(Vector2, float)> stepsTaken = [];
 58
 159    while (distanceRemaining > 0 && path.Count > 0) {
 060      Vector2 nextPoint = path[0];
 061      Vector2 toNext = nextPoint - _position;
 62
 063      if (distanceRemaining >= toNext.Length() / _tileSpeed(_position)) {
 064        _position = nextPoint;
 065        path.RemoveAt(0);
 066        distanceRemaining -= toNext.Length() / _tileSpeed(_position);
 067      } else {
 068        Vector2 direction = toNext.Normalized();
 069        _position += direction * distanceRemaining * _tileSpeed(_position);
 070        distanceRemaining = 0;
 071      }
 072      stepsTaken.Add((_position, _moveSpeed * _tileSpeed(_position)));
 073    }
 174    OnStepsTaken(stepsTaken);
 175  }
 76
 77  /// <summary>
 78  /// Sets the provided animal (if it's a predator) as this ranger's target.
 79  /// </summary>
 80  /// <param name="target">The target animal</param>
 181  public void SelectTarget(Animal target) {
 182    if (target is Predator predator) {
 183      _target = predator;
 184    }
 185  }
 86
 187  public void CancelTargetSelection() {
 188    _target = null;
 189    State = new IdleRangerState(this);
 190  }
 91
 192  public void KillTarget() {
 193    _target?.Kill();
 194    _lastKilled = _target;
 195    _target = null;
 196  }
 97
 198  private void OnStepsTaken(List<(Vector2, float)> steps) {
 199    StepsTaken?.Invoke(steps);
 1100  }
 101
 1102  private void OnStateChanged() {
 1103    StateChanged?.Invoke();
 1104  }
 105}