| | | 1 | | using Godot; |
| | | 2 | | |
| | | 3 | | |
| | | 4 | | namespace Safarimacik.Model; |
| | | 5 | | |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// Jeeps are purchasable vehicles that can move along a path. |
| | | 9 | | /// </summary> |
| | | 10 | | /// <remarks> |
| | | 11 | | /// Jeeps are used to transport passengers and can spot animals along their route. Each jeep has a path it follows, a st |
| | | 12 | | /// </remarks> |
| | | 13 | | public class Jeep : Purchasable { |
| | | 14 | | private int _passengers; |
| | | 15 | | private List<Vector2> _path; |
| | | 16 | | private Vector2 _position; |
| | | 17 | | private HashSet<Animal> _animalsSeen; |
| | | 18 | | |
| | 1 | 19 | | public Jeep(List<Vector2> path, Vector2 position, int passengers) { |
| | 1 | 20 | | _passengers = passengers; |
| | 1 | 21 | | _path = path; |
| | 1 | 22 | | _position = position; |
| | 1 | 23 | | _animalsSeen = []; |
| | 1 | 24 | | } |
| | | 25 | | |
| | | 26 | | /// <summary>Occurs when the jeep is moving</summary> |
| | | 27 | | public event EventHandler<EventArgs>? Moving; |
| | | 28 | | /// <summary>Occurs when the jeep reaches its goal</summary> |
| | | 29 | | public event EventHandler<JeepFinishedEventArgs>? Finished; |
| | | 30 | | |
| | 1 | 31 | | public Vector2 Position => _position; |
| | | 32 | | public const int Price = 70; |
| | 1 | 33 | | int Purchasable.Price => Price; |
| | 1 | 34 | | public float Speed => 200f; |
| | | 35 | | |
| | | 36 | | /// <summary> |
| | | 37 | | /// Moves the jeep along it's path. |
| | | 38 | | /// </summary> |
| | | 39 | | /// <returns>True, if it reached the end</returns> |
| | 1 | 40 | | public bool Move() { |
| | 1 | 41 | | if (_position == _path[0]) { _path.RemoveAt(0); } |
| | | 42 | | |
| | 1 | 43 | | if (_path.Count == 0) { |
| | 1 | 44 | | OnFinished(); |
| | 1 | 45 | | return true; |
| | | 46 | | } |
| | | 47 | | |
| | 1 | 48 | | if (_position.DistanceTo(_path[0]) <= Speed) { |
| | 1 | 49 | | _position = _path[0]; |
| | 1 | 50 | | } else { |
| | 1 | 51 | | Vector2 direction = (_path[0] - _position).Normalized(); |
| | 1 | 52 | | _position += direction * Speed; |
| | 1 | 53 | | } |
| | 1 | 54 | | OnMoving(); |
| | 1 | 55 | | return false; |
| | 1 | 56 | | } |
| | | 57 | | |
| | | 58 | | /// <summary> |
| | | 59 | | /// Adds an animal to the jeep's seen animals set. |
| | | 60 | | /// </summary> |
| | | 61 | | /// <param name="animal">The animal spotted by the jeep.</param> |
| | 1 | 62 | | public void AnimalSeen(Animal animal) { |
| | 1 | 63 | | _animalsSeen.Add(animal); |
| | 1 | 64 | | } |
| | | 65 | | |
| | 1 | 66 | | private void OnFinished() { |
| | 1 | 67 | | Finished?.Invoke(this, new JeepFinishedEventArgs(_animalsSeen.Count, _passengers)); |
| | 1 | 68 | | } |
| | | 69 | | |
| | 1 | 70 | | private void OnMoving() { |
| | 1 | 71 | | Moving?.Invoke(this, EventArgs.Empty); |
| | 1 | 72 | | } |
| | | 73 | | } |