Below is my CombatUI.cs file. This is used to manage the user interface when the player is inside a combat arena with another ship. The UI will display the players ship, target ship, damage indicators for each subsystem for each ship, and the hull/shield condition for each ship. I use events to update the indicators whenever the subsystems take damage. Everything works perfectly, however, the design feels flawed and cumbersome if I want to add new subsystems. Also checking the owner of the ship (Player vs AI) seems out of place, like there is a better way to implement it.
public class CombatUI
{
public Ship playerShip = null;
public Ship targetShip = null;
public GUIText player_computerCoreDamageIndicator;
public GUIText player_engineCoreDamageIndicator;
public GUIText player_weaponSystemDamageIndicator;
public GUIText player_sensorGridDamageIndicator;
public GUIText player_shieldEmittersDamageIndicator;
public GUIText player_shieldStrengthDamageIndicator;
public GUIText player_hullStrengthDamageIndicator;
public GUIText target_computerCoreDamageIndicator;
public GUIText target_engineCoreDamageIndicator;
public GUIText target_weaponSystemDamageIndicator;
public GUIText target_targetingComputerDamageIndicator;
public GUIText target_shieldEmittersDamageIndicator;
public GUIText target_shieldStrengthDamageIndicator;
public GUIText target_hullStrengthDamageIndicator;
public CombatUI(Ship playerShip, Ship targetShip)
{
this.playerShip = playerShip;
this.targetShip = targetShip;
ObserveShipSystems(playerShip);
//ObserveShipSystems(targetShip);
}
private void ObserveShipSystems(Ship ship)
{
ship.OnShieldChangeCondition += new Ship.ConditionHandler(UpdateShieldConditionIndicator);
ship.OnHullChangeCondition += new Ship.ConditionHandler(UpdateHullConditionIndicator);
ObserveShipSubsystems(ship.Subsystems);
UpdateSubsystemIndicators(ship);
UpdateShieldConditionIndicator(ship);
UpdateHullConditionIndicator(ship);
}
private void ObserveShipSubsystems(Dictionary<SubsystemType, Subsystem> subsystems)
{
foreach (KeyValuePair<SubsystemType, Subsystem> subsystem in subsystems)
{
subsystem.Value.subsystemDamaged += new Subsystem.DamageHandler(UpdateSubsystemIndicators);
}
}
private void UpdateShieldConditionIndicator(Ship ship)
{
float shieldStrength = ((ShieldEmitter)ship.Subsystems[SubsystemType.ShieldEmitter]).ShieldStrength;
player_shieldStrengthDamageIndicator.text = "SS: " + shieldStrength;
}
private void UpdateHullConditionIndicator(Ship ship)
{
float hullStrength = ((HullPlating)ship.Subsystems[SubsystemType.HullPlating]).HullStrength;
player_hullStrengthDamageIndicator.text = "HS: " + hullStrength;
}
private void UpdateSubsystemIndicators(Ship ship)
{
if (ship.Controller == Controller.Player)
{
// Prototype display for showing damage
// TODO: Change from GuiText to a GuiTexture
player_computerCoreDamageIndicator.text = "CC: " + ship.Subsystems[SubsystemType.ComputerCore].DamagePercent().ToString() + "%";
player_engineCoreDamageIndicator.text = "EC: " + ship.Subsystems[SubsystemType.EngineCore].DamagePercent().ToString() + "%";
player_weaponSystemDamageIndicator.text = "WS: " + ship.Subsystems[SubsystemType.WeaponSystem].DamagePercent().ToString() + "%";
player_shieldEmittersDamageIndicator.text = "SE: " + ship.Subsystems[SubsystemType.ShieldEmitter].DamagePercent().ToString() + "%";
player_sensorGridDamageIndicator.text = "SG: " + ship.Subsystems[SubsystemType.SensorGrid].DamagePercent().ToString() + "%";
}
else
{
target_computerCoreDamageIndicator.text = "CC: " + ship.Subsystems[SubsystemType.ComputerCore].DamagePercent().ToString() + "%";
target_engineCoreDamageIndicator.text = "EC: " + ship.Subsystems[SubsystemType.EngineCore].DamagePercent().ToString() + "%";
target_weaponSystemDamageIndicator.text = "WS: " + ship.Subsystems[SubsystemType.WeaponSystem].DamagePercent().ToString() + "%";
target_shieldEmittersDamageIndicator.text = "SE: " + ship.Subsystems[SubsystemType.ShieldEmitter].DamagePercent().ToString() + "%";
player_sensorGridDamageIndicator.text = "SG: " + ship.Subsystems[SubsystemType.SensorGrid].DamagePercent().ToString() + "%";
}
}
}






