Cargo items can be set to spawn inside specific types of containers instead of being scattered on the cargo room floor.

This commit is contained in:
Joonas Rikkonen
2018-01-10 15:56:11 +02:00
parent 0f23635f49
commit 9f58d1b74d
5 changed files with 100 additions and 41 deletions
@@ -246,29 +246,30 @@ namespace Barotrauma.Networking
return true; return true;
}; };
foreach (MapEntityPrefab pf in MapEntityPrefab.List) foreach (MapEntityPrefab pf in MapEntityPrefab.List)
{ {
if (!(pf is ItemPrefab) || (pf.Price <= 0.0f && !pf.Tags.Contains("smallitem"))) continue; ItemPrefab ip = pf as ItemPrefab;
if (ip == null || (ip.Price <= 0.0f && !ip.Tags.Contains("smallitem"))) continue;
GUITextBlock textBlock = new GUITextBlock( GUITextBlock textBlock = new GUITextBlock(
new Rectangle(0, 0, 260, 25), new Rectangle(0, 0, 260, 25),
pf.Name, "", ip.Name, "",
Alignment.Left, Alignment.CenterLeft, cargoFrame, false, GUI.SmallFont); Alignment.Left, Alignment.CenterLeft, cargoFrame, false, GUI.SmallFont);
textBlock.Padding = new Vector4(40.0f, 3.0f, 0.0f, 0.0f); textBlock.Padding = new Vector4(40.0f, 3.0f, 0.0f, 0.0f);
textBlock.UserData = cargoFrame; textBlock.UserData = cargoFrame;
textBlock.CanBeFocused = false; textBlock.CanBeFocused = false;
if (pf.sprite != null) if (ip.sprite != null)
{ {
float scale = Math.Min(Math.Min(30.0f / pf.sprite.SourceRect.Width, 30.0f / pf.sprite.SourceRect.Height), 1.0f); float scale = Math.Min(Math.Min(30.0f / ip.sprite.SourceRect.Width, 30.0f / ip.sprite.SourceRect.Height), 1.0f);
GUIImage img = new GUIImage(new Rectangle(-20 - (int)(pf.sprite.SourceRect.Width * scale * 0.5f), 12 - (int)(pf.sprite.SourceRect.Height * scale * 0.5f), 40, 40), pf.sprite, Alignment.Left, textBlock); GUIImage img = new GUIImage(new Rectangle(-20 - (int)(ip.sprite.SourceRect.Width * scale * 0.5f), 12 - (int)(ip.sprite.SourceRect.Height * scale * 0.5f), 40, 40), ip.sprite, Alignment.Left, textBlock);
img.Color = pf.SpriteColor; img.Color = ip.SpriteColor;
img.Scale = scale; img.Scale = scale;
} }
int cargoVal = 0; int cargoVal = 0;
extraCargo.TryGetValue(pf.Name, out cargoVal); extraCargo.TryGetValue(ip, out cargoVal);
var amountInput = new GUINumberInput(new Rectangle(160, 0, 50, 20), "", GUINumberInput.NumberType.Int, textBlock); var amountInput = new GUINumberInput(new Rectangle(160, 0, 50, 20), "", GUINumberInput.NumberType.Int, textBlock);
amountInput.MinValueInt = 0; amountInput.MinValueInt = 0;
amountInput.MaxValueInt = 100; amountInput.MaxValueInt = 100;
@@ -276,13 +277,13 @@ namespace Barotrauma.Networking
amountInput.OnValueChanged += (numberInput) => amountInput.OnValueChanged += (numberInput) =>
{ {
if (extraCargo.ContainsKey(pf.Name)) if (extraCargo.ContainsKey(ip))
{ {
extraCargo[pf.Name] = numberInput.IntValue; extraCargo[ip] = numberInput.IntValue;
} }
else else
{ {
extraCargo.Add(pf.Name, numberInput.IntValue); extraCargo.Add(ip, numberInput.IntValue);
} }
}; };
} }
@@ -1,4 +1,5 @@
using Microsoft.Xna.Framework; using Barotrauma.Items.Components;
using Microsoft.Xna.Framework;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
@@ -54,6 +55,11 @@ namespace Barotrauma
} }
public void CreateItems() public void CreateItems()
{
CreateItems(purchasedItems);
}
public static void CreateItems(List<ItemPrefab> itemsToSpawn)
{ {
WayPoint wp = WayPoint.GetRandom(SpawnType.Cargo, null, Submarine.MainSub); WayPoint wp = WayPoint.GetRandom(SpawnType.Cargo, null, Submarine.MainSub);
@@ -71,24 +77,82 @@ namespace Barotrauma
return; return;
} }
foreach (ItemPrefab prefab in purchasedItems) Dictionary<ItemContainer, int> availableContainers = new Dictionary<ItemContainer, int>();
foreach (ItemPrefab prefab in itemsToSpawn)
{ {
Vector2 position = new Vector2( Vector2 position = new Vector2(
Rand.Range(cargoRoom.Rect.X + 20, cargoRoom.Rect.Right - 20), Rand.Range(cargoRoom.Rect.X + 20, cargoRoom.Rect.Right - 20),
cargoRoom.Rect.Y - cargoRoom.Rect.Height + prefab.Size.Y/2); cargoRoom.Rect.Y - cargoRoom.Rect.Height + prefab.Size.Y / 2);
if (GameMain.Server != null) ItemContainer itemContainer = null;
if (!string.IsNullOrEmpty(prefab.CargoContainerName))
{ {
Entity.Spawner.AddToSpawnQueue(prefab, position, wp.Submarine); itemContainer = availableContainers.Keys.ToList().Find(ac =>
ac.Item.Prefab.NameMatches(prefab.CargoContainerName) ||
ac.Item.Prefab.Tags.Contains(prefab.CargoContainerName.ToLowerInvariant()));
if (itemContainer == null)
{
var containerPrefab = MapEntityPrefab.List.Find(ep =>
ep.NameMatches(prefab.CargoContainerName) ||
(ep.Tags != null && ep.Tags.Contains(prefab.CargoContainerName.ToLowerInvariant()))) as ItemPrefab;
if (containerPrefab == null)
{
DebugConsole.ThrowError("Cargo spawning failed - could not find the item prefab for container \"" + containerPrefab.Name + "\"!");
continue;
}
Item containerItem = new Item(containerPrefab, position, wp.Submarine);
itemContainer = containerItem.GetComponent<ItemContainer>();
if (itemContainer == null)
{
DebugConsole.ThrowError("Cargo spawning failed - container \"" + containerItem.Name + "\" does not have an ItemContainer component!");
continue;
}
availableContainers.Add(itemContainer, itemContainer.Capacity);
if (GameMain.Server != null)
{
Entity.Spawner.CreateNetworkEvent(itemContainer.Item, false);
}
}
}
if (itemContainer == null)
{
//no container, place at the waypoint
if (GameMain.Server != null)
{
Entity.Spawner.AddToSpawnQueue(prefab, position, wp.Submarine);
}
else
{
new Item(prefab, position, wp.Submarine);
}
} }
else else
{ {
new Item(prefab, position, wp.Submarine); //place in the container
} if (GameMain.Server != null)
{
Entity.Spawner.AddToSpawnQueue(prefab, itemContainer.Inventory);
}
else
{
var item = new Item(prefab, position, wp.Submarine);
itemContainer.Inventory.TryPutItem(item, null);
}
//reduce the number of available slots in the container
availableContainers[itemContainer]--;
if (availableContainers[itemContainer] <= 0)
{
availableContainers.Remove(itemContainer);
}
}
} }
purchasedItems.Clear(); itemsToSpawn.Clear();
} }
} }
} }
@@ -145,6 +145,13 @@ namespace Barotrauma
private set; private set;
} }
[Serialize("", false)]
public string CargoContainerName
{
get;
private set;
}
public bool CanSpriteFlipX public bool CanSpriteFlipX
{ {
get { return canSpriteFlipX; } get { return canSpriteFlipX; }
@@ -1303,29 +1303,16 @@ namespace Barotrauma.Networking
{ {
if (sub == null) continue; if (sub == null) continue;
WayPoint cargoSpawnPos = WayPoint.GetRandom(SpawnType.Cargo, null, sub); List<ItemPrefab> spawnList = new List<ItemPrefab>();
foreach (KeyValuePair<ItemPrefab, int> kvp in extraCargo)
if (cargoSpawnPos?.CurrentHull == null)
{ {
DebugConsole.ThrowError("Couldn't spawn additional cargo (no cargo spawnpoint inside any of the hulls)"); for (int i = 0; i < kvp.Value; i++)
continue;
}
var cargoRoom = cargoSpawnPos.CurrentHull;
Vector2 position = new Vector2(
cargoSpawnPos.Position.X,
cargoRoom.Rect.Y - cargoRoom.Rect.Height);
foreach (string s in extraCargo.Keys)
{
ItemPrefab itemPrefab = MapEntityPrefab.Find(s) as ItemPrefab;
if (itemPrefab == null) continue;
for (int i = 0; i < extraCargo[s]; i++)
{ {
Entity.Spawner.AddToSpawnQueue(itemPrefab, position + new Vector2(Rand.Range(-20.0f, 20.0f), itemPrefab.Size.Y / 2), sub); spawnList.Add(kvp.Key);
} }
} }
CargoManager.CreateItems(spawnList);
} }
TraitorManager = null; TraitorManager = null;
@@ -49,7 +49,7 @@ namespace Barotrauma.Networking
private set; private set;
} }
public Dictionary<string, int> extraCargo; public Dictionary<ItemPrefab, int> extraCargo;
public bool ShowNetStats; public bool ShowNetStats;
@@ -345,7 +345,7 @@ namespace Barotrauma.Networking
{ {
monsterEnabled.Add(s, true); monsterEnabled.Add(s, true);
} }
extraCargo = new Dictionary<string, int>(); extraCargo = new Dictionary<ItemPrefab, int>();
} }
public void LoadClientPermissions() public void LoadClientPermissions()