Merge branch 'master' into new-netcode

Conflicts:
	Subsurface/Source/Items/Components/Machines/Deconstructor.cs
	Subsurface/Source/Items/Components/Machines/Fabricator.cs
This commit is contained in:
Regalis
2017-03-23 18:58:18 +02:00
9 changed files with 156 additions and 89 deletions
@@ -133,53 +133,74 @@ namespace Barotrauma
if (!prefab.SpawnOnWalls) return randomPos; if (!prefab.SpawnOnWalls) return randomPos;
var cells = level.GetCells(randomPos);
if (!cells.Any()) return null;
VoronoiCell cell = cells[Rand.Int(cells.Count, false)];
List<GraphEdge> edges = new List<GraphEdge>(); List<GraphEdge> edges = new List<GraphEdge>();
List<Vector2> normals = new List<Vector2>(); List<Vector2> normals = new List<Vector2>();
foreach (GraphEdge edge in cell.edges)
var cells = level.GetCells(randomPos);
if (cells.Any())
{ {
if (!edge.isSolid || edge.OutsideLevel) continue; VoronoiCell cell = cells[Rand.Int(cells.Count, false)];
Vector2 normal = edge.GetNormal(cell); foreach (GraphEdge edge in cell.edges)
{
if (!edge.isSolid || edge.OutsideLevel) continue;
Vector2 normal = edge.GetNormal(cell);
if (prefab.Alignment.HasFlag(Alignment.Bottom) && normal.Y < -0.5f) if (prefab.Alignment.HasFlag(Alignment.Bottom) && normal.Y < -0.5f)
{ {
edges.Add(edge); edges.Add(edge);
} }
else if (prefab.Alignment.HasFlag(Alignment.Top) && normal.Y > 0.5f) else if (prefab.Alignment.HasFlag(Alignment.Top) && normal.Y > 0.5f)
{ {
edges.Add(edge); edges.Add(edge);
} }
else if (prefab.Alignment.HasFlag(Alignment.Left) && normal.X < -0.5f) else if (prefab.Alignment.HasFlag(Alignment.Left) && normal.X < -0.5f)
{ {
edges.Add(edge); edges.Add(edge);
} }
else if (prefab.Alignment.HasFlag(Alignment.Right) && normal.X > 0.5f) else if (prefab.Alignment.HasFlag(Alignment.Right) && normal.X > 0.5f)
{ {
edges.Add(edge); edges.Add(edge);
} }
else else
{ {
continue; continue;
} }
normals.Add(normal); normals.Add(normal);
}
}
foreach (RuinGeneration.Ruin ruin in Level.Loaded.Ruins)
{
Rectangle expandedArea = ruin.Area;
expandedArea.Inflate(ruin.Area.Width, ruin.Area.Height);
if (!expandedArea.Contains(randomPos)) continue;
foreach (var ruinShape in ruin.RuinShapes)
{
foreach (var wall in ruinShape.Walls)
{
if (!prefab.Alignment.HasFlag(ruinShape.GetLineAlignment(wall))) continue;
edges.Add(new GraphEdge(wall.A, wall.B));
normals.Add((wall.A + wall.B) / 2.0f - ruinShape.Center);
}
}
} }
if (!edges.Any()) return null; if (!edges.Any()) return null;
int index = Rand.Int(edges.Count,false); int index = Rand.Int(edges.Count, false);
closestEdge = edges[index]; closestEdge = edges[index];
edgeNormal = normals[index]; edgeNormal = normals[index];
float length = Vector2.Distance(closestEdge.point1, closestEdge.point2); float length = Vector2.Distance(closestEdge.point1, closestEdge.point2);
Vector2 dir = (closestEdge.point1 - closestEdge.point2) / length; Vector2 dir = (closestEdge.point1 - closestEdge.point2) / length;
Vector2 pos = closestEdge.point2 + dir * Rand.Range(prefab.Sprite.size.X / 2.0f, length - prefab.Sprite.size.X / 2.0f, false); Vector2 pos = closestEdge.point2 + dir * Rand.Range(prefab.Sprite.size.X / 2.0f, length - prefab.Sprite.size.X / 2.0f, false);
return pos; return pos;
} }
+18 -1
View File
@@ -10,6 +10,8 @@ namespace Barotrauma
protected Alignment textAlignment; protected Alignment textAlignment;
private float textScale;
protected Vector2 textPos; protected Vector2 textPos;
protected Vector2 origin; protected Vector2 origin;
@@ -88,6 +90,19 @@ namespace Barotrauma
get { return textPos; } get { return textPos; }
} }
public float TextScale
{
get { return textScale; }
set
{
if (value != textScale)
{
textScale = value;
SetTextPos();
}
}
}
public Vector2 Origin public Vector2 Origin
{ {
get { return origin; } get { return origin; }
@@ -162,6 +177,8 @@ namespace Barotrauma
SetTextPos(); SetTextPos();
TextScale = 1.0f;
if (rect.Height == 0 && !string.IsNullOrEmpty(Text)) if (rect.Height == 0 && !string.IsNullOrEmpty(Text))
{ {
this.rect.Height = (int)Font.MeasureString(wrappedText).Y; this.rect.Height = (int)Font.MeasureString(wrappedText).Y;
@@ -267,7 +284,7 @@ namespace Barotrauma
Wrap ? wrappedText : text, Wrap ? wrappedText : text,
new Vector2(rect.X, rect.Y) + textPos + offset, new Vector2(rect.X, rect.Y) + textPos + offset,
textColor * (textColor.A / 255.0f), textColor * (textColor.A / 255.0f),
0.0f, origin, 1.0f, 0.0f, origin, TextScale,
SpriteEffects.None, textDepth); SpriteEffects.None, textDepth);
} }
@@ -36,6 +36,16 @@ namespace Barotrauma.Items.Components
} }
} }
[Editable, HasDefaultValue(1.0f, true)]
public float TextScale
{
get { return textBlock == null ? 1.0f : textBlock.TextScale; }
set
{
if (textBlock != null) textBlock.TextScale = MathHelper.Clamp(value, 0.1f, 10.0f);
}
}
private GUITextBlock TextBlock private GUITextBlock TextBlock
{ {
get get
@@ -49,6 +59,7 @@ namespace Barotrauma.Items.Components
textBlock.Font = GUI.SmallFont; textBlock.Font = GUI.SmallFont;
textBlock.Padding = new Vector4(5.0f, 5.0f, 5.0f, 5.0f); textBlock.Padding = new Vector4(5.0f, 5.0f, 5.0f, 5.0f);
textBlock.TextDepth = item.Sprite.Depth - 0.0001f; textBlock.TextDepth = item.Sprite.Depth - 0.0001f;
textBlock.TextScale = TextScale;
} }
return textBlock; return textBlock;
} }
@@ -30,12 +30,9 @@ namespace Barotrauma.Items.Components
public override void Update(float deltaTime, Camera cam) public override void Update(float deltaTime, Camera cam)
{ {
if (container == null || !container.Inventory.Items.Any(i=>i!=null)) if (container == null || container.Inventory.Items.All(i => i == null))
{ {
progressBar.BarSize = 0.0f; SetActive(false);
//activateButton.Enabled = true;
if (container != null) container.Inventory.Locked = false;
IsActive = false;
return; return;
} }
@@ -51,10 +48,10 @@ namespace Barotrauma.Items.Components
if (progressTimer>targetItem.Prefab.DeconstructTime) if (progressTimer>targetItem.Prefab.DeconstructTime)
{ {
var containers = item.GetComponents<ItemContainer>(); var containers = item.GetComponents<ItemContainer>();
if (containers.Count<2) if (containers.Count < 2)
{ {
DebugConsole.ThrowError("Error in Deconstructor.Update: Deconstructors must have two ItemContainer components!"); DebugConsole.ThrowError("Error in Deconstructor.Update: Deconstructors must have two ItemContainer components!");
return; return;
} }
@@ -62,21 +59,32 @@ namespace Barotrauma.Items.Components
{ {
if (deconstructProduct.RequireFullCondition && targetItem.Condition < 100.0f) continue; if (deconstructProduct.RequireFullCondition && targetItem.Condition < 100.0f) continue;
var itemPrefab = ItemPrefab.list.FirstOrDefault(ip => ip.Name.ToLowerInvariant() == deconstructProduct.ItemPrefabName.ToLowerInvariant()) as ItemPrefab; var itemPrefab = MapEntityPrefab.list.FirstOrDefault(ip => ip.Name.ToLowerInvariant() == deconstructProduct.ItemPrefabName.ToLowerInvariant()) as ItemPrefab;
if (itemPrefab==null) if (itemPrefab == null)
{ {
DebugConsole.ThrowError("Tried to deconstruct item \""+targetItem.Name+"\" but couldn't find item prefab \""+deconstructProduct+"\"!"); DebugConsole.ThrowError("Tried to deconstruct item \"" + targetItem.Name + "\" but couldn't find item prefab \"" + deconstructProduct + "\"!");
continue; continue;
} }
Item.Spawner.AddToSpawnQueue(itemPrefab, containers[1].Inventory);
//container full, drop the items outside the deconstructor
if (containers[1].Inventory.Items.All(i => i != null))
{
Entity.Spawner.AddToSpawnQueue(itemPrefab, item.Position, item.Submarine);
}
else
{
Entity.Spawner.AddToSpawnQueue(itemPrefab, containers[1].Inventory);
}
} }
container.Inventory.RemoveItem(targetItem); container.Inventory.RemoveItem(targetItem);
Item.Spawner.AddToRemoveQueue(targetItem); Entity.Spawner.AddToRemoveQueue(targetItem);
activateButton.Text = "Deconstruct"; if (container.Inventory.Items.Any(i => i != null))
progressBar.BarSize = 0.0f; {
progressTimer = 0.0f; progressTimer = 0.0f;
progressBar.BarSize = 0.0f;
}
} }
} }
@@ -122,6 +130,8 @@ namespace Barotrauma.Items.Components
return; return;
} }
if (container.Inventory.Items.All(i => i == null)) active = false;
IsActive = active; IsActive = active;
if (!IsActive) if (!IsActive)
@@ -133,7 +143,6 @@ namespace Barotrauma.Items.Components
} }
else else
{ {
if (container.Inventory.Items.All(i => i == null)) return;
activateButton.Text = "Cancel"; activateButton.Text = "Cancel";
} }
@@ -360,25 +360,32 @@ namespace Barotrauma.Items.Components
if (timeUntilReady > 0.0f) return; if (timeUntilReady > 0.0f) return;
var containers = item.GetComponents<ItemContainer>(); var containers = item.GetComponents<ItemContainer>();
if (containers.Count<2) if (containers.Count < 2)
{ {
DebugConsole.ThrowError("Error while fabricating a new item: fabricators must have two ItemContainer components"); DebugConsole.ThrowError("Error while fabricating a new item: fabricators must have two ItemContainer components");
return; return;
} }
foreach (Tuple<ItemPrefab,int> ip in fabricatedItem.RequiredItems) foreach (Tuple<ItemPrefab, int> ip in fabricatedItem.RequiredItems)
{ {
for (int i = 0; i<ip.Item2; i++) for (int i = 0; i < ip.Item2; i++)
{ {
var requiredItem = containers[0].Inventory.Items.FirstOrDefault(it => it != null && it.Prefab == ip.Item1); var requiredItem = containers[0].Inventory.Items.FirstOrDefault(it => it != null && it.Prefab == ip.Item1);
if (requiredItem == null) continue; if (requiredItem == null) continue;
Item.Spawner.AddToRemoveQueue(requiredItem); Entity.Spawner.AddToRemoveQueue(requiredItem);
containers[0].Inventory.RemoveItem(requiredItem); containers[0].Inventory.RemoveItem(requiredItem);
} }
} }
Item.Spawner.AddToSpawnQueue(fabricatedItem.TargetItem, containers[1].Inventory); if (containers[1].Inventory.Items.All(i => i != null))
{
Entity.Spawner.AddToSpawnQueue(fabricatedItem.TargetItem, item.Position, item.Submarine);
}
else
{
Entity.Spawner.AddToSpawnQueue(fabricatedItem.TargetItem, containers[1].Inventory);
}
CancelFabricating(); CancelFabricating();
} }
+7 -1
View File
@@ -735,7 +735,13 @@ namespace Barotrauma
{ {
if (MathUtils.GetLineRectangleIntersection(e.point1, e.point2, ruinShape.Rect) != null) if (MathUtils.GetLineRectangleIntersection(e.point1, e.point2, ruinShape.Rect) != null)
{ {
cell.CellType = CellType.Empty; cell.CellType = CellType.Removed;
int x = (int)Math.Floor(cell.Center.X / GridCellSize);
int y = (int)Math.Floor(cell.Center.Y / GridCellSize);
cellGrid[x, y].Remove(cell);
cells.Remove(cell);
break; break;
} }
} }
+1 -3
View File
@@ -538,10 +538,8 @@ namespace Voronoi2
private void pushGraphEdge( Site leftSite, Site rightSite, Vector2 point1, Vector2 point2 ) private void pushGraphEdge( Site leftSite, Site rightSite, Vector2 point1, Vector2 point2 )
{ {
GraphEdge newEdge = new GraphEdge (); GraphEdge newEdge = new GraphEdge(point1, point2);
allEdges.Add ( newEdge ); allEdges.Add ( newEdge );
newEdge.point1 = point1;
newEdge.point2 = point2;
newEdge.site1 = leftSite; newEdge.site1 = leftSite;
newEdge.site2 = rightSite; newEdge.site2 = rightSite;
@@ -155,18 +155,14 @@ namespace Voronoi2
for (int i = 1; i < vertices.Length; i++ ) for (int i = 1; i < vertices.Length; i++ )
{ {
GraphEdge ge = new GraphEdge(); GraphEdge ge = new GraphEdge(vertices[i-1], vertices[i]);
ge.point1 = vertices[i-1];
ge.point2 = vertices[i];
System.Diagnostics.Debug.Assert(ge.point1 != ge.point2); System.Diagnostics.Debug.Assert(ge.point1 != ge.point2);
edges.Add(ge); edges.Add(ge);
} }
GraphEdge lastEdge = new GraphEdge(); GraphEdge lastEdge = new GraphEdge(vertices[0], vertices[vertices.Length-1]);
lastEdge.point1 = vertices[0];
lastEdge.point2 = vertices[vertices.Length-1];
edges.Add(lastEdge); edges.Add(lastEdge);
@@ -208,6 +204,12 @@ namespace Voronoi2
get { return (point1 + point2) / 2.0f; } get { return (point1 + point2) / 2.0f; }
} }
public GraphEdge(Vector2 point1, Vector2 point2)
{
this.point1 = point1;
this.point2 = point2;
}
public VoronoiCell AdjacentCell(VoronoiCell cell) public VoronoiCell AdjacentCell(VoronoiCell cell)
{ {
if (cell1==cell) if (cell1==cell)
+21 -25
View File
@@ -464,44 +464,43 @@ namespace Barotrauma
return d[n, m]; return d[n, m];
} }
public static string WrapText(string text, float lineLength, ScalableFont font) //TODO: could integrate this into the ScalableFont class directly public static string WrapText(string text, float lineLength, ScalableFont font) //TODO: could integrate this into the ScalableFont class directly
{ {
if (font.MeasureString(text).X < lineLength) return text; if (font.MeasureString(text).X < lineLength) return text;
text = text.Replace("\n", " \n "); text = text.Replace("\n", " \n ");
string[] words = text.Split(' ');//, '\n'); string[] words = text.Split(' ');
StringBuilder wrappedText = new StringBuilder(); StringBuilder wrappedText = new StringBuilder();
float linePos = 0f; float linePos = 0f;
float spaceWidth = font.MeasureString(" ").X; float spaceWidth = font.MeasureString(" ").X;
for (int i = 0; i < words.Length; ++i) for (int i = 0; i < words.Length; ++i)
{ {
if (string.IsNullOrWhiteSpace(words[i]) && words[i]!="\n") continue; if (string.IsNullOrWhiteSpace(words[i]) && words[i] != "\n") continue;
Vector2 size; Vector2 size = font.MeasureString(words[i]);
string tempWord = words[i]; if (size.X > lineLength)
string prevWord = words[i];
while ((size = font.MeasureString(tempWord)).X > lineLength)
{ {
tempWord = tempWord.Remove(tempWord.Length - 1, 1); while (words[i].Length > 0 &&
} (size = font.MeasureString((words[i][0]).ToString())).X + linePos < lineLength)
{
wrappedText.Append(words[i][0]);
words[i] = words[i].Remove(0, 1);
words[i] = tempWord; linePos += size.X;
if (prevWord.Length> tempWord.Length) }
{
wrappedText.Append(words[i]); wrappedText.Append("\n");
wrappedText.Append(" \n"); linePos = 0.0f;
wrappedText.Append(prevWord.Remove(0, tempWord.Length)); i--;
linePos = lineLength*2.0f;
continue; continue;
} }
if (linePos + size.X < lineLength) if (linePos + size.X < lineLength)
{ {
wrappedText.Append(words[i]); wrappedText.Append(words[i]);
if (words[i] == "\n") if (words[i] == "\n")
{ {
linePos = 0.0f; linePos = 0.0f;
@@ -514,13 +513,10 @@ namespace Barotrauma
} }
else else
{ {
//if (i>0)wrappedText.Remove(wrappedText.Length - 1, 1); wrappedText.Append("\n");
wrappedText.Append(words[i]);
wrappedText.Append("\n"); linePos = size.X + spaceWidth;
wrappedText.Append(words[i]);
linePos = size.X + spaceWidth;
} }
if (i < words.Length - 1) wrappedText.Append(" "); if (i < words.Length - 1) wrappedText.Append(" ");