Skip to content

Commit e75abc3

Browse files
committed
SkytechThrusters setup
1 parent 9c63adf commit e75abc3

12 files changed

Lines changed: 1073 additions & 0 deletions
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
using Sandbox.ModAPI;
2+
using System.Collections.Generic;
3+
using VRage.Game.ModAPI;
4+
using Skytech.Thrusters.ModularAssemblies.Communication;
5+
6+
namespace Skytech.Thrusters
7+
{
8+
/// <summary>
9+
/// Single assembly instance logic class. You should put your assembly logic here!
10+
/// </summary>
11+
internal abstract class AssemblyBase
12+
{
13+
private static ModularDefinitionApi ModularApi => ModularAssemblies.ModularDefinition.ModularApi;
14+
15+
public int AssemblyId { get; private set; }
16+
public IMyCubeGrid Grid { get; private set; }
17+
18+
private HashSet<IMyCubeBlock> _blocks = new HashSet<IMyCubeBlock>();
19+
20+
protected AssemblyBase()
21+
{
22+
// fuckery to get around generic constructor limitations
23+
}
24+
25+
public static TAssembly Create<TAssembly>(int assemblyId) where TAssembly : AssemblyBase, new()
26+
{
27+
TAssembly asm = new TAssembly
28+
{
29+
AssemblyId = assemblyId,
30+
Grid = ModularApi.GetAssemblyGrid(assemblyId)
31+
};
32+
33+
asm.Init();
34+
return asm;
35+
}
36+
37+
protected virtual void Init()
38+
{
39+
}
40+
41+
/// <summary>
42+
/// Triggered once per game tick (60ups).
43+
/// </summary>
44+
public virtual void UpdateTick()
45+
{
46+
}
47+
48+
/// <summary>
49+
/// Triggered when the assembly is closed (zero blocks contained).
50+
/// </summary>
51+
public virtual void Unload()
52+
{
53+
}
54+
55+
/// <summary>
56+
/// Triggers whenever a new part is added to an assembly.
57+
/// </summary>
58+
/// <param name="block"></param>
59+
/// <param name="isBasePart"></param>
60+
public virtual void OnPartAdd(IMyCubeBlock block, bool isBasePart)
61+
{
62+
_blocks.Add(block);
63+
}
64+
65+
/// <summary>
66+
/// Triggers whenever a part is removed from an assembly.
67+
/// </summary>
68+
/// <param name="block"></param>
69+
/// <param name="isBasePart"></param>
70+
public virtual void OnPartRemove(IMyCubeBlock block, bool isBasePart)
71+
{
72+
_blocks.Remove(block);
73+
}
74+
75+
/// <summary>
76+
/// Triggers whenever a part is destroyed, just after OnPartRemove.
77+
/// </summary>
78+
/// <param name="block"></param>
79+
/// <param name="isBasePart"></param>
80+
public virtual void OnPartDestroy(IMyCubeBlock block, bool isBasePart)
81+
{
82+
}
83+
}
84+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
using System.Collections.Generic;
2+
using VRage.Game.ModAPI;
3+
using Skytech.Thrusters.ModularAssemblies.Communication;
4+
namespace Skytech.Thrusters
5+
{
6+
internal class AssemblyManager<TAssembly> : IAssemblyManager
7+
where TAssembly : AssemblyBase, new()
8+
{
9+
// This class is a singleton.
10+
protected static AssemblyManager<TAssembly> I { get; private set; } = null;
11+
private static ModularDefinitionApi ModularApi => ModularAssemblies.ModularDefinition.ModularApi;
12+
13+
14+
private Dictionary<int, AssemblyBase> _assemblies = new Dictionary<int, AssemblyBase>();
15+
16+
17+
public static void Load()
18+
{
19+
if (I != null)
20+
return;
21+
I = new AssemblyManager<TAssembly>();
22+
}
23+
24+
public void Unload()
25+
{
26+
foreach (var system in _assemblies.Values)
27+
{
28+
system.Unload();
29+
}
30+
I = null;
31+
}
32+
33+
public void Update()
34+
{
35+
foreach (var assembly in _assemblies.Values)
36+
{
37+
assembly.UpdateTick();
38+
}
39+
}
40+
41+
public static void OnPartAdd(int assemblyId, IMyCubeBlock block, bool isBaseBlock)
42+
{
43+
if (I == null)
44+
return;
45+
46+
// find assembly, and register new one if not present.
47+
// ID is unique per-session
48+
AssemblyBase assemblyBase;
49+
if (!I._assemblies.TryGetValue(assemblyId, out assemblyBase))
50+
{
51+
assemblyBase = AssemblyBase.Create<TAssembly>(assemblyId);
52+
I._assemblies.Add(assemblyId, assemblyBase);
53+
ModularApi.Log($"AssemblyManager created new assembly {assemblyId}.");
54+
}
55+
56+
assemblyBase.OnPartAdd(block, isBaseBlock);
57+
}
58+
59+
public static void OnPartRemove(int assemblyId, IMyCubeBlock block, bool isBaseBlock)
60+
{
61+
// find assembly, and skip if not present.
62+
AssemblyBase assemblyBase;
63+
if (I == null || !I._assemblies.TryGetValue(assemblyId, out assemblyBase))
64+
return;
65+
66+
assemblyBase.OnPartRemove(block, isBaseBlock);
67+
}
68+
69+
public static void OnPartDestroy(int assemblyId, IMyCubeBlock block, bool isBaseBlock)
70+
{
71+
// find assembly, and skip if not present.
72+
AssemblyBase assemblyBase;
73+
if (I == null || !I._assemblies.TryGetValue(assemblyId, out assemblyBase))
74+
return;
75+
76+
assemblyBase.OnPartDestroy(block, isBaseBlock);
77+
}
78+
79+
public static void OnAssemblyClose(int assemblyId)
80+
{
81+
// find assembly, and skip if not present.
82+
AssemblyBase assemblyBase;
83+
if (I == null || !I._assemblies.TryGetValue(assemblyId, out assemblyBase))
84+
return;
85+
86+
assemblyBase.Unload();
87+
I._assemblies.Remove(assemblyId);
88+
ModularApi.Log($"AssemblyManager removed assembly {assemblyId}.");
89+
}
90+
}
91+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace Skytech.Thrusters
2+
{
3+
internal interface IAssemblyManager
4+
{
5+
void Unload();
6+
void Update();
7+
}
8+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using Skytech.Thrusters.ModularAssemblies.Communication;
2+
using static Skytech.Thrusters.ModularAssemblies.Communication.DefinitionDefs;
3+
4+
// ReSharper disable once CheckNamespace
5+
namespace Skytech.Thrusters.ModularAssemblies
6+
{
7+
internal partial class ModularDefinition
8+
{
9+
internal static ModularDefinitionApi ModularApi = new ModularDefinitionApi();
10+
internal ModularDefinitionContainer Container = new ModularDefinitionContainer();
11+
12+
internal void LoadDefinitions(params ModularPhysicalDefinition[] defs)
13+
{
14+
Container.PhysicalDefs = defs;
15+
}
16+
17+
/// <summary>
18+
/// Load all definitions for DefinitionSender
19+
/// </summary>
20+
/// <param name="baseDefs"></param>
21+
internal static ModularDefinitionContainer GetBaseDefinitions()
22+
{
23+
return new Skytech.Thrusters.ModularAssemblies.ModularDefinition().Container;
24+
}
25+
}
26+
}

0 commit comments

Comments
 (0)