Skip to content

Commit

Permalink
Add entity inspector tool to god menu
Browse files Browse the repository at this point in the history
  • Loading branch information
Blecki committed May 14, 2020
1 parent ec48660 commit d8741ad
Show file tree
Hide file tree
Showing 15 changed files with 9,293 additions and 8,465 deletions.
1 change: 1 addition & 0 deletions DwarfCorp/Components/GameComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public partial class GameComponent
{
public string Name { get; set; }
public uint GlobalID { get; set; }
public Gui.Widget GuiTag = null;

[JsonProperty] private uint ParentID = ComponentManager.InvalidID;
[JsonIgnore] private GameComponent CachedParent = null;
Expand Down
13 changes: 5 additions & 8 deletions DwarfCorp/Components/Graphics/SelectionCircle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,20 @@ public SelectionCircle()
{
}

public SelectionCircle(ComponentManager manager) :
public SelectionCircle(ComponentManager manager, GameComponent Creature) :
base(manager, "Selection", Matrix.CreateRotationX((float)Math.PI), Vector3.One, Vector3.Zero)
{
var shadowTransform = Matrix.CreateRotationX((float)Math.PI * 0.5f);
shadowTransform.Translation = new Vector3(0.0f, -0.25f, 0.0f);

LocalTransform = shadowTransform;
FitToCreature(Creature);
CreateCosmeticChildren(manager);
SetFlagRecursive(Flag.Visible, false);
}

public void FitToParent()
public void FitToCreature(GameComponent Parent)
{
var shadowTransform = Matrix.CreateRotationX((float)Math.PI * 0.5f);
var bbox = (Parent as GameComponent).GetBoundingBox();
shadowTransform.Translation = new Vector3(0.0f, -0.5f * (bbox.Max.Y - bbox.Min.Y), 0.0f);
float scale = bbox.Max.X - bbox.Min.X;
shadowTransform.Translation = new Vector3(0.0f, (Parent.BoundingBoxSize.Y * -0.5f) - Parent.LocalBoundingBoxOffset.Y, 0.0f);
float scale = Parent.BoundingBoxSize.X * 2;
shadowTransform = shadowTransform * Matrix.CreateScale(scale);
LocalTransform = shadowTransform;
}
Expand Down
3 changes: 2 additions & 1 deletion DwarfCorp/Components/Graphics/Shadow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,11 @@ private void ImplementUpdate(DwarfTime gameTime, ChunkManager chunks)
var h = shadowTarget.Coordinate.Y + 1;
Vector3 pos = p.GlobalTransform.Translation;
pos.Y = h;
pos += VertexNoise.GetNoiseVectorFromRepeatingTexture(pos);
float scaleFactor = GlobalScale / (Math.Max((p.GlobalTransform.Translation.Y - h) * 0.25f, 1));
Matrix newTrans = OriginalTransform;
newTrans *= Matrix.CreateScale(scaleFactor);
newTrans.Translation = (pos - p.GlobalTransform.Translation) + new Vector3(0.0f, 0.1f, 0.0f);
newTrans.Translation = (pos - p.GlobalTransform.Translation) + new Vector3(0.0f, 0.05f, 0.0f);
LightRamp = new Color(LightRamp.R, LightRamp.G, LightRamp.B, (int)(scaleFactor * 255));
Matrix globalRotation = p.GlobalTransform;
globalRotation.Translation = Vector3.Zero;
Expand Down
2 changes: 1 addition & 1 deletion DwarfCorp/DCMono.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\$(MonoGamePlatform)\$(Platform)\$(Configuration)\</OutputPath>
<DefineConstants>TRACE;WINDOWS</DefineConstants>
<DefineConstants>TRACE;WINDOWS GEMMONO</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
Expand Down
2 changes: 2 additions & 0 deletions DwarfCorp/DwarfCorpXNA.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,8 @@
<Compile Include="Player\Crafting\DeconstructObjectTool.cs" />
<Compile Include="Player\Crafting\GetResourcesOfApparentType.cs" />
<Compile Include="Player\Crafting\Resources\ResourceDes.cs" />
<Compile Include="Player\Tools\EntityInspector\EntityInspector.cs" />
<Compile Include="Player\Tools\EntityInspector\EntityInspectorPanel.cs" />
<Compile Include="TaskManagement\ActHelper-CreateEquipmentCheckAct.cs" />
<Compile Include="TaskManagement\Always.cs" />
<Compile Include="TaskManagement\ActHelper-ApplyWearToTool.cs" />
Expand Down
2 changes: 1 addition & 1 deletion DwarfCorp/Entities/Monsters/Elf.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public Elf()
public Elf(CreatureStats stats, Faction faction, ComponentManager manager, string name, Vector3 position) :
base(manager, stats, faction, name)
{
Physics = new Physics(manager, "Elf", Matrix.CreateTranslation(position), new Vector3(0.5f, 1.0f, 0.5f), new Vector3(0.0f, -0.0f, 0.0f), 1.0f, 1.0f, 0.999f, 0.999f, new Vector3(0, -10, 0));
Physics = new Physics(manager, "Elf", Matrix.CreateTranslation(position), new Vector3(0.5f, 1.0f, 0.5f), new Vector3(0.0f, 0.0f, 0.0f), 1.0f, 1.0f, 0.999f, 0.999f, new Vector3(0, -10, 0));
Physics.AddChild(this);
Initialize();
}
Expand Down
6 changes: 6 additions & 0 deletions DwarfCorp/GameStates/Play/GodMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,12 @@ public override void Construct()
}
},

