GameMode/CrewManager bugfixes, fabricators

This commit is contained in:
Regalis
2015-06-19 11:53:31 +03:00
parent 2ef92c4af0
commit 24ba9b1b98
26 changed files with 420 additions and 98 deletions
+43 -29
View File
@@ -13,25 +13,13 @@ namespace Subsurface.Items.Components
public ItemInventory inventory;
//how many items can be contained
private int capacity;
private bool hideItems;
private bool drawInventory;
//the position of the first item in the container
private Vector2 itemPos;
//item[i].Pos = itemPos + itemInterval*i
private Vector2 itemInterval;
private float itemRotation;
[HasDefaultValue(5, false)]
public int Capacity
{
get { return capacity; }
set { capacity = Math.Max(value, 1); }
}
private int capacity;
[HasDefaultValue(true, false)]
public bool HideItems
@@ -39,6 +27,7 @@ namespace Subsurface.Items.Components
get { return hideItems; }
set { hideItems = value; }
}
private bool hideItems;
[HasDefaultValue(false, false)]
public bool DrawInventory
@@ -46,6 +35,25 @@ namespace Subsurface.Items.Components
get { return drawInventory; }
set { drawInventory = value; }
}
private bool drawInventory;
//the position of the first item in the container
[HasDefaultValue("0.0,0.0", false)]
public string ItemPos
{
get { return ToolBox.Vector2ToString(itemPos); }
set { itemPos = ToolBox.ParseToVector2(value); }
}
private Vector2 itemPos;
//item[i].Pos = itemPos + itemInterval*i
[HasDefaultValue("0.0,0.0", false)]
public string ItemInterval
{
get { return ToolBox.Vector2ToString(itemInterval); }
set { itemInterval = ToolBox.ParseToVector2(value); }
}
private Vector2 itemInterval;
[HasDefaultValue(0.0f, false)]
public float ItemRotation
@@ -53,25 +61,33 @@ namespace Subsurface.Items.Components
get { return itemRotation; }
set { itemRotation = value; }
}
private float itemRotation;
[HasDefaultValue("0.0,0.0", false)]
public string ItemPos
{
get { return ToolBox.Vector2ToString(itemPos); }
set { itemPos = ToolBox.ParseToVector2(value); }
}
[HasDefaultValue("0.0,0.0", false)]
public string ItemInterval
[HasDefaultValue("0.5,0.9", false)]
public string HudPos
{
get { return ToolBox.Vector2ToString(itemInterval); }
set { itemInterval = ToolBox.ParseToVector2(value); }
get { return ToolBox.Vector2ToString(hudPos); }
set
{
hudPos = ToolBox.ParseToVector2(value);
//inventory.CenterPos = hudPos;
}
}
private Vector2 hudPos;
[HasDefaultValue(5, false)]
public int SlotsPerRow
{
get { return slotsPerRow; }
set { slotsPerRow = value; }
}
private int slotsPerRow;
public ItemContainer(Item item, XElement element)
: base (item, element)
{
inventory = new ItemInventory(this, capacity);
inventory = new ItemInventory(this, capacity, hudPos, slotsPerRow);
containableItems = new List<RelatedItem>();
//itemPos = ToolBox.GetAttributeVector2(element, "ItemPos", Vector2.Zero);
@@ -99,6 +115,7 @@ namespace Subsurface.Items.Components
public bool CanBeContained(Item item)
{
if (containableItems.Count == 0) return true;
return (containableItems.Find(x => x.MatchesItem(item)) != null);
}
@@ -182,10 +199,7 @@ namespace Subsurface.Items.Components
public override bool Pick(Character picker)
{
if (picker == null) return false;
//picker.SelectedConstruction = item;
return true;
return (picker != null);
}
+216
View File
@@ -0,0 +1,216 @@
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
namespace Subsurface.Items.Components
{
class FabricableItem
{
//public static List<FabricableItem> list = new List<FabricableItem>();
//public readonly string[] FabricatorTags;
public readonly ItemPrefab TargetItem;
public readonly List<ItemPrefab> RequiredItems;
public readonly float RequiredTime;
//ListOrSomething requiredLevels
public FabricableItem(XElement element)
{
string name = ToolBox.GetAttributeString(element, "name", "").ToLower();
TargetItem = ItemPrefab.list.Find(ip => ip.Name.ToLower() == name) as ItemPrefab;
if (TargetItem == null)
{
DebugConsole.ThrowError("Error in Fabricable Item! Item ''" + element.Name + "'' not found.");
return;
}
RequiredItems = new List<ItemPrefab>();
string[] requiredItemNames = ToolBox.GetAttributeString(element, "requireditems", "").Split(',');
foreach (string requiredItemName in requiredItemNames)
{
ItemPrefab requiredItem = ItemPrefab.list.Find(ip => ip.Name.ToLower() == requiredItemName.Trim().ToLower()) as ItemPrefab;
if (requiredItem == null) continue;
RequiredItems.Add(requiredItem);
}
RequiredTime = ToolBox.GetAttributeFloat(element, "requiredtime", 1.0f);
}
}
class Fabricator : ItemComponent
{
List<FabricableItem> fabricableItems;
GUIListBox itemList;
GUIFrame selectedItemFrame;
FabricableItem fabricatedItem;
float timeUntilReady;
public Fabricator(Item item, XElement element)
: base(item, element)
{
fabricableItems = new List<FabricableItem>();
foreach (XElement subElement in element.Elements())
{
if (subElement.Name.ToString() != "fabricableitem") continue;
FabricableItem fabricableItem = new FabricableItem(subElement);
if (fabricableItem.TargetItem != null) fabricableItems.Add(fabricableItem);
}
int width = 400, height = 300;
itemList = new GUIListBox(new Rectangle(Game1.GraphicsWidth / 2 - width / 2, Game1.GraphicsHeight / 2 - height / 2, width, height), Color.White * 0.7f);
itemList.OnSelected = SelectItem;
//structureList.CheckSelected = MapEntityPrefab.GetSelected;
foreach (FabricableItem fi in fabricableItems)
{
Color color = ((itemList.CountChildren % 2) == 0) ? Color.White : Color.LightGray;
//GUIFrame frame = new GUIFrame(new Rectangle(0, 0, 0, 50), Color.Transparent, itemList);
//frame.UserData = fi;
//frame.Padding = new Vector4(5.0f, 5.0f, 5.0f, 5.0f);
//frame.Color = color;
//frame.HoverColor = Color.Gold * 0.2f;
//frame.SelectedColor = Color.Gold * 0.5f;
GUITextBlock textBlock = new GUITextBlock(
new Rectangle(0, 0, 0, 25),
fi.TargetItem.Name,
color, Color.Black,
Alignment.Left,
Alignment.Left,
itemList);
textBlock.UserData = fi;
textBlock.Padding = new Vector4(5.0f, 5.0f, 5.0f, 5.0f);
//if (fi.TargetItem.sprite != null)
//{
// GUIImage img = new GUIImage(new Rectangle(0, 0, 40, 40), fi.TargetItem.sprite, Alignment.Left, frame);
// img.Scale = Math.Min(Math.Min(40.0f / img.SourceRect.Width, 40.0f / img.SourceRect.Height), 1.0f);
//}
}
}
private bool SelectItem(object obj)
{
FabricableItem targetItem = obj as FabricableItem;
if (targetItem == null) return false;
int width = 200, height = 150;
selectedItemFrame = new GUIFrame(new Rectangle(Game1.GraphicsWidth / 2 - width / 2, itemList.Rect.Bottom+20, width, height), Color.Black*0.8f);
selectedItemFrame.Padding = GUI.style.smallPadding;
if (targetItem.TargetItem.sprite != null)
{
GUIImage img = new GUIImage(new Rectangle(0, 0, 40, 40), targetItem.TargetItem.sprite, Alignment.CenterX, selectedItemFrame);
img.Scale = Math.Min(Math.Min(40.0f / img.SourceRect.Width, 40.0f / img.SourceRect.Height), 1.0f);
string text = targetItem.TargetItem.Name + "\n";
text += "Required items:\n";
foreach (ItemPrefab ip in targetItem.RequiredItems)
{
text += " - " + ip.Name + "\n";
}
text += "Required time: "+targetItem.RequiredTime+" s";
GUITextBlock textBlock = new GUITextBlock(
new Rectangle(0, 0, 0, 25),
text,
Color.Transparent, Color.White,
Alignment.CenterX | Alignment.CenterY,
Alignment.Left,
selectedItemFrame);
GUIButton button = new GUIButton(new Rectangle(0,0,100,20), "Create", Color.White, Alignment.CenterX | Alignment.Bottom, selectedItemFrame);
button.OnClicked = StartFabricating;
button.UserData = targetItem;
}
return true;
}
public override bool Pick(Character picker)
{
return (picker != null);
}
private bool StartFabricating(GUIButton button, object obj)
{
GUIComponent listElement = itemList.GetChild(obj);
listElement.Color = Color.Green;
itemList.Enabled = false;
fabricatedItem = obj as FabricableItem;
isActive = true;
timeUntilReady = fabricatedItem.RequiredTime;
return true;
}
public override void Update(float deltaTime, Camera cam)
{
timeUntilReady -= deltaTime;
if (timeUntilReady > 0.0f) return;
ItemContainer container = item.GetComponent<ItemContainer>();
foreach (ItemPrefab ip in fabricatedItem.RequiredItems)
{
var requiredItem = Array.Find(container.inventory.items, it => it != null && it.Prefab == ip);
container.inventory.RemoveItem(requiredItem);
}
new Item(fabricatedItem.TargetItem, item.Position);
isActive = false;
fabricatedItem = null;
}
public override void DrawHUD(SpriteBatch spriteBatch, Character character)
{
FabricableItem targetItem = itemList.SelectedData as FabricableItem;
if (targetItem != null)
{
selectedItemFrame.GetChild<GUIButton>().Enabled = true;
ItemContainer container = item.GetComponent<ItemContainer>();
foreach (ItemPrefab ip in targetItem.RequiredItems)
{
if (Array.Find(container.inventory.items, it => it != null && it.Prefab == ip) != null) continue;
selectedItemFrame.GetChild<GUIButton>().Enabled = false;
break;
}
}
itemList.Update(0.016f);
itemList.Draw(spriteBatch);
if (selectedItemFrame != null)
{
selectedItemFrame.Update(0.016f);
selectedItemFrame.Draw(spriteBatch);
}
}
}
}
+30 -8
View File
@@ -10,20 +10,44 @@ namespace Subsurface
{
class Inventory : Entity
{
public static Item draggingItem;
public static Item doubleClickedItem;
private int slotsPerRow;
public int SlotsPerRow
{
set { slotsPerRow = Math.Max(1, value); }
}
protected int capacity;
public static Item draggingItem;
public Vector2 CenterPos
{
get { return centerPos; }
set
{
centerPos = value;
centerPos.X *= Game1.GraphicsWidth;
centerPos.Y *= Game1.GraphicsHeight;
}
}
public static Item doubleClickedItem;
private Vector2 centerPos;
protected int selectedSlot;
public Item[] items;
public Inventory(int capacity)
public Inventory(int capacity, Vector2? centerPos = null, int slotsPerRow=5)
{
this.capacity = capacity;
this.slotsPerRow = slotsPerRow;
items = new Item[capacity];
CenterPos = (centerPos==null) ? new Vector2(0.5f, 0.5f) : (Vector2)centerPos;
}
public int FindIndex(Item item)
@@ -132,13 +156,11 @@ namespace Subsurface
int rectWidth = 40, rectHeight = 40;
int spacing = 10;
int slotsPerRow = 5;
int rows = (int)Math.Ceiling((double)capacity / slotsPerRow);
int startX = Game1.GraphicsWidth / 2 - (rectWidth * slotsPerRow + spacing * (slotsPerRow - 1)) / 2;
int startY = (int)(Game1.GraphicsHeight * 0.9) - rows*(spacing+rectHeight);
int startX = (int)centerPos.X - (rectWidth * slotsPerRow + spacing * (slotsPerRow - 1)) / 2;
int startY = (int)centerPos.Y - rows * (spacing + rectHeight);
Rectangle slotRect = new Rectangle(startX, startY, rectWidth, rectHeight);
Rectangle draggingItemSlot = slotRect;
+6
View File
@@ -166,6 +166,12 @@ namespace Subsurface
}
public Item(ItemPrefab itemPrefab, Vector2 position)
: this(new Rectangle((int)position.X, (int)position.Y, (int)itemPrefab.sprite.size.X, (int)itemPrefab.sprite.size.Y), itemPrefab)
{
}
public Item(Rectangle newRect, ItemPrefab itemPrefab)
{
prefab = itemPrefab;
+4 -3
View File
@@ -1,4 +1,5 @@
using Subsurface.Items.Components;
using Microsoft.Xna.Framework;
using Subsurface.Items.Components;
namespace Subsurface
{
@@ -6,8 +7,8 @@ namespace Subsurface
{
ItemContainer container;
public ItemInventory(ItemContainer container, int capacity)
: base(capacity)
public ItemInventory(ItemContainer container, int capacity, Vector2? centerPos = null, int slotsPerRow = 5)
: base(capacity, centerPos, slotsPerRow)
{
this.container = container;
}