public override void AddElement(AUnitElementModel element) { base.AddElement(element); // A facility that is in Idle without being part of a unit might attempt something it is not yet prepared for FacilityModel facility = element as FacilityModel; D.Assert(facility.CurrentState != FacilityState.Idling, "{0} is adding {1} while Idling.".Inject(FullName, facility.FullName)); }
public override void AddElement(AUnitElementModel element) { base.AddElement(element); element.Command = this; if (HQElement != null) { _formationGenerator.RegenerateFormation(); // Bases simply regenerate the formation when adding an element } }
/// <summary> /// Adds the Element to this Command including parenting if needed. /// </summary> /// <param name="element">The Element to add.</param> public virtual void AddElement(AUnitElementModel element) { D.Assert(!Elements.Contains(element), "{0} attempting to add {1} that is already present.".Inject(FullName, element.FullName)); D.Assert(!element.IsHQElement, "{0} adding element {1} already designated as the HQ Element.".Inject(FullName, element.FullName)); // elements should already be enabled when added to a Cmd as that is commonly their state when transferred during runtime D.Assert((element as MonoBehaviour).enabled, "{0} is not yet enabled.".Inject(element.FullName)); element.onDeathOneShot += OnSubordinateElementDeath; Elements.Add(element); Data.AddElement(element.Data); Transform parentTransform = _transform.parent; if (element.Transform.parent != parentTransform) { element.Transform.parent = parentTransform; // local position, rotation and scale are auto adjusted to keep ship unchanged in worldspace } //TODO consider changing HQElement }
public virtual void RemoveElement(AUnitElementModel element) { element.onDeathOneShot -= OnSubordinateElementDeath; bool isRemoved = Elements.Remove(element); isRemoved = isRemoved && Data.RemoveElement(element.Data); D.Assert(isRemoved, "{0} not found.".Inject(element.FullName)); if (Elements.Count == Constants.Zero) { D.Assert(Data.UnitHealth <= Constants.ZeroF, "{0} UnitHealth error.".Inject(FullName)); KillCommand(); } }
protected internal virtual void PositionElementInFormation(AUnitElementModel element, Vector3 stationOffset) { element.Transform.position = HQElement.Position + stationOffset; //D.Log("{0} positioned at {1}, offset by {2} from {3} at {4}.", // element.FullName, element.Transform.position, stationOffset, HQElement.FullName, HQElement.Transform.position); }
protected virtual void OnHQElementChanging(AUnitElementModel newHQElement) { Arguments.ValidateNotNull(newHQElement); if (HQElement != null) { HQElement.IsHQElement = false; } if (!Elements.Contains(newHQElement)) { // the player will typically select/change the HQ element of a Unit from the elements already present in the unit D.Warn("{0} assigned HQElement {1} that is not already present in Unit.", FullName, newHQElement.FullName); AddElement(newHQElement); } }
protected override void PositionElementInFormation(AUnitElementModel element, Vector3 stationOffset) { ShipModel ship = element as ShipModel; if (ship.transform.rigidbody.isKinematic) { // if kinematic, this ship came directly from the FleetCreator so it needs to get its initial position // instantly places the ship in its proper position before assigning it to a station so the station will find it 'onStation' // during runtime, ships that already exist (aka aren't kinematic) will move under power to their station when they are idle base.PositionElementInFormation(element, stationOffset); // as ships were temporarily set to be immune to physics in FleetUnitCreator. Now that they are properly positioned, change them back ship.Transform.rigidbody.isKinematic = false; } IFormationStation shipStation = ship.Data.FormationStation; if (shipStation == null) { // the ship does not yet have a formation station so find or make one var unusedStations = _formationStations.Where(fst => fst.AssignedShip == null); if (!unusedStations.IsNullOrEmpty()) { // there are unused stations so assign the ship to one of them //D.Log("{0} is being assigned an existing but unassigned FormationStation.", ship.FullName); shipStation = unusedStations.First(); shipStation.AssignedShip = ship; ship.Data.FormationStation = shipStation; } else { // there are no unused stations so make a new one and assign the ship to it //D.Log("{0} is adding a new FormationStation.", ship.FullName); shipStation = UnitFactory.Instance.MakeFormationStationInstance(stationOffset, this); shipStation.AssignedShip = ship; ship.Data.FormationStation = shipStation; _formationStations.Add(shipStation); } } else { //D.Log("{0} already has a FormationStation.", ship.FullName); } //D.Log("{0} FormationStation assignment offset position = {1}.", ship.FullName, stationOffset); shipStation.StationOffset = stationOffset; }
// A fleetCmd causes heading and speed changes to occur by issuing orders to // ships, not by directly telling ships to modify their speed or heading. As such, // the ChangeHeading(), ChangeSpeed() and AllStop() methods have been removed. protected override void OnHQElementChanging(AUnitElementModel newHQElement) { base.OnHQElementChanging(newHQElement); _navigator.OnHQElementChanging(HQElement, newHQElement as ShipModel); }
public override void RemoveElement(AUnitElementModel element) { base.RemoveElement(element); // remove the formationStation from the ship and the ship from the FormationStation var ship = element as ShipModel; var shipFst = ship.Data.FormationStation; shipFst.AssignedShip = null; ship.Data.FormationStation = null; if (!this.IsAlive) { // fleetCmd has died return; } if (ship == HQElement) { // HQ Element has left HQElement = SelectHQElement(); } }
public override void AddElement(AUnitElementModel element) { base.AddElement(element); ShipModel ship = element as ShipModel; //D.Log("{0}.CurrentState = {1} when being added to {2}.", ship.FullName, ship.CurrentState.GetName(), FullName); // fleets have formation stations (or create them) and allocate them to new ships. Ships should not come with their own D.Assert(ship.Data.FormationStation == null, "{0} should not yet have a FormationStation.".Inject(ship.FullName)); ship.Command = this; if (HQElement != null) { // regeneration of a formation requires a HQ element var unusedFormationStations = _formationStations.Where(fst => fst.AssignedShip == null); if (!unusedFormationStations.IsNullOrEmpty()) { var unusedFst = unusedFormationStations.First(); ship.Data.FormationStation = unusedFst; unusedFst.AssignedShip = ship; } else { // there are no empty formation stations so regenerate the whole formation _formationGenerator.RegenerateFormation(); //TODO instead, create a new one at the rear of the formation } } }