new HorizontalMenuTray.MenuItem
{
Text = "ENTITY INSPECTOR",
OnClick = (sender, args) => World.UserInterface.ChangeTool("EntityInspector")
},

new HorizontalMenuTray.MenuItem
{
Text = "SPAWN",
Expand Down
4 changes: 3 additions & 1 deletion DwarfCorp/Graphics/Animation/AnimationLibrary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ public static Dictionary<String, Animation> LoadCompositeAnimationSet(String Pat
Frames = a.CompositeFrames.Select(f => f.Cells[0].Tile).ToList(),
};
}

a.PushFrames();
return a as Animation;

Expand Down Expand Up @@ -165,7 +166,8 @@ private static List<CompositeAnimation> GenerateAnimations(
Name = descriptor.Name,
Speeds = descriptor.Speed,
Loops = !descriptor.PlayOnce,
YOffset = descriptor.YOffset
YOffset = descriptor.YOffset,
FrameHZ = 5.0f
}).ToList();
}

Expand Down
202 changes: 202 additions & 0 deletions DwarfCorp/Player/Tools/EntityInspector/EntityInspector.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DwarfCorp.GameStates;
using LibNoise.Modifiers;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Newtonsoft.Json;

namespace DwarfCorp
{
public class EntityInspectorTool : PlayerTool
{
public static GameComponent SelectedEntity = null;
public static EntityInspectionPanel InspectorGui = null;

[ToolFactory("EntityInspector")]
private static PlayerTool _factory(WorldManager World)
{
return new EntityInspectorTool(World);
}

private List<GameComponent> underMouse = null;

public EntityInspectorTool(WorldManager World)
{
this.World = World;
}

public override void Destroy()
{
}

public override void OnBegin(Object Arguments)
{
SelectedEntity = null;
}

public override void OnEnd()
{
SelectedEntity = null;

// Clean up the GUI if it exists.
if (InspectorGui != null)
InspectorGui.Close();
InspectorGui = null;
}

public EntityInspectorTool()
{

}

public override void OnVoxelsSelected(List<VoxelHandle> voxels, InputManager.MouseButton button)
{

}

bool IsNotSelectedDwarf(GameComponent body)
{
if (body == null)
return true;

var dwarves = body.EnumerateAll().OfType<Creature>().ToList();

if (dwarves.Count <= 0)
return false;

Creature dwarf = dwarves[0];
return dwarf.Faction == World.PlayerFaction && !World.PersistentData.SelectedMinions.Contains(dwarf.AI);
}

bool IsDwarf(GameComponent body)
{
if (body == null)
return false;

var dwarves = body.EnumerateAll().OfType<Creature>().ToList();

if (dwarves.Count <= 0)
return false;

return dwarves[0].Faction == World.PlayerFaction;
}

protected void SelectDwarves(List<GameComponent> bodies)
{
if (bodies.Count > 0)
SelectedEntity = bodies[0].GetRoot();
}

public override void OnBodiesSelected(List<GameComponent> bodies, InputManager.MouseButton button)
{
switch(button)
{
case InputManager.MouseButton.Left:
SelectDwarves(bodies);
break;
}
}

public static string GetMouseOverText(IEnumerable<GameComponent> bodies)
{
var sb = new StringBuilder();
var bodyList = bodies.ToList();
var first = true;

for (int i = 0; i < bodyList.Count; i++)
{
if (!first)
sb.AppendLine();
first = false;

if (bodyList[i].GetComponent<Creature>().HasValue(out var dwarf))
{
sb.Append(dwarf.Stats.FullName + " (" + (dwarf.Stats.Title ?? dwarf.Stats.CurrentClass.Name) + ")");

if (dwarf.Stats.IsAsleep)
sb.Append(" UNCONSCIOUS ");

if (dwarf.Stats.IsOnStrike)
sb.Append(" ON STRIKE");
}
else
sb.Append(bodyList[i].GetDescription());
}

return sb.ToString();
}

public override void DefaultOnMouseOver(IEnumerable<GameComponent> bodies)
{
World.UserInterface.ShowTooltip(GetMouseOverText(bodies));
underMouse = bodies.ToList();
}

public override void OnMouseOver(IEnumerable<GameComponent> bodies)
{
DefaultOnMouseOver(bodies);
}

public override void Update(DwarfGame game, DwarfTime time)
{
World.UserInterface.VoxSelector.Enabled = false;
World.UserInterface.BodySelector.Enabled = true;
World.UserInterface.BodySelector.AllowRightClickSelection = false;

World.UserInterface.SetMouse(World.UserInterface.MousePointer);


// If no gui bit exists, go ahead and create it. If it does, update the selected entity part of it.
if (InspectorGui == null)
{
InspectorGui = World.UserInterface.Gui.ConstructWidget(new EntityInspectionPanel()) as EntityInspectionPanel;
InspectorGui.Rect = new Rectangle(0, 0, 256, 512);
InspectorGui.Layout();
World.UserInterface.Gui.RootItem.AddChild(InspectorGui);
}
InspectorGui.SelectedEntity = SelectedEntity;
}

public Rectangle GetScreenRect(BoundingBox Box, Camera Camera)
{
Vector3 ext = (Box.Max - Box.Min);
Vector3 center = Box.Center();

Vector3 p1 = Camera.Project(Box.Min);
Vector3 p2 = Camera.Project(Box.Max);
Vector3 p3 = Camera.Project(Box.Min + new Vector3(ext.X, 0, 0));
Vector3 p4 = Camera.Project(Box.Min + new Vector3(0, ext.Y, 0));
Vector3 p5 = Camera.Project(Box.Min + new Vector3(0, 0, ext.Z));
Vector3 p6 = Camera.Project(Box.Min + new Vector3(ext.X, ext.Y, 0));

Vector3 min = MathFunctions.Min(p1, p2, p3, p4, p5, p6);
Vector3 max = MathFunctions.Max(p1, p2, p3, p4, p5, p6);

return new Rectangle((int)min.X, (int)min.Y, (int)(max.X - min.X), (int)(max.Y - min.Y));
}

public override void Render2D(DwarfGame game, DwarfTime time)
{
DwarfGame.SpriteBatch.Begin();
var entity = World.UserInterface.BodySelector.CurrentBodies.FirstOrDefault();
if (entity != null)
Drawer2D.DrawRect(DwarfGame.SpriteBatch, GetScreenRect(entity.BoundingBox, World.Renderer.Camera), Color.White, 1.0f);

DwarfGame.SpriteBatch.End();
}

public override void Render3D(DwarfGame game, DwarfTime time)
{
}


public override void OnVoxelsDragged(List<VoxelHandle> voxels, InputManager.MouseButton button)
{

}
}
}
Loading

0 comments on commit d8741ad

Please sign in to comment.