Docking System

Entities now have dedicated docking bays with their own cargo area for loading/unloading
Inventory panel allows display of any ship or cargo bay at the currently docked station or within the current ship if undocked
This commit is contained in:
Reinier Vens 2020-12-19 02:37:13 +00:00
commit 2d9bec0e33
24 changed files with 553 additions and 465 deletions

View file

@ -4,6 +4,7 @@ using System.Collections.Generic;
using System.Linq;
using TMPro;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.UI;
public class ContextMenu : MonoBehaviour
@ -104,19 +105,21 @@ public class ContextMenu : MonoBehaviour
public void Show()
{
gameObject.SetActive(true);
var rect = transform as RectTransform;
var pivot = rect.pivot;
var canvas = Parent != null ? Parent.Canvas : Canvas;
var mousePosition = Mouse.current.position.ReadValue();
if (Parent!=null)
pivot.x = ForceDirectionRight ? 0 : 1;
else
pivot.x = Input.mousePosition.x > Screen.width - rect.sizeDelta.x * canvas.scaleFactor ? 1 : 0;
pivot.x = mousePosition.x > Screen.width - rect.sizeDelta.x * canvas.scaleFactor ? 1 : 0;
var scaleFactor = canvas.scaleFactor;
_dropdownRight = Input.mousePosition.x < Screen.width - rect.sizeDelta.x * scaleFactor * 2;
var pivotTop = (Parent==null ? Input.mousePosition.y : ForcePosition.y) > (PaddingHeight + _options.Count * OptionHeight) * scaleFactor;
_dropdownRight = mousePosition.x < Screen.width - rect.sizeDelta.x * scaleFactor * 2;
var pivotTop = (Parent==null ? mousePosition.y : ForcePosition.y) > (PaddingHeight + _options.Count * OptionHeight) * scaleFactor;
pivot.y = pivotTop ? 1 : 0;
rect.pivot = pivot;
rect.position = Parent!=null ? ForcePosition - (pivotTop ? Vector3.zero : Vector3.up * (OptionHeight * scaleFactor)) : Input.mousePosition;
rect.position = Parent!=null ? ForcePosition - (pivotTop ? Vector3.zero : Vector3.up * (OptionHeight * scaleFactor)) : (Vector3)mousePosition;
if (Parent==null)
CancelClickCatcher.gameObject.SetActive(true);
}

View file

@ -1,4 +1,5 @@
using System.Collections;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using TMPro;
@ -10,12 +11,15 @@ using int2 = Unity.Mathematics.int2;
public class InventoryPanel : MonoBehaviour
{
public ActionGameManager GameManager;
public ContextMenu ContextMenu;
public TextMeshProUGUI Title;
public Button Dropdown;
public GridLayoutGroup Grid;
public Sprite[] NodeTextures;
public Prototype NodePrototype;
private Dictionary<int2, GameObject> _nodeInstances;
private Dictionary<int2, GameObject> _nodeInstances = new Dictionary<int2, GameObject>();
private int2[] _offsets = new[]
{
int2(0, 1),
@ -28,8 +32,40 @@ public class InventoryPanel : MonoBehaviour
int2(-1, 1)
};
private void Start()
{
Dropdown.onClick.AddListener(() =>
{
ContextMenu.Clear();
foreach (var ship in GameManager.AvailableShips())
{
if (ship.CargoBays.Any())
{
ContextMenu.AddDropdown(ship.Name,
ship.CargoBays
.Select<EquippedCargoBay, (string text, Action action, bool enabled)>((bay, index) =>
($"Bay {index}", () => Display(GameManager.ItemManager, bay), true))
.Prepend(("Equipment", () => Display(GameManager.ItemManager, ship), true)));
}
else ContextMenu.AddOption(ship.Name, () => Display(GameManager.ItemManager, ship));
}
foreach (var bay in GameManager.AvailableCargoBays())
{
ContextMenu.AddOption(bay.Name, () => Display(GameManager.ItemManager, bay));
}
ContextMenu.Show();
});
}
public void Display(ItemManager manager, Entity entity)
{
foreach(var node in _nodeInstances.Values)
Destroy(node);
_nodeInstances.Clear();
Title.text = entity.Name;
var hullData = manager.GetData(entity.Hull) as HullData;
Grid.constraintCount = hullData.Shape.Width;
_nodeInstances = hullData.Shape.AllCoordinates
@ -90,12 +126,18 @@ public class InventoryPanel : MonoBehaviour
public void Display(ItemManager manager, EquippedCargoBay cargo)
{
Grid.constraintCount = cargo.Data.Shape.Width;
_nodeInstances = cargo.Data.Shape.AllCoordinates
foreach(var node in _nodeInstances.Values)
Destroy(node);
_nodeInstances.Clear();
Title.text = cargo.Name;
Grid.constraintCount = cargo.Data.InteriorShape.Width;
_nodeInstances = cargo.Data.InteriorShape.AllCoordinates
.ToDictionary(v => v,
v =>
{
if (!cargo.Data.Shape[v])
if (!cargo.Data.InteriorShape[v])
{
var empty = new GameObject("Empty Node", typeof(RectTransform));
empty.transform.SetParent(Grid.transform);
@ -106,7 +148,7 @@ public class InventoryPanel : MonoBehaviour
var item = cargo.Occupancy[v.x, v.y];
bool ItemMatch(int2 offset) =>
!cargo.Data.Shape[v + offset] ||
!cargo.Data.InteriorShape[v + offset] ||
item == null ||
cargo.Occupancy[v.x + offset.x, v.y + offset.y] != item;
@ -127,10 +169,9 @@ public class InventoryPanel : MonoBehaviour
var node = NodePrototype.Instantiate<Prototype>().gameObject;
var image = node.GetComponentInChildren<Image>();
image.sprite = NodeTextures[spriteIndex];
var itemData = manager.GetData(item);
if (item == null)
image.color = Color.white * .5f;
else if (itemData is EquippableItemData equippable)
else if (manager.GetData(item) is EquippableItemData equippable)
image.color = HardpointData.GetColor(equippable.HardpointType).ToColor();
else
image.color = Color.white;