diff options
| author | Fuwn <[email protected]> | 2025-11-20 19:17:14 -0800 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2025-11-20 19:17:14 -0800 |
| commit | 9858b069ea4264840d98094e2e36ad2517f2215f (patch) | |
| tree | 367ac8bcab6107ef8dfa4be524f834a5336d9bc3 /MorgSimulator/Morg.cs | |
| parent | feat: Add Assignment 2 diagrams (diff) | |
| download | cst276-9858b069ea4264840d98094e2e36ad2517f2215f.tar.xz cst276-9858b069ea4264840d98094e2e36ad2517f2215f.zip | |
feat: Implement Assignment 3 functionality
Diffstat (limited to 'MorgSimulator/Morg.cs')
| -rw-r--r-- | MorgSimulator/Morg.cs | 82 |
1 files changed, 2 insertions, 80 deletions
diff --git a/MorgSimulator/Morg.cs b/MorgSimulator/Morg.cs index cfac9dd..ba91564 100644 --- a/MorgSimulator/Morg.cs +++ b/MorgSimulator/Morg.cs @@ -1,87 +1,9 @@ -using System.Collections.Generic; -using System.Linq; +using MorgSimulator.Framework; namespace MorgSimulator { public class Morg(int id, (int x, int y) location, (int x, int y) direction) + : Entity(id, location, direction) { - public int Id { get; set; } = id; - public string Type { get; set; } = string.Empty; - public (int x, int y) Location { get; set; } = location; - public (int x, int y) Direction { get; set; } = direction; - public bool IsAlive { get; set; } = true; - public IMovementStrategy MovementStrategy { get; set; } = null!; - public IFeedingStrategy FeedingStrategy { get; set; } = null!; - public List<string> PreyTypes { get; set; } = []; -#nullable enable - public Morg? Prey { get; set; } -#nullable disable - - private readonly List<Morg> _observers = []; - - public void Attach(Morg observer) - { - if (!_observers.Contains(observer)) - _observers.Add(observer); - } - - public void Detach(Morg observer) - { - _observers.Remove(observer); - } - - public void Notify() - { - foreach (var observer in _observers) - observer.Update(this); - } - - public void Update(Morg subject) - { - Direction = CalculateDirectionToTarget(subject.Location); - } - - public void Move() - { - if (!IsAlive) return; - - var newLocation = (Location.x + Direction.x, Location.y + Direction.y); - - MovementStrategy.Move(this, newLocation); - Notify(); - } - - public void Feed() - { - if (!IsAlive || Prey == null || !Prey.IsAlive) return; - - FeedingStrategy.Feed(this, Prey); - - Prey = null; - } - - public bool CanEat(string targetType) - { - return PreyTypes.Contains(targetType); - } - - public (int x, int y) CalculateDirectionToTarget((int x, int y) target) - { - int deltaX = target.x - Location.x; - int deltaY = target.y - Location.y; - int directionX = deltaX > 0 ? 1 : (deltaX < 0 ? -1 : 0); - int directionY = deltaY > 0 ? 1 : (deltaY < 0 ? -1 : 0); - - return (directionX, directionY); - } - - public double DistanceTo((int x, int y) target) - { - int deltaX = target.x - Location.x; - int deltaY = target.y - Location.y; - - return System.Math.Sqrt(deltaX * deltaX + deltaY * deltaY); - } - } }
\ No newline at end of file |