Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
716 changes: 716 additions & 0 deletions Assets/Resources/Butterfly.prefab

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions Assets/Resources/Butterfly.prefab.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions Assets/Resources/Data/ButterflyData.asset
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
8 changes: 8 additions & 0 deletions Assets/Resources/Data/ButterflyData.asset.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Assets/Resources/Data/Data.asset
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 1b31b7596ae2b4a4ab972d36383fc2fb, type: 3}
m_Name: Data
m_EditorClassIdentifier:
_butterflyDataPath: ButterflyData
_sphereDataPath: SphereData
_characterDataPath: CharacterData
_startDialogueDataPath: StartDialogueData
Expand Down
2 changes: 1 addition & 1 deletion Assets/Scenes/ITickTest.unity
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ GameObject:
- component: {fileID: 966662972}
m_Layer: 0
m_Name: BattlePlate
m_TagString: Untagged
m_TagString: Ground
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 8
Expand Down

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.

59 changes: 59 additions & 0 deletions Assets/Scripts/ContextTest/MVC/Butterfly/ButterflyController.cs
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.

192 changes: 192 additions & 0 deletions Assets/Scripts/ContextTest/MVC/Butterfly/ButterflyData.cs
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;
}
Comment on lines +179 to +192

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Убрать класс, функции вынести в отдельный регион внутри Methods

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Это методы расширения, которые могут находится только в статическом классе, поэтому просто перенести их не получится( Я могу переписать их как обычные методы, чтобы разместить в регионе внутри Methods, но тогда придется немного переписать и те места, где я их использовала. В общем, я так поняла, что лучше все делать через обычные методы?))

11 changes: 11 additions & 0 deletions Assets/Scripts/ContextTest/MVC/Butterfly/ButterflyData.cs.meta

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
}
}
Loading