Skip to content

Commit

Permalink
chore: change player mover, player...
Browse files Browse the repository at this point in the history
  • Loading branch information
yeonjae11 committed Dec 17, 2024
1 parent a407936 commit 4b8af39
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 77 deletions.
34 changes: 16 additions & 18 deletions Assets/SWPPT3/Scripts/Main/PlayerLogic/Player.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using SWPPT3.Main.Prop;
using UnityEngine.InputSystem;
using SWPPT3.Main.PlayerLogic.State;
using SWPPT3.SoftbodyPhysics;

namespace SWPPT3.Main.PlayerLogic
{
Expand All @@ -15,37 +16,25 @@ public class Player : MonoBehaviour
private int slimeCount , metalCount, rubberCount;
public Dictionary<PlayerStates, int> 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, PlayerState> _playerStates = new()
{
{ PlayerStates.Metal, new MetalState() },
{ 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<SoftbodyGenerator>();
if (Item == null)
{
Item = new Dictionary<PlayerStates, int>
Expand All @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions Assets/SWPPT3/Scripts/Main/PlayerLogic/PlayerMover.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ private void Update()
}
if (_player.CurrentState == PlayerStates.Rubber && _groundedObjects.Count == 0 && _isHoldingJump)
{
_player.SetBounciness(1.0f);
// _player.SetBounciness(1.0f);
}
}

Expand Down Expand Up @@ -135,7 +135,7 @@ private void HandleJump()
_isHoldingJump = true;
if (_player.CurrentState == PlayerStates.Rubber)
{
_player.SetBounciness(1.0f);
// _player.SetBounciness(1.0f);
}
}

Expand All @@ -144,7 +144,7 @@ private void HandleJumpCancel()
_isHoldingJump = false;
if (_player.CurrentState == PlayerStates.Rubber)
{
_player.SetBounciness(0.5f);
// _player.SetBounciness(0.5f);
}
}

Expand Down
16 changes: 0 additions & 16 deletions Assets/SWPPT3/Scripts/Main/PlayerLogic/State/MetalState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
3 changes: 0 additions & 3 deletions Assets/SWPPT3/Scripts/Main/PlayerLogic/State/PlayerState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

}
14 changes: 0 additions & 14 deletions Assets/SWPPT3/Scripts/Main/PlayerLogic/State/RubberState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
14 changes: 0 additions & 14 deletions Assets/SWPPT3/Scripts/Main/PlayerLogic/State/SlimeState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
66 changes: 57 additions & 9 deletions Assets/SWPPT3/Scripts/SoftbodyPhysics/SoftbodyGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ModifiableContactPair> 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;
Expand Down Expand Up @@ -296,7 +325,7 @@ private void Awake()


// Add sphere collider and rigidbody to root object
rootRB = gameObject.AddComponent<Rigidbody>();
rootRB = gameObject.GetComponent<Rigidbody>();
rootRB.mass = 1;
_rigidbodyList.Add(rootRB);

Expand Down Expand Up @@ -470,16 +499,26 @@ public void FreeJoint()

List<Vector2Int> noDupesListOfSprings = new List<Vector2Int>();



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()
Expand All @@ -491,6 +530,10 @@ public void SetMetal()
if (IsSlime)
{
FixJoint();
foreach (var sc in _sphereColliderArray)
{
sc.hasModifiableContacts = false;
}
}
IsSlime = false;
}
Expand All @@ -504,6 +547,10 @@ public void SetRubberNonJump()
if (IsSlime)
{
FixJoint();
foreach (var sc in _sphereColliderArray)
{
sc.hasModifiableContacts = false;
}
}
IsSlime = false;
}
Expand All @@ -517,6 +564,10 @@ public void SetRubberJump()
if (IsSlime)
{
FixJoint();
foreach (var sc in _sphereColliderArray)
{
sc.hasModifiableContacts = false;
}
}
IsSlime = false;
}
Expand All @@ -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)
{
Expand Down

0 comments on commit 4b8af39

Please sign in to comment.