2020-06-01 03:11:04 +01:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using System.Reflection;
|
2020-06-02 07:01:03 +01:00
|
|
|
|
using TMPro;
|
2020-06-01 03:11:04 +01:00
|
|
|
|
using UnityEngine;
|
2020-06-02 07:01:03 +01:00
|
|
|
|
using UnityEngine.EventSystems;
|
2020-06-01 03:11:04 +01:00
|
|
|
|
|
|
|
|
|
|
public class ColonyTab : MonoBehaviour
|
|
|
|
|
|
{
|
2020-06-02 07:01:03 +01:00
|
|
|
|
public TextMeshProUGUI Title;
|
2020-06-01 03:11:04 +01:00
|
|
|
|
public PropertiesPanel General;
|
|
|
|
|
|
public PropertiesPanel Inventory;
|
|
|
|
|
|
public PropertiesPanel Details;
|
2020-06-02 07:01:03 +01:00
|
|
|
|
public ContextMenu ContextMenu;
|
|
|
|
|
|
|
2020-06-01 03:11:04 +01:00
|
|
|
|
[HideInInspector]
|
2020-06-02 07:01:03 +01:00
|
|
|
|
public GameContext Context
|
|
|
|
|
|
{
|
|
|
|
|
|
get => _context;
|
|
|
|
|
|
set
|
|
|
|
|
|
{
|
|
|
|
|
|
_context = value;
|
|
|
|
|
|
General.Context = value;
|
|
|
|
|
|
Inventory.Context = value;
|
|
|
|
|
|
Details.Context = value;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2020-06-01 03:11:04 +01:00
|
|
|
|
|
|
|
|
|
|
private Guid _selectedColony;
|
2020-06-02 07:01:03 +01:00
|
|
|
|
private GameContext _context;
|
2020-06-05 04:16:33 +01:00
|
|
|
|
private Hardpoint _selectedHardpoint;
|
2020-06-01 03:11:04 +01:00
|
|
|
|
|
|
|
|
|
|
public void Open(Guid colony)
|
|
|
|
|
|
{
|
|
|
|
|
|
_selectedColony = colony;
|
2020-06-02 07:01:03 +01:00
|
|
|
|
_selectedHardpoint = null;
|
|
|
|
|
|
|
|
|
|
|
|
var entity = Context.Cache.Get<Entity>(_selectedColony);
|
2020-06-04 01:36:55 +01:00
|
|
|
|
|
|
|
|
|
|
Title.text = entity.Name;
|
|
|
|
|
|
|
|
|
|
|
|
UpdateGeneral(entity);
|
|
|
|
|
|
|
|
|
|
|
|
entity.OnInventoryUpdate += () => UpdateInventory(entity);
|
|
|
|
|
|
UpdateInventory(entity);
|
2020-06-02 07:01:03 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void OnDisable()
|
|
|
|
|
|
{
|
|
|
|
|
|
var entity = Context.Cache.Get<Entity>(_selectedColony);
|
|
|
|
|
|
entity.ClearInventoryListeners();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-06-04 01:36:55 +01:00
|
|
|
|
public void UpdateGeneral(Entity entity)
|
|
|
|
|
|
{
|
2020-06-01 03:11:04 +01:00
|
|
|
|
General.Clear();
|
2020-06-04 01:36:55 +01:00
|
|
|
|
|
|
|
|
|
|
General.AddProperty("Capacity", () => $"{entity.OccupiedCapacity}/{entity.Capacity:0}");
|
2020-06-01 03:11:04 +01:00
|
|
|
|
General.AddProperty("Mass", () => $"{entity.Mass.SignificantDigits(Context.GlobalData.SignificantDigits)}");
|
|
|
|
|
|
General.AddProperty("Temperature", () => $"{entity.Temperature:0}°K");
|
|
|
|
|
|
General.AddProperty("Energy", () => $"{entity.Energy:0}/{entity.GetBehaviors<Reactor>().First().Capacitance:0}");
|
|
|
|
|
|
General.AddProperty("Population", () => $"{entity.Population}");
|
2020-06-04 01:36:55 +01:00
|
|
|
|
var personalityList = General.AddList("Personality");
|
|
|
|
|
|
foreach (var attribute in entity.Personality.Keys)
|
|
|
|
|
|
personalityList.AddPersonalityProperty(Context.Cache.Get<PersonalityAttribute>(attribute),
|
|
|
|
|
|
() => entity.Personality[attribute]);
|
|
|
|
|
|
|
2020-06-01 03:11:04 +01:00
|
|
|
|
General.RefreshValues();
|
2020-06-04 01:36:55 +01:00
|
|
|
|
}
|
2020-06-01 03:11:04 +01:00
|
|
|
|
|
2020-06-04 01:36:55 +01:00
|
|
|
|
public void UpdateInventory(Entity entity)
|
|
|
|
|
|
{
|
|
|
|
|
|
Inventory.Clear();
|
|
|
|
|
|
|
|
|
|
|
|
var corporation = Context.Cache.Get<Corporation>(entity.Corporation);
|
|
|
|
|
|
var blueprints = corporation.UnlockedBlueprints.Select(id => Context.Cache.Get<BlueprintData>(id));
|
|
|
|
|
|
var hull = Context.Cache.Get<Gear>(entity.Hull);
|
|
|
|
|
|
var hullData = Context.Cache.Get<HullData>(hull.Data);
|
|
|
|
|
|
|
2020-06-05 04:16:33 +01:00
|
|
|
|
// Create item data mapping for cargo
|
2020-06-02 07:01:03 +01:00
|
|
|
|
var cargoData = entity.Cargo.ToDictionary(id => _context.Cache.Get<ItemInstance>(id),
|
|
|
|
|
|
id => Context.Cache.Get<ItemData>(_context.Cache.Get<ItemInstance>(id).Data));
|
2020-06-04 01:36:55 +01:00
|
|
|
|
|
|
|
|
|
|
|
2020-06-05 04:16:33 +01:00
|
|
|
|
Inventory.AddSection("Gear");
|
|
|
|
|
|
|
|
|
|
|
|
// Skip first hardpoint, that one is always the hull
|
|
|
|
|
|
foreach (var hardpoint in entity.Hardpoints.Skip(1))
|
2020-06-02 07:01:03 +01:00
|
|
|
|
{
|
2020-06-04 01:36:55 +01:00
|
|
|
|
// These are the items in the inventory which can be equipped to the current hardpoint
|
2020-06-02 07:01:03 +01:00
|
|
|
|
var matchingCargoGear = cargoData
|
2020-06-04 01:36:55 +01:00
|
|
|
|
.Where(x =>
|
2020-06-05 04:16:33 +01:00
|
|
|
|
x.Value is GearData gearData &&
|
|
|
|
|
|
gearData.HardpointType == hardpoint.HardpointData.Type)
|
2020-06-04 01:36:55 +01:00
|
|
|
|
.Select(x => x.Key as Gear)
|
2020-06-02 07:01:03 +01:00
|
|
|
|
.ToArray();
|
2020-06-05 04:16:33 +01:00
|
|
|
|
|
|
|
|
|
|
// Hardpoint has no gear assigned to it
|
|
|
|
|
|
if (hardpoint.Gear == null)
|
2020-06-02 07:01:03 +01:00
|
|
|
|
{
|
2020-06-05 04:16:33 +01:00
|
|
|
|
// Check whether we're currently building some gear for this hardpoint
|
|
|
|
|
|
var incompleteGear = entity.IncompleteGear.Keys
|
|
|
|
|
|
.Select(id => Context.Cache.Get<Gear>(id))
|
|
|
|
|
|
.FirstOrDefault(g => g.ItemData.HardpointType == hardpoint.HardpointData.Type);
|
2020-06-04 01:36:55 +01:00
|
|
|
|
|
2020-06-05 04:16:33 +01:00
|
|
|
|
// We're building something for this hardpoint, show a progress bar!
|
|
|
|
|
|
if (incompleteGear != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
Inventory.AddProgressField(incompleteGear.Name, () =>
|
2020-06-02 07:01:03 +01:00
|
|
|
|
{
|
2020-06-05 04:16:33 +01:00
|
|
|
|
var productionTime = _context.Cache.Get<BlueprintData>(incompleteGear.Blueprint).ProductionTime;
|
|
|
|
|
|
return (productionTime - (float) entity.IncompleteGear[incompleteGear.ID]) / productionTime;
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
else // We're not building anything
|
|
|
|
|
|
{
|
|
|
|
|
|
// These are the blueprints we could build onto this hardpoint directly
|
|
|
|
|
|
var matchingBlueprints = blueprints.Where(bp =>
|
|
|
|
|
|
bp.FactoryItem == Guid.Empty &&
|
|
|
|
|
|
Context.Cache.Get<ItemData>(bp.Item) is GearData gearData &&
|
|
|
|
|
|
gearData.HardpointType == hardpoint.HardpointData.Type).ToArray();
|
|
|
|
|
|
|
|
|
|
|
|
Action<PointerEventData> onClick = null;
|
|
|
|
|
|
if (matchingCargoGear.Any() || matchingBlueprints.Any())
|
|
|
|
|
|
onClick = data =>
|
2020-06-02 07:01:03 +01:00
|
|
|
|
{
|
2020-06-05 04:16:33 +01:00
|
|
|
|
if (data.button == PointerEventData.InputButton.Left)
|
|
|
|
|
|
PopulateDetails(entity, matchingBlueprints);
|
|
|
|
|
|
|
|
|
|
|
|
else if (data.button == PointerEventData.InputButton.Right)
|
2020-06-02 07:01:03 +01:00
|
|
|
|
{
|
2020-06-05 04:16:33 +01:00
|
|
|
|
ContextMenu.gameObject.SetActive(true);
|
|
|
|
|
|
ContextMenu.Clear();
|
|
|
|
|
|
if (matchingCargoGear.Any())
|
|
|
|
|
|
ContextMenu.AddDropdown("Install", matchingCargoGear
|
|
|
|
|
|
.Select<Gear, (string, Action, bool)>(gear => (gear.Name, () => entity.Equip(gear, hardpoint), true)));
|
|
|
|
|
|
if (matchingBlueprints.Any())
|
|
|
|
|
|
ContextMenu.AddDropdown("Build", matchingBlueprints
|
|
|
|
|
|
.Select<BlueprintData, (string, Action, bool)>(blueprint =>
|
|
|
|
|
|
(blueprint.Name, () =>
|
|
|
|
|
|
entity.Build(blueprint, 1, _context.Cache.Get<ItemData>(blueprint.Item).Name, true),
|
|
|
|
|
|
entity.GetBlueprintIngredients(blueprint, out _, out _))));
|
|
|
|
|
|
ContextMenu.Show();
|
2020-06-02 07:01:03 +01:00
|
|
|
|
}
|
2020-06-05 04:16:33 +01:00
|
|
|
|
};
|
|
|
|
|
|
Inventory.AddProperty($"Empty {Enum.GetName(typeof(HardpointType), hardpoint.HardpointData.Type)} Hardpoint", null, onClick, matchingBlueprints.Any());
|
|
|
|
|
|
}
|
2020-06-02 07:01:03 +01:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2020-06-05 04:16:33 +01:00
|
|
|
|
var prop = Inventory.AddProperty(hardpoint.Gear.Name, null, _ =>
|
2020-06-02 07:01:03 +01:00
|
|
|
|
{
|
2020-06-05 04:16:33 +01:00
|
|
|
|
_selectedHardpoint = hardpoint;
|
|
|
|
|
|
PopulateDetails(entity, hardpoint);
|
|
|
|
|
|
}, true);
|
2020-06-02 07:01:03 +01:00
|
|
|
|
|
2020-06-05 04:16:33 +01:00
|
|
|
|
if (hardpoint == _selectedHardpoint) prop.Button.OnPointerClick(new PointerEventData(null){button = PointerEventData.InputButton.Left});
|
2020-06-02 07:01:03 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-06-01 03:11:04 +01:00
|
|
|
|
if(!entity.Cargo.Any())
|
2020-06-05 04:16:33 +01:00
|
|
|
|
Inventory.AddSection("No Cargo");
|
2020-06-01 03:11:04 +01:00
|
|
|
|
else
|
|
|
|
|
|
{
|
2020-06-05 04:16:33 +01:00
|
|
|
|
Inventory.AddSection("Cargo");
|
2020-06-02 07:01:03 +01:00
|
|
|
|
foreach (var x in cargoData)
|
2020-06-01 03:11:04 +01:00
|
|
|
|
{
|
2020-06-02 07:01:03 +01:00
|
|
|
|
if(x.Key is SimpleCommodity simpleCommodity)
|
2020-06-05 04:16:33 +01:00
|
|
|
|
Inventory.AddProperty(x.Value.Name, () => simpleCommodity.Quantity.ToString());
|
2020-06-01 03:11:04 +01:00
|
|
|
|
else
|
2020-06-05 04:16:33 +01:00
|
|
|
|
Inventory.AddProperty(x.Value.Name);
|
2020-06-01 03:11:04 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
Inventory.RefreshValues();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-06-05 04:16:33 +01:00
|
|
|
|
void PopulateDetails(Entity entity, IEnumerable<BlueprintData> blueprints)
|
2020-06-01 03:11:04 +01:00
|
|
|
|
{
|
|
|
|
|
|
Details.Clear();
|
2020-06-05 04:16:33 +01:00
|
|
|
|
foreach (var blueprint in blueprints)
|
2020-06-01 03:11:04 +01:00
|
|
|
|
{
|
2020-06-05 04:16:33 +01:00
|
|
|
|
var blueprintList = Details.AddList(blueprint.Name);
|
|
|
|
|
|
|
|
|
|
|
|
foreach (var ingredient in blueprint.Ingredients)
|
2020-06-02 07:01:03 +01:00
|
|
|
|
{
|
2020-06-05 04:16:33 +01:00
|
|
|
|
var itemData = _context.Cache.Get<ItemData>(ingredient.Key);
|
|
|
|
|
|
blueprintList.AddProperty(itemData.Name, () => ingredient.Value.ToString());
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var bpItemData = _context.Cache.Get<ItemData>(blueprint.Item);
|
|
|
|
|
|
|
|
|
|
|
|
if(entity.GetBlueprintIngredients(blueprint, out _, out _))
|
|
|
|
|
|
blueprintList.AddButton("Build", _ => entity.Build(blueprint, 1, bpItemData.Name, true));
|
|
|
|
|
|
}
|
|
|
|
|
|
Details.RefreshValues();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void PopulateDetails(Entity entity, Hardpoint hardpoint)
|
|
|
|
|
|
{
|
|
|
|
|
|
Details.Clear();
|
|
|
|
|
|
Details.AddProperty("Durability",
|
|
|
|
|
|
() => $"{hardpoint.Gear.Durability.SignificantDigits(Context.GlobalData.SignificantDigits)}/{Context.Evaluate(hardpoint.ItemData.Durability, hardpoint.Gear).SignificantDigits(Context.GlobalData.SignificantDigits)}");
|
|
|
|
|
|
foreach (var behavior in hardpoint.Behaviors)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (behavior is Factory factory)
|
|
|
|
|
|
{
|
|
|
|
|
|
factory.OnProductionUpdate += () => PopulateDetails(entity, hardpoint);
|
|
|
|
|
|
Details.AddField("Production Quality", () => factory.ProductionQuality, f => factory.ProductionQuality = f, 0, 1);
|
|
|
|
|
|
var corporation = Context.Cache.Get<Corporation>(entity.Corporation);
|
|
|
|
|
|
var compatibleBlueprints = corporation.UnlockedBlueprints
|
|
|
|
|
|
.Select(id => _context.Cache.Get<BlueprintData>(id))
|
|
|
|
|
|
.Where(bp => bp.FactoryItem == hardpoint.ItemData.ID).ToList();
|
|
|
|
|
|
if (factory.RetoolingTime > 0)
|
2020-06-02 07:01:03 +01:00
|
|
|
|
{
|
2020-06-05 04:16:33 +01:00
|
|
|
|
Details.AddProgressField("Retooling", () => (factory.ToolingTime - (float) factory.RetoolingTime) / factory.ToolingTime);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
Details.AddField("Item",
|
|
|
|
|
|
() => compatibleBlueprints.FindIndex(bp=>bp.ID== factory.Blueprint) + 1,
|
|
|
|
|
|
i => factory.Blueprint = i == 0 ? Guid.Empty : compatibleBlueprints[i - 1].ID,
|
|
|
|
|
|
new []{"None"}.Concat(compatibleBlueprints.Select(bp=>bp.Name)).ToArray());
|
|
|
|
|
|
if (factory.Blueprint != Guid.Empty)
|
2020-06-02 07:01:03 +01:00
|
|
|
|
{
|
2020-06-05 04:16:33 +01:00
|
|
|
|
if (factory.ItemUnderConstruction != Guid.Empty)
|
2020-06-02 07:01:03 +01:00
|
|
|
|
{
|
2020-06-05 04:16:33 +01:00
|
|
|
|
Details.AddProgressField("Production", () =>
|
2020-06-02 07:01:03 +01:00
|
|
|
|
{
|
2020-06-05 04:16:33 +01:00
|
|
|
|
var itemInstance = _context.Cache.Get<CraftedItemInstance>(factory.ItemUnderConstruction);
|
|
|
|
|
|
var blueprintData = _context.Cache.Get<BlueprintData>(itemInstance.Blueprint);
|
|
|
|
|
|
return (blueprintData.ProductionTime - (float) entity.IncompleteCargo[factory.ItemUnderConstruction]) / blueprintData.ProductionTime;
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
var ingredientsList = Details.AddList("Ingredients Needed");
|
|
|
|
|
|
var blueprintData = _context.Cache.Get<BlueprintData>(factory.Blueprint);
|
|
|
|
|
|
foreach (var ingredient in blueprintData.Ingredients)
|
2020-06-02 07:01:03 +01:00
|
|
|
|
{
|
2020-06-05 04:16:33 +01:00
|
|
|
|
var itemData = _context.Cache.Get<ItemData>(ingredient.Key);
|
|
|
|
|
|
ingredientsList.AddProperty(itemData.Name, () => ingredient.Value.ToString());
|
2020-06-02 07:01:03 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2020-06-05 04:16:33 +01:00
|
|
|
|
}
|
|
|
|
|
|
foreach (var behavior in hardpoint.ItemData.Behaviors)
|
|
|
|
|
|
{
|
|
|
|
|
|
var type = behavior.GetType();
|
|
|
|
|
|
if (type.GetCustomAttribute(typeof(RuntimeInspectable)) != null)
|
2020-06-01 03:11:04 +01:00
|
|
|
|
{
|
2020-06-05 04:16:33 +01:00
|
|
|
|
foreach (var field in type.GetFields().Where(f => f.GetCustomAttribute<RuntimeInspectable>() != null))
|
2020-06-01 03:11:04 +01:00
|
|
|
|
{
|
2020-06-05 04:16:33 +01:00
|
|
|
|
var fieldType = field.FieldType;
|
|
|
|
|
|
if (fieldType == typeof(float))
|
|
|
|
|
|
Details.AddProperty(field.Name, () => $"{((float) field.GetValue(behavior)).SignificantDigits(Context.GlobalData.SignificantDigits)}");
|
|
|
|
|
|
else if (fieldType == typeof(int))
|
|
|
|
|
|
Details.AddProperty(field.Name, () => $"{(int) field.GetValue(behavior)}");
|
|
|
|
|
|
else if (fieldType == typeof(PerformanceStat))
|
2020-06-01 03:11:04 +01:00
|
|
|
|
{
|
2020-06-05 04:16:33 +01:00
|
|
|
|
var stat = (PerformanceStat) field.GetValue(behavior);
|
|
|
|
|
|
Details.AddProperty(field.Name, () => $"{Context.Evaluate(stat, hardpoint.Gear, entity).SignificantDigits(Context.GlobalData.SignificantDigits)}");
|
2020-06-01 03:11:04 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
Details.RefreshValues();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|