Hey all,
I haven’t done much Object Oriented Software/Scripting before.
I usually add a bunch of small scripts to components by what they do.
That is fine but this “Generic_Unit_Script” is getting big!
Is there any way I can break it up with inheritance or composition?
Here is a sample of what its doing now. And this isn’t even including the function definitions or their sub functions. Or their external dependencies. I don’t know how to make it into a small script because the AI is related to Movement, and the statistics are related to the AI. And selection is related to the unit. I’m starting to get lost in the script. Are there any strategies you guys use when you find your script is too big to handle?
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Generic_Unit_Script : MonoBehaviour {
//This is an area where animations needs to be implimented;
public AnimationClip idle_animation;
public AnimationClip attack_animation;
public AnimationClip move_animation;
public AnimationClip Death_animation;
//a working definition for two types of units
public enum BaseAttackType {Melee, Shooting}
public BaseAttackType AttackType = BaseAttackType.Melee;
public bool IsCPU_Constrolled = true;
//Effects scripts and spawn points
//includes effects based on variable name
public GameObject waypointPf;
public GameObject muzzleflashPF;
public GameObject bulletPF;
public GameObject bulletcasePF;
public Transform bulletcaseSpawn;
public Transform bulletSpawnPt;
//Other Components Required
CharacterController controller;
//this script uses a single waypoint to tell unit where to go
public Transform current_Waypoint;
//The enemy is a waypoint with direct motion on the enemy
public GameObject enemy;
//Unit Statistics (Under The Hood)
public float range = 4.0f;
public float line_of_sight = 5.0f;
public float firing_delay = 0.1f;
public float time_last_shot_fired =0;
public float Damage = 1;
public float speed = 5.0f;
public float step = 5.0f;
//AI State Machine
//Help:
//Moving: Basically moves to a waypoint
//Idle: Looks around for enemies
//Attack: Should look around while moving attacking the first enemy it sees
//Attack_Target: Attacks the current target ignoring everything in the path.
//Sleep: Ignores all line of sight but if woken up the unit will start attacking
//Dead: The unit will play death animation then disappear
public enum UnitStates {Moving, Idle, Attack_Move, Attack_Target, Sleep, Dead};
public UnitStates state = UnitStates.Idle;
//Graphics
public MeshRenderer SelectionRectile;
bool selected = false;
//Health Bar Script;
public bool Can_See_Health = true;
private Health_Bar_Script HealthBar;
public Transform healthbarpos;
//Required for selection of unit with point and click, needed here for capture of click.
public Renderer UnitMesh;
//Line Of Sight Script
//This must be linked for the unit to see enemies
public LineOfSight SightScript;
// Use this for initialization
void Start () {
Death_animation.wrapMode = WrapMode.Once;
controller = GetComponent<CharacterController>();
HealthBar = GetComponent<Health_Bar_Script>();
}
// Update is called once per frame
void Update () {
// Updates attack delay;
time_last_shot_fired = time_last_shot_fired + Time.deltaTime;
if ( current_Waypoint != null (state == UnitStates.Moving || state == UnitStates.Attack_Move))
{
Movement();
if (state == UnitStates.Attack_Move)
{
}
}
else if(state == UnitStates.Attack_Target)
{
Attack_Current_Target();
}
else if (state != UnitStates.Dead)
{
state = UnitStates.Idle;
}
if (state == UnitStates.Idle)
{
RunAwakeState();
}
DrawHealth();
DetectSelectionBox();
CheckDeath();
}
}