From 4b8af39de6dbfab924cc37a3143428ceecff0179 Mon Sep 17 00:00:00 2001 From: yeonjae Date: Wed, 18 Dec 2024 08:11:56 +0900 Subject: [PATCH] chore: change player mover, player... --- .../SWPPT3/Scripts/Main/PlayerLogic/Player.cs | 34 +++++----- .../Scripts/Main/PlayerLogic/PlayerMover.cs | 6 +- .../Main/PlayerLogic/State/MetalState.cs | 16 ----- .../Main/PlayerLogic/State/PlayerState.cs | 3 - .../Main/PlayerLogic/State/RubberState.cs | 14 ---- .../Main/PlayerLogic/State/SlimeState.cs | 14 ---- .../SoftbodyPhysics/SoftbodyGenerator.cs | 66 ++++++++++++++++--- 7 files changed, 76 insertions(+), 77 deletions(-) diff --git a/Assets/SWPPT3/Scripts/Main/PlayerLogic/Player.cs b/Assets/SWPPT3/Scripts/Main/PlayerLogic/Player.cs index 13f242c7..67af2261 100644 --- a/Assets/SWPPT3/Scripts/Main/PlayerLogic/Player.cs +++ b/Assets/SWPPT3/Scripts/Main/PlayerLogic/Player.cs @@ -5,6 +5,7 @@ using SWPPT3.Main.Prop; using UnityEngine.InputSystem; using SWPPT3.Main.PlayerLogic.State; +using SWPPT3.SoftbodyPhysics; namespace SWPPT3.Main.PlayerLogic { @@ -15,20 +16,14 @@ public class Player : MonoBehaviour private int slimeCount , metalCount, rubberCount; public Dictionary Item; - public event Action OnItemChanged; + private SoftbodyGenerator _softbody; - [SerializeField] - private Rigidbody _rb; - // [SerializeField] - public PhysicMaterial _physicMaterial; - [SerializeField] - public Collider _collider; + public event Action OnItemChanged; private PlayerState PlayerState => _playerStates[_currentState]; public PlayerStates CurrentState => _currentState; - public Rigidbody Rigidbody => _rb; private readonly Dictionary _playerStates = new() { @@ -36,16 +31,10 @@ public class Player : MonoBehaviour { PlayerStates.Rubber, new RubberState() }, { PlayerStates.Slime, new SlimeState() }, }; - public void SetBounciness(float bounciness, PhysicMaterialCombine bounceCombine = PhysicMaterialCombine.Maximum) - { - _physicMaterial.bounciness = bounciness; - _physicMaterial.bounceCombine = bounceCombine; - - _collider.material = _physicMaterial; - } private void Awake() { + _softbody = GetComponent(); if (Item == null) { Item = new Dictionary @@ -67,9 +56,18 @@ public void TryChangeState(PlayerStates newState) if (newState != PlayerStates.Slime) Item[newState]--; OnItemChanged?.Invoke(); _currentState = newState; - PlayerState.ChangeRigidbody(_rb); - PlayerState.ChangePhysics(_collider, _physicMaterial); - _collider.hasModifiableContacts = newState == PlayerStates.Slime; + if (newState == PlayerStates.Rubber) + { + _softbody.SetRubberJump(); + } + else if (newState == PlayerStates.Metal) + { + _softbody.SetMetal(); + } + else if (newState == PlayerStates.Slime) + { + _softbody.SetSlime(); + } } Debug.Log(_currentState); diff --git a/Assets/SWPPT3/Scripts/Main/PlayerLogic/PlayerMover.cs b/Assets/SWPPT3/Scripts/Main/PlayerLogic/PlayerMover.cs index d7dc90d7..32bc28b2 100644 --- a/Assets/SWPPT3/Scripts/Main/PlayerLogic/PlayerMover.cs +++ b/Assets/SWPPT3/Scripts/Main/PlayerLogic/PlayerMover.cs @@ -66,7 +66,7 @@ private void Update() } if (_player.CurrentState == PlayerStates.Rubber && _groundedObjects.Count == 0 && _isHoldingJump) { - _player.SetBounciness(1.0f); + // _player.SetBounciness(1.0f); } } @@ -135,7 +135,7 @@ private void HandleJump() _isHoldingJump = true; if (_player.CurrentState == PlayerStates.Rubber) { - _player.SetBounciness(1.0f); + // _player.SetBounciness(1.0f); } } @@ -144,7 +144,7 @@ private void HandleJumpCancel() _isHoldingJump = false; if (_player.CurrentState == PlayerStates.Rubber) { - _player.SetBounciness(0.5f); + // _player.SetBounciness(0.5f); } } diff --git a/Assets/SWPPT3/Scripts/Main/PlayerLogic/State/MetalState.cs b/Assets/SWPPT3/Scripts/Main/PlayerLogic/State/MetalState.cs index 1a1583be..343bc304 100644 --- a/Assets/SWPPT3/Scripts/Main/PlayerLogic/State/MetalState.cs +++ b/Assets/SWPPT3/Scripts/Main/PlayerLogic/State/MetalState.cs @@ -16,21 +16,5 @@ public override void InteractWithProp(Player player, PropBase obstacle) base.InteractWithProp(player, obstacle); } } - - public override void ChangeRigidbody(Rigidbody rb) - { - - } - - public override void ChangePhysics(Collider collider, PhysicMaterial physicMaterial) - { - physicMaterial.bounciness = 0f; - physicMaterial.dynamicFriction = 0f; - physicMaterial.staticFriction = 0f; - - physicMaterial.bounceCombine = PhysicMaterialCombine.Maximum; - - collider.material = physicMaterial; - } } } diff --git a/Assets/SWPPT3/Scripts/Main/PlayerLogic/State/PlayerState.cs b/Assets/SWPPT3/Scripts/Main/PlayerLogic/State/PlayerState.cs index 1c62dcdf..59fc08f2 100644 --- a/Assets/SWPPT3/Scripts/Main/PlayerLogic/State/PlayerState.cs +++ b/Assets/SWPPT3/Scripts/Main/PlayerLogic/State/PlayerState.cs @@ -50,9 +50,6 @@ public virtual void StopInteractWithProp(Player player, PropBase obstacle) obstacle.StopInteractWithPlayer(player.CurrentState); } - - public abstract void ChangeRigidbody(Rigidbody rb); - public abstract void ChangePhysics(Collider collider, PhysicMaterial physicMaterial); } } diff --git a/Assets/SWPPT3/Scripts/Main/PlayerLogic/State/RubberState.cs b/Assets/SWPPT3/Scripts/Main/PlayerLogic/State/RubberState.cs index 2282aa4f..1b5a0c70 100644 --- a/Assets/SWPPT3/Scripts/Main/PlayerLogic/State/RubberState.cs +++ b/Assets/SWPPT3/Scripts/Main/PlayerLogic/State/RubberState.cs @@ -17,19 +17,5 @@ public override void InteractWithProp(Player player, PropBase obstacle) base.InteractWithProp(player, obstacle); } } - public override void ChangeRigidbody(Rigidbody rb) - { - - } - public override void ChangePhysics(Collider collider, PhysicMaterial physicMaterial) - { - physicMaterial.bounciness = 0.5f; - physicMaterial.dynamicFriction = 0f; - physicMaterial.staticFriction = 0f; - - physicMaterial.bounceCombine = PhysicMaterialCombine.Maximum; - - collider.material = physicMaterial; - } } } diff --git a/Assets/SWPPT3/Scripts/Main/PlayerLogic/State/SlimeState.cs b/Assets/SWPPT3/Scripts/Main/PlayerLogic/State/SlimeState.cs index 45f84298..d10f8136 100644 --- a/Assets/SWPPT3/Scripts/Main/PlayerLogic/State/SlimeState.cs +++ b/Assets/SWPPT3/Scripts/Main/PlayerLogic/State/SlimeState.cs @@ -17,19 +17,5 @@ public override void InteractWithProp(Player player, PropBase obstacle) base.InteractWithProp(player, obstacle); } } - public override void ChangeRigidbody(Rigidbody rb) - { - - } - public override void ChangePhysics(Collider collider, PhysicMaterial physicMaterial) - { - physicMaterial.bounciness = 0f; - physicMaterial.dynamicFriction = 0f; - physicMaterial.staticFriction = 0f; - - physicMaterial.bounceCombine = PhysicMaterialCombine.Maximum; - - collider.material = physicMaterial; - } } } diff --git a/Assets/SWPPT3/Scripts/SoftbodyPhysics/SoftbodyGenerator.cs b/Assets/SWPPT3/Scripts/SoftbodyPhysics/SoftbodyGenerator.cs index 43f67367..a400788c 100644 --- a/Assets/SWPPT3/Scripts/SoftbodyPhysics/SoftbodyGenerator.cs +++ b/Assets/SWPPT3/Scripts/SoftbodyPhysics/SoftbodyGenerator.cs @@ -169,6 +169,35 @@ public float PhysicsRoughness { } } + public void OnEnable() + { + Physics.ContactModifyEvent += ModificationEvent; + } + + public void OnDisable() + { + Physics.ContactModifyEvent -= ModificationEvent; + } + + public void ModificationEvent(PhysicsScene scene, NativeArray pairs) + { + for (int i = 0; i < pairs.Length; i++) + { + var pair = pairs[i]; + + var properties = pair.massProperties; + + properties.inverseMassScale = 1f; + properties.inverseInertiaScale = 1f; + properties.otherInverseMassScale = 0; + properties.otherInverseInertiaScale = 0; + + pair.massProperties = properties; + + pairs[i] = pair; + } + } + private GameObject centerOfMasObj = null; @@ -296,7 +325,7 @@ private void Awake() // Add sphere collider and rigidbody to root object - rootRB = gameObject.AddComponent(); + rootRB = gameObject.GetComponent(); rootRB.mass = 1; _rigidbodyList.Add(rootRB); @@ -470,16 +499,26 @@ public void FreeJoint() List noDupesListOfSprings = new List(); + + public void SetSlime() { + if(_physicsMaterial == null) return; _physicsMaterial.bounciness = 0f; _physicsMaterial.dynamicFriction = 0f; _physicsMaterial.staticFriction = 0f; _physicsMaterial.bounceCombine = PhysicMaterialCombine.Maximum; + if (!IsSlime) + { + FreeJoint(); + foreach (var sc in _sphereColliderArray) + { + sc.hasModifiableContacts = true; + } + } IsSlime = true; - FreeJoint(); } public void SetMetal() @@ -491,6 +530,10 @@ public void SetMetal() if (IsSlime) { FixJoint(); + foreach (var sc in _sphereColliderArray) + { + sc.hasModifiableContacts = false; + } } IsSlime = false; } @@ -504,6 +547,10 @@ public void SetRubberNonJump() if (IsSlime) { FixJoint(); + foreach (var sc in _sphereColliderArray) + { + sc.hasModifiableContacts = false; + } } IsSlime = false; } @@ -517,6 +564,10 @@ public void SetRubberJump() if (IsSlime) { FixJoint(); + foreach (var sc in _sphereColliderArray) + { + sc.hasModifiableContacts = false; + } } IsSlime = false; } @@ -541,13 +592,10 @@ public void move(Vector3 force) public void Update() { - if (IsSlime) - { - Softness = _script.Softness; - Mass = _script.Mass; - PhysicsRoughness = _script.PhysicsRoughness; - Damp = _script.Damp; - } + Softness = _script.Softness; + Mass = _script.Mass; + PhysicsRoughness = _script.PhysicsRoughness; + Damp = _script.Damp; if (DebugMode) {