Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding recruitment menu to attacker player in siege #1

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions content/Gamemode/Controller/Siege.Unit.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using Keg.Extensions;
using TC2.Base.Components;

namespace TC2.Siege
{
[IAsset.Hjson(prefix: "unit.", capacity_world: 512, capacity_region: 256, capacity_local: 0)]
public partial interface IUnit: IAsset2<IUnit, IUnit.Data>
{
/*static void IAsset2<IUnit, IUnit.Data>.OnUpdate(IUnit.Definition definition, ref IUnit.Data data_new)
{

}

static void IAsset2<IUnit, IUnit.Data>.OnInit(out string prefix, out string[] extensions, out int capacity_world, out int capacity_region, out int capacity_local)
{
prefix = "unit.";
extensions = new string[]
{
".hjson"
};

capacity_world = 64;
capacity_region = 64;
capacity_local = 64;
}*/

[Serializable]
public partial struct Data
{
public string name;
public string desc;
public Sprite icon;

public int price;
public int reward;

public Prefab.Handle creature;

public Prefab.Handle[] items;
public Prefab.Handle[] equipment;
[Save.NewLine]
public IUnit.ResourceStruct[] resource;
}
[Serializable]
public struct ResourceStruct
{
public string material;
public int quantity;
}

}
}
277 changes: 266 additions & 11 deletions content/Gamemode/Siege.GUI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ namespace TC2.Siege
{
public static partial class Siege
{

#if CLIENT
public partial struct SiegeDefenderGUI: IGUICommand
{
Expand Down Expand Up @@ -93,34 +94,285 @@ public static void OnGUIDefender(Entity entity, [Source.Owned] in Player.Data pl
}
#endif



#if CLIENT
public partial struct SiegeAttackerGUI: IGUICommand
{
public Siege.Gamemode g_siege;
public Siege.Gamemode.State g_siege_state;
public Entity entity;
public static List<uint> unit_indices = new List<uint>(64);
public static float scale = 4;
public Region.Data region;
public Player.Data player;

public void Draw()
{
var window_pos = (GUI.CanvasSize * new Vector2(0.50f, 0.00f)) + new Vector2(0, 64);
using (var window = GUI.Window.Standalone("Siege2", position: window_pos, size: new Vector2(100, 100), pivot: new Vector2(0.50f, 0.00f), padding: new(6, 6)))

using (var widget = Sidebar.Widget.New("attacker_build", "Recruit", "Rifleman_Armor", new Vector2(440, 600), lockable: false))
{
this.StoreCurrentWindowTypeID();
if (window.show)
if (widget.show)
{
GUI.DrawWindowBackground();
var time_left = MathF.Max(this.g_siege_state.t_next_wave - this.g_siege_state.t_match_elapsed, 0.00f);

ref var region = ref Client.GetRegion();
ref var world = ref Client.GetWorld();
ref var game_info = ref Client.GetGameInfo();
GUI.Title("Purchase units");
GUI.NewLine(8);
GUI.DrawMoney(GetMoney(ref region), new Vector2(64, 16));
GUI.SameLine(8);
GUI.TitleCentered($"Next wave {(time_left):0} s", size: 16, color: time_left > 10.00f ? GUI.font_color_title : GUI.font_color_yellow);
GUI.NewLine(26);
GUI.Title("Catalog");
GUI.SameLine(170);
GUI.Title("Queued");
GUI.NewLine();
using (GUI.Scrollbox.New("attacker_unitshop", new Vector2(GUI.GetAvailableWidth() / 2, GUI.GetAvailableHeight())))
{
using (var grid = GUI.Grid.New(size: GUI.GetRemainingSpace()))
{
unit_indices.Clear();
var recipes = IUnit.Database.GetAssets();
foreach (var d_recipe in recipes)
{
ref var recipe = ref d_recipe.GetData();
if (recipe.IsNotNull())
{
unit_indices.Add(d_recipe.id);
}
}

foreach (var pair in unit_indices)
{
//GUI.Text($"{pair.rank}");

ref var recipe = ref IUnit.Database.GetData(pair);
var unit = new IUnit.Handle(pair);

if (new IUnit.Handle(pair).id != 0)
{
var frame_size = recipe.icon.GetFrameSize(scale);
frame_size += new Vector2(8, 8);
frame_size = frame_size.ScaleToNearestMultiple(new Vector2(48, 48));

grid.Push(frame_size);
using (var button = GUI.CustomButton.New(recipe.name, frame_size, sound: GUI.sound_select, sound_volume: 0.10f))
{
GUI.Draw9Slice(GUI.tex_slot_simple, new Vector4(4), button.bb);
GUI.DrawSpriteCentered(recipe.icon, button.bb, scale: scale);

if (button.pressed && GetMoney(ref region) >= recipe.price)
{
PurchaseUnit(ref region, unit);
}
}
if (GUI.IsItemHovered())
{
using (GUI.Tooltip.New())
{
using (GUI.Wrap.Push(325))
{
GUI.Title(recipe.name);
GUI.Text(recipe.desc, color: GUI.font_color_default);
GUI.NewLine(10);
GUI.DrawMoney(recipe.price, new Vector2(32, 8));
GUI.NewLine(10);
GUI.Text("Inventory");
foreach (var item in recipe.items)
{
var itemIcon = item.GetPrefab().GetIcon();
GUI.DrawSprite(itemIcon);
GUI.SameLine();
}
GUI.NewLine();
foreach (var item in recipe.equipment)
{
var itemIcon = item.GetPrefab().GetIcon();
GUI.DrawSprite(itemIcon);
GUI.SameLine();
}
GUI.NewLine();
foreach (var item in recipe.resource)
{
var itemIcon = IMaterial.Database.GetData(item.material).icon;
GUI.DrawSprite(itemIcon);
GUI.SameLine();
}

GUI.Title($"{this.g_siege_state.faction_defenders.id}");
}
}
}
}
}
}
}
GUI.SameLine();
using (GUI.Scrollbox.New("attacker_wavelist", GUI.GetAvailableSize()))
{
FixedArray32<IUnit.Handle> units = GetUnits(ref region);
using (var grid = GUI.Grid.New(size: GUI.GetRemainingSpace()))
{
for (int i = 0; i < units.Length; i++)
{
if (units[i].id != 0)
{
IUnit.Data unit = units[i].GetData();
var frame_size = unit.icon.GetFrameSize(scale);
frame_size += new Vector2(8, 8);
frame_size = frame_size.ScaleToNearestMultiple(new Vector2(48, 48));
grid.Push(frame_size);

using (var button = GUI.CustomButton.New(unit.name + i, frame_size, sound: GUI.sound_select, sound_volume: 0.10f))
{
GUI.Draw9Slice(GUI.tex_slot_simple, new Vector4(4), button.bb);
GUI.DrawSpriteCentered(unit.icon, button.bb, scale: scale);

if (button.pressed)
{
RemoveUnit(ref region, i);
}

}
if (GUI.IsItemHovered())
{
using (GUI.Tooltip.New())
{
using (GUI.Wrap.Push(325))
{
GUI.Title("Remove: " + unit.name);
GUI.Text(unit.desc, color: GUI.font_color_default);
GUI.DrawMoney(unit.price, new Vector2(8, 8));
}
}
}
}
}
}
}
}
}
}
}
[Query]
public delegate void GetPlannnerQuery(ISystem.Info info, Entity entity, [Source.Owned] ref Siege.Planner planner);

private struct PurchaseUnitArgs
{
public IUnit.Handle unit_id;

public PurchaseUnitArgs(IAsset2<IUnit, IUnit.Data>.Handle unit_id)
{
this.unit_id = unit_id;
}
}

public static void PurchaseUnit(ref Region.Data region, IUnit.Handle unit)
{
var arg = new PurchaseUnitArgs(unit);
region.Query<GetPlannnerQuery>(Func).Execute(ref arg);
static void Func(ISystem.Info info, Entity ent_planner, [Source.Owned] ref Siege.Planner planner)
{
ref var arg = ref info.GetParameter<PurchaseUnitArgs>();
if (!arg.IsNull())
{
var rpc = new Siege.BuyUnitRPC
{
unit = arg.unit_id,
};
rpc.Send(ent_planner);
}
}
}

[Query]
public delegate void GetPlannnerUnitsQuery(ISystem.Info info, Entity entity, [Source.Owned] ref Siege.Planner planner);

private struct GetUnitArgs
{
public FixedArray32<IUnit.Handle> units = new FixedArray32<IAsset2<IUnit, IUnit.Data>.Handle>();

public GetUnitArgs()
{
}
}

public static FixedArray32<IUnit.Handle> GetUnits(ref Region.Data region)
{
var arg = new GetUnitArgs()
{
units = new FixedArray32<IUnit.Handle>()
};
region.Query<GetPlannnerUnitsQuery>(Func).Execute(ref arg);
static void Func(ISystem.Info info, Entity ent_planner, [Source.Owned] ref Siege.Planner planner)
{
ref var arg = ref info.GetParameter<GetUnitArgs>();
if (!arg.IsNull())
{
arg.units = planner.orderedUnits;
}
}
return arg.units;
}

[Query]
public delegate void GetPlannnerMoneyQuery(ISystem.Info info, Entity entity, [Source.Owned] ref Siege.Planner planner);

private struct GetMoneyArgs
{
public int money = 0;

public GetMoneyArgs()
{
}
}

public static int GetMoney(ref Region.Data region)
{
var arg = new GetMoneyArgs();
region.Query<GetPlannnerMoneyQuery>(Func).Execute(ref arg);
static void Func(ISystem.Info info, Entity ent_planner, [Source.Owned] ref Siege.Planner planner)
{
ref var arg = ref info.GetParameter<GetMoneyArgs>();
if (!arg.IsNull())
{
arg.money = planner.money;
}
}
return arg.money;
}

[Query]
public delegate void RemovePlannnerUnitQuery(ISystem.Info info, Entity entity, [Source.Owned] ref Siege.Planner planner);

private struct RemoveUnitArgs
{
public int index;

public RemoveUnitArgs(int index)
{
this.index = index;
}
}

public static void RemoveUnit(ref Region.Data region, int index)
{
var arg = new RemoveUnitArgs(index);
region.Query<RemovePlannnerUnitQuery>(Func).Execute(ref arg);
static void Func(ISystem.Info info, Entity ent_planner, [Source.Owned] ref Siege.Planner planner)
{
ref var arg = ref info.GetParameter<RemoveUnitArgs>();
if (!arg.IsNull())
{
var rpc = new RemoveUnitRPC
{
index = arg.index,
};
rpc.Send(ent_planner);
}
}
}

[ISystem.EarlyGUI(ISystem.Mode.Single), HasTag("local", true, Source.Modifier.Owned)]
public static void OnGUIAttacker(Entity entity, [Source.Owned] in Player.Data player, [Source.Global] in Siege.Gamemode g_siege, [Source.Global] in Siege.Gamemode.State g_siege_state)
public static void OnGUIAttacker(Entity entity, ISystem.Info info, [Source.Owned] in Player.Data player, [Source.Global] in Siege.Gamemode g_siege, [Source.Global] in Siege.Gamemode.State g_siege_state)
{
if (player.IsLocal() && player.faction_id == g_siege_state.faction_attackers)
{
Expand All @@ -129,7 +381,10 @@ public static void OnGUIAttacker(Entity entity, [Source.Owned] in Player.Data pl
var gui = new SiegeAttackerGUI()
{
g_siege = g_siege,
g_siege_state = g_siege_state
g_siege_state = g_siege_state,
entity = entity,
player = player,
region = info.GetRegion(),
};
gui.Submit();
}
Expand Down
Loading