Question by
Miki_505 · Feb 14, 2020 at 01:07 PM ·
scripting problemoptimizationvisual studiooopsolid
Refactoring code for S.O.L.I.D principles
I want to learn SOLID but its really hard for me. I don' t know where to start. After watching materials and still not being able to refactor my own code I decided to ask here. How would you refactor such a code. Seeing an example of someone more experienced would mean a lot to me.
Thanks in advice :)
public class Player : MonoBehaviour
{
[SerializeField] [Range(0f, 1000f)] float movementSpeed = 654;
[SerializeField] [Range(0f, 60f)] float playerControlledJumpForce = 30.3f;
[SerializeField] [Range(1f, 40f)] float timesJumpForceAtFrameIsSmaller = 31.25f;
[SerializeField] [Range(0f, 60f)] float fromEnemyJumpForce = 15.9f;
[SerializeField] int maxJumps = 1;
[SerializeField] [Range (0f, 1f)] float minTimeBetweenJumps = 0.005f;
float playerAtFrameControlledJumpForce;
//private IJump;
private int jumpsLeft;
private bool isJumping = false;
float lastJumpTime = 0;
new Rigidbody rigidbody;
SessionManager sessionManager;
// Start is called before the first frame update
void Start()
{
sessionManager = FindObjectOfType<SessionManager>();
rigidbody = GetComponent<Rigidbody>();
jumpsLeft = maxJumps;
playerAtFrameControlledJumpForce = playerControlledJumpForce / timesJumpForceAtFrameIsSmaller;
}
// Update is called once per frame
void Update()
{
PlayerControlledJump();
HorizontalMovement();
}
private void Jump(float jumpForce)
{
Vector3 jumpVelocityToAdd = new Vector3(rigidbody.velocity.x, jumpForce,0);
rigidbody.velocity += jumpVelocityToAdd;
}
private void PlayerControlledJump()
{
if (Input.GetKeyDown(KeyCode.Space)
&& jumpsLeft > 0)
{
#region
if (!(Time.time - lastJumpTime >= minTimeBetweenJumps))
{
return;
}
lastJumpTime = Time.time;
#endregion
jumpsLeft--;
isJumping = true;
Jump(playerControlledJumpForce);
}
if (Input.GetKey(KeyCode.Space) && isJumping)
{
Jump(playerAtFrameControlledJumpForce);
}
if (Input.GetKeyUp(KeyCode.Space) || rigidbody.velocity.y < 0f)
{
isJumping = false;
}
}
private void HorizontalMovement() // TODO, take it out to separete class for SOLID principles
{
float movementScalar = movementSpeed * Time.deltaTime;
rigidbody.velocity = new Vector3(Input.GetAxisRaw("Horizontal") * movementScalar, rigidbody.velocity.y, rigidbody.velocity.z);
}
private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.tag == "Enemy")
{
GameOver();
}
if(collision.gameObject.tag == "DeathCollider")
{
Jump(fromEnemyJumpForce);
}
if (collision.gameObject.layer == 8)
{
jumpsLeft = maxJumps;
}
else if(collision.gameObject.layer == 9)//Zero Level layer
{
GameOver();
}
}
private void OnCollisionExit(Collision collision)
{
if(collision.gameObject.layer == 8) // Ground layer
{
jumpsLeft = 0;
}
}
private void GameOver()
{
Debug.Log("GAME OVER");
sessionManager.ReloadCurrentScene();
Destroy(gameObject);
//TODO proceed game over
}
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612223324im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Disable a script when its not hit by raycast. still uses 17 to 20% in profiler 0 Answers
Can this script be better optimized? 1 Answer
How to display random questions without repeating the previous? 2 Answers
How do I make a coding sheet that's not in visual studios? 0 Answers
A simple shoting code that make lags 1 Answer