Skip to content

Commit

Permalink
chore: rename 'States' and redefinition some functions in Player
Browse files Browse the repository at this point in the history
  • Loading branch information
yeonjae11 committed Nov 11, 2024
1 parent 1107442 commit 84bba5a
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 34 deletions.
59 changes: 40 additions & 19 deletions Assets/SWPPT3/Scripts/Main/PlayerLogic/Player.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ namespace SWPPT3.Main.PlayerLogic
{
public class Player : MonoBehaviour
{
private States _currentState = States.Slime;
private PlayerStates _currentState = PlayerStates.Slime;
private Vector2 _inputMovement;
private Vector3 _moveDirection;
private float _moveSpeed = 4f;

private bool _isGameOver = false;

private bool _isJumping;
private int _isGrounded;
private bool _isHoldingJump;

[SerializeField]
Expand All @@ -26,22 +26,24 @@ public class Player : MonoBehaviour
private Collider _collider;
private float _jumpForce = 2f;

public Dictionary<States, int> Item = new()
// private HashSet<GameObject> _colliderObjects= new HashSet<GameObject>();

public Dictionary<PlayerStates, int> Item = new()
{
{ States.Slime, 0 },
{ States.Metal, 0 },
{ States.Rubber, 0 },
{ PlayerStates.Slime, 0 },
{ PlayerStates.Metal, 0 },
{ PlayerStates.Rubber, 0 },
};

private PlayerState PlayerState => _playerStates[_currentState];

private PlayerInputActions _inputActions;

private readonly Dictionary<States, PlayerState> _playerStates = new()
private readonly Dictionary<PlayerStates, PlayerState> _playerStates = new()
{
{ States.Metal, new MetalState() },
{ States.Rubber, new RubberState() },
{ States.Slime, new SlimeState() },
{ PlayerStates.Metal, new MetalState() },
{ PlayerStates.Rubber, new RubberState() },
{ PlayerStates.Slime, new SlimeState() },
};

private void Awake()
Expand All @@ -50,6 +52,7 @@ private void Awake()
_collider = GetComponent<Collider>();
_physicMaterial = _collider.material;
_isHoldingJump = false;
_isGrounded = 0;
_inputActions = new PlayerInputActions();
_inputActions.PlayerActions.Move.performed += OnMove;
_inputActions.PlayerActions.Move.canceled += OnMove;
Expand Down Expand Up @@ -88,7 +91,7 @@ private void Update()
transform.Translate(Vector3.forward * (_moveSpeed * Time.deltaTime));
}

if (_currentState == States.Rubber && !IsGrounded() && _isHoldingJump)
if (_currentState == PlayerStates.Rubber && !IsGrounded() && _isHoldingJump)
{
_physicMaterial.bounciness = 1.0f;
_physicMaterial.bounceCombine = PhysicMaterialCombine.Maximum;
Expand Down Expand Up @@ -116,7 +119,7 @@ public void OnJump(InputAction.CallbackContext context)
private void OnJumpCanceled(InputAction.CallbackContext context)
{
_isHoldingJump = false;
if (_currentState == States.Rubber)
if (_currentState == PlayerStates.Rubber)
{
_physicMaterial.bounciness = 0.5f;
_physicMaterial.bounceCombine = PhysicMaterialCombine.Maximum;
Expand All @@ -125,26 +128,37 @@ private void OnJumpCanceled(InputAction.CallbackContext context)
}
}

// private bool IsGrounded()
// {
// foreach (GameObject colliderObject in _colliderObjects)
// {
// if (colliderObject.CompareTag("Ground"))
// {
// return true;
// }
// }
// return false;
// }
private bool IsGrounded()
{
return Physics.Raycast(transform.position, Vector3.down, 1f);
return _isGrounded > 0;
}

public void OnChangeState(InputAction.CallbackContext context)
{
if (_isGameOver) return;
string keyPressed = context.control.displayName;

States newState = keyPressed switch
PlayerStates newState = keyPressed switch
{
"1" => States.Slime,
"2" => States.Metal,
"3" => States.Rubber,
"1" => PlayerStates.Slime,
"2" => PlayerStates.Metal,
"3" => PlayerStates.Rubber,
_ => _currentState
};
if (newState == States.Slime || Item[newState] > 0)
if (newState == PlayerStates.Slime || Item[newState] > 0)
{
if(newState != States.Slime) Item[newState]--;
if(newState != PlayerStates.Slime) Item[newState]--;
_currentState = newState;
PlayerState.ChangeRigidbody(_rb);
PlayerState.ChangePhysics(_collider, _physicMaterial);
Expand All @@ -165,6 +179,13 @@ private void OnCollisionEnter(Collision collision)
{
InteractWithProp(obstacle);
}
// _colliderObjects.Add(collision.gameObject);
_isGrounded += collision.gameObject.CompareTag("Ground") ? 1 : 0;
}

private void OnCollisionExit(Collision collision)
{
_isGrounded -= collision.gameObject.CompareTag("Ground") ? 1 : 0;
}

public void GameOver()
Expand Down
4 changes: 0 additions & 4 deletions Assets/SWPPT3/Scripts/Main/PlayerLogic/State/MetalState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,6 @@ public override void ChangeRigidbody(Rigidbody rb)

public override void ChangePhysics(Collider collider, PhysicMaterial physicMaterial)
{
if (physicMaterial == null)
{
physicMaterial = new PhysicMaterial();
}
physicMaterial.bounciness = 0f;
physicMaterial.dynamicFriction = 0f;
physicMaterial.staticFriction = 0f;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace SWPPT3.Main.PlayerLogic.State
{
public enum States
public enum PlayerStates
{
Slime = 0,
Metal,
Expand Down
4 changes: 0 additions & 4 deletions Assets/SWPPT3/Scripts/Main/PlayerLogic/State/RubberState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,6 @@ public override void ChangeRigidbody(Rigidbody rb)
}
public override void ChangePhysics(Collider collider, PhysicMaterial physicMaterial)
{
if (physicMaterial == null)
{
physicMaterial = new PhysicMaterial();
}
physicMaterial.bounciness = 0.5f;
physicMaterial.dynamicFriction = 0f;
physicMaterial.staticFriction = 0f;
Expand Down
4 changes: 0 additions & 4 deletions Assets/SWPPT3/Scripts/Main/PlayerLogic/State/SlimeState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@ public override void ChangeRigidbody(Rigidbody rb)
}
public override void ChangePhysics(Collider collider, PhysicMaterial physicMaterial)
{
if (physicMaterial == null)
{
physicMaterial = new PhysicMaterial();
}
physicMaterial.bounciness = 0f;
physicMaterial.dynamicFriction = 0f;
physicMaterial.staticFriction = 0f;
Expand Down
2 changes: 1 addition & 1 deletion Assets/SWPPT3/Scripts/Main/Prop/ItemBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace SWPPT3.Main.Prop
{
public class ItemBox : StatelessProp
{
public States ItemState;
public PlayerStates ItemState;

public override void InteractWithPlayer()
{
Expand Down
2 changes: 1 addition & 1 deletion Assets/SWPPT3/Scripts/Main/Prop/PropBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public virtual void InteractWithPlayer()
{

}
public virtual void InteractWithPlayer(States state)
public virtual void InteractWithPlayer(PlayerStates state)
{

}
Expand Down

0 comments on commit 84bba5a

Please sign in to comment.