-
Notifications
You must be signed in to change notification settings - Fork 55
code/olga-naz/test-butterfly #97
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: example/sphere
Are you sure you want to change the base?
Changes from all commits
4eab7d1
d569db3
649caaa
ba62ff3
c8de99f
3fac5a9
6ffa85d
b91348d
3895200
35497b7
347b3fe
62b70a1
803f9e0
9319411
a649b38
a1dfabf
44675ad
5f3357a
5de471f
a00687f
2579cd7
489e424
f7e671c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| %YAML 1.1 | ||
| %TAG !u! tag:unity3d.com,2011: | ||
| --- !u!114 &11400000 | ||
| MonoBehaviour: | ||
| m_ObjectHideFlags: 0 | ||
| m_CorrespondingSourceObject: {fileID: 0} | ||
| m_PrefabInstance: {fileID: 0} | ||
| m_PrefabAsset: {fileID: 0} | ||
| m_GameObject: {fileID: 0} | ||
| m_Enabled: 1 | ||
| m_EditorHideFlags: 0 | ||
| m_Script: {fileID: 11500000, guid: 9198d4fb6d83f4c44b9a7f026a956854, type: 3} | ||
| m_Name: ButterflyData | ||
| m_EditorClassIdentifier: | ||
| Struct: | ||
| Prefab: {fileID: 2770142703575531339, guid: 0167d9e91d1e6e742a4510e447cbef03, | ||
| type: 3} | ||
| Size: 0.5 | ||
| MaxDistanceFromCurrentPosition: 10 | ||
| Speed: 0.1 | ||
| TurnSpeed: 0.03 | ||
| MaxFlyAltitudeFromSpawn: 5 |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| namespace BeastHunter | ||
| { | ||
| class ButterflyBehaviour: InteractableObjectBehavior | ||
| { | ||
| } | ||
| } |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| using UnityEngine; | ||
|
|
||
| namespace BeastHunter | ||
| { | ||
| public class ButterflyController: IAwake, IUpdate | ||
| { | ||
| #region Fields | ||
|
|
||
| private readonly GameContext _context; | ||
|
|
||
| #endregion | ||
|
|
||
|
|
||
| #region ClassLifeCycle | ||
|
|
||
| public ButterflyController(GameContext context) | ||
| { | ||
| _context = context; | ||
| } | ||
|
|
||
| #endregion | ||
|
|
||
|
|
||
| #region IUpdate | ||
|
|
||
| public void Updating() | ||
| { | ||
| _context.ButterflyModel.Execute(); | ||
| } | ||
|
|
||
| #endregion | ||
|
|
||
|
|
||
| #region IAwake | ||
|
|
||
| public void OnAwake() | ||
| { | ||
| var butterflies = _context.GetTriggers(InteractableObjectType.Butterfly); | ||
| foreach (var trigger in butterflies) | ||
| { | ||
| ButterflyBehaviour butterflyBehaviour = trigger as ButterflyBehaviour; | ||
| butterflyBehaviour.OnFilterHandler += OnFilter; | ||
| butterflyBehaviour.OnTriggerEnterHandler += OnTriggerEnter; | ||
| } | ||
| } | ||
|
|
||
| private bool OnFilter(Collider collider) | ||
| { | ||
| return collider.CompareTag(TagManager.GROUND); | ||
| } | ||
|
|
||
| private void OnTriggerEnter(ITrigger trigger, Collider collider) | ||
| { | ||
| _context.ButterflyModel.OnTriggerEnter(collider); | ||
| } | ||
|
|
||
| #endregion | ||
| } | ||
| } |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,192 @@ | ||
| using BeastHunter; | ||
| using UnityEngine; | ||
|
|
||
|
|
||
| [CreateAssetMenu(fileName = "NewButterfly", menuName = "CreateData/Butterfly", order = 0)] | ||
| public class ButterflyData : ScriptableObject | ||
| { | ||
| #region Fields | ||
|
|
||
| private const float SITTING_CHANCE = 75.00f; | ||
| private const float MIN_SITTING_TIME = 1.5f; | ||
| private const float MAX_SITTING_TIME = 4.0f; | ||
|
|
||
| public ButterflyStruct Struct; | ||
|
|
||
| #endregion | ||
|
|
||
|
|
||
| #region ClassLifeCycle | ||
|
|
||
| public ButterflyData() | ||
| { | ||
| Struct.Size = 0.5f; | ||
| Struct.Speed = 0.1f; | ||
| Struct.TurnSpeed = 0.03f; | ||
| Struct.MaxFlyAltitudeFromSpawn = 5.0f; | ||
| Struct.MaxDistanceFromCurrentPosition = 10.0f; | ||
| Struct.CircularRotationSpeed = 6.0f; | ||
| Struct.MinCircleSize = 1.0f; | ||
| Struct.MaxCircleSize = 3.0f; | ||
| } | ||
|
|
||
| #endregion | ||
|
|
||
|
|
||
| #region Methods | ||
|
|
||
| public void Act(ButterflyModel model) | ||
| { | ||
| Transform transform = model.ObjTransform; | ||
|
|
||
| if (model.IsCircling) | ||
| { | ||
| float rotateAroundSpeed = Struct.CircularRotationSpeed / model.CirclePoint.ToVectorXZ().DirectionTo(transform.position.ToVectorXZ()).magnitude; | ||
| transform.RotateAround(model.CirclePoint, Vector3.up * model.RotateAroundDirection, rotateAroundSpeed); | ||
| transform.rotation = CirclingRotate(model.CirclePoint, transform.position, transform.forward, model.RotateAroundDirection); | ||
|
|
||
| float dot = Vector2.Dot(transform.forward.ToVectorXZ().normalized, model.TargetPoint.ToVectorXZ().normalized); | ||
| if (dot > 0.99f) model.IsCircling = false; | ||
| } | ||
| else | ||
| { | ||
| if (model.IsSitting) | ||
| { | ||
| model.SittingTimer -= Time.deltaTime; | ||
| if (model.SittingTimer <= 0) model.IsSitting = false; | ||
| } | ||
| else | ||
| { | ||
| if (transform.position != model.TargetPoint) | ||
| { | ||
| if (transform.position.y >= model.MaxFlyAltitude && model.TargetPoint.y > transform.position.y) | ||
| { | ||
| Debug.Log(this + " maxFlyAltitude has reached"); | ||
| model.TargetPoint = NewTargetPointInOppositeDirection(transform.position, model.TargetPoint.DirectionTo(transform.position), "Y"); | ||
| } | ||
| transform.rotation = Turn(model.TargetPoint, transform.position, transform.forward); | ||
| transform.position = Move(model.TargetPoint, transform.position); | ||
| } | ||
| else | ||
| { | ||
| model.TargetPoint = NewTargetPoint(transform.position); | ||
| model.CirclePoint = NewCirclePoint(transform.position); | ||
| model.IsCircling = true; | ||
|
|
||
| Vector2 directionToCirclePoint = model.CirclePoint.ToVectorXZ().DirectionTo(transform.position.ToVectorXZ()); | ||
| float dot = Vector2.Dot(transform.forward.ToVectorXZ().TurnToRight(), directionToCirclePoint); | ||
| if (dot > 0) model.RotateAroundDirection = 1; | ||
| else model.RotateAroundDirection = -1; | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public void TriggerEnter(Collider collider, ButterflyModel model) | ||
| { | ||
| Debug.Log(this + " OnTriggerEnter(Collider collider)"); | ||
|
|
||
| if (collider.gameObject.tag == TagManager.GROUND) | ||
| { | ||
| model.TargetPoint = NewTargetPointInOppositeDirection(model.ObjTransform.position, model.TargetPoint - model.ObjTransform.position, "Y"); | ||
| if (Random.Range(1, 100) <= SITTING_CHANCE) | ||
| { | ||
| Debug.Log(this + " is sitting"); | ||
|
|
||
| model.IsSitting = true; | ||
| model.SittingTimer = Random.Range(MIN_SITTING_TIME, MAX_SITTING_TIME); | ||
|
|
||
| Debug.Log(this + " sittingTimer: " + model.SittingTimer); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private Vector3 Move(Vector3 targetPoint, Vector3 currentPosition) | ||
| { | ||
| return Vector3.MoveTowards(currentPosition, targetPoint, Struct.Speed); | ||
| } | ||
|
|
||
| private Quaternion Turn(Vector3 targetPoint, Vector3 position, Vector3 forward) | ||
| { | ||
| Vector3 targetDirection = targetPoint - position; | ||
| Vector3 newDirection = Vector3.RotateTowards(forward, targetDirection, Struct.TurnSpeed, 0.0f); | ||
| newDirection.y = forward.y; | ||
| return Quaternion.LookRotation(newDirection); | ||
| } | ||
|
|
||
| private Quaternion CirclingRotate(Vector3 circlePoint, Vector3 position, Vector3 forward, int rotateAroundDirection) | ||
| { | ||
| Vector3 circlePointDirectionRotate90 = Quaternion.AngleAxis(-90 * rotateAroundDirection, Vector3.up) * circlePoint.DirectionTo(position); | ||
| Vector3 newDirection = Vector3.RotateTowards(forward, circlePointDirectionRotate90, Struct.TurnSpeed, 0.0f); | ||
| newDirection.y = forward.y; | ||
| return Quaternion.LookRotation(newDirection); | ||
| } | ||
|
|
||
| /// <summary>Return random coordinates within MaxDistanceFromCurrentPosition</summary> | ||
| private Vector3 NewTargetPoint(Vector3 currentPosition) | ||
| { | ||
| float x = GetRandomCoord(currentPosition.x); | ||
| float y = GetRandomCoord(currentPosition.y); | ||
| float z = GetRandomCoord(currentPosition.z); | ||
| return new Vector3(x, y, z); | ||
| } | ||
|
|
||
| /// <summary>Return random coordinates within MaxDistanceFromCurrentPosition in the opposite direction along the specified coordinate axis</summary> | ||
| private Vector3 NewTargetPointInOppositeDirection(Vector3 currentPosition, Vector3 currentDirection, string axis) | ||
| { | ||
| Vector3 newTarget = NewTargetPoint(currentPosition); | ||
|
|
||
| switch (axis.ToUpper()) | ||
| { | ||
| case "X": newTarget.x = GetRandomCoord(currentPosition.x, currentDirection.x); break; | ||
| case "Y": newTarget.y = GetRandomCoord(currentPosition.y, currentDirection.y); break; | ||
| case "Z": newTarget.z = GetRandomCoord(currentPosition.z, currentDirection.z); break; | ||
| } | ||
|
|
||
| return newTarget; | ||
| } | ||
|
|
||
| private float GetRandomCoord(float currentCoord, float? directionCoord = null) | ||
| { | ||
| if (directionCoord.HasValue && directionCoord.Value != 0) | ||
| { | ||
| if (directionCoord > 0) return Random.Range(currentCoord - Struct.MaxDistanceFromCurrentPosition, currentCoord - 0.5f); | ||
| else return Random.Range(currentCoord + 0.5f, currentCoord + Struct.MaxDistanceFromCurrentPosition); | ||
| } | ||
| return Random.Range(currentCoord - Struct.MaxDistanceFromCurrentPosition, currentCoord + Struct.MaxDistanceFromCurrentPosition); ; | ||
| } | ||
|
|
||
| private Vector3 NewCirclePoint(Vector3 currentPosition) | ||
| { | ||
| float x = GetRandomCoordForCircle(currentPosition.x); | ||
| float z = GetRandomCoordForCircle(currentPosition.z); | ||
| return new Vector3(x, currentPosition.y, z); | ||
| } | ||
|
|
||
| private float GetRandomCoordForCircle(float coord) | ||
| { | ||
| int sign = 1; | ||
| if (Random.Range(1, 100) > 50) sign = -sign; | ||
| float random = Random.Range(Struct.MinCircleSize, Struct.MaxCircleSize); | ||
| return coord + random * sign; | ||
| } | ||
|
|
||
| #endregion | ||
| } | ||
|
|
||
|
|
||
|
|
||
| static class VectorExtension | ||
| { | ||
| /// <summary>Convert Vector3 into Vector2 for XZ-plane</summary> | ||
| public static Vector2 ToVectorXZ(this Vector3 vector3) => new Vector2(vector3.x, vector3.z); | ||
|
|
||
| /// <summary>Rotates the vector2 90 degrees</summary> | ||
| public static Vector2 TurnToRight(this Vector2 vector2) => new Vector2(vector2.y, -vector2.x); | ||
|
|
||
| /// <summary>Returns the direction vector2 to the specified point</summary> | ||
| public static Vector2 DirectionTo(this Vector2 vector2, Vector2 target) => vector2 - target; | ||
|
|
||
| /// <summary>Returns the direction vector3 to the specified point</summary> | ||
| public static Vector3 DirectionTo(this Vector3 vector3, Vector3 target) => vector3 - target; | ||
| } | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| using UnityEngine; | ||
|
|
||
| namespace BeastHunter | ||
| { | ||
| public class ButterflyInitilizeController: IAwake | ||
| { | ||
| #region Field | ||
|
|
||
| private GameContext _context; | ||
|
|
||
| #endregion | ||
|
|
||
|
|
||
| #region ClassLifeCycle | ||
|
|
||
| public ButterflyInitilizeController(GameContext context) | ||
| { | ||
| _context = context; | ||
| } | ||
|
|
||
| #endregion | ||
|
|
||
|
|
||
| #region IAwake | ||
|
|
||
| public void OnAwake() | ||
| { | ||
| ButterflyData butterflyData = Data.ButterflyData; | ||
| GameObject butterflyObject = Object.Instantiate(butterflyData.Struct.Prefab); | ||
| ButterflyModel butterflyModel = new ButterflyModel(butterflyObject, butterflyData); | ||
| _context.ButterflyModel = butterflyModel; | ||
| } | ||
|
|
||
| #endregion | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Убрать класс, функции вынести в отдельный регион внутри Methods
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Это методы расширения, которые могут находится только в статическом классе, поэтому просто перенести их не получится( Я могу переписать их как обычные методы, чтобы разместить в регионе внутри Methods, но тогда придется немного переписать и те места, где я их использовала. В общем, я так поняла, что лучше все делать через обычные методы?))