diff --git a/FallUnity/Assets/IsoSorting/IsoItem.cs b/FallUnity/Assets/IsoSorting/IsoItem.cs index ef70a5d..d8bc615 100644 --- a/FallUnity/Assets/IsoSorting/IsoItem.cs +++ b/FallUnity/Assets/IsoSorting/IsoItem.cs @@ -55,10 +55,13 @@ } } - void Destroy() + void OnDestroy() { IsoHandler iso = GameObject.FindObjectOfType(); - iso.RemoveIsoItem(this); + if (iso != null) + { + iso.RemoveIsoItem(this); + } } // Update is called once per frame diff --git a/FallUnity/Assets/IsoSorting/IsoItem.cs b/FallUnity/Assets/IsoSorting/IsoItem.cs index ef70a5d..d8bc615 100644 --- a/FallUnity/Assets/IsoSorting/IsoItem.cs +++ b/FallUnity/Assets/IsoSorting/IsoItem.cs @@ -55,10 +55,13 @@ } } - void Destroy() + void OnDestroy() { IsoHandler iso = GameObject.FindObjectOfType(); - iso.RemoveIsoItem(this); + if (iso != null) + { + iso.RemoveIsoItem(this); + } } // Update is called once per frame diff --git a/FallUnity/Assets/Leaves/LeafPhysics.cs b/FallUnity/Assets/Leaves/LeafPhysics.cs index a0469c4..8328ab8 100755 --- a/FallUnity/Assets/Leaves/LeafPhysics.cs +++ b/FallUnity/Assets/Leaves/LeafPhysics.cs @@ -87,14 +87,9 @@ // Update is called once per frame void Update () { - if (body.velocity.sqrMagnitude > 0.01f) + if (!body.isKinematic && body.velocity.sqrMagnitude > 0.01f) { - if (Debug) - { - bool bp = true; - } - - float baseForceUp = Mathf.Max(BaseWindForce * Mathf.Pow(Mathf.Abs(transform.up.y), AnglePow) * -body.velocity.y); + float baseForceUp = Mathf.Max(BaseWindForce * Mathf.Pow(Mathf.Abs(transform.up.y), AnglePow) * -body.velocity.y) * Time.deltaTime * 60; body.AddForceAtPosition(Vector3.up * baseForceUp * (1 + PlusX.Current), transform.position + Vector3.right * 0.4f); body.AddForceAtPosition(Vector3.up * baseForceUp * (1 + MinusX.Current), transform.position - Vector3.right * 0.4f); @@ -111,9 +106,9 @@ PushUpCurrent = Mathf.Max(0, PushUpCurrent - PushUpDecayRate * Time.deltaTime); */ - - float torqueX = -transform.up.x; - float torqueZ = -transform.up.z; + Vector3 up = transform.up; + float torqueX = -up.x; + float torqueZ = -up.z; float velYScale = Math.Max(0, -body.velocity.y); @@ -121,7 +116,7 @@ //body.AddForce(Vector3.up * Mathf.Pow(Mathf.Max(-body.velocity.y / VelDownForceDivide), 2) * VelDownForceScale); body.AddRelativeTorque(new Vector3(Mathf.Max(-TorqueBounds, Mathf.Min(TorqueBounds, torqueX * TorqueScale * 1.2f)) + TorqueX.Current * 0.5f, 0, Mathf.Max(-TorqueBounds, Mathf.Min(TorqueBounds, torqueZ * TorqueScale)) + TorqueZ.Current * 0.5f) * velYScale); - body.AddForce(new Vector3(-body.velocity.x, 0, -body.velocity.z) * SideRestitution); + body.AddForce(new Vector3(-body.velocity.x, 0, -body.velocity.z) * SideRestitution * Time.deltaTime * 60); } if (transform.position.y < -5) diff --git a/FallUnity/Assets/IsoSorting/IsoItem.cs b/FallUnity/Assets/IsoSorting/IsoItem.cs index ef70a5d..d8bc615 100644 --- a/FallUnity/Assets/IsoSorting/IsoItem.cs +++ b/FallUnity/Assets/IsoSorting/IsoItem.cs @@ -55,10 +55,13 @@ } } - void Destroy() + void OnDestroy() { IsoHandler iso = GameObject.FindObjectOfType(); - iso.RemoveIsoItem(this); + if (iso != null) + { + iso.RemoveIsoItem(this); + } } // Update is called once per frame diff --git a/FallUnity/Assets/Leaves/LeafPhysics.cs b/FallUnity/Assets/Leaves/LeafPhysics.cs index a0469c4..8328ab8 100755 --- a/FallUnity/Assets/Leaves/LeafPhysics.cs +++ b/FallUnity/Assets/Leaves/LeafPhysics.cs @@ -87,14 +87,9 @@ // Update is called once per frame void Update () { - if (body.velocity.sqrMagnitude > 0.01f) + if (!body.isKinematic && body.velocity.sqrMagnitude > 0.01f) { - if (Debug) - { - bool bp = true; - } - - float baseForceUp = Mathf.Max(BaseWindForce * Mathf.Pow(Mathf.Abs(transform.up.y), AnglePow) * -body.velocity.y); + float baseForceUp = Mathf.Max(BaseWindForce * Mathf.Pow(Mathf.Abs(transform.up.y), AnglePow) * -body.velocity.y) * Time.deltaTime * 60; body.AddForceAtPosition(Vector3.up * baseForceUp * (1 + PlusX.Current), transform.position + Vector3.right * 0.4f); body.AddForceAtPosition(Vector3.up * baseForceUp * (1 + MinusX.Current), transform.position - Vector3.right * 0.4f); @@ -111,9 +106,9 @@ PushUpCurrent = Mathf.Max(0, PushUpCurrent - PushUpDecayRate * Time.deltaTime); */ - - float torqueX = -transform.up.x; - float torqueZ = -transform.up.z; + Vector3 up = transform.up; + float torqueX = -up.x; + float torqueZ = -up.z; float velYScale = Math.Max(0, -body.velocity.y); @@ -121,7 +116,7 @@ //body.AddForce(Vector3.up * Mathf.Pow(Mathf.Max(-body.velocity.y / VelDownForceDivide), 2) * VelDownForceScale); body.AddRelativeTorque(new Vector3(Mathf.Max(-TorqueBounds, Mathf.Min(TorqueBounds, torqueX * TorqueScale * 1.2f)) + TorqueX.Current * 0.5f, 0, Mathf.Max(-TorqueBounds, Mathf.Min(TorqueBounds, torqueZ * TorqueScale)) + TorqueZ.Current * 0.5f) * velYScale); - body.AddForce(new Vector3(-body.velocity.x, 0, -body.velocity.z) * SideRestitution); + body.AddForce(new Vector3(-body.velocity.x, 0, -body.velocity.z) * SideRestitution * Time.deltaTime * 60); } if (transform.position.y < -5) diff --git a/FallUnity/Assets/Player/Player.prefab b/FallUnity/Assets/Player/Player.prefab index 250744f..a333473 100644 --- a/FallUnity/Assets/Player/Player.prefab +++ b/FallUnity/Assets/Player/Player.prefab @@ -228,6 +228,15 @@ RunCycleTime: 0.3 RunFrameTime: 0.15 RunFrameSpeedBoost: 1.25 + PushRadius: 0.6 + PushForce: 300 + PushPower: 2 + TimeToPushRake: 0.3 + RakeBackDist: 0.5 + RakeBackTime: 0.15 + RakeReachDist: 0.7 + RakeReachRadius: 0.8 + RakeFlyForce: 100 --- !u!114 &114917061756029764 MonoBehaviour: m_ObjectHideFlags: 1 @@ -271,7 +280,7 @@ m_Enabled: 1 serializedVersion: 2 m_Height: 2.03 - m_Radius: 0.28 + m_Radius: 0.67 m_SlopeLimit: 45 m_StepOffset: 0.3 m_SkinWidth: 0.08 diff --git a/FallUnity/Assets/IsoSorting/IsoItem.cs b/FallUnity/Assets/IsoSorting/IsoItem.cs index ef70a5d..d8bc615 100644 --- a/FallUnity/Assets/IsoSorting/IsoItem.cs +++ b/FallUnity/Assets/IsoSorting/IsoItem.cs @@ -55,10 +55,13 @@ } } - void Destroy() + void OnDestroy() { IsoHandler iso = GameObject.FindObjectOfType(); - iso.RemoveIsoItem(this); + if (iso != null) + { + iso.RemoveIsoItem(this); + } } // Update is called once per frame diff --git a/FallUnity/Assets/Leaves/LeafPhysics.cs b/FallUnity/Assets/Leaves/LeafPhysics.cs index a0469c4..8328ab8 100755 --- a/FallUnity/Assets/Leaves/LeafPhysics.cs +++ b/FallUnity/Assets/Leaves/LeafPhysics.cs @@ -87,14 +87,9 @@ // Update is called once per frame void Update () { - if (body.velocity.sqrMagnitude > 0.01f) + if (!body.isKinematic && body.velocity.sqrMagnitude > 0.01f) { - if (Debug) - { - bool bp = true; - } - - float baseForceUp = Mathf.Max(BaseWindForce * Mathf.Pow(Mathf.Abs(transform.up.y), AnglePow) * -body.velocity.y); + float baseForceUp = Mathf.Max(BaseWindForce * Mathf.Pow(Mathf.Abs(transform.up.y), AnglePow) * -body.velocity.y) * Time.deltaTime * 60; body.AddForceAtPosition(Vector3.up * baseForceUp * (1 + PlusX.Current), transform.position + Vector3.right * 0.4f); body.AddForceAtPosition(Vector3.up * baseForceUp * (1 + MinusX.Current), transform.position - Vector3.right * 0.4f); @@ -111,9 +106,9 @@ PushUpCurrent = Mathf.Max(0, PushUpCurrent - PushUpDecayRate * Time.deltaTime); */ - - float torqueX = -transform.up.x; - float torqueZ = -transform.up.z; + Vector3 up = transform.up; + float torqueX = -up.x; + float torqueZ = -up.z; float velYScale = Math.Max(0, -body.velocity.y); @@ -121,7 +116,7 @@ //body.AddForce(Vector3.up * Mathf.Pow(Mathf.Max(-body.velocity.y / VelDownForceDivide), 2) * VelDownForceScale); body.AddRelativeTorque(new Vector3(Mathf.Max(-TorqueBounds, Mathf.Min(TorqueBounds, torqueX * TorqueScale * 1.2f)) + TorqueX.Current * 0.5f, 0, Mathf.Max(-TorqueBounds, Mathf.Min(TorqueBounds, torqueZ * TorqueScale)) + TorqueZ.Current * 0.5f) * velYScale); - body.AddForce(new Vector3(-body.velocity.x, 0, -body.velocity.z) * SideRestitution); + body.AddForce(new Vector3(-body.velocity.x, 0, -body.velocity.z) * SideRestitution * Time.deltaTime * 60); } if (transform.position.y < -5) diff --git a/FallUnity/Assets/Player/Player.prefab b/FallUnity/Assets/Player/Player.prefab index 250744f..a333473 100644 --- a/FallUnity/Assets/Player/Player.prefab +++ b/FallUnity/Assets/Player/Player.prefab @@ -228,6 +228,15 @@ RunCycleTime: 0.3 RunFrameTime: 0.15 RunFrameSpeedBoost: 1.25 + PushRadius: 0.6 + PushForce: 300 + PushPower: 2 + TimeToPushRake: 0.3 + RakeBackDist: 0.5 + RakeBackTime: 0.15 + RakeReachDist: 0.7 + RakeReachRadius: 0.8 + RakeFlyForce: 100 --- !u!114 &114917061756029764 MonoBehaviour: m_ObjectHideFlags: 1 @@ -271,7 +280,7 @@ m_Enabled: 1 serializedVersion: 2 m_Height: 2.03 - m_Radius: 0.28 + m_Radius: 0.67 m_SlopeLimit: 45 m_StepOffset: 0.3 m_SkinWidth: 0.08 diff --git a/FallUnity/Assets/Player/PlayerController.cs b/FallUnity/Assets/Player/PlayerController.cs index d3ca2ab..f050f76 100755 --- a/FallUnity/Assets/Player/PlayerController.cs +++ b/FallUnity/Assets/Player/PlayerController.cs @@ -23,7 +23,11 @@ void Start () { charC = GetComponent(); anim = GetComponent(); - } + rakeTimer = new SimpleTimer(); + rakeBackTween = new SimpleTween(); + oldPos = transform.position; + + } private float velY = 0; @@ -40,10 +44,16 @@ private float rakeTime = 0.0f; public float TimeToPushRake = 0.3f; - - // Update is called once per frame - void Update () { - Vector3 pos = transform.position; + + private SimpleTimer rakeTimer; + + private Vector3 rakeDir = new Vector2(); + + private Vector3 oldPos = new Vector3(); + + // Update is called once per frame + void Update () { + Vector3 dir = new Vector3(); if (input != null) @@ -92,20 +102,23 @@ } } - dir.y = velY; - - velY -= Gravity * Time.deltaTime; - - gameObject.layer = LayerMask.NameToLayer("player_controller_no_collide"); - charC.Move(dir * Time.deltaTime); - gameObject.layer = LayerMask.NameToLayer("player_controller"); - - if (charC.isGrounded) + if (state == State.Walking) { - velY = 0; + dir.y = velY; + + velY -= Gravity * Time.deltaTime; + + gameObject.layer = LayerMask.NameToLayer("player_controller_no_collide"); + charC.Move(dir * Time.deltaTime); + gameObject.layer = LayerMask.NameToLayer("player_controller"); + + if (charC.isGrounded) + { + velY = 0; + } } - Vector3 moved = transform.position - pos; + Vector3 moved = transform.position - oldPos; if (moved.magnitude > 0.7f * Time.deltaTime) { int layer = LayerMask.GetMask("leaf"); @@ -119,42 +132,140 @@ float power = Mathf.Pow(1.0f - dist / PushRadius, PushPower) * Time.deltaTime * PushForce; //toLeaf.Normalize(); toLeaf.y += UnityEngine.Random.Range(0.4f, 1.0f); - leaf.Body.AddForce(toLeaf * (9.0f + UnityEngine.Random.Range(0, 9.0f)) * power); + leaf.Body.AddForce(toLeaf * (9.0f + UnityEngine.Random.Range(0, 9.0f)) * power * Time.deltaTime * 60); } } } if (state == State.Walking) { - Vector2 rake = new Vector2(input.Axis(InputSource.InputAxis.RakeX), input.Axis(InputSource.InputAxis.RakeY)); - if (rake.magnitude < 0.2f) + rakeDir = new Vector3(input.Axis(InputSource.InputAxis.RakeX), 0, input.Axis(InputSource.InputAxis.RakeY)); + if (rakeDir.magnitude < 0.2f) { rakeTime = 0; } else { - rakeTime += Time.deltaTime; + if (rakeTime >= 0) + { + rakeTime += Time.deltaTime; + } } - if (rake.magnitude > 0.7f && rakeTime < TimeToPushRake) + if (rakeTime >= 0 && rakeDir.magnitude > 0.7f && rakeTime < TimeToPushRake) { state = State.Raking; - float angle = Mathf.Atan2(rake.y, rake.x); + float angle = Mathf.Atan2(-rakeDir.z, -rakeDir.x); if (angle < 0) angle += Mathf.PI * 2; - AnimationHandler.Direction rakeDir = AnimationHandler.Direction.UpRight; + AnimationHandler.Direction rakeAnimDir = AnimationHandler.Direction.UpRight; - if (angle < Mathf.PI / 2.0f) rakeDir = AnimationHandler.Direction.UpRight; - if (angle < Mathf.PI && angle >= Mathf.PI / 2.0f) rakeDir = AnimationHandler.Direction.UpLeft; - if (angle < Mathf.PI * 3.0f / 2.0f && angle >= Mathf.PI) rakeDir = AnimationHandler.Direction.DownLeft; - if (angle >= Mathf.PI * 3.0f / 2.0f) rakeDir = AnimationHandler.Direction.DownRight; + if (angle < Mathf.PI / 2.0f) rakeAnimDir = AnimationHandler.Direction.UpRight; + if (angle < Mathf.PI && angle >= Mathf.PI / 2.0f) rakeAnimDir = AnimationHandler.Direction.UpLeft; + if (angle < Mathf.PI * 3.0f / 2.0f && angle >= Mathf.PI) rakeAnimDir = AnimationHandler.Direction.DownLeft; + if (angle >= Mathf.PI * 3.0f / 2.0f) rakeAnimDir = AnimationHandler.Direction.DownRight; - anim.SetState("Rake", rakeDir, 0); + rakeTime = -1; + + anim.SetState("Rake", rakeAnimDir, 0); + + rakeTimer.Start(0.15f, Rake); } } - } + + oldPos = transform.position; + + } + + public float RakeBackDist = 0.8f; + public float RakeBackTime = 0.2f; + + private SimpleTween rakeBackTween; + + private Vector3 rakeBackStart = new Vector3(); + private Vector3 rakeBackEnd = new Vector3(); + + public float RakeReachDist = 0.6f; + public float RakeReachRadius = 0.6f; + + public float RakeFlyForce = 100; + + private void Rake() + { + Vector3 rakeMove = rakeDir * RakeBackDist; + rakeBackStart = transform.position; + charC.Move(rakeMove); + rakeBackEnd = transform.position; + transform.position = rakeBackStart; + + rakeBackTween.Init(UpdateRakeBack, SimpleTween.QuadEaseInOut, 0, 1, RakeBackTime); + rakeBackTween.OnFinish = FinishedRake; + + List leafs = new List(); + + int layer = LayerMask.GetMask("leaf"); + foreach (Collider col in Physics.OverlapSphere(rakeBackStart - rakeDir * RakeReachDist, RakeReachRadius, layer)) + { + LeafPhysics leaf = col.GetComponentInParent(); + if (leaf != null) + { + leafs.Add(leaf); + } + } + + foreach (LeafPhysics leaf in leafs) + { + float rand = UnityEngine.Random.Range(0, 1); + Vector3 startPos = leaf.transform.position; + float dist = Vector3.Dot(startPos - transform.position, -rakeDir); + dist += UnityEngine.Random.Range(-0.1f, 0.1f); + Vector3 pull = rakeDir * dist; + + if (rand < 0.8f) + { + + // pull the leaf back, and then destroy + leaf.Body.isKinematic = true; + + pull.x += UnityEngine.Random.Range(-0.05f, 0.05f); + pull.z += UnityEngine.Random.Range(-0.05f, 0.05f); + + float pullTime = RakeBackTime + UnityEngine.Random.Range(-0.04f, 0.04f); + + SimpleTween tween = new SimpleTween(v => + { + leaf.transform.position = startPos + pull * v; + }, SimpleTween.TweenType.EaseLinear, 0, 1, pullTime); + tween.OnFinish = () => + { + GameObject.Destroy(leaf.gameObject); + }; + } + else + { + pull.Normalize(); + pull = Quaternion.Euler(UnityEngine.Random.Range(-30, 30), UnityEngine.Random.Range(-30, 30), UnityEngine.Random.Range(-30, 30)) * pull; + pull.y += 0.3f; + leaf.Body.AddForce(pull * RakeFlyForce, ForceMode.Impulse); + } + } + } + + private void FinishedRake() + { + state = State.Walking; + anim.SetState("Stand", anim.Dir, anim.Frame); + } + + private void UpdateRakeBack(float val) + { + Vector3 pos = transform.position; + pos.x = rakeBackStart.x * (1 - val) + rakeBackEnd.x * val; + pos.z = rakeBackStart.z * (1 - val) + rakeBackEnd.z * val; + transform.position = pos; + } private InputSource input = null; private InputManager.PlayerInfo playerInfo; diff --git a/FallUnity/Assets/IsoSorting/IsoItem.cs b/FallUnity/Assets/IsoSorting/IsoItem.cs index ef70a5d..d8bc615 100644 --- a/FallUnity/Assets/IsoSorting/IsoItem.cs +++ b/FallUnity/Assets/IsoSorting/IsoItem.cs @@ -55,10 +55,13 @@ } } - void Destroy() + void OnDestroy() { IsoHandler iso = GameObject.FindObjectOfType(); - iso.RemoveIsoItem(this); + if (iso != null) + { + iso.RemoveIsoItem(this); + } } // Update is called once per frame diff --git a/FallUnity/Assets/Leaves/LeafPhysics.cs b/FallUnity/Assets/Leaves/LeafPhysics.cs index a0469c4..8328ab8 100755 --- a/FallUnity/Assets/Leaves/LeafPhysics.cs +++ b/FallUnity/Assets/Leaves/LeafPhysics.cs @@ -87,14 +87,9 @@ // Update is called once per frame void Update () { - if (body.velocity.sqrMagnitude > 0.01f) + if (!body.isKinematic && body.velocity.sqrMagnitude > 0.01f) { - if (Debug) - { - bool bp = true; - } - - float baseForceUp = Mathf.Max(BaseWindForce * Mathf.Pow(Mathf.Abs(transform.up.y), AnglePow) * -body.velocity.y); + float baseForceUp = Mathf.Max(BaseWindForce * Mathf.Pow(Mathf.Abs(transform.up.y), AnglePow) * -body.velocity.y) * Time.deltaTime * 60; body.AddForceAtPosition(Vector3.up * baseForceUp * (1 + PlusX.Current), transform.position + Vector3.right * 0.4f); body.AddForceAtPosition(Vector3.up * baseForceUp * (1 + MinusX.Current), transform.position - Vector3.right * 0.4f); @@ -111,9 +106,9 @@ PushUpCurrent = Mathf.Max(0, PushUpCurrent - PushUpDecayRate * Time.deltaTime); */ - - float torqueX = -transform.up.x; - float torqueZ = -transform.up.z; + Vector3 up = transform.up; + float torqueX = -up.x; + float torqueZ = -up.z; float velYScale = Math.Max(0, -body.velocity.y); @@ -121,7 +116,7 @@ //body.AddForce(Vector3.up * Mathf.Pow(Mathf.Max(-body.velocity.y / VelDownForceDivide), 2) * VelDownForceScale); body.AddRelativeTorque(new Vector3(Mathf.Max(-TorqueBounds, Mathf.Min(TorqueBounds, torqueX * TorqueScale * 1.2f)) + TorqueX.Current * 0.5f, 0, Mathf.Max(-TorqueBounds, Mathf.Min(TorqueBounds, torqueZ * TorqueScale)) + TorqueZ.Current * 0.5f) * velYScale); - body.AddForce(new Vector3(-body.velocity.x, 0, -body.velocity.z) * SideRestitution); + body.AddForce(new Vector3(-body.velocity.x, 0, -body.velocity.z) * SideRestitution * Time.deltaTime * 60); } if (transform.position.y < -5) diff --git a/FallUnity/Assets/Player/Player.prefab b/FallUnity/Assets/Player/Player.prefab index 250744f..a333473 100644 --- a/FallUnity/Assets/Player/Player.prefab +++ b/FallUnity/Assets/Player/Player.prefab @@ -228,6 +228,15 @@ RunCycleTime: 0.3 RunFrameTime: 0.15 RunFrameSpeedBoost: 1.25 + PushRadius: 0.6 + PushForce: 300 + PushPower: 2 + TimeToPushRake: 0.3 + RakeBackDist: 0.5 + RakeBackTime: 0.15 + RakeReachDist: 0.7 + RakeReachRadius: 0.8 + RakeFlyForce: 100 --- !u!114 &114917061756029764 MonoBehaviour: m_ObjectHideFlags: 1 @@ -271,7 +280,7 @@ m_Enabled: 1 serializedVersion: 2 m_Height: 2.03 - m_Radius: 0.28 + m_Radius: 0.67 m_SlopeLimit: 45 m_StepOffset: 0.3 m_SkinWidth: 0.08 diff --git a/FallUnity/Assets/Player/PlayerController.cs b/FallUnity/Assets/Player/PlayerController.cs index d3ca2ab..f050f76 100755 --- a/FallUnity/Assets/Player/PlayerController.cs +++ b/FallUnity/Assets/Player/PlayerController.cs @@ -23,7 +23,11 @@ void Start () { charC = GetComponent(); anim = GetComponent(); - } + rakeTimer = new SimpleTimer(); + rakeBackTween = new SimpleTween(); + oldPos = transform.position; + + } private float velY = 0; @@ -40,10 +44,16 @@ private float rakeTime = 0.0f; public float TimeToPushRake = 0.3f; - - // Update is called once per frame - void Update () { - Vector3 pos = transform.position; + + private SimpleTimer rakeTimer; + + private Vector3 rakeDir = new Vector2(); + + private Vector3 oldPos = new Vector3(); + + // Update is called once per frame + void Update () { + Vector3 dir = new Vector3(); if (input != null) @@ -92,20 +102,23 @@ } } - dir.y = velY; - - velY -= Gravity * Time.deltaTime; - - gameObject.layer = LayerMask.NameToLayer("player_controller_no_collide"); - charC.Move(dir * Time.deltaTime); - gameObject.layer = LayerMask.NameToLayer("player_controller"); - - if (charC.isGrounded) + if (state == State.Walking) { - velY = 0; + dir.y = velY; + + velY -= Gravity * Time.deltaTime; + + gameObject.layer = LayerMask.NameToLayer("player_controller_no_collide"); + charC.Move(dir * Time.deltaTime); + gameObject.layer = LayerMask.NameToLayer("player_controller"); + + if (charC.isGrounded) + { + velY = 0; + } } - Vector3 moved = transform.position - pos; + Vector3 moved = transform.position - oldPos; if (moved.magnitude > 0.7f * Time.deltaTime) { int layer = LayerMask.GetMask("leaf"); @@ -119,42 +132,140 @@ float power = Mathf.Pow(1.0f - dist / PushRadius, PushPower) * Time.deltaTime * PushForce; //toLeaf.Normalize(); toLeaf.y += UnityEngine.Random.Range(0.4f, 1.0f); - leaf.Body.AddForce(toLeaf * (9.0f + UnityEngine.Random.Range(0, 9.0f)) * power); + leaf.Body.AddForce(toLeaf * (9.0f + UnityEngine.Random.Range(0, 9.0f)) * power * Time.deltaTime * 60); } } } if (state == State.Walking) { - Vector2 rake = new Vector2(input.Axis(InputSource.InputAxis.RakeX), input.Axis(InputSource.InputAxis.RakeY)); - if (rake.magnitude < 0.2f) + rakeDir = new Vector3(input.Axis(InputSource.InputAxis.RakeX), 0, input.Axis(InputSource.InputAxis.RakeY)); + if (rakeDir.magnitude < 0.2f) { rakeTime = 0; } else { - rakeTime += Time.deltaTime; + if (rakeTime >= 0) + { + rakeTime += Time.deltaTime; + } } - if (rake.magnitude > 0.7f && rakeTime < TimeToPushRake) + if (rakeTime >= 0 && rakeDir.magnitude > 0.7f && rakeTime < TimeToPushRake) { state = State.Raking; - float angle = Mathf.Atan2(rake.y, rake.x); + float angle = Mathf.Atan2(-rakeDir.z, -rakeDir.x); if (angle < 0) angle += Mathf.PI * 2; - AnimationHandler.Direction rakeDir = AnimationHandler.Direction.UpRight; + AnimationHandler.Direction rakeAnimDir = AnimationHandler.Direction.UpRight; - if (angle < Mathf.PI / 2.0f) rakeDir = AnimationHandler.Direction.UpRight; - if (angle < Mathf.PI && angle >= Mathf.PI / 2.0f) rakeDir = AnimationHandler.Direction.UpLeft; - if (angle < Mathf.PI * 3.0f / 2.0f && angle >= Mathf.PI) rakeDir = AnimationHandler.Direction.DownLeft; - if (angle >= Mathf.PI * 3.0f / 2.0f) rakeDir = AnimationHandler.Direction.DownRight; + if (angle < Mathf.PI / 2.0f) rakeAnimDir = AnimationHandler.Direction.UpRight; + if (angle < Mathf.PI && angle >= Mathf.PI / 2.0f) rakeAnimDir = AnimationHandler.Direction.UpLeft; + if (angle < Mathf.PI * 3.0f / 2.0f && angle >= Mathf.PI) rakeAnimDir = AnimationHandler.Direction.DownLeft; + if (angle >= Mathf.PI * 3.0f / 2.0f) rakeAnimDir = AnimationHandler.Direction.DownRight; - anim.SetState("Rake", rakeDir, 0); + rakeTime = -1; + + anim.SetState("Rake", rakeAnimDir, 0); + + rakeTimer.Start(0.15f, Rake); } } - } + + oldPos = transform.position; + + } + + public float RakeBackDist = 0.8f; + public float RakeBackTime = 0.2f; + + private SimpleTween rakeBackTween; + + private Vector3 rakeBackStart = new Vector3(); + private Vector3 rakeBackEnd = new Vector3(); + + public float RakeReachDist = 0.6f; + public float RakeReachRadius = 0.6f; + + public float RakeFlyForce = 100; + + private void Rake() + { + Vector3 rakeMove = rakeDir * RakeBackDist; + rakeBackStart = transform.position; + charC.Move(rakeMove); + rakeBackEnd = transform.position; + transform.position = rakeBackStart; + + rakeBackTween.Init(UpdateRakeBack, SimpleTween.QuadEaseInOut, 0, 1, RakeBackTime); + rakeBackTween.OnFinish = FinishedRake; + + List leafs = new List(); + + int layer = LayerMask.GetMask("leaf"); + foreach (Collider col in Physics.OverlapSphere(rakeBackStart - rakeDir * RakeReachDist, RakeReachRadius, layer)) + { + LeafPhysics leaf = col.GetComponentInParent(); + if (leaf != null) + { + leafs.Add(leaf); + } + } + + foreach (LeafPhysics leaf in leafs) + { + float rand = UnityEngine.Random.Range(0, 1); + Vector3 startPos = leaf.transform.position; + float dist = Vector3.Dot(startPos - transform.position, -rakeDir); + dist += UnityEngine.Random.Range(-0.1f, 0.1f); + Vector3 pull = rakeDir * dist; + + if (rand < 0.8f) + { + + // pull the leaf back, and then destroy + leaf.Body.isKinematic = true; + + pull.x += UnityEngine.Random.Range(-0.05f, 0.05f); + pull.z += UnityEngine.Random.Range(-0.05f, 0.05f); + + float pullTime = RakeBackTime + UnityEngine.Random.Range(-0.04f, 0.04f); + + SimpleTween tween = new SimpleTween(v => + { + leaf.transform.position = startPos + pull * v; + }, SimpleTween.TweenType.EaseLinear, 0, 1, pullTime); + tween.OnFinish = () => + { + GameObject.Destroy(leaf.gameObject); + }; + } + else + { + pull.Normalize(); + pull = Quaternion.Euler(UnityEngine.Random.Range(-30, 30), UnityEngine.Random.Range(-30, 30), UnityEngine.Random.Range(-30, 30)) * pull; + pull.y += 0.3f; + leaf.Body.AddForce(pull * RakeFlyForce, ForceMode.Impulse); + } + } + } + + private void FinishedRake() + { + state = State.Walking; + anim.SetState("Stand", anim.Dir, anim.Frame); + } + + private void UpdateRakeBack(float val) + { + Vector3 pos = transform.position; + pos.x = rakeBackStart.x * (1 - val) + rakeBackEnd.x * val; + pos.z = rakeBackStart.z * (1 - val) + rakeBackEnd.z * val; + transform.position = pos; + } private InputSource input = null; private InputManager.PlayerInfo playerInfo; diff --git a/FallUnity/Assets/Utils/SimpleTimer.cs b/FallUnity/Assets/Utils/SimpleTimer.cs index e24678b..7439d65 100644 --- a/FallUnity/Assets/Utils/SimpleTimer.cs +++ b/FallUnity/Assets/Utils/SimpleTimer.cs @@ -31,6 +31,7 @@ public SimpleTimer(float time, SimpleTimerTick callback, bool loop = false) { + efm = GameObject.FindObjectOfType(); Start(time, callback, loop); } diff --git a/FallUnity/Assets/IsoSorting/IsoItem.cs b/FallUnity/Assets/IsoSorting/IsoItem.cs index ef70a5d..d8bc615 100644 --- a/FallUnity/Assets/IsoSorting/IsoItem.cs +++ b/FallUnity/Assets/IsoSorting/IsoItem.cs @@ -55,10 +55,13 @@ } } - void Destroy() + void OnDestroy() { IsoHandler iso = GameObject.FindObjectOfType(); - iso.RemoveIsoItem(this); + if (iso != null) + { + iso.RemoveIsoItem(this); + } } // Update is called once per frame diff --git a/FallUnity/Assets/Leaves/LeafPhysics.cs b/FallUnity/Assets/Leaves/LeafPhysics.cs index a0469c4..8328ab8 100755 --- a/FallUnity/Assets/Leaves/LeafPhysics.cs +++ b/FallUnity/Assets/Leaves/LeafPhysics.cs @@ -87,14 +87,9 @@ // Update is called once per frame void Update () { - if (body.velocity.sqrMagnitude > 0.01f) + if (!body.isKinematic && body.velocity.sqrMagnitude > 0.01f) { - if (Debug) - { - bool bp = true; - } - - float baseForceUp = Mathf.Max(BaseWindForce * Mathf.Pow(Mathf.Abs(transform.up.y), AnglePow) * -body.velocity.y); + float baseForceUp = Mathf.Max(BaseWindForce * Mathf.Pow(Mathf.Abs(transform.up.y), AnglePow) * -body.velocity.y) * Time.deltaTime * 60; body.AddForceAtPosition(Vector3.up * baseForceUp * (1 + PlusX.Current), transform.position + Vector3.right * 0.4f); body.AddForceAtPosition(Vector3.up * baseForceUp * (1 + MinusX.Current), transform.position - Vector3.right * 0.4f); @@ -111,9 +106,9 @@ PushUpCurrent = Mathf.Max(0, PushUpCurrent - PushUpDecayRate * Time.deltaTime); */ - - float torqueX = -transform.up.x; - float torqueZ = -transform.up.z; + Vector3 up = transform.up; + float torqueX = -up.x; + float torqueZ = -up.z; float velYScale = Math.Max(0, -body.velocity.y); @@ -121,7 +116,7 @@ //body.AddForce(Vector3.up * Mathf.Pow(Mathf.Max(-body.velocity.y / VelDownForceDivide), 2) * VelDownForceScale); body.AddRelativeTorque(new Vector3(Mathf.Max(-TorqueBounds, Mathf.Min(TorqueBounds, torqueX * TorqueScale * 1.2f)) + TorqueX.Current * 0.5f, 0, Mathf.Max(-TorqueBounds, Mathf.Min(TorqueBounds, torqueZ * TorqueScale)) + TorqueZ.Current * 0.5f) * velYScale); - body.AddForce(new Vector3(-body.velocity.x, 0, -body.velocity.z) * SideRestitution); + body.AddForce(new Vector3(-body.velocity.x, 0, -body.velocity.z) * SideRestitution * Time.deltaTime * 60); } if (transform.position.y < -5) diff --git a/FallUnity/Assets/Player/Player.prefab b/FallUnity/Assets/Player/Player.prefab index 250744f..a333473 100644 --- a/FallUnity/Assets/Player/Player.prefab +++ b/FallUnity/Assets/Player/Player.prefab @@ -228,6 +228,15 @@ RunCycleTime: 0.3 RunFrameTime: 0.15 RunFrameSpeedBoost: 1.25 + PushRadius: 0.6 + PushForce: 300 + PushPower: 2 + TimeToPushRake: 0.3 + RakeBackDist: 0.5 + RakeBackTime: 0.15 + RakeReachDist: 0.7 + RakeReachRadius: 0.8 + RakeFlyForce: 100 --- !u!114 &114917061756029764 MonoBehaviour: m_ObjectHideFlags: 1 @@ -271,7 +280,7 @@ m_Enabled: 1 serializedVersion: 2 m_Height: 2.03 - m_Radius: 0.28 + m_Radius: 0.67 m_SlopeLimit: 45 m_StepOffset: 0.3 m_SkinWidth: 0.08 diff --git a/FallUnity/Assets/Player/PlayerController.cs b/FallUnity/Assets/Player/PlayerController.cs index d3ca2ab..f050f76 100755 --- a/FallUnity/Assets/Player/PlayerController.cs +++ b/FallUnity/Assets/Player/PlayerController.cs @@ -23,7 +23,11 @@ void Start () { charC = GetComponent(); anim = GetComponent(); - } + rakeTimer = new SimpleTimer(); + rakeBackTween = new SimpleTween(); + oldPos = transform.position; + + } private float velY = 0; @@ -40,10 +44,16 @@ private float rakeTime = 0.0f; public float TimeToPushRake = 0.3f; - - // Update is called once per frame - void Update () { - Vector3 pos = transform.position; + + private SimpleTimer rakeTimer; + + private Vector3 rakeDir = new Vector2(); + + private Vector3 oldPos = new Vector3(); + + // Update is called once per frame + void Update () { + Vector3 dir = new Vector3(); if (input != null) @@ -92,20 +102,23 @@ } } - dir.y = velY; - - velY -= Gravity * Time.deltaTime; - - gameObject.layer = LayerMask.NameToLayer("player_controller_no_collide"); - charC.Move(dir * Time.deltaTime); - gameObject.layer = LayerMask.NameToLayer("player_controller"); - - if (charC.isGrounded) + if (state == State.Walking) { - velY = 0; + dir.y = velY; + + velY -= Gravity * Time.deltaTime; + + gameObject.layer = LayerMask.NameToLayer("player_controller_no_collide"); + charC.Move(dir * Time.deltaTime); + gameObject.layer = LayerMask.NameToLayer("player_controller"); + + if (charC.isGrounded) + { + velY = 0; + } } - Vector3 moved = transform.position - pos; + Vector3 moved = transform.position - oldPos; if (moved.magnitude > 0.7f * Time.deltaTime) { int layer = LayerMask.GetMask("leaf"); @@ -119,42 +132,140 @@ float power = Mathf.Pow(1.0f - dist / PushRadius, PushPower) * Time.deltaTime * PushForce; //toLeaf.Normalize(); toLeaf.y += UnityEngine.Random.Range(0.4f, 1.0f); - leaf.Body.AddForce(toLeaf * (9.0f + UnityEngine.Random.Range(0, 9.0f)) * power); + leaf.Body.AddForce(toLeaf * (9.0f + UnityEngine.Random.Range(0, 9.0f)) * power * Time.deltaTime * 60); } } } if (state == State.Walking) { - Vector2 rake = new Vector2(input.Axis(InputSource.InputAxis.RakeX), input.Axis(InputSource.InputAxis.RakeY)); - if (rake.magnitude < 0.2f) + rakeDir = new Vector3(input.Axis(InputSource.InputAxis.RakeX), 0, input.Axis(InputSource.InputAxis.RakeY)); + if (rakeDir.magnitude < 0.2f) { rakeTime = 0; } else { - rakeTime += Time.deltaTime; + if (rakeTime >= 0) + { + rakeTime += Time.deltaTime; + } } - if (rake.magnitude > 0.7f && rakeTime < TimeToPushRake) + if (rakeTime >= 0 && rakeDir.magnitude > 0.7f && rakeTime < TimeToPushRake) { state = State.Raking; - float angle = Mathf.Atan2(rake.y, rake.x); + float angle = Mathf.Atan2(-rakeDir.z, -rakeDir.x); if (angle < 0) angle += Mathf.PI * 2; - AnimationHandler.Direction rakeDir = AnimationHandler.Direction.UpRight; + AnimationHandler.Direction rakeAnimDir = AnimationHandler.Direction.UpRight; - if (angle < Mathf.PI / 2.0f) rakeDir = AnimationHandler.Direction.UpRight; - if (angle < Mathf.PI && angle >= Mathf.PI / 2.0f) rakeDir = AnimationHandler.Direction.UpLeft; - if (angle < Mathf.PI * 3.0f / 2.0f && angle >= Mathf.PI) rakeDir = AnimationHandler.Direction.DownLeft; - if (angle >= Mathf.PI * 3.0f / 2.0f) rakeDir = AnimationHandler.Direction.DownRight; + if (angle < Mathf.PI / 2.0f) rakeAnimDir = AnimationHandler.Direction.UpRight; + if (angle < Mathf.PI && angle >= Mathf.PI / 2.0f) rakeAnimDir = AnimationHandler.Direction.UpLeft; + if (angle < Mathf.PI * 3.0f / 2.0f && angle >= Mathf.PI) rakeAnimDir = AnimationHandler.Direction.DownLeft; + if (angle >= Mathf.PI * 3.0f / 2.0f) rakeAnimDir = AnimationHandler.Direction.DownRight; - anim.SetState("Rake", rakeDir, 0); + rakeTime = -1; + + anim.SetState("Rake", rakeAnimDir, 0); + + rakeTimer.Start(0.15f, Rake); } } - } + + oldPos = transform.position; + + } + + public float RakeBackDist = 0.8f; + public float RakeBackTime = 0.2f; + + private SimpleTween rakeBackTween; + + private Vector3 rakeBackStart = new Vector3(); + private Vector3 rakeBackEnd = new Vector3(); + + public float RakeReachDist = 0.6f; + public float RakeReachRadius = 0.6f; + + public float RakeFlyForce = 100; + + private void Rake() + { + Vector3 rakeMove = rakeDir * RakeBackDist; + rakeBackStart = transform.position; + charC.Move(rakeMove); + rakeBackEnd = transform.position; + transform.position = rakeBackStart; + + rakeBackTween.Init(UpdateRakeBack, SimpleTween.QuadEaseInOut, 0, 1, RakeBackTime); + rakeBackTween.OnFinish = FinishedRake; + + List leafs = new List(); + + int layer = LayerMask.GetMask("leaf"); + foreach (Collider col in Physics.OverlapSphere(rakeBackStart - rakeDir * RakeReachDist, RakeReachRadius, layer)) + { + LeafPhysics leaf = col.GetComponentInParent(); + if (leaf != null) + { + leafs.Add(leaf); + } + } + + foreach (LeafPhysics leaf in leafs) + { + float rand = UnityEngine.Random.Range(0, 1); + Vector3 startPos = leaf.transform.position; + float dist = Vector3.Dot(startPos - transform.position, -rakeDir); + dist += UnityEngine.Random.Range(-0.1f, 0.1f); + Vector3 pull = rakeDir * dist; + + if (rand < 0.8f) + { + + // pull the leaf back, and then destroy + leaf.Body.isKinematic = true; + + pull.x += UnityEngine.Random.Range(-0.05f, 0.05f); + pull.z += UnityEngine.Random.Range(-0.05f, 0.05f); + + float pullTime = RakeBackTime + UnityEngine.Random.Range(-0.04f, 0.04f); + + SimpleTween tween = new SimpleTween(v => + { + leaf.transform.position = startPos + pull * v; + }, SimpleTween.TweenType.EaseLinear, 0, 1, pullTime); + tween.OnFinish = () => + { + GameObject.Destroy(leaf.gameObject); + }; + } + else + { + pull.Normalize(); + pull = Quaternion.Euler(UnityEngine.Random.Range(-30, 30), UnityEngine.Random.Range(-30, 30), UnityEngine.Random.Range(-30, 30)) * pull; + pull.y += 0.3f; + leaf.Body.AddForce(pull * RakeFlyForce, ForceMode.Impulse); + } + } + } + + private void FinishedRake() + { + state = State.Walking; + anim.SetState("Stand", anim.Dir, anim.Frame); + } + + private void UpdateRakeBack(float val) + { + Vector3 pos = transform.position; + pos.x = rakeBackStart.x * (1 - val) + rakeBackEnd.x * val; + pos.z = rakeBackStart.z * (1 - val) + rakeBackEnd.z * val; + transform.position = pos; + } private InputSource input = null; private InputManager.PlayerInfo playerInfo; diff --git a/FallUnity/Assets/Utils/SimpleTimer.cs b/FallUnity/Assets/Utils/SimpleTimer.cs index e24678b..7439d65 100644 --- a/FallUnity/Assets/Utils/SimpleTimer.cs +++ b/FallUnity/Assets/Utils/SimpleTimer.cs @@ -31,6 +31,7 @@ public SimpleTimer(float time, SimpleTimerTick callback, bool loop = false) { + efm = GameObject.FindObjectOfType(); Start(time, callback, loop); } diff --git a/FallUnity/Assets/Utils/SimpleTween.cs b/FallUnity/Assets/Utils/SimpleTween.cs index 82b4657..fea8e11 100644 --- a/FallUnity/Assets/Utils/SimpleTween.cs +++ b/FallUnity/Assets/Utils/SimpleTween.cs @@ -65,6 +65,7 @@ public SimpleTween(SimpleTweenUpdate onUpdate, TweenType tType, float start, float end, float time, bool toInt = false) { + efm = GameObject.FindObjectOfType(); InstanceID = NextID++; TweenFunction func = GetTweenFunction(tType); Init(onUpdate, func, start, end, time, toInt); @@ -72,6 +73,7 @@ public SimpleTween(SimpleTweenUpdate onUpdate, TweenFunction tweenFunc, float start, float end, float time, bool toInt = false) { + efm = GameObject.FindObjectOfType(); InstanceID = NextID++; Init(onUpdate, tweenFunc, start, end, time, toInt); } diff --git a/FallUnity/Assets/IsoSorting/IsoItem.cs b/FallUnity/Assets/IsoSorting/IsoItem.cs index ef70a5d..d8bc615 100644 --- a/FallUnity/Assets/IsoSorting/IsoItem.cs +++ b/FallUnity/Assets/IsoSorting/IsoItem.cs @@ -55,10 +55,13 @@ } } - void Destroy() + void OnDestroy() { IsoHandler iso = GameObject.FindObjectOfType(); - iso.RemoveIsoItem(this); + if (iso != null) + { + iso.RemoveIsoItem(this); + } } // Update is called once per frame diff --git a/FallUnity/Assets/Leaves/LeafPhysics.cs b/FallUnity/Assets/Leaves/LeafPhysics.cs index a0469c4..8328ab8 100755 --- a/FallUnity/Assets/Leaves/LeafPhysics.cs +++ b/FallUnity/Assets/Leaves/LeafPhysics.cs @@ -87,14 +87,9 @@ // Update is called once per frame void Update () { - if (body.velocity.sqrMagnitude > 0.01f) + if (!body.isKinematic && body.velocity.sqrMagnitude > 0.01f) { - if (Debug) - { - bool bp = true; - } - - float baseForceUp = Mathf.Max(BaseWindForce * Mathf.Pow(Mathf.Abs(transform.up.y), AnglePow) * -body.velocity.y); + float baseForceUp = Mathf.Max(BaseWindForce * Mathf.Pow(Mathf.Abs(transform.up.y), AnglePow) * -body.velocity.y) * Time.deltaTime * 60; body.AddForceAtPosition(Vector3.up * baseForceUp * (1 + PlusX.Current), transform.position + Vector3.right * 0.4f); body.AddForceAtPosition(Vector3.up * baseForceUp * (1 + MinusX.Current), transform.position - Vector3.right * 0.4f); @@ -111,9 +106,9 @@ PushUpCurrent = Mathf.Max(0, PushUpCurrent - PushUpDecayRate * Time.deltaTime); */ - - float torqueX = -transform.up.x; - float torqueZ = -transform.up.z; + Vector3 up = transform.up; + float torqueX = -up.x; + float torqueZ = -up.z; float velYScale = Math.Max(0, -body.velocity.y); @@ -121,7 +116,7 @@ //body.AddForce(Vector3.up * Mathf.Pow(Mathf.Max(-body.velocity.y / VelDownForceDivide), 2) * VelDownForceScale); body.AddRelativeTorque(new Vector3(Mathf.Max(-TorqueBounds, Mathf.Min(TorqueBounds, torqueX * TorqueScale * 1.2f)) + TorqueX.Current * 0.5f, 0, Mathf.Max(-TorqueBounds, Mathf.Min(TorqueBounds, torqueZ * TorqueScale)) + TorqueZ.Current * 0.5f) * velYScale); - body.AddForce(new Vector3(-body.velocity.x, 0, -body.velocity.z) * SideRestitution); + body.AddForce(new Vector3(-body.velocity.x, 0, -body.velocity.z) * SideRestitution * Time.deltaTime * 60); } if (transform.position.y < -5) diff --git a/FallUnity/Assets/Player/Player.prefab b/FallUnity/Assets/Player/Player.prefab index 250744f..a333473 100644 --- a/FallUnity/Assets/Player/Player.prefab +++ b/FallUnity/Assets/Player/Player.prefab @@ -228,6 +228,15 @@ RunCycleTime: 0.3 RunFrameTime: 0.15 RunFrameSpeedBoost: 1.25 + PushRadius: 0.6 + PushForce: 300 + PushPower: 2 + TimeToPushRake: 0.3 + RakeBackDist: 0.5 + RakeBackTime: 0.15 + RakeReachDist: 0.7 + RakeReachRadius: 0.8 + RakeFlyForce: 100 --- !u!114 &114917061756029764 MonoBehaviour: m_ObjectHideFlags: 1 @@ -271,7 +280,7 @@ m_Enabled: 1 serializedVersion: 2 m_Height: 2.03 - m_Radius: 0.28 + m_Radius: 0.67 m_SlopeLimit: 45 m_StepOffset: 0.3 m_SkinWidth: 0.08 diff --git a/FallUnity/Assets/Player/PlayerController.cs b/FallUnity/Assets/Player/PlayerController.cs index d3ca2ab..f050f76 100755 --- a/FallUnity/Assets/Player/PlayerController.cs +++ b/FallUnity/Assets/Player/PlayerController.cs @@ -23,7 +23,11 @@ void Start () { charC = GetComponent(); anim = GetComponent(); - } + rakeTimer = new SimpleTimer(); + rakeBackTween = new SimpleTween(); + oldPos = transform.position; + + } private float velY = 0; @@ -40,10 +44,16 @@ private float rakeTime = 0.0f; public float TimeToPushRake = 0.3f; - - // Update is called once per frame - void Update () { - Vector3 pos = transform.position; + + private SimpleTimer rakeTimer; + + private Vector3 rakeDir = new Vector2(); + + private Vector3 oldPos = new Vector3(); + + // Update is called once per frame + void Update () { + Vector3 dir = new Vector3(); if (input != null) @@ -92,20 +102,23 @@ } } - dir.y = velY; - - velY -= Gravity * Time.deltaTime; - - gameObject.layer = LayerMask.NameToLayer("player_controller_no_collide"); - charC.Move(dir * Time.deltaTime); - gameObject.layer = LayerMask.NameToLayer("player_controller"); - - if (charC.isGrounded) + if (state == State.Walking) { - velY = 0; + dir.y = velY; + + velY -= Gravity * Time.deltaTime; + + gameObject.layer = LayerMask.NameToLayer("player_controller_no_collide"); + charC.Move(dir * Time.deltaTime); + gameObject.layer = LayerMask.NameToLayer("player_controller"); + + if (charC.isGrounded) + { + velY = 0; + } } - Vector3 moved = transform.position - pos; + Vector3 moved = transform.position - oldPos; if (moved.magnitude > 0.7f * Time.deltaTime) { int layer = LayerMask.GetMask("leaf"); @@ -119,42 +132,140 @@ float power = Mathf.Pow(1.0f - dist / PushRadius, PushPower) * Time.deltaTime * PushForce; //toLeaf.Normalize(); toLeaf.y += UnityEngine.Random.Range(0.4f, 1.0f); - leaf.Body.AddForce(toLeaf * (9.0f + UnityEngine.Random.Range(0, 9.0f)) * power); + leaf.Body.AddForce(toLeaf * (9.0f + UnityEngine.Random.Range(0, 9.0f)) * power * Time.deltaTime * 60); } } } if (state == State.Walking) { - Vector2 rake = new Vector2(input.Axis(InputSource.InputAxis.RakeX), input.Axis(InputSource.InputAxis.RakeY)); - if (rake.magnitude < 0.2f) + rakeDir = new Vector3(input.Axis(InputSource.InputAxis.RakeX), 0, input.Axis(InputSource.InputAxis.RakeY)); + if (rakeDir.magnitude < 0.2f) { rakeTime = 0; } else { - rakeTime += Time.deltaTime; + if (rakeTime >= 0) + { + rakeTime += Time.deltaTime; + } } - if (rake.magnitude > 0.7f && rakeTime < TimeToPushRake) + if (rakeTime >= 0 && rakeDir.magnitude > 0.7f && rakeTime < TimeToPushRake) { state = State.Raking; - float angle = Mathf.Atan2(rake.y, rake.x); + float angle = Mathf.Atan2(-rakeDir.z, -rakeDir.x); if (angle < 0) angle += Mathf.PI * 2; - AnimationHandler.Direction rakeDir = AnimationHandler.Direction.UpRight; + AnimationHandler.Direction rakeAnimDir = AnimationHandler.Direction.UpRight; - if (angle < Mathf.PI / 2.0f) rakeDir = AnimationHandler.Direction.UpRight; - if (angle < Mathf.PI && angle >= Mathf.PI / 2.0f) rakeDir = AnimationHandler.Direction.UpLeft; - if (angle < Mathf.PI * 3.0f / 2.0f && angle >= Mathf.PI) rakeDir = AnimationHandler.Direction.DownLeft; - if (angle >= Mathf.PI * 3.0f / 2.0f) rakeDir = AnimationHandler.Direction.DownRight; + if (angle < Mathf.PI / 2.0f) rakeAnimDir = AnimationHandler.Direction.UpRight; + if (angle < Mathf.PI && angle >= Mathf.PI / 2.0f) rakeAnimDir = AnimationHandler.Direction.UpLeft; + if (angle < Mathf.PI * 3.0f / 2.0f && angle >= Mathf.PI) rakeAnimDir = AnimationHandler.Direction.DownLeft; + if (angle >= Mathf.PI * 3.0f / 2.0f) rakeAnimDir = AnimationHandler.Direction.DownRight; - anim.SetState("Rake", rakeDir, 0); + rakeTime = -1; + + anim.SetState("Rake", rakeAnimDir, 0); + + rakeTimer.Start(0.15f, Rake); } } - } + + oldPos = transform.position; + + } + + public float RakeBackDist = 0.8f; + public float RakeBackTime = 0.2f; + + private SimpleTween rakeBackTween; + + private Vector3 rakeBackStart = new Vector3(); + private Vector3 rakeBackEnd = new Vector3(); + + public float RakeReachDist = 0.6f; + public float RakeReachRadius = 0.6f; + + public float RakeFlyForce = 100; + + private void Rake() + { + Vector3 rakeMove = rakeDir * RakeBackDist; + rakeBackStart = transform.position; + charC.Move(rakeMove); + rakeBackEnd = transform.position; + transform.position = rakeBackStart; + + rakeBackTween.Init(UpdateRakeBack, SimpleTween.QuadEaseInOut, 0, 1, RakeBackTime); + rakeBackTween.OnFinish = FinishedRake; + + List leafs = new List(); + + int layer = LayerMask.GetMask("leaf"); + foreach (Collider col in Physics.OverlapSphere(rakeBackStart - rakeDir * RakeReachDist, RakeReachRadius, layer)) + { + LeafPhysics leaf = col.GetComponentInParent(); + if (leaf != null) + { + leafs.Add(leaf); + } + } + + foreach (LeafPhysics leaf in leafs) + { + float rand = UnityEngine.Random.Range(0, 1); + Vector3 startPos = leaf.transform.position; + float dist = Vector3.Dot(startPos - transform.position, -rakeDir); + dist += UnityEngine.Random.Range(-0.1f, 0.1f); + Vector3 pull = rakeDir * dist; + + if (rand < 0.8f) + { + + // pull the leaf back, and then destroy + leaf.Body.isKinematic = true; + + pull.x += UnityEngine.Random.Range(-0.05f, 0.05f); + pull.z += UnityEngine.Random.Range(-0.05f, 0.05f); + + float pullTime = RakeBackTime + UnityEngine.Random.Range(-0.04f, 0.04f); + + SimpleTween tween = new SimpleTween(v => + { + leaf.transform.position = startPos + pull * v; + }, SimpleTween.TweenType.EaseLinear, 0, 1, pullTime); + tween.OnFinish = () => + { + GameObject.Destroy(leaf.gameObject); + }; + } + else + { + pull.Normalize(); + pull = Quaternion.Euler(UnityEngine.Random.Range(-30, 30), UnityEngine.Random.Range(-30, 30), UnityEngine.Random.Range(-30, 30)) * pull; + pull.y += 0.3f; + leaf.Body.AddForce(pull * RakeFlyForce, ForceMode.Impulse); + } + } + } + + private void FinishedRake() + { + state = State.Walking; + anim.SetState("Stand", anim.Dir, anim.Frame); + } + + private void UpdateRakeBack(float val) + { + Vector3 pos = transform.position; + pos.x = rakeBackStart.x * (1 - val) + rakeBackEnd.x * val; + pos.z = rakeBackStart.z * (1 - val) + rakeBackEnd.z * val; + transform.position = pos; + } private InputSource input = null; private InputManager.PlayerInfo playerInfo; diff --git a/FallUnity/Assets/Utils/SimpleTimer.cs b/FallUnity/Assets/Utils/SimpleTimer.cs index e24678b..7439d65 100644 --- a/FallUnity/Assets/Utils/SimpleTimer.cs +++ b/FallUnity/Assets/Utils/SimpleTimer.cs @@ -31,6 +31,7 @@ public SimpleTimer(float time, SimpleTimerTick callback, bool loop = false) { + efm = GameObject.FindObjectOfType(); Start(time, callback, loop); } diff --git a/FallUnity/Assets/Utils/SimpleTween.cs b/FallUnity/Assets/Utils/SimpleTween.cs index 82b4657..fea8e11 100644 --- a/FallUnity/Assets/Utils/SimpleTween.cs +++ b/FallUnity/Assets/Utils/SimpleTween.cs @@ -65,6 +65,7 @@ public SimpleTween(SimpleTweenUpdate onUpdate, TweenType tType, float start, float end, float time, bool toInt = false) { + efm = GameObject.FindObjectOfType(); InstanceID = NextID++; TweenFunction func = GetTweenFunction(tType); Init(onUpdate, func, start, end, time, toInt); @@ -72,6 +73,7 @@ public SimpleTween(SimpleTweenUpdate onUpdate, TweenFunction tweenFunc, float start, float end, float time, bool toInt = false) { + efm = GameObject.FindObjectOfType(); InstanceID = NextID++; Init(onUpdate, tweenFunc, start, end, time, toInt); } diff --git a/Flash/RECOVER_Player.fla b/Flash/RECOVER_Player.fla new file mode 100644 index 0000000..d2f0385 --- /dev/null +++ b/Flash/RECOVER_Player.fla Binary files differ