117 lines
4.4 KiB
C#
117 lines
4.4 KiB
C#
using Microsoft.Xna.Framework;
|
|
using Microsoft.Xna.Framework.Graphics;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Xml.Linq;
|
|
|
|
namespace Barotrauma
|
|
{
|
|
class BrokenItemSprite
|
|
{
|
|
//sprite will be rendered if the condition of the item is below this
|
|
public readonly float MaxCondition;
|
|
public readonly Sprite Sprite;
|
|
public readonly bool FadeIn;
|
|
|
|
public BrokenItemSprite(Sprite sprite, float maxCondition, bool fadeIn)
|
|
{
|
|
Sprite = sprite;
|
|
MaxCondition = MathHelper.Clamp(maxCondition, 0.0f, 100.0f);
|
|
FadeIn = fadeIn;
|
|
}
|
|
}
|
|
|
|
class ContainedItemSprite
|
|
{
|
|
public readonly Sprite Sprite;
|
|
public readonly bool UseWhenAttached;
|
|
public readonly string[] AllowedContainerIdentifiers;
|
|
public readonly string[] AllowedContainerTags;
|
|
|
|
public ContainedItemSprite(XElement element, string path = "", bool lazyLoad = false)
|
|
{
|
|
Sprite = new Sprite(element, path, lazyLoad: lazyLoad);
|
|
UseWhenAttached = element.GetAttributeBool("usewhenattached", false);
|
|
AllowedContainerIdentifiers = element.GetAttributeStringArray("allowedcontaineridentifiers", new string[0], convertToLowerInvariant: true);
|
|
AllowedContainerTags = element.GetAttributeStringArray("allowedcontainertags", new string[0], convertToLowerInvariant: true);
|
|
}
|
|
|
|
public bool MatchesContainer(Item container)
|
|
{
|
|
if (container == null) { return false; }
|
|
return AllowedContainerIdentifiers.Contains(container.prefab.Identifier) ||
|
|
AllowedContainerTags.Any(t => container.prefab.Tags.Contains(t));
|
|
}
|
|
}
|
|
|
|
partial class ItemPrefab : MapEntityPrefab
|
|
{
|
|
public List<BrokenItemSprite> BrokenSprites = new List<BrokenItemSprite>();
|
|
public List<DecorativeSprite> DecorativeSprites = new List<DecorativeSprite>();
|
|
public List<ContainedItemSprite> ContainedSprites = new List<ContainedItemSprite>();
|
|
public Dictionary<int, List<DecorativeSprite>> DecorativeSpriteGroups = new Dictionary<int, List<DecorativeSprite>>();
|
|
public Sprite InventoryIcon;
|
|
|
|
//only used to display correct color in the sub editor, item instances have their own property that can be edited on a per-item basis
|
|
[Serialize("1.0,1.0,1.0,1.0", false)]
|
|
public Color InventoryIconColor
|
|
{
|
|
get;
|
|
protected set;
|
|
}
|
|
|
|
|
|
[Serialize("", false)]
|
|
public string ImpactSoundTag { get; private set; }
|
|
|
|
public override void DrawPlacing(SpriteBatch spriteBatch, Camera cam)
|
|
{
|
|
Vector2 position = Submarine.MouseToWorldGrid(cam, Submarine.MainSub);
|
|
|
|
if (PlayerInput.RightButtonClicked())
|
|
{
|
|
selected = null;
|
|
return;
|
|
}
|
|
|
|
if (!ResizeHorizontal && !ResizeVertical)
|
|
{
|
|
sprite.Draw(spriteBatch, new Vector2(position.X, -position.Y) + sprite.size / 2.0f * Scale, SpriteColor, scale: Scale);
|
|
|
|
}
|
|
else
|
|
{
|
|
Vector2 placeSize = size;
|
|
if (placePosition == Vector2.Zero)
|
|
{
|
|
if (PlayerInput.LeftButtonHeld()) placePosition = position;
|
|
}
|
|
else
|
|
{
|
|
if (ResizeHorizontal)
|
|
placeSize.X = Math.Max(position.X - placePosition.X, size.X);
|
|
if (ResizeVertical)
|
|
placeSize.Y = Math.Max(placePosition.Y - position.Y, size.Y);
|
|
|
|
position = placePosition;
|
|
}
|
|
|
|
if (sprite != null) sprite.DrawTiled(spriteBatch, new Vector2(position.X, -position.Y), placeSize, color: SpriteColor);
|
|
}
|
|
}
|
|
|
|
public override void DrawPlacing(SpriteBatch spriteBatch, Rectangle placeRect, float scale = 1.0f, SpriteEffects spriteEffects = SpriteEffects.None)
|
|
{
|
|
if (!ResizeHorizontal && !ResizeVertical)
|
|
{
|
|
sprite.Draw(spriteBatch, new Vector2(placeRect.Center.X, -(placeRect.Y - placeRect.Height / 2)), SpriteColor * 0.8f, scale: scale);
|
|
}
|
|
else
|
|
{
|
|
if (sprite != null) sprite.DrawTiled(spriteBatch, new Vector2(placeRect.X, -placeRect.Y), placeRect.Size.ToVector2(), null, SpriteColor * 0.8f);
|
|
}
|
|
}
|
|
}
|
|
}
|