Files
LuaCsForBarotraumaEP/Barotrauma/BarotraumaShared/Source/Items/Components/Holdable/Propulsion.cs
T
Joonas Rikkonen a0d606ef5f Working on improving the serialization system and ObjectProperty/IPropertyObject (TODO: come up with better names). The plan is to:
- Add support for some of the most common types (vectors, colors, rects) so there's no need to parse the values in the setters of the serializable properties (see Holdable.HoldPos for example).
- Make a generic version of the item editing HUD that can be used on any IPropertyObject. Should make it easier to implement things like the character editor, editing structure properties, particle editor, etc.
- Improve the interface of the editing HUD. Instead of having to type in a string value into a textbox, there should be number input fields for numeric properties, sliders for properties that only accept a range of values, a color picker, etc. And tooltips.
2017-11-03 19:41:21 +02:00

115 lines
3.2 KiB
C#

using System.Xml.Linq;
using Microsoft.Xna.Framework;
#if CLIENT
using Microsoft.Xna.Framework.Graphics;
using Barotrauma.Particles;
#endif
namespace Barotrauma.Items.Components
{
class Propulsion : ItemComponent
{
enum UsableIn
{
Air,Water,Both
};
private float force;
private string particles;
private float useState;
private UsableIn usableIn;
[SerializableProperty(0.0f, false)]
public float Force
{
get { return force; }
set { force = value; }
}
#if CLIENT
[SerializableProperty("", false)]
public string Particles
{
get { return particles; }
set { particles = value; }
}
#endif
public Propulsion(Item item, XElement element)
: base(item,element)
{
switch (element.GetAttributeString("usablein", "both").ToLowerInvariant())
{
case "air":
usableIn = UsableIn.Air;
break;
case "water":
usableIn = UsableIn.Water;
break;
case "both":
default:
usableIn = UsableIn.Both;
break;
}
}
public override bool Use(float deltaTime, Character character = null)
{
if (character == null) return false;
if (!character.IsKeyDown(InputType.Aim) || character.Stun>0.0f) return false;
IsActive = true;
useState = 0.1f;
if (character.AnimController.InWater)
{
if (usableIn == UsableIn.Air) return true;
}
else
{
if (usableIn == UsableIn.Water) return true;
}
Vector2 dir = Vector2.Normalize(character.CursorPosition - character.Position);
Vector2 propulsion = dir * force;
if (character.AnimController.InWater) character.AnimController.TargetMovement = dir;
foreach (Limb limb in character.AnimController.Limbs)
{
if (limb.WearingItems.Find(w => w.WearableComponent.Item == this.item)==null) continue;
limb.body.ApplyForce(propulsion);
}
character.AnimController.Collider.ApplyForce(propulsion);
if (character.SelectedItems[0] == item) character.AnimController.GetLimb(LimbType.RightHand).body.ApplyForce(propulsion);
if (character.SelectedItems[1] == item) character.AnimController.GetLimb(LimbType.LeftHand).body.ApplyForce(propulsion);
#if CLIENT
if (!string.IsNullOrWhiteSpace(particles))
{
GameMain.ParticleManager.CreateParticle(particles, item.WorldPosition,
item.body.Rotation + ((item.body.Dir > 0.0f) ? 0.0f : MathHelper.Pi), 0.0f, item.CurrentHull);
}
#endif
return true;
}
public override void Update(float deltaTime, Camera cam)
{
useState -= deltaTime;
if (useState <= 0.0f) IsActive = false;
}
}
}