/// <summary> /// Checks if the position is available for the device. /// </summary> /// <param name="device">The device.</param> /// <param name="x">X coordinate of the position.</param> /// <param name="y">Y coordinate of the position.</param> /// <returns>True if the position is available.</returns> public bool IsValidPosition(EnergyDevice device, int x, int y) { // check if in bounds if (x < 0 || y < 0) { return(false); } if (x + device.grid.width > grid.width || y + device.grid.height > grid.height) { return(false); } // check if area occupied for (int ix = 0; ix < device.grid.width; ++ix) { for (int iy = 0; iy < device.grid.height; ++iy) { if (GetSlot(x + ix, y + iy).attachedDevice != null) { return(false); } } } return(true); }
public void SetElementsActive(EnergyDevice device, bool active, int childIndex = -1) { for (int x = 0; x < grid.width; ++x) { for (int y = 0; y < grid.height; ++y) { var slot = GetSlot(x, y); if (slot.attachedDevice == device) { var go = grid.GetElementObject(x, y); if (go != null) { if (childIndex >= 0) { var child = go.transform.GetChild(childIndex); if (child != null) { child.gameObject.SetActive(active); } } else { go.SetActive(active); } } } } } }
public bool Remove(EnergyDevice device) { if (!attachedDevices.Remove(device)) { return(false); } device.slot = null; int x, y; if (FindPositionOfDevice(device, out x, out y)) { RemoveFromMatrix(x, y); } for (int i = 0; i < deviceEffects.Count; ++i) { device.RemoveDeviceEffects(deviceEffects[i].source); } if (DeviceRemoved != null) { DeviceRemoved(this, new EnergyDeviceSlotEventArgs { device = device, x = x, y = y }); } return(true); }
/// <summary> /// Adds the device effect to this device. /// </summary> /// <param name="device">The device that is the source of the effect.</param> /// <param name="effect">The effect data.</param> public void AddDeviceEffect(EnergyDevice device, EnergyDeviceType.DeviceEffect effect) { deviceEffects.Add(new DeviceEffect { source = device, efficiencyMultiplier = effect.efficiencyMultiplier }); }
/// <summary> /// Tries to find an available position for the device in this slot and add it to that position. /// </summary> /// <param name="device">The device.</param> /// <param name="x">X coordinate of the position of the added device.</param> /// <param name="y">Y coordinate of the position of the added device.</param> /// <returns>True if the device was added.</returns> public bool TryAddToFirstAvailablePosition(EnergyDevice device, out int x, out int y) { if (FindFirstAvailablePosition(device, out x, out y)) { AddToPosition(device, x, y); return(true); } return(false); }
/// <summary> /// Tries to add the device to the position in this slot. /// </summary> /// <param name="device">The device.</param> /// <param name="x">X coordinate of the position.</param> /// <param name="y">Y coordinate of the position.</param> /// <returns>True if the device was added.</returns> public bool TryAddToPosition(EnergyDevice device, int x, int y) { if (!IsValidPosition(device, x, y)) { return(false); } AddToPosition(device, x, y); return(true); }
/// <summary> /// Removes all effects of the device from this device. /// </summary> /// <param name="device">The device whose effects will be removed.</param> public void RemoveDeviceEffects(EnergyDevice device) { for (int i = deviceEffects.Count - 1; i >= 0; --i) { if (deviceEffects[i].source == device) { deviceEffects.RemoveAt(i); } } lastEffectTime = Time.time; }
protected void SetToMatrix(EnergyDevice device, int x, int y) { for (int ix = 0; ix < device.grid.width; ++ix) { for (int iy = 0; iy < device.grid.height; ++iy) { var slot = GetSlot(x + ix, y + iy); slot.attachedDevice = device; } } }
/// <summary> /// Adds the device effect to this slot and all attached devices. /// </summary> /// <param name="device">The device that is the source of the effect.</param> /// <param name="effect">The effect data.</param> public void AddDeviceEffect(EnergyDevice device, EnergyDeviceType.DeviceEffect effect) { deviceEffects.Add(new DeviceEffect { source = device, efficiencyMultiplier = effect.efficiencyMultiplier }); for (int i = 0; i < attachedDevices.Count; ++i) { attachedDevices[i].AddDeviceEffect(device, effect); } }
protected void RemoveFromMatrix(EnergyDevice device) { for (int x = 0; x < grid.width; ++x) { for (int y = 0; y < grid.height; ++y) { var slot = GetSlot(x, y); if (slot.attachedDevice == device) { slot.attachedDevice = null; } } } }
public bool FindPositionOfDevice(EnergyDevice device, out int x, out int y) { for (int ix = 0; ix < grid.width; ++ix) { for (int iy = 0; iy < grid.height; ++iy) { if (GetSlot(ix, iy).attachedDevice == device) { x = ix; y = iy; return(true); } } } x = y = -1; return(false); }
/// <summary> /// Finds an available position for the device in this slot. /// </summary> /// <param name="device">The device.</param> /// <param name="x">X coordinate of the found position.</param> /// <param name="y">Y coordinate of the found position.</param> /// <returns>True if a free position was found.</returns> public bool FindFirstAvailablePosition(EnergyDevice device, out int x, out int y) { // naive, inefficient for (int ix = 0; ix < grid.width; ++ix) { for (int iy = 0; iy < grid.height; ++iy) { if (IsValidPosition(device, ix, iy)) { x = ix; y = iy; return(true); } } } x = y = -1; return(false); }
/* protected because this does not check if the position is valid */ protected void AddToPosition(EnergyDevice device, int x, int y) { SetToMatrix(device, x, y); if (attachedDevices.Count == 0) { lastEffectTime = Time.time; } attachedDevices.Add(device); for (int i = 0; i < deviceEffects.Count; ++i) { device.deviceEffects.Add(deviceEffects[0]); } device.slot = this; if (DeviceAttached != null) { DeviceAttached(this, new EnergyDeviceSlotEventArgs { device = device, x = x, y = y }); } }
private void RemoveHazard(EnergyDevice hazard) { hazard.gameObject.SetActive(false); hazard.SetTarget(null); }
private void AddHazard(EnergyDevice hazard, EnergyDeviceSlot target) { hazard.SetTarget(target); hazard.gameObject.SetActive(true); }