Skip to content
This repository has been archived by the owner on Apr 10, 2023. It is now read-only.

Commit

Permalink
Apply SRP part 4
Browse files Browse the repository at this point in the history
Extract input and animations behaviours from Player script to single different scripts
  • Loading branch information
Kassout committed Apr 10, 2023
1 parent dc111e2 commit 1579a32
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 54 deletions.
4 changes: 1 addition & 3 deletions SOLID/SOLID/Assets/Scenes/GameScene.unity
Original file line number Diff line number Diff line change
Expand Up @@ -973,9 +973,6 @@ MonoBehaviour:
m_Name:
m_EditorClassIdentifier:
playerAnimator: {fileID: 815885425}
raycastPoint: {fileID: 1706832592}
playerMovement: {fileID: 837455035}
playerRenderer: {fileID: 815885426}
ui_window: {fileID: 614753882}
--- !u!82 &837455034
AudioSource:
Expand Down Expand Up @@ -1135,6 +1132,7 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 7169d424f5174e441a0d1fddfdc9e9ab, type: 3}
m_Name:
m_EditorClassIdentifier:
raycastPoint: {fileID: 1706832592}
--- !u!1 &844677121
GameObject:
m_ObjectHideFlags: 0
Expand Down
46 changes: 13 additions & 33 deletions SOLID/SOLID/Assets/Scripts/Player.cs
Original file line number Diff line number Diff line change
@@ -1,58 +1,38 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine;

public class Player : MonoBehaviour
{
public Animator playerAnimator;

private PlayerMovement _playerMovement;
private PlayerRenderer _playerRenderer;
private PlayerAIInteractions _playerAIInteractions;
private PlayerInput _playerInput;
private PlayerAnimations _playerAnimations;

public GameObject ui_window;

private Vector2 movementVector;

private void Start()
{
_playerAnimations = GetComponent<PlayerAnimations>();
_playerMovement = GetComponent<PlayerMovement>();
_playerRenderer = GetComponent<PlayerRenderer>();
_playerAIInteractions = GetComponent<PlayerAIInteractions>();
}
private void Update()
{
movementVector = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
movementVector.Normalize();
if (Input.GetAxisRaw("Fire1") > 0)
{
_playerAIInteractions.Interact(_playerRenderer.IsSpriteFlipped);
}
_playerInput = GetComponent<PlayerInput>();

_playerInput.OnInteractEvent += () => _playerAIInteractions.Interact(_playerRenderer.IsSpriteFlipped);
}

private void FixedUpdate()
{
MovePlayer(movementVector);
if (movementVector.magnitude > 0)
_playerMovement.MovePlayer(_playerInput.MovementInput);
_playerRenderer.RenderPlayer(_playerInput.MovementInput);
_playerAnimations.SetupAnimations(_playerInput.MovementInput);

if (_playerInput.MovementInput.magnitude > 0)
{
ui_window.SetActive(false);
}
else
{
playerAnimator.SetBool("Walk", false);
}
}

private void MovePlayer(Vector2 movementVector)
{
playerAnimator.SetBool("Walk", true);
_playerMovement.MovePlayer(movementVector);

_playerRenderer.RenderPlayer(movementVector);
}


public void ReceiveDamaged()
{
_playerRenderer.FlashRed();
Expand Down
21 changes: 11 additions & 10 deletions SOLID/SOLID/Assets/Scripts/PlayerAnimations.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerAnimations : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{

}
[SerializeField]
private Animator playerAnimator;

// Update is called once per frame
void Update()
public void SetupAnimations(Vector2 movement)
{

if (movement.magnitude > 0)
{
playerAnimator.SetBool("Walk", true);
}
else
{
playerAnimator.SetBool("Walk", false);
}
}
}
27 changes: 19 additions & 8 deletions SOLID/SOLID/Assets/Scripts/PlayerInput.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,29 @@
using System.Collections;
using System.Collections.Generic;
using System;
using UnityEngine;

public class PlayerInput : MonoBehaviour
{
// Start is called before the first frame update
void Start()
public Vector2 MovementInput { get; private set; }

public event Action OnInteractEvent;

private void Update()
{
GetInteractInput();
GetMovementInput();
}

private void GetMovementInput()
{

MovementInput = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
MovementInput.Normalize();
}

// Update is called once per frame
void Update()
private void GetInteractInput()
{

if (Input.GetAxisRaw("Fire1") > 0)
{
OnInteractEvent?.Invoke();
}
}
}

0 comments on commit 1579a32

Please sign in to comment.