Crew commands

This commit is contained in:
Regalis
2015-11-26 23:04:02 +02:00
parent f1e1b0b4f0
commit c9cc94f701
19 changed files with 406 additions and 69 deletions

View File

@@ -60,6 +60,8 @@
<Compile Include="Source\Characters\AICharacter.cs" />
<Compile Include="Source\Characters\AI\AIController.cs" />
<Compile Include="Source\Characters\AI\AITarget.cs" />
<Compile Include="Source\Characters\AI\Order.cs" />
<Compile Include="Source\Characters\AI\CrewCommander.cs" />
<Compile Include="Source\Characters\AI\HumanAIController.cs" />
<Compile Include="Source\Characters\AI\Objectives\AIObjectiveFindSafety.cs" />
<Compile Include="Source\Characters\AI\Objectives\AIObjectiveFixLeak.cs" />
@@ -71,7 +73,7 @@
<Compile Include="Source\Characters\AI\Objectives\AIObjective.cs" />
<Compile Include="Source\Characters\AI\PathFinder.cs" />
<Compile Include="Source\Characters\AI\PathSteeringManager.cs" />
<Compile Include="Source\Characters\AnimController.cs" />
<Compile Include="Source\Characters\Animation\AnimController.cs" />
<Compile Include="Source\Characters\Attack.cs" />
<Compile Include="Source\Characters\BackgroundSprite\BackgroundSprite.cs" />
<Compile Include="Source\Characters\BackgroundSprite\BackgroundSpriteManager.cs" />
@@ -127,10 +129,10 @@
<Compile Include="Source\Events\RepairTask.cs" />
<Compile Include="Source\GameSession\CrewManager.cs" />
<Compile Include="Source\Characters\AI\EnemyAIController.cs" />
<Compile Include="Source\Characters\FishAnimController.cs" />
<Compile Include="Source\Characters\Animation\FishAnimController.cs" />
<Compile Include="Source\GameSession\GameModes\GameMode.cs" />
<Compile Include="Source\GameSession\HireManager.cs" />
<Compile Include="Source\Characters\HumanoidAnimController.cs" />
<Compile Include="Source\Characters\Animation\HumanoidAnimController.cs" />
<Compile Include="Source\GameSession\GameModes\SinglePlayerMode.cs" />
<Compile Include="Source\GameSession\GameModes\TraitorMode.cs" />
<Compile Include="Source\GUI\ComponentStyle.cs" />
@@ -160,7 +162,7 @@
<Compile Include="Source\Items\ItemInventory.cs" />
<Compile Include="Source\Characters\Limb.cs" />
<Compile Include="Source\GameSession\GameSession.cs" />
<Compile Include="Source\Characters\Ragdoll.cs" />
<Compile Include="Source\Characters\Animation\Ragdoll.cs" />
<Compile Include="Source\Characters\StatusEffect.cs" />
<Compile Include="Source\DebugConsole.cs" />
<Compile Include="Source\Events\ScriptedTask.cs" />

View File

