From 3ec6f008d5efa15a6b2eab2d8d185666b84b0119 Mon Sep 17 00:00:00 2001 From: Regalis Date: Mon, 3 Oct 2016 16:43:03 +0300 Subject: [PATCH 1/2] Item/structure sprites flip by default --- .../Content/Items/Containers/containers.xml | 4 +-- .../Content/Items/Electricity/poweritems.xml | 2 +- Subsurface/Content/Map/StructurePrefabs.xml | 26 +++++++++---------- Subsurface/Source/Items/ItemPrefab.cs | 3 +-- Subsurface/Source/Map/StructurePrefab.cs | 4 +-- 5 files changed, 19 insertions(+), 20 deletions(-) diff --git a/Subsurface/Content/Items/Containers/containers.xml b/Subsurface/Content/Items/Containers/containers.xml index b892ccd08..8dcb3d0b5 100644 --- a/Subsurface/Content/Items/Containers/containers.xml +++ b/Subsurface/Content/Items/Containers/containers.xml @@ -16,7 +16,7 @@ linkable="true" pickdistance ="150"> - + @@ -29,7 +29,7 @@ linkable="true" pickdistance ="150"> - + diff --git a/Subsurface/Content/Items/Electricity/poweritems.xml b/Subsurface/Content/Items/Electricity/poweritems.xml index d7b33a2ad..744d114e0 100644 --- a/Subsurface/Content/Items/Electricity/poweritems.xml +++ b/Subsurface/Content/Items/Electricity/poweritems.xml @@ -7,7 +7,7 @@ pickdistance="150" description="Serves as a hub for power distribution and relaying signals between devices."> - + diff --git a/Subsurface/Content/Map/StructurePrefabs.xml b/Subsurface/Content/Map/StructurePrefabs.xml index 5d045427b..bf1c8422e 100644 --- a/Subsurface/Content/Map/StructurePrefabs.xml +++ b/Subsurface/Content/Map/StructurePrefabs.xml @@ -29,39 +29,39 @@ - + - + - + - + - + - + - + - + - + - + - + - + - + diff --git a/Subsurface/Source/Items/ItemPrefab.cs b/Subsurface/Source/Items/ItemPrefab.cs index 1c77e90ae..574fb1be1 100644 --- a/Subsurface/Source/Items/ItemPrefab.cs +++ b/Subsurface/Source/Items/ItemPrefab.cs @@ -288,8 +288,7 @@ namespace Barotrauma spriteFolder = Path.GetDirectoryName(filePath); } - if (ToolBox.GetAttributeBool(subElement, "canflipx", false)) - canSpriteFlipX = true; + canSpriteFlipX = ToolBox.GetAttributeBool(subElement, "canflipx", true); sprite = new Sprite(subElement, spriteFolder); size = sprite.size; diff --git a/Subsurface/Source/Map/StructurePrefab.cs b/Subsurface/Source/Map/StructurePrefab.cs index e26609623..9779779d6 100644 --- a/Subsurface/Source/Map/StructurePrefab.cs +++ b/Subsurface/Source/Map/StructurePrefab.cs @@ -113,8 +113,8 @@ namespace Barotrauma sp.sprite.effects = SpriteEffects.FlipHorizontally; if (ToolBox.GetAttributeBool(subElement, "flipvertical", false)) sp.sprite.effects = SpriteEffects.FlipVertically; - if (ToolBox.GetAttributeBool(subElement, "canflipx", false)) - sp.canSpriteFlipX = true; + + sp.canSpriteFlipX = ToolBox.GetAttributeBool(subElement, "canflipx", true); break; case "backgroundsprite": From 292218b9b022ee210dd68b7d061aa622f5c81c71 Mon Sep 17 00:00:00 2001 From: Regalis Date: Mon, 3 Oct 2016 22:40:14 +0300 Subject: [PATCH 2/2] WallSection flipping --- Subsurface/Source/Map/Structure.cs | 78 ++++++++++++++++++++++----- Subsurface/Source/Sprite.cs | 85 +++++++++++++++++++----------- 2 files changed, 120 insertions(+), 43 deletions(-) diff --git a/Subsurface/Source/Map/Structure.cs b/Subsurface/Source/Map/Structure.cs index bfc3f0b4b..e8b67d086 100644 --- a/Subsurface/Source/Map/Structure.cs +++ b/Subsurface/Source/Map/Structure.cs @@ -63,6 +63,8 @@ namespace Barotrauma public SpriteEffects SpriteEffects = SpriteEffects.None; + private bool flippedX; + public override Sprite Sprite { get { return prefab.sprite; } @@ -260,6 +262,14 @@ namespace Barotrauma int xsections = 1, ysections = 1; int width, height; + if (!HasBody) + { + sections = new WallSection[1]; + sections[0] = new WallSection(rect); + return; + + } + if (isHorizontal) { xsections = (int)Math.Ceiling((float)rect.Width / wallSectionSize); @@ -275,15 +285,33 @@ namespace Barotrauma height = (int)wallSectionSize; } + for (int x = 0; x < xsections; x++) { for (int y = 0; y < ysections; y++) { - Rectangle sectionRect = new Rectangle(rect.X + x * width, rect.Y - y * height, width, height); - sectionRect.Width -= (int)Math.Max((sectionRect.X + sectionRect.Width) - (rect.X + rect.Width), 0.0f); - sectionRect.Height -= (int)Math.Max((rect.Y - rect.Height) - (sectionRect.Y - sectionRect.Height), 0.0f); + if (flippedX) + { + Rectangle sectionRect = new Rectangle(rect.Right - (x + 1) * width, rect.Y - y * height, width, height); - sections[x + y] = new WallSection(sectionRect); + int over = Math.Max(rect.X - sectionRect.X, 0); + + sectionRect.X += over; + sectionRect.Width -= over; + + sectionRect.Height -= (int)Math.Max((rect.Y - rect.Height) - (sectionRect.Y - sectionRect.Height), 0.0f); + + sections[xsections - 1 - x + y] = new WallSection(sectionRect); + } + + else + { + Rectangle sectionRect = new Rectangle(rect.X + x * width, rect.Y - y * height, width, height); + sectionRect.Width -= (int)Math.Max(sectionRect.Right - rect.Right, 0.0f); + sectionRect.Height -= (int)Math.Max((rect.Y - rect.Height) - (sectionRect.Y - sectionRect.Height), 0.0f); + + sections[x + y] = new WallSection(sectionRect); + } } } } @@ -437,9 +465,9 @@ namespace Barotrauma if (prefab.BackgroundSprite != null) { prefab.BackgroundSprite.DrawTiled( - spriteBatch, - new Vector2(rect.X + drawOffset.X, -(rect.Y + drawOffset.Y)), - new Vector2(rect.Width, rect.Height), + spriteBatch, + new Vector2(rect.X + drawOffset.X, -(rect.Y + drawOffset.Y)), + new Vector2(rect.Width, rect.Height), Vector2.Zero, color, Point.Zero); } } @@ -449,16 +477,16 @@ namespace Barotrauma if (back == prefab.sprite.Depth > 0.5f || editing) { - foreach (WallSection s in sections) + for (int i = 0; i < sections.Length; i++) { if (damageEffect != null) { - float newCutoff = Math.Min((s.damage / prefab.MaxHealth), 0.65f); + float newCutoff = Math.Min((sections[i].damage / prefab.MaxHealth), 0.65f); if (Math.Abs(newCutoff - prevCutoff) > 0.01f) { damageEffect.Parameters["aCutoff"].SetValue(newCutoff); - damageEffect.Parameters["cCutoff"].SetValue(newCutoff*1.2f); + damageEffect.Parameters["cCutoff"].SetValue(newCutoff * 1.2f); damageEffect.CurrentTechnique.Passes[0].Apply(); @@ -466,8 +494,21 @@ namespace Barotrauma } } - Point offset = new Point(Math.Abs(rect.Location.X - s.rect.Location.X), Math.Abs(rect.Location.Y - s.rect.Location.Y)); - prefab.sprite.DrawTiled(spriteBatch, new Vector2(s.rect.X + drawOffset.X, -(s.rect.Y + drawOffset.Y)), new Vector2(s.rect.Width, s.rect.Height), Vector2.Zero, color, offset); + Point textureOffset = new Point( + Math.Abs(rect.Location.X - sections[i].rect.Location.X), + Math.Abs(rect.Location.Y - sections[i].rect.Location.Y)); + + if (flippedX && isHorizontal) + { + textureOffset.X = rect.Width - textureOffset.X - sections[i].rect.Width; + } + + prefab.sprite.DrawTiled( + spriteBatch, + new Vector2(sections[i].rect.X + drawOffset.X, -(sections[i].rect.Y + drawOffset.Y)), + new Vector2(sections[i].rect.Width, sections[i].rect.Height), + Vector2.Zero, color, + textureOffset); } } @@ -577,6 +618,15 @@ namespace Barotrauma public int FindSectionIndex(Vector2 displayPos) { + if (!sections.Any()) return -1; + + //if the sub has been flipped horizontally, the first section may be smaller than wallSectionSize + //and we need to adjust the position accordingly + if (sections[0].rect.Width < wallSectionSize) + { + displayPos.X += wallSectionSize - sections[0].rect.Width; + } + int index = (isHorizontal) ? (int)Math.Floor((displayPos.X - rect.X) / wallSectionSize) : (int)Math.Floor((rect.Y - displayPos.Y) / wallSectionSize); @@ -757,6 +807,8 @@ namespace Barotrauma public override void FlipX() { base.FlipX(); + + flippedX = !flippedX; if (prefab.CanSpriteFlipX) { @@ -771,7 +823,7 @@ namespace Barotrauma CreateStairBodies(); } - //todo: flip sprites & wall sections + CreateSections(); } public override XElement Save(XElement parentElement) diff --git a/Subsurface/Source/Sprite.cs b/Subsurface/Source/Sprite.cs index e2851f3f0..b0b41696f 100644 --- a/Subsurface/Source/Sprite.cs +++ b/Subsurface/Source/Sprite.cs @@ -222,56 +222,81 @@ namespace Barotrauma { DrawTiled(spriteBatch, pos, targetSize, Vector2.Zero, color); } + public void DrawTiled(SpriteBatch spriteBatch, Vector2 pos, Vector2 targetSize, Vector2 startOffset, Color color, Point offset) { //how many times the texture needs to be drawn on the x-axis int xTiles = (int)Math.Ceiling((targetSize.X + startOffset.X) / sourceRect.Width); //how many times the texture needs to be drawn on the y-axis int yTiles = (int)Math.Ceiling((targetSize.Y + startOffset.Y) / sourceRect.Height); + + Rectangle texPerspective = sourceRect; + texPerspective.Location += offset; + while (texPerspective.Location.X >= sourceRect.X + sourceRect.Width) + texPerspective.X = sourceRect.X + (texPerspective.Location.X - (sourceRect.X + sourceRect.Width)); + while (texPerspective.Location.Y >= sourceRect.Y + sourceRect.Height) + texPerspective.Y = sourceRect.Y + (texPerspective.Location.Y - (sourceRect.Y + sourceRect.Height)); - Rectangle TexPerspective = sourceRect; - - TexPerspective.Location += offset; - while (TexPerspective.Location.X >= sourceRect.X + sourceRect.Width) - TexPerspective.X = sourceRect.X + (TexPerspective.Location.X - (sourceRect.X + sourceRect.Width)); - while (TexPerspective.Location.Y >= sourceRect.Y + sourceRect.Height) - TexPerspective.Y = sourceRect.Y + (TexPerspective.Location.Y - (sourceRect.Y + sourceRect.Height)); - TexPerspective.Width = (int)Math.Min(targetSize.X, sourceRect.Width); - TexPerspective.Height = (int)Math.Min(targetSize.Y, sourceRect.Height); + texPerspective.Width = (int)Math.Min(targetSize.X, sourceRect.Width); + texPerspective.Height = (int)Math.Min(targetSize.Y, sourceRect.Height); for (int y = 0; y < yTiles; y++) { - TexPerspective.Width = (int)Math.Min(targetSize.X, sourceRect.Width); - TexPerspective.Height = (int)Math.Min(targetSize.Y, sourceRect.Height); - float top = pos.Y + TexPerspective.Height * y; + texPerspective.Width = (int)Math.Min(targetSize.X, sourceRect.Width); + texPerspective.Height = (int)Math.Min(targetSize.Y, sourceRect.Height); + float top = pos.Y + texPerspective.Height * y; for (int x = 0; x < xTiles; x++) { - float left = pos.X + TexPerspective.Width * x; - TexPerspective.Width = Math.Min((int)(targetSize.X - TexPerspective.Width * x), TexPerspective.Width); - TexPerspective.Height = Math.Min((int)(targetSize.Y - TexPerspective.Height * y), TexPerspective.Height); - var movementX = TexPerspective.Width; - var movementY = TexPerspective.Height; - if (TexPerspective.X+TexPerspective.Width > sourceRect.X + sourceRect.Width) + float left = pos.X + texPerspective.Width * x; + texPerspective.Width = Math.Min((int)(targetSize.X - texPerspective.Width * x), texPerspective.Width); + texPerspective.Height = Math.Min((int)(targetSize.Y - texPerspective.Height * y), texPerspective.Height); + + var movementX = texPerspective.Width; + var movementY = texPerspective.Height; + if (texPerspective.Right > sourceRect.Right) { - float diff = (TexPerspective.X + TexPerspective.Width) - (sourceRect.X + sourceRect.Width); - TexPerspective.Width -= (int)diff; - spriteBatch.Draw(texture, new Vector2(left+ TexPerspective.Width, top), new Rectangle(sourceRect.X, TexPerspective.Y,(int)diff, TexPerspective.Height), color, rotation, Vector2.Zero, 1.0f, effects, depth); + if (effects.HasFlag(SpriteEffects.FlipHorizontally)) + { + int diff = (texPerspective.X + texPerspective.Width) - (sourceRect.Right); + + spriteBatch.Draw(texture, + new Vector2(left, top), + new Rectangle(sourceRect.X, texPerspective.Y, diff, texPerspective.Height), + color, rotation, Vector2.Zero, 1.0f, effects, depth); + + texPerspective.Width -= diff; + left += diff; + } + else + { + float diff = (texPerspective.X + texPerspective.Width) - (sourceRect.X + sourceRect.Width); + texPerspective.Width -= (int)diff; + spriteBatch.Draw(texture, + new Vector2(left + texPerspective.Width, top), + new Rectangle(sourceRect.X, texPerspective.Y, (int)diff, texPerspective.Height), + color, rotation, Vector2.Zero, 1.0f, effects, depth); + } } - else if (TexPerspective.Y + TexPerspective.Height > sourceRect.Y + sourceRect.Height) + else if (texPerspective.Bottom > sourceRect.Bottom) { - float diff = (TexPerspective.Y + TexPerspective.Height) - (sourceRect.Y + sourceRect.Height); - TexPerspective.Height -= (int)diff; - spriteBatch.Draw(texture, new Vector2(left, top+ TexPerspective.Height), new Rectangle(TexPerspective.X, sourceRect.Y, TexPerspective.Width, (int)diff), color, rotation, Vector2.Zero, 1.0f, effects, depth); + int diff = (texPerspective.Bottom) - (sourceRect.Bottom); + texPerspective.Height -= diff; + spriteBatch.Draw(texture, + new Vector2(left, top + texPerspective.Height), + new Rectangle(texPerspective.X, sourceRect.Y, texPerspective.Width, diff), + color, rotation, Vector2.Zero, 1.0f, effects, depth); } - spriteBatch.Draw(texture, new Vector2(left,top),TexPerspective, color, rotation, Vector2.Zero, 1.0f, effects, depth); - if (TexPerspective.X+ movementX >= sourceRect.X + sourceRect.Width) - TexPerspective.X = sourceRect.X; - if (TexPerspective.Y + movementY >= sourceRect.Y + sourceRect.Height) - TexPerspective.Y = sourceRect.Y; + spriteBatch.Draw(texture, new Vector2(left, top), texPerspective, color, rotation, Vector2.Zero, 1.0f, effects, depth); + + if (texPerspective.X + movementX >= sourceRect.Right) + texPerspective.X = sourceRect.X; + if (texPerspective.Y + movementY >= sourceRect.Y + sourceRect.Height) + texPerspective.Y = sourceRect.Y; } } } + public void DrawTiled(SpriteBatch spriteBatch, Vector2 pos, Vector2 targetSize, Vector2 startOffset, Color color) { //pos.X = (int)pos.X;