| | | 1 | | using Godot; |
| | | 2 | | |
| | | 3 | | |
| | | 4 | | namespace Safarimacik.Model; |
| | | 5 | | |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// Base class for all animal states. |
| | | 9 | | /// </summary> |
| | | 10 | | public 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> |
| | | 19 | | public 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> |
| | | 30 | | public 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> |
| | | 51 | | public 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> |
| | 1 | 86 | | public class SleepingAnimalState(Animal animal, int duration) : AnimalState(animal) { |
| | 1 | 87 | | private int _duration = duration; |
| | | 88 | | |
| | 1 | 89 | | public override void Tick() { |
| | 1 | 90 | | _animal.Age(); |
| | 1 | 91 | | _animal.CheckVitals(); |
| | 1 | 92 | | if (_duration > 0) { |
| | 1 | 93 | | _duration -= 1; |
| | 1 | 94 | | } else { |
| | 1 | 95 | | _animal.State = new IdleAnimalState(_animal); |
| | 1 | 96 | | } |
| | 1 | 97 | | } |
| | | 98 | | } |
| | | 99 | | |
| | | 100 | | /// <summary> |
| | | 101 | | /// Represents a state in which the animal is mating with another animal. |
| | | 102 | | /// </summary> |
| | | 103 | | public 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 | | } |