lab3
This commit is contained in:
1 parent
a70076ebe9
commit
854835878c
20 files changed
+2542
No files matched your search
@@ -6,6 +6,8 @@ namespace Code.Base
|
||||
{
|
||||
public float maxSpeed;
|
||||
public float maxAcceleration;
|
||||
public float maxRotation;
|
||||
public float maxAngularAcceleration;
|
||||
public float orientation;
|
||||
public float rotation;
|
||||
public Vector3 velocity;
|
||||
|
||||
@@ -21,5 +21,27 @@ namespace Code.Base
|
||||
{
|
||||
return new Steering();
|
||||
}
|
||||
|
||||
public float MapToRange(float rotation)
|
||||
{
|
||||
rotation %= 360;
|
||||
|
||||
if (Mathf.Abs(rotation) <= 180) return rotation;
|
||||
|
||||
if (rotation < 0)
|
||||
rotation += 360;
|
||||
else
|
||||
rotation -= 360;
|
||||
|
||||
return rotation;
|
||||
}
|
||||
|
||||
public Vector3 GetOriAsVec(float orientation)
|
||||
{
|
||||
var vector = Vector3.zero;
|
||||
vector.x = Mathf.Sin(orientation * Mathf.Deg2Rad) * 1;
|
||||
vector.z = Mathf.Cos(orientation * Mathf.Deg2Rad) * 1;
|
||||
return vector.normalized;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b64ec4089a9b47808fd78c31e265313c
|
||||
timeCreated: 1770707286
|
||||
@@ -0,0 +1,42 @@
|
||||
using Code.Base;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Code.Lab3
|
||||
{
|
||||
public class Align : AgentBehaviour
|
||||
{
|
||||
public float targetRadius;
|
||||
public float slowRadius;
|
||||
public float timeToTarget = 0.1f;
|
||||
|
||||
public override Steering GetSteering()
|
||||
{
|
||||
var steering = new Steering();
|
||||
var targetOrientation = target.GetComponent<Agent>().orientation;
|
||||
var rotation = targetOrientation - Agent.orientation;
|
||||
rotation = MapToRange(rotation);
|
||||
|
||||
var rotationSize = Mathf.Abs(rotation);
|
||||
if (rotationSize < targetRadius)
|
||||
return steering;
|
||||
|
||||
float targetRotation;
|
||||
if (rotationSize > slowRadius)
|
||||
targetRotation = Agent.maxRotation;
|
||||
else
|
||||
targetRotation = Agent.maxRotation * rotationSize / slowRadius;
|
||||
targetRotation *= rotation / rotationSize;
|
||||
|
||||
steering.Angular = targetRotation - Agent.rotation;
|
||||
steering.Angular /= timeToTarget;
|
||||
var angularAccel = Mathf.Abs(steering.Angular);
|
||||
|
||||
if (!(angularAccel > Agent.maxAngularAcceleration)) return steering;
|
||||
|
||||
steering.Angular /= angularAccel;
|
||||
steering.Angular *= Agent.maxAngularAcceleration;
|
||||
|
||||
return steering;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 34b1d91c065347f9976b5ac85d935996
|
||||
timeCreated: 1770707295
|
||||
@@ -0,0 +1,35 @@
|
||||
using Code.Base;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Code.Lab3
|
||||
{
|
||||
public class Face : Align
|
||||
{
|
||||
protected GameObject TargetAux;
|
||||
|
||||
public override void Awake()
|
||||
{
|
||||
base.Awake();
|
||||
TargetAux = target;
|
||||
target = new GameObject();
|
||||
target.AddComponent<Agent>();
|
||||
}
|
||||
|
||||
void OnDestroy()
|
||||
{
|
||||
Destroy(target);
|
||||
}
|
||||
|
||||
public override Steering GetSteering()
|
||||
{
|
||||
var direction = TargetAux.transform.position - transform.position;
|
||||
|
||||
if (direction.magnitude <= 0) return base.GetSteering();
|
||||
|
||||
var targetOrientation = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg;
|
||||
target.GetComponent<Agent>().orientation = targetOrientation;
|
||||
|
||||
return base.GetSteering();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 0e1c0c61798542378c8688e96a5fa60f
|
||||
timeCreated: 1770707773
|
||||
@@ -0,0 +1,99 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Code.Lab3
|
||||
{
|
||||
public class Path : MonoBehaviour
|
||||
{
|
||||
public List<GameObject> nodes;
|
||||
private List<PathSegment> _segments;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
_segments = GetSegments();
|
||||
}
|
||||
|
||||
public List<PathSegment> GetSegments()
|
||||
{
|
||||
var segments = new List<PathSegment>();
|
||||
for (var i = 0; i < nodes.Count - 1; i++)
|
||||
{
|
||||
var src = nodes[i].transform.position;
|
||||
var dst = nodes[i + 1].transform.position;
|
||||
var segment = new PathSegment(src, dst);
|
||||
segments.Add(segment);
|
||||
}
|
||||
return segments;
|
||||
}
|
||||
|
||||
public float GetParam(Vector3 position, float lastParam)
|
||||
{
|
||||
PathSegment currentSegment = null;
|
||||
var tempParam = 0f;
|
||||
foreach (var ps in _segments)
|
||||
{
|
||||
tempParam += Vector3.Distance(ps.A, ps.B);
|
||||
|
||||
if (!(lastParam <= tempParam)) continue;
|
||||
|
||||
currentSegment = ps;
|
||||
break;
|
||||
}
|
||||
|
||||
if (currentSegment == null)
|
||||
return 0;
|
||||
|
||||
var currPos = position - currentSegment.A;
|
||||
var segmentDirection = currentSegment.B - currentSegment.A;
|
||||
segmentDirection.Normalize();
|
||||
|
||||
var pointInSegment = Vector3.Project(currPos, segmentDirection);
|
||||
|
||||
var param = tempParam - Vector3.Distance(currentSegment.A, currentSegment.B) + pointInSegment.magnitude;
|
||||
return param;
|
||||
}
|
||||
|
||||
public Vector3 GetPosition(float param)
|
||||
{
|
||||
PathSegment currentSegment = null;
|
||||
var tempParam = 0f;
|
||||
|
||||
foreach (var ps in _segments)
|
||||
{
|
||||
tempParam += Vector3.Distance(ps.A, ps.B);
|
||||
|
||||
if (!(param <= tempParam)) continue;
|
||||
|
||||
currentSegment = ps;
|
||||
break;
|
||||
}
|
||||
|
||||
if (currentSegment == null)
|
||||
return Vector3.zero;
|
||||
|
||||
var segmentDirection = currentSegment.B - currentSegment.A;
|
||||
segmentDirection.Normalize();
|
||||
|
||||
var localParam = param - (tempParam - Vector3.Distance(currentSegment.A, currentSegment.B));
|
||||
|
||||
var position = currentSegment.A + segmentDirection * localParam;
|
||||
|
||||
return position;
|
||||
}
|
||||
|
||||
private void OnDrawGizmos()
|
||||
{
|
||||
var tmp = Gizmos.color;
|
||||
Gizmos.color = Color.magenta;
|
||||
for (var i = 0; i < nodes.Count - 1; i++)
|
||||
{
|
||||
var src = nodes[i].transform.position;
|
||||
var dst = nodes[i + 1].transform.position;
|
||||
var direction = dst - src;
|
||||
Gizmos.DrawRay(src, direction);
|
||||
}
|
||||
Gizmos.color = tmp;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 0a98807d2ea046a68da5884b9471a540
|
||||
timeCreated: 1770708805
|
||||
@@ -0,0 +1,30 @@
|
||||
using Code.Base;
|
||||
using Code.Lab1;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Code.Lab3
|
||||
{
|
||||
public class PathFollower : Seek
|
||||
{
|
||||
public Path path;
|
||||
public float pathOffset;
|
||||
private float _currentParam;
|
||||
|
||||
public override void Awake()
|
||||
{
|
||||
base.Awake();
|
||||
target = new GameObject();
|
||||
_currentParam = 0;
|
||||
}
|
||||
|
||||
public override Steering GetSteering()
|
||||
{
|
||||
_currentParam = path.GetParam(transform.position, _currentParam);
|
||||
|
||||
var targetParam = _currentParam + pathOffset;
|
||||
target.transform.position = path.GetPosition(targetParam);
|
||||
|
||||
return base.GetSteering();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 350da6a4a8ee483c8fc2227fe023b2bc
|
||||
timeCreated: 1770709719
|
||||
@@ -0,0 +1,18 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace Code.Lab3
|
||||
{
|
||||
public class PathSegment
|
||||
{
|
||||
public Vector3 A;
|
||||
public Vector3 B;
|
||||
|
||||
public PathSegment () : this (Vector3.zero, Vector3.zero) {}
|
||||
|
||||
public PathSegment (Vector3 a, Vector3 b)
|
||||
{
|
||||
A = a;
|
||||
B = b;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 7bb840fe429f440fa4c967f2c10c5763
|
||||
timeCreated: 1770708735
|
||||
@@ -0,0 +1,39 @@
|
||||
using Code.Base;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Code.Lab3
|
||||
{
|
||||
public class Wander : Face
|
||||
{
|
||||
public float offset;
|
||||
public float radius;
|
||||
public float rate;
|
||||
|
||||
public override void Awake()
|
||||
{
|
||||
target = new GameObject
|
||||
{
|
||||
transform =
|
||||
{
|
||||
position = transform.position
|
||||
}
|
||||
};
|
||||
base.Awake();
|
||||
}
|
||||
|
||||
public override Steering GetSteering()
|
||||
{
|
||||
var wanderOrientation = Random.Range(-1f, 1f) * rate;
|
||||
var targetOrientation = wanderOrientation + Agent.orientation;
|
||||
var orientationVec = GetOriAsVec(Agent.orientation);
|
||||
var targetPosition = offset * orientationVec + transform.position;
|
||||
targetPosition += GetOriAsVec(targetOrientation) * radius;
|
||||
TargetAux.transform.position = targetPosition;
|
||||
var steering = base.GetSteering();
|
||||
steering.Linear = TargetAux.transform.position - transform.position;
|
||||
steering.Linear.Normalize();
|
||||
steering.Linear *= Agent.maxAcceleration;
|
||||
return steering;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b4ac78d3da57460888ac6eb24c0f2153
|
||||
timeCreated: 1770708395
|
||||
@@ -0,0 +1,29 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.SceneManagement;
|
||||
using UnityEngine.UI;
|
||||
|
||||
namespace Code.UI
|
||||
{
|
||||
public class Lab3Controller : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private Button toMenuButton;
|
||||
[SerializeField] private Button resetButton;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
if (toMenuButton != null)
|
||||
toMenuButton.onClick.AddListener(LoadMainMenu);
|
||||
if (resetButton != null)
|
||||
resetButton.onClick.AddListener(Reload);
|
||||
}
|
||||
|
||||
private static void LoadMainMenu() => SceneManager.LoadScene("MainMenu");
|
||||
private static void Reload() => SceneManager.LoadScene("Lab3");
|
||||
|
||||
private void OnDestroy()
|
||||
{
|
||||
if (toMenuButton != null) toMenuButton.onClick.RemoveListener(LoadMainMenu);
|
||||
if (resetButton != null) resetButton.onClick.RemoveListener(Reload);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 603166ff302d49d1938a5b524b9d2d71
|
||||
timeCreated: 1770713534
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,7 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 8771d14e8e4beeac0918e597dbd7fbab
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in new issue
Block a user