From fcfa11c284b124d3fd10b121d636ff57eb55881e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C5=A1a=20Ceviel?= Date: Fri, 6 Feb 2026 10:06:04 +0300 Subject: [PATCH] lab1 base --- Assets/Code/Base/AgentBehaviour.cs | 5 ++++ Assets/Code/Lab1.meta | 3 +++ Assets/Code/Lab1/Evade.cs | 42 ++++++++++++++++++++++++++++++ Assets/Code/Lab1/Evade.cs.meta | 3 +++ Assets/Code/Lab1/Flee.cs | 19 ++++++++++++++ Assets/Code/Lab1/Flee.cs.meta | 3 +++ Assets/Code/Lab1/Pursue.cs | 42 ++++++++++++++++++++++++++++++ Assets/Code/Lab1/Pursue.cs.meta | 3 +++ Assets/Code/Lab1/Seek.cs | 19 ++++++++++++++ Assets/Code/Lab1/Seek.cs.meta | 3 +++ 10 files changed, 142 insertions(+) create mode 100644 Assets/Code/Lab1.meta create mode 100644 Assets/Code/Lab1/Evade.cs create mode 100644 Assets/Code/Lab1/Evade.cs.meta create mode 100644 Assets/Code/Lab1/Flee.cs create mode 100644 Assets/Code/Lab1/Flee.cs.meta create mode 100644 Assets/Code/Lab1/Pursue.cs create mode 100644 Assets/Code/Lab1/Pursue.cs.meta create mode 100644 Assets/Code/Lab1/Seek.cs create mode 100644 Assets/Code/Lab1/Seek.cs.meta diff --git a/Assets/Code/Base/AgentBehaviour.cs b/Assets/Code/Base/AgentBehaviour.cs index e8d9679..7da7872 100644 --- a/Assets/Code/Base/AgentBehaviour.cs +++ b/Assets/Code/Base/AgentBehaviour.cs @@ -7,6 +7,11 @@ namespace Code.Base public GameObject target; protected Agent Agent; + public virtual void Awake() + { + Agent = GetComponent(); + } + public virtual void Update() { Agent.SetSteering(GetSteering()); diff --git a/Assets/Code/Lab1.meta b/Assets/Code/Lab1.meta new file mode 100644 index 0000000..8df22ad --- /dev/null +++ b/Assets/Code/Lab1.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: d7cf9369a03c4325914d2dd923ef315f +timeCreated: 1770360608 \ No newline at end of file diff --git a/Assets/Code/Lab1/Evade.cs b/Assets/Code/Lab1/Evade.cs new file mode 100644 index 0000000..44270b3 --- /dev/null +++ b/Assets/Code/Lab1/Evade.cs @@ -0,0 +1,42 @@ +using Code.Base; +using UnityEngine; + +namespace Code.Lab1 +{ + public class Evade : Flee + { + public float maxPrediction; + private GameObject _targetAux; + private Agent _targetAgent; + + public override void Awake() + { + base.Awake(); + _targetAgent = target.GetComponent(); + _targetAux = target; + target = new GameObject(); + } + + private void OnDestroy() + { + Destroy(_targetAux); + } + + public override Steering GetSteering() + { + var direction = _targetAux.transform.position - transform.position; + var distance = direction.magnitude; + var speed = Agent.velocity.magnitude; + + float prediction; + if (speed <= distance / maxPrediction) + prediction = maxPrediction; + else + prediction = distance / speed; + + target.transform.position = _targetAux.transform.position; + target.transform.position += _targetAgent.velocity * prediction; + return base.GetSteering(); + } + } +} \ No newline at end of file diff --git a/Assets/Code/Lab1/Evade.cs.meta b/Assets/Code/Lab1/Evade.cs.meta new file mode 100644 index 0000000..5f9e977 --- /dev/null +++ b/Assets/Code/Lab1/Evade.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 0ca3f96f0e3e4d4689a7fd323f40553c +timeCreated: 1770361482 \ No newline at end of file diff --git a/Assets/Code/Lab1/Flee.cs b/Assets/Code/Lab1/Flee.cs new file mode 100644 index 0000000..cdbb6f0 --- /dev/null +++ b/Assets/Code/Lab1/Flee.cs @@ -0,0 +1,19 @@ +using Code.Base; + +namespace Code.Lab1 +{ + public class Flee : AgentBehaviour + { + public override Steering GetSteering() + { + var steering = new Steering + { + Linear = transform.position - target.transform.position + }; + + steering.Linear.Normalize(); + steering.Linear *= Agent.maxAcceleration; + return steering; + } + } +} \ No newline at end of file diff --git a/Assets/Code/Lab1/Flee.cs.meta b/Assets/Code/Lab1/Flee.cs.meta new file mode 100644 index 0000000..ef56e40 --- /dev/null +++ b/Assets/Code/Lab1/Flee.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: f4548b6a916c440b8868ad33fe6f7ace +timeCreated: 1770360860 \ No newline at end of file diff --git a/Assets/Code/Lab1/Pursue.cs b/Assets/Code/Lab1/Pursue.cs new file mode 100644 index 0000000..5f5335f --- /dev/null +++ b/Assets/Code/Lab1/Pursue.cs @@ -0,0 +1,42 @@ +using Code.Base; +using UnityEngine; + +namespace Code.Lab1 +{ + public class Pursue : Seek + { + public float maxPrediction; + private GameObject _targetAux; + private Agent _targetAgent; + + public override void Awake() + { + base.Awake(); + _targetAgent = target.GetComponent(); + _targetAux = target; + target = new GameObject(); + } + + private void OnDestroy() + { + Destroy(_targetAux); + } + + public override Steering GetSteering() + { + var direction = _targetAux.transform.position - transform.position; + var distance = direction.magnitude; + var speed = Agent.velocity.magnitude; + + float prediction; + if (speed <= distance / maxPrediction) + prediction = maxPrediction; + else + prediction = distance / speed; + + target.transform.position = _targetAux.transform.position; + target.transform.position += _targetAgent.velocity * prediction; + return base.GetSteering(); + } + } +} \ No newline at end of file diff --git a/Assets/Code/Lab1/Pursue.cs.meta b/Assets/Code/Lab1/Pursue.cs.meta new file mode 100644 index 0000000..5e6dea6 --- /dev/null +++ b/Assets/Code/Lab1/Pursue.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 6ec8d34239cc41588d59d9fc17112c25 +timeCreated: 1770361071 \ No newline at end of file diff --git a/Assets/Code/Lab1/Seek.cs b/Assets/Code/Lab1/Seek.cs new file mode 100644 index 0000000..1089d86 --- /dev/null +++ b/Assets/Code/Lab1/Seek.cs @@ -0,0 +1,19 @@ +using Code.Base; + +namespace Code.Lab1 +{ + public class Seek : AgentBehaviour + { + public override Steering GetSteering() + { + var steering = new Steering + { + Linear = target.transform.position - transform.position + }; + + steering.Linear.Normalize(); + steering.Linear *= Agent.maxAcceleration; + return steering; + } + } +} \ No newline at end of file diff --git a/Assets/Code/Lab1/Seek.cs.meta b/Assets/Code/Lab1/Seek.cs.meta new file mode 100644 index 0000000..e8401e5 --- /dev/null +++ b/Assets/Code/Lab1/Seek.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: a8ff81d84dc54a1fa40f2ebff39c9ef6 +timeCreated: 1770360733 \ No newline at end of file