@@ -0,0 +1,160 @@
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Barotrauma
{
class CrewCommander
{
CrewManager crewManager;
GUIFrame frame;
//GUIListBox characterList;
public GUIFrame Frame
{
get { return IsOpen ? frame : null; }
}
public bool IsOpen
{
get;
private set;
}
public CrewCommander(CrewManager crewManager)
{
this.crewManager = crewManager;
}
public void ToggleGUIFrame()
{
IsOpen = !IsOpen;
if (IsOpen && frame == null) CreateGUIFrame();
}
private void CreateGUIFrame()
{
frame = new GUIFrame(Rectangle.Empty, Color.Black * 0.3f);
frame.Padding = new Vector4(200.0f, 100.0f, 200.0f, 100.0f);
int x = 0, y = 0;
foreach (Character character in crewManager.characters)
{
GUIButton characterButton = new GUIButton(new Rectangle(x,y, 150, 40), "", Color.Transparent, null, frame);
characterButton.UserData = character;
characterButton.Padding = new Vector4(5.0f, 5.0f, 5.0f, 5.0f);
if (character == Character.Controlled)
{
characterButton.CanBeSelected = false;
characterButton.Color = Color.LightGray * 0.3f;
}
else
{
characterButton.HoverColor = Color.LightGray * 0.5f;
characterButton.SelectedColor = Color.Gold * 0.5f;
}
string name = character.Info.Name.Replace(' ', '\n');
GUITextBlock textBlock = new GUITextBlock(
new Rectangle(40, 0, 0, 25),
name,
Color.Transparent, Color.White,
Alignment.Left, Alignment.Left,
null, characterButton, false);
textBlock.Font = GUI.SmallFont;
textBlock.Padding = new Vector4(5.0f, 0.0f, 5.0f, 0.0f);
new GUIImage(new Rectangle(-10, -5, 0, 0), character.AnimController.Limbs[0].sprite, Alignment.Left, characterButton);
x += 160;
}
x = 0;
y = 150;
foreach (Order command in Order.List)
{
var button = new GUIButton(new Rectangle(x, y, 150, 20), command.Name, GUI.Style, frame);
button.UserData = command;
button.OnClicked = SetOrder;
x += button.Rect.Width + 10;
}
}
private bool SetOrder(GUIButton button, object userData)
{
Order order = userData as Order;
List<Character> selectedCharacters = new List<Character>();
foreach (GUIComponent child in frame.children)
{
var characterButton = child as GUIButton;
characterButton.State = GUIComponent.ComponentState.None;
if (!characterButton.Selected) continue;
characterButton.Selected = false;
var character = child.UserData as Character;
if (character == null) continue;
var humanAi = character.AIController as HumanAIController;
if (humanAi == null) continue;
var existingOrder = characterButton.children.Find(c => c.UserData as Order != null);
if (existingOrder != null) characterButton.RemoveChild(existingOrder);
var orderFrame = new GUIFrame(new Rectangle(0, characterButton.Rect.Height, 0, 30 + order.Options.Length*15), null, characterButton);
orderFrame.OutlineColor = Color.LightGray * 0.8f;
orderFrame.Padding = new Vector4(5.0f, 5.0f, 5.0f, 5.0f);
orderFrame.UserData = order;
new GUITextBlock(new Rectangle(0,0,0,20), order.DoingText, GUI.Style, Alignment.TopLeft, Alignment.TopCenter, orderFrame);
var optionList = new GUIListBox(new Rectangle(0,20,0,80), Color.Transparent, null, orderFrame);
optionList.UserData = order;
optionList.OnSelected = SelectOrderOption;
foreach (string option in order.Options)
{
var optionBox = new GUITextBlock(new Rectangle(0,0,0,15), option, GUI.Style, optionList);
optionBox.Font = GUI.SmallFont;
optionBox.UserData = option;
}
humanAi.SetOrder(order, "");
}
//characterList.Deselect();
return true;
}
private bool SelectOrderOption(GUIComponent component, object userData)
{
string option = userData.ToString();
Order order = component.Parent.UserData as Order;
Character character = component.Parent.Parent.Parent.UserData as Character;
var humanAi = character.AIController as HumanAIController;
if (humanAi == null) return false;
humanAi.SetOrder(order, option);
return true;
}
public void Draw(SpriteBatch spriteBatch)
{
if (!IsOpen) return;
frame.Draw(spriteBatch);
}
}
}

View File

@@ -58,12 +58,17 @@ namespace Barotrauma
Character.AnimController.TargetDir = Character.AnimController.TargetMovement.X > 0.0f ? Direction.Right : Direction.Left;
}
float currObjectivePriority = objectiveManager.CurrentObjective == null ? 0.0f : objectiveManager.CurrentObjective.GetPriority(Character);
float currObjectivePriority = objectiveManager.GetCurrentPriority(Character);
float moveSpeed = MathHelper.Clamp(currObjectivePriority/10.0f, 1.0f, 3.0f);
steeringManager.Update(moveSpeed);
}
public void SetOrder(Order order, string option)
{
objectiveManager.SetOrder(order, option);
}
public override void SelectTarget(AITarget target)
{
selectedAiTarget = target;

View File

@@ -14,10 +14,13 @@ namespace Barotrauma
Vector2 targetPos;
bool repeat;
public override bool CanBeCompleted
{
get
{
if (repeat) return true;
var pathSteering = character.AIController.SteeringManager as IndoorsSteeringManager;
return (pathSteering.CurrentPath == null || !pathSteering.CurrentPath.Unreachable);
}
@@ -28,10 +31,11 @@ namespace Barotrauma
get { return target; }
}
public AIObjectiveGoTo(AITarget target, Character character)
public AIObjectiveGoTo(AITarget target, Character character, bool repeat = false)
: base (character)
{
this.target = target;
this.repeat = false;
}
@@ -51,6 +55,7 @@ namespace Barotrauma
public override bool IsCompleted()
{
if (repeat) return false;
return Vector2.Distance(target != null ? target.SimPosition : ConvertUnits.ToDisplayUnits(targetPos), character.SimPosition) < 0.5f;
}
}

View File

@@ -1,4 +1,5 @@
using System;
using Barotrauma.Items.Components;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
@@ -7,14 +8,19 @@ namespace Barotrauma
{
class AIObjectiveManager
{
const float OrderPriority = 50.0f;
private List<AIObjective> objectives;
private Character character;
private AIObjective currentOrder;
public AIObjective CurrentObjective
{
get
{
if (currentOrder != null) return currentOrder;
return objectives.Any() ? objectives[0] : null;
}
}
@@ -33,9 +39,15 @@ namespace Barotrauma
objectives.Add(objective);
}
public float GetCurrentPriority(Character character)
{
if (currentOrder != null) return OrderPriority;
return (CurrentObjective == null) ? 0.0f : CurrentObjective.GetPriority(character);
}
public void UpdateObjectives()
{
if (!objectives.Any()) return;
if (currentOrder != null || !objectives.Any()) return;
//remove completed objectives
objectives = objectives.FindAll(o => !o.IsCompleted());
@@ -57,8 +69,33 @@ namespace Barotrauma
public void DoCurrentObjective(float deltaTime)
{
if (currentOrder != null)
{
currentOrder.TryComplete(deltaTime);
return;
}
if (!objectives.Any()) return;
objectives[0].TryComplete(deltaTime);
}
public void SetOrder(Order order, string option)
{
switch (order.Name.ToLower())
{
case "follow":
currentOrder = new AIObjectiveGoTo(Character.Controlled.AiTarget, character, true);
break;
case "operate reactor":
var reactorItem = Item.ItemList.Find(i => i.GetComponent<Reactor>() != null);
if (reactorItem == null) return;
currentOrder = new AIObjectiveOperateItem(reactorItem.GetComponent<Reactor>(), character);
break;
default:
currentOrder = null;
break;
}
}
}
}

View File

