Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections.Generic;
- public class NovintFalconForces : MonoBehaviour
- {
- private List<BaseForce> _forces=new List<BaseForce>();
- [SerializeField] private Haptics _novintFalcon=null;
- public int IndexNovintFalcon = 0;
- void LateUpdate ()
- {
- var time = Time.deltaTime;
- var currentPos = _novintFalcon.GetServoPos(IndexNovintFalcon);
- Vector3 totalForce=Vector3.zero;
- for (int i = 0; i < _forces.Count;)
- {
- var force = _forces[i];
- if (force.UpdateAndCheckForDeath(time,currentPos))
- {
- _forces.RemoveAt(i);
- }
- else
- {
- i++;
- }
- totalForce += force.GetForce();
- }
- if (float.IsNaN(totalForce.x)) totalForce.x = 0;
- if (float.IsNaN(totalForce.y)) totalForce.y = 0;
- if (float.IsNaN(totalForce.z)) totalForce.z = 0;
- _novintFalcon.SetServo(totalForce, IndexNovintFalcon);
- }
- public void AddNewForce(BaseForce force)
- {
- _forces.Add(force);
- }
- }
- //Workspace
- public abstract class BaseForce
- {
- protected BaseForceLife ForceLife;
- protected BaseForceDirectionAndStrength ForceDirectionAndStrength;
- public virtual bool UpdateAndCheckForDeath(float deltaTime,Vector3 position)
- {
- ForceLife.Update(deltaTime);
- TransformPosToWorld(ref position);
- ForceDirectionAndStrength.Update(position);
- return ForceLife.IsDeath();
- }
- public Vector3 GetForce()
- {
- var force=ForceDirectionAndStrength.GetCompleteForce();
- TransformForceToLocal(ref force);
- return force;
- }
- protected virtual void TransformForceToLocal(ref Vector3 force)
- {
- }
- protected virtual void TransformPosToWorld(ref Vector3 position)
- {
- }
- }
- public class LocalForce : BaseForce
- {
- public LocalForce(BaseForceLife forceLife, BaseForceDirectionAndStrength forceDirectionAndStrength)
- {
- ForceLife = forceLife;
- ForceDirectionAndStrength = forceDirectionAndStrength;
- }
- }
- public class WorldForce : BaseForce
- {
- public Transform Listener;
- public float WorldToLocalDistanceRatio;
- public WorldForce(Transform listener,float worldToLocalDistanceRatio, BaseForceLife forceLife, BaseForceDirectionAndStrength forceDirectionAndStrength)
- {
- Listener = listener;
- WorldToLocalDistanceRatio = worldToLocalDistanceRatio;
- ForceLife = forceLife;
- ForceDirectionAndStrength = forceDirectionAndStrength;
- }
- protected override void TransformForceToLocal(ref Vector3 force)
- {
- force= Listener.InverseTransformDirection(force);
- }
- protected override void TransformPosToWorld(ref Vector3 pos)
- {
- pos *= WorldToLocalDistanceRatio;
- pos = Listener.TransformPoint(pos);
- }
- }
- //Death and life
- public abstract class BaseForceLife
- {
- protected bool Death=false;
- public bool IsDeath()
- {
- return Death;
- }
- public virtual void Update(float time)
- {
- }
- }
- public class TimedForceLife:BaseForceLife
- {
- private float _lifeTime;
- public TimedForceLife(float lifeTime)
- {
- _lifeTime = lifeTime+Time.deltaTime;
- }
- public override void Update(float time)
- {
- _lifeTime -= time;
- if (_lifeTime <= 0) Death = true;
- }
- }
- public class UserControlledForceLife : BaseForceLife
- {
- public void KillForce()
- {
- Death = true;
- }
- }
- //Direction and Strength
- public abstract class BaseForceDirectionAndStrength
- {
- protected Vector3 Direction = new Vector3();
- protected float Strength;
- public Vector3 GetDirection()
- {
- return Direction.normalized;
- }
- public float GetStrength()
- {
- return Strength;
- }
- public Vector3 GetCompleteForce()
- {
- return Direction.normalized * Strength;
- }
- public virtual void SetStrength(float strength)
- {
- Strength = strength;
- }
- public virtual void Update(Vector3 currentPos)
- {
- }
- }
- public class ForceDirectionAndFixedStrength: BaseForceDirectionAndStrength
- {
- public ForceDirectionAndFixedStrength(Vector3 direction,float strength)
- {
- Direction = direction;
- Strength = strength;
- }
- public void SetDirection(Vector3 direction)
- {
- Direction = direction;
- }
- public override void SetStrength(float strength)
- {
- Strength = strength;
- }
- }
- public class ForcePosDirectionAndFixedStrength : BaseForceDirectionAndStrength
- {
- private Vector3 _goal;
- public Vector3 Goal
- {
- set
- {
- _goal = value;
- Direction = Vector3.zero;
- }
- get { return _goal; }
- }
- public ForcePosDirectionAndFixedStrength(Vector3 goal,float strength)
- {
- Goal = goal;
- Direction = Vector3.zero;
- Strength = strength;
- }
- public override void Update(Vector3 currentPos)
- {
- Direction = Goal - currentPos;
- }
- public override void SetStrength(float strength)
- {
- Strength = strength;
- }
- }
- public class ForcePosDirectionAndLinearStrength : BaseForceDirectionAndStrength
- {
- private Vector3 _goal;
- public Vector3 Goal
- {
- set
- {
- _goal = value;
- Direction = Vector3.zero;
- }
- get { return _goal; }
- }
- private float _baseStrength;
- public float DistanceStrengthMultiplier;
- public ForcePosDirectionAndLinearStrength(Vector3 goal, float strength,float distanceStrengthMultiplier)
- {
- Goal = goal;
- _baseStrength = strength;
- DistanceStrengthMultiplier = distanceStrengthMultiplier;
- Strength =0;
- }
- public override void Update(Vector3 currentPos)
- {
- Direction = Goal - currentPos;
- var distance = Direction.magnitude;
- Strength = _baseStrength*DistanceStrengthMultiplier*distance;
- }
- public override void SetStrength(float strength)
- {
- _baseStrength = strength;
- }
- }
- public class ForcePosDirectionAndExponentialStrength : BaseForceDirectionAndStrength
- {
- private Vector3 _goal;
- public Vector3 Goal
- {
- set
- {
- _goal = value;
- Direction = Vector3.zero;
- }
- get { return _goal; }
- }
- private float _baseStrength;
- public float DistanceStrengthMultiplier;
- public ForcePosDirectionAndExponentialStrength(Vector3 goal, float strength, float distanceStrengthMultiplier)
- {
- Goal = _goal;
- _baseStrength = strength;
- DistanceStrengthMultiplier = distanceStrengthMultiplier;
- Direction = Vector3.zero;
- Strength = 0;
- }
- public override void Update(Vector3 currentPos)
- {
- Direction = Goal - currentPos;
- var distance = Direction.magnitude;
- Strength = _baseStrength * (DistanceStrengthMultiplier*distance) * (DistanceStrengthMultiplier * distance);
- }
- public override void SetStrength(float strength)
- {
- _baseStrength = strength;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement