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
@@ -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);
}
}
}
@@ -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;
@@ -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;
}
}
@@ -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;
}
}
}
}
@@ -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)
+39
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);
}
}
}
@@ -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)
@@ -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)
{
@@ -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);
}