@@ -1,4 +1,6 @@
using System;
using Barotrauma.Items.Components;
using Microsoft.Xna.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
@@ -7,9 +9,9 @@ namespace Barotrauma
{
class AIObjectiveOperateItem : AIObjective
{
private Item targetItem;
private ItemComponent targetItem;
public AIObjectiveOperateItem(Item item, Character character)
public AIObjectiveOperateItem(ItemComponent item, Character character)
:base (character)
{
targetItem = item;
@@ -17,7 +19,13 @@ namespace Barotrauma
protected override void Act(float deltaTime)
{
//item.AIOperate(float deltaTime, Character character) or something
if (Vector2.Distance(character.SimPosition, targetItem.Item.SimPosition) < targetItem.Item.PickDistance)
{
//targetItem.Pick(character, false, true);
return;
}
subObjectives.Add(new AIObjectiveGoTo(targetItem.Item.SimPosition, character));
}
public override bool IsDuplicate(AIObjective otherObjective)

View File

@@ -0,0 +1,39 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Barotrauma
{
class Order
{
public static List<Order> List;
public readonly string Name;
public readonly string DoingText;
Sprite buttonSprite;
public readonly string[] Options;
static Order()
{
List = new List<Order>();
new Order("Follow", "Following");
new Order("Operate Reactor", "Operating reactor", new string[] {"Power Up", "Shutdown"});
new Order("Dismiss", "Dismissed");
}
private Order(string name, string doingText, string[] parameters = null)
{
this.Name = name;
this.DoingText = doingText;
this.Options = parameters == null ? new string[0] : parameters;
List.Add(this);
}
}
}

View File

@@ -35,9 +35,9 @@ namespace Barotrauma
wanderAngle = Rand.Range(0.0f, MathHelper.TwoPi);
}
public void SteeringSeek(Vector2 target, float speed = 1.0f)
public void SteeringSeek(Vector2 targetSimPos, float speed = 1.0f)
{
steering += DoSteeringSeek(target, speed);
steering += DoSteeringSeek(targetSimPos, speed);
}
public void SteeringWander(float speed = 1.0f)

View File

@@ -306,7 +306,7 @@ namespace Barotrauma
float stairPosY = structure.StairDirection == Direction.Right ?
lowestLimb.Position.X - structure.Rect.X : structure.Rect.Width - (lowestLimb.Position.X - structure.Rect.X);
if (lowestLimb.Position.Y < stairPosY) return;
if (lowestLimb.Position.Y < structure.Rect.Y - structure.Rect.Height + stairPosY) return false;
if (targetMovement.Y < 0.5f)
{
@@ -383,7 +383,9 @@ namespace Barotrauma
}
public virtual void Draw(SpriteBatch spriteBatch)
{
{
if (simplePhysicsEnabled) return;
foreach (Limb limb in Limbs)
{
limb.Draw(spriteBatch);
@@ -392,7 +394,8 @@ namespace Barotrauma
public void DebugDraw(SpriteBatch spriteBatch)
{
if (!GameMain.DebugDraw) return;
if (!GameMain.DebugDraw || !character.Enabled) return;
if (simplePhysicsEnabled) return;
foreach (Limb limb in Limbs)
{

View File

@@ -67,6 +67,8 @@ namespace Barotrauma
Rand.Range(-prefab.Speed, prefab.Speed),
Rand.Range(-prefab.Speed, prefab.Speed),
Rand.Range(0.0f, prefab.WanderZAmount));
checkWallsTimer = Rand.Range(0.0f, CheckWallsInterval);
}

View File

@@ -39,6 +39,19 @@ namespace Barotrauma
}
}
public override Color SelectedColor
{
get
{
return base.SelectedColor;
}
set
{
base.SelectedColor = value;
frame.SelectedColor = value;
}
}
public override Color OutlineColor
{
get { return base.OutlineColor; }
@@ -120,7 +133,7 @@ namespace Barotrauma
{
if (!Visible) return;
if (rect.Contains(PlayerInput.MousePosition) && Enabled && (MouseOn == null || MouseOn == this || IsParentOf(MouseOn)))
if (rect.Contains(PlayerInput.MousePosition) && CanBeSelected && Enabled && (MouseOn == null || MouseOn == this || IsParentOf(MouseOn)))
{
state = ComponentState.Hover;
if (PlayerInput.GetMouseState.LeftButton == ButtonState.Pressed)
@@ -136,6 +149,11 @@ namespace Barotrauma
{
if (OnClicked(this, UserData) && CanBeSelected) state = ComponentState.Selected;
}
else
{
Selected = !Selected;
// state = state == ComponentState.Selected ? ComponentState.None : ComponentState.Selected;
}
}
}
else

View File

@@ -1,13 +1,15 @@
using System;
using System.Linq;
using System.Diagnostics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System.Collections.Generic;
namespace Barotrauma
{
public class GUIListBox : GUIComponent
{
protected GUIComponent selected;
protected List<GUIComponent> selected;
public delegate bool OnSelectedHandler(GUIComponent component, object obj);
public OnSelectedHandler OnSelected;
@@ -27,19 +29,26 @@ namespace Barotrauma
private bool enabled;
public bool SelectMultiple;
public GUIComponent Selected
{
get
{
return selected;
return selected.Any() ? selected[0] : null;
}
}
public List<GUIComponent> AllSelected
{
get { return selected; }
}
public object SelectedData
{
get
{
return (selected == null) ? null : selected.UserData;
return (Selected == null) ? null : Selected.UserData;
}
}
@@ -47,8 +56,8 @@ namespace Barotrauma
{
get
{
if (selected == null) return -1;
return children.FindIndex(x => x == selected);
if (Selected == null) return -1;
return children.FindIndex(x => x == Selected);
}
}
@@ -108,12 +117,14 @@ namespace Barotrauma
{
}
public GUIListBox(Rectangle rect, Color? color, Alignment alignment, GUIStyle style = null, GUIComponent parent = null)
public GUIListBox(Rectangle rect, Color? color, Alignment alignment, GUIStyle style = null, GUIComponent parent = null, bool isHorizontal = false)
: base(style)
{
this.rect = rect;
this.alignment = alignment;
selected = new List<GUIComponent>();
if (color!=null) this.color = (Color)color;
if (parent != null)
@@ -121,8 +132,17 @@ namespace Barotrauma
scrollBarHidden = true;
scrollBar = new GUIScrollBar(
new Rectangle(this.rect.X + this.rect.Width-20, this.rect.Y, 20, this.rect.Height), color, 1.0f, GUI.Style);
if (isHorizontal)
{
scrollBar = new GUIScrollBar(
new Rectangle(this.rect.X, this.rect.Bottom-20, this.rect.Width, 20), color, 1.0f, GUI.Style);
}
else
{
scrollBar = new GUIScrollBar(
new Rectangle(this.rect.Right - 20, this.rect.Y, 20, this.rect.Height), color, 1.0f, GUI.Style);
}
frame = new GUIFrame(Rectangle.Empty, style, this);
if (style != null) style.Apply(frame, this);
@@ -140,16 +160,17 @@ namespace Barotrauma
public void Select(object selection)
{
foreach (GUIComponent child in children)
for (int i = 0; i < children.Count; i++ )
{
if (child.UserData != selection) continue;
selected = child;
if (OnSelected != null) OnSelected(selected, selected.UserData);
return;
if (children[i].UserData != selection) continue;
Select(i);
if (OnSelected != null) OnSelected(Selected, Selected.UserData);
if (!SelectMultiple) return;
}
}
public override void Update(float deltaTime)
{
if (!Visible) return;
@@ -167,17 +188,34 @@ namespace Barotrauma
public void Select(int childIndex)
{
//children[0] is the GUIFrame, ignore it
childIndex += 1;
//childIndex += 1;
if (childIndex >= children.Count || childIndex<0) return;
selected = children[childIndex];
if (OnSelected != null) OnSelected(selected, selected.UserData);
if (SelectMultiple)
{
if (selected.Contains(children[childIndex]))
{
selected.Remove(children[childIndex]);
}
else
{
selected.Add(children[childIndex]);
}
}
else
{
selected.Clear();
selected.Add(children[childIndex]);
}
if (OnSelected != null) OnSelected(Selected, Selected.UserData);
}
public void Deselect()
{
selected = null;
selected.Clear();
}
public void UpdateScrollBarSize()
@@ -218,14 +256,14 @@ namespace Barotrauma
public override void ClearChildren()
{
base.ClearChildren();
selected = null;
selected.Clear();
}
public override void RemoveChild(GUIComponent child)
{
base.RemoveChild(child);
if (selected == child) selected = null;
if (selected.Contains(child)) selected.Remove(child);
UpdateScrollBarSize();
}
@@ -272,8 +310,16 @@ namespace Barotrauma
GUIComponent child = children[i];
if (child == frame) continue;
child.Rect = new Rectangle(child.Rect.X, y, child.Rect.Width, child.Rect.Height);
y += child.Rect.Height + spacing;
child.Rect = new Rectangle(x, y, child.Rect.Width, child.Rect.Height);
if (scrollBar.IsHorizontal)
{
x += child.Rect.Width + spacing;
}
else
{
y += child.Rect.Height + spacing;
}
if (child.Rect.Y + child.Rect.Height < rect.Y) continue;
if (child.Rect.Y + child.Rect.Height > rect.Y + rect.Height) break;
@@ -284,30 +330,31 @@ namespace Barotrauma
continue;
}
if (selected == child)
{
child.State = ComponentState.Selected;
if (CheckSelected != null)
{
if (CheckSelected() != selected.UserData) selected = null;
}
}
else if (enabled && child.CanBeFocused &&
if (enabled && child.CanBeFocused &&
(MouseOn == this || (MouseOn != null && this.IsParentOf(MouseOn))) && child.Rect.Contains(PlayerInput.MousePosition))
{
child.State = ComponentState.Hover;
if (PlayerInput.LeftButtonClicked())
{
Debug.WriteLine("clicked");
selected = child;
if (OnSelected != null)
{
if (!OnSelected(selected, child.UserData)) selected = null;
}
Select(i);
//selected = child;
//if (OnSelected != null)
//{
// if (!OnSelected(selected, child.UserData)) selected = null;
//}
}
}
else if(selected.Contains(child))
{
child.State = ComponentState.Selected;
if (CheckSelected != null)
{
if (CheckSelected() != child.UserData) selected.Remove(child);
}
}
else
{
child.State = ComponentState.None;

View File

@@ -12,9 +12,6 @@ namespace Barotrauma
public List<Character> characters;
public List<CharacterInfo> characterInfos;
//public static string mapFile;
//public string saveFile;
private int money;
private GUIFrame guiFrame;
@@ -24,7 +21,8 @@ namespace Barotrauma
private GUIButton crewButton;
protected GUIFrame crewFrame;
private CrewCommander commander;
public int Money
{
get { return money; }
@@ -45,6 +43,8 @@ namespace Barotrauma
crewButton = new GUIButton(new Rectangle(0, 00, 100, 20), "Crew", GUI.Style, guiFrame);
crewButton.OnClicked = ToggleCrewFrame;
commander = new CrewCommander(this);
money = 10000;
}
@@ -102,13 +102,19 @@ namespace Barotrauma
textBlock.Font = GUI.SmallFont;
textBlock.Padding = new Vector4(5.0f, 0.0f, 5.0f, 0.0f);
new GUIImage(new Rectangle(-10, -10, 0, 0), character.AnimController.Limbs[0].sprite, Alignment.Left, frame);
new GUIImage(new Rectangle(-10, -5, 0, 0), character.AnimController.Limbs[0].sprite, Alignment.Left, frame);
}
public void Update(float deltaTime)
{
guiFrame.Update(deltaTime);
if (PlayerInput.KeyHit(Microsoft.Xna.Framework.Input.Keys.I))
{
commander.ToggleGUIFrame();
}
if (commander.Frame != null) commander.Frame.Update(deltaTime);
if (crewFrameOpen) crewFrame.Update(deltaTime);
}
@@ -235,9 +241,15 @@ namespace Barotrauma
public void Draw(SpriteBatch spriteBatch)
{
guiFrame.Draw(spriteBatch);
if (crewFrameOpen) crewFrame.Draw(spriteBatch);
if (commander.IsOpen)
{
commander.Draw(spriteBatch);
}
else
{
guiFrame.Draw(spriteBatch);
if (crewFrameOpen) crewFrame.Draw(spriteBatch);
}
}
public void Save(XElement parentElement)

View File

@@ -229,10 +229,10 @@ namespace Barotrauma
Vector2 textSize = GUI.Font.MeasureString(toolTip);
Vector2 rectSize = textSize * 1.2f;
Vector2 pos = new Vector2(highlightedSlot.Center.X, highlightedSlot.Bottom);
Vector2 pos = new Vector2(highlightedSlot.Center.X, highlightedSlot.Y-rectSize.Y);
pos.X = (int)pos.X;
pos.Y = (int)pos.Y;
GUI.DrawRectangle(spriteBatch, pos, rectSize, Color.Black * 0.8f, true);
spriteBatch.DrawString(GUI.Font, toolTip,
new Vector2((int)pos.X + rectSize.X * 0.5f, (int)pos.Y + rectSize.Y * 0.5f),

View File

@@ -89,9 +89,8 @@ namespace Barotrauma
Color clr = (isSelected) ? Color.Red : Color.LightGreen;
GUI.DrawRectangle(spriteBatch, new Rectangle(pos.X - rect.Width / 2, -pos.Y - rect.Height / 2, rect.Width, rect.Height), clr, true);
spriteBatch.DrawString(GUI.SmallFont, Position.ToString(), new Vector2(Position.X, -Position.Y), Color.White);
//spriteBatch.DrawString(GUI.SmallFont, Position.ToString(), new Vector2(Position.X, -Position.Y), Color.White);
foreach (MapEntity e in linkedTo)
{

Binary file not shown.