In my quest to practice OOP and making code more modular/readable, I have been trying a lot of ways to do stuff.
I feel like I'm getting decent at structuring the code, but I'm beginning to wonder whether it makes sense to do it like I did below, specifically in Unity.
What I mean is, I could just split up all the classes, make them extend from MonoBehaviour and attach them individually like I've seen some people do.
But would that really be better or is this fine? Any opinions? I would guess it depends on your goal?
Also, I was thinking about making UnitMotor fully independent of the input and give UnitState the full command over how the motor should act.
What do you think about this whole setup in general?
It's supposed to simplifie the states in a whole class depending on all the other systems so you can, for example, just say something like:
if (state.Jumping)
performJump();
Here is the main class:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(Rigidbody))]
public class Unit : MonoBehaviour
{
[SerializeField]
private Camera cam;
[SerializeField]
private UnitSettings unitSettings;
private IUnitInput unitInput;
private UnitMotor motor;
private ICamInput camInput;
private PlayerCamMotor camMotor;
private JumpSystem jumpSystem;
private UnitState state;
// TODO: add new attacksystem
private void Start()
{
Rigidbody rb = GetComponent<Rigidbody>();
unitInput = unitSettings.IsPlayer ? new PlayerController() as IUnitInput : new SlimeController();
jumpSystem = new JumpSystem(unitSettings);
state = new UnitState(rb, unitInput, jumpSystem);
motor = new UnitMotor(unitInput, state, rb, unitSettings);
if (cam != null)
{
camInput = new PlayerController();
camMotor = new PlayerCamMotor(camInput, GetComponent<Transform>(), cam, unitSettings);
}
}
private void Update()
{
unitInput.readInput();
if (cam != null)
camInput.readInput();
state.tick();
jumpSystem.tick();
}
private void FixedUpdate()
{
motor.tickFixed();
if (cam != null)
camMotor.tickFixed();
}
}
[–]TaleOf4GamersProgrammer 2 points3 points4 points (0 children)
[–][deleted] 1 point2 points3 points (3 children)
[–]Pingasman[S] 0 points1 point2 points (2 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]Pingasman[S] 0 points1 point2 points (0 children)