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

modular shield projector + resist block #1078

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?xml version="1.0" encoding="utf-8"?>
<Definitions xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<CubeBlocks>

<Definition xsi:type="MyObjectBuilder_DecoyDefinition">
<Id>
<TypeId>Decoy</TypeId>
<SubtypeId>LargeDecoy_MetalFoam</SubtypeId>
</Id>
<DisplayName>Metal Foam Decoy</DisplayName>
<Icon>Textures\GUI\Icons\Cubes\metalfoam.png</Icon>
<Description>Extremely densely stored material. When activated, spills out fill in the space around it with Metal Foam (light armor). Activates when damaged below functional.</Description>
<CubeSize>Large</CubeSize>
<BlockTopology>TriangleMesh</BlockTopology>
<Size x="1" y="1" z="1" />
<ModelOffset x="0" y="0" z="0" />
<Model>Models\LargeDecoy_MetalFoam.mwm</Model>
<Components>
<Component Subtype="SteelPlate" Count="5775" />
<Component Subtype="Construction" Count="10" />
<Component Subtype="Computer" Count="10" />
<Component Subtype="LargeTube" Count="2" />
<Component Subtype="RadioCommunication" Count="1" />
<Component Subtype="SteelPlate" Count="125" />
</Components>
<CriticalComponent Subtype="RadioCommunication" Index="0" />
<MountPoints>
<MountPoint Side="Front" StartX="0.00" StartY="0.00" EndX="1.00" EndY="0.20"/>
<MountPoint Side="Back" StartX="0.00" StartY="0.00" EndX="1.00" EndY="0.20"/>
<MountPoint Side="Left" StartX="0.00" StartY="0.00" EndX="1.00" EndY="0.20"/>
<MountPoint Side="Right" StartX="0.00" StartY="0.00" EndX="1.00" EndY="0.20"/>
<MountPoint Side="Bottom" StartX="0.00" StartY="0.00" EndX="1.00" EndY="1.00"/>
</MountPoints>
<Public>true</Public>
<BlockPairName>LargeDecoy_MetalFoam</BlockPairName>
<EdgeType>Light</EdgeType>
<BuildTimeSeconds>16</BuildTimeSeconds>
<!--<UseModelIntersection>true</UseModelIntersection>-->
<DamageEffectName>Damage_Electrical_Damaged</DamageEffectName>
<DamagedSound>ParticleElectrical</DamagedSound>
<DestroyEffect>BlockDestroyedExplosion_Large</DestroyEffect>
<DestroySound>WepSmallWarheadExpl</DestroySound>
<EmissiveColorPreset>Default</EmissiveColorPreset>
<PCU>50</PCU>
<MirroringY>Z</MirroringY>
<MirroringZ>Y</MirroringZ>
<IsAirTight>false</IsAirTight>
<TargetingGroups>
<string>Weapons</string>
<string>Propulsion</string>
<string>PowerSystems</string>
</TargetingGroups>
<PriorityModifier>10</PriorityModifier>
<NotWorkingPriorityMultiplier>0</NotWorkingPriorityMultiplier>
</Definition>


</CubeBlocks>
</Definitions>
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0" ?>
<Definitions xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<CubeBlocks>
<Definition xsi:type="MyObjectBuilder_UpgradeModuleDefinition">
<Id>
<TypeId>UpgradeModule</TypeId>
<SubtypeId>ShieldProjector_Large</SubtypeId>
</Id>
<DisplayName>Shield Projector</DisplayName>
<Description>Shield Projector</Description>
<Icon>Textures\GUI\Icons\AstronautBackpack.dds</Icon>
<CubeSize>Large</CubeSize>
<BlockTopology>TriangleMesh</BlockTopology>
<Size x="1" y="1" z="1"/>
<ModelOffset x="0" y="0" z="0"/>
<Model>Models\ShieldProjector_Large.mwm</Model>
<Components>
<Component Subtype="SteelPlate" Count="10"/>
<Component Subtype="Computer" Count="10"/>
</Components>
<CriticalComponent Subtype="SteelPlate" Index="0"/>
<BlockPairName>ShieldProjector_Large</BlockPairName>
</Definition>
</CubeBlocks>
</Definitions>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Definitions xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<EntityComponents>
<EntityComponent xsi:type="MyObjectBuilder_ModStorageComponentDefinition">
<Id>
<TypeId>ModStorageComponent</TypeId>
<SubtypeId>ShieldProjector</SubtypeId>
</Id>
<RegisteredStorageGuids>
<guid>6200280a-be69-48ed-afa9-cf376ea62b8b</guid>
</RegisteredStorageGuids>
</EntityComponent>
</EntityComponents>
</Definitions>
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
using Sandbox.Common.ObjectBuilders;
using Sandbox.Game.Entities;
using Sandbox.ModAPI;
using VRage.Game.Components;
using VRage.ModAPI;
using VRageMath;
using VRage.Utils;
using VRageRender;
using System;
using System.Collections.Generic;
using Sandbox.Game.EntityComponents;
using Sandbox.ModAPI.Interfaces.Terminal;
using VRage.Game;
using VRage.ObjectBuilders;
using VRage.Game.ModAPI;

namespace Invalid.ShieldProjector
{
[MyEntityComponentDescriptor(typeof(MyObjectBuilder_UpgradeModule), false, "ShieldProjector_Large")]
public class ShieldProjector : MyGameLogicComponent
{
private IMyCubeBlock block;
private ShieldProjectorSettings Settings;
public readonly Guid ShieldProjectorGUID = new Guid("6200280a-be69-48ed-afa9-cf376ea62b8b"); // Preserved GUID

private Vector3D offset = new Vector3D(0, 0, 10); // 10m in front of the block
private float squareSize = 5.0f; // Default square size of 5 meters
static bool m_controlsCreated = false;

public override void Init(MyObjectBuilder_EntityBase objectBuilder)
{
base.Init(objectBuilder);
NeedsUpdate |= MyEntityUpdateEnum.BEFORE_NEXT_FRAME | MyEntityUpdateEnum.EACH_FRAME;
block = (IMyCubeBlock)Entity;
}

public override void UpdateOnceBeforeFrame()
{
if (block == null || block.CubeGrid?.Physics == null)
return;

CreateTerminalControls();
Settings = new ShieldProjectorSettings(this);
LoadSettings();
SaveSettings();
}

public override bool IsSerialized()
{
try
{
SaveSettings();
}
catch (Exception e)
{
// Optional: Log the exception or handle it appropriately
}
return base.IsSerialized();
}

public override void UpdateBeforeSimulation()
{
DrawDebugSquare();
}

private void DrawDebugSquare()
{
Vector3D blockPosition = block.WorldMatrix.Translation;
Vector3D front = blockPosition + block.WorldMatrix.Forward * offset.Z;
Vector3D up = block.WorldMatrix.Up * squareSize;
Vector3D left = block.WorldMatrix.Left * squareSize;

// Drawing a square in space
MyTransparentGeometry.AddLineBillboard(MyStringId.GetOrCompute("Square"), Color.White, front - up - left, block.WorldMatrix.Forward, squareSize * 2, 0.1f);
MyTransparentGeometry.AddLineBillboard(MyStringId.GetOrCompute("Square"), Color.White, front - up + left, block.WorldMatrix.Forward, squareSize * 2, 0.1f);
MyTransparentGeometry.AddLineBillboard(MyStringId.GetOrCompute("Square"), Color.White, front + up - left, block.WorldMatrix.Forward, squareSize * 2, 0.1f);
MyTransparentGeometry.AddLineBillboard(MyStringId.GetOrCompute("Square"), Color.White, front + up + left, block.WorldMatrix.Forward, squareSize * 2, 0.1f);
}

static void CreateTerminalControls()
{
if (m_controlsCreated)
return;

m_controlsCreated = true;

// Control to adjust the square size
var sizeSlider = MyAPIGateway.TerminalControls.CreateControl<IMyTerminalControlSlider, IMyUpgradeModule>("SquareSize");
sizeSlider.Title = MyStringId.GetOrCompute("Square Size");
sizeSlider.SetLimits(1, 10); // Min 1m, Max 10m
sizeSlider.Getter = block => ((ShieldProjector)block.GameLogic).squareSize;
sizeSlider.Setter = (block, value) => ((ShieldProjector)block.GameLogic).squareSize = value;
sizeSlider.Writer = (block, text) => text.AppendFormat("{0:0.0} m", ((ShieldProjector)block.GameLogic).squareSize);
MyAPIGateway.TerminalControls.AddControl<IMyUpgradeModule>(sizeSlider);
}

internal bool LoadSettings()
{
string rawData;
if (block.Storage != null && block.Storage.TryGetValue(ShieldProjectorGUID, out rawData))
{
try
{
var loadedSettings = MyAPIGateway.Utilities.SerializeFromBinary<ShieldProjectorSettings>(Convert.FromBase64String(rawData));
if (loadedSettings != null)
{
squareSize = loadedSettings.SquareSize; // Example additional field
return true;
}
}
catch (Exception e)
{
MyAPIGateway.Utilities.ShowNotification("Failed to load settings! Check the logs for more info.");
MyLog.Default.WriteLineAndConsole("Failed to load settings! Exception: " + e);
}
}
return false;
}

internal void SaveSettings()
{
if (block == null || Settings == null)
return;

try
{
if (block.Storage == null)
block.Storage = new MyModStorageComponent();

Settings.SquareSize = squareSize; // Save the current square size
string rawData = Convert.ToBase64String(MyAPIGateway.Utilities.SerializeToBinary(Settings));
block.Storage.Add(ShieldProjectorGUID, rawData);
}
catch (Exception e)
{
MyAPIGateway.Utilities.ShowNotification("Failed to save settings! Check the logs for more info.");
MyLog.Default.WriteLineAndConsole("Failed to save settings! Exception: " + e);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using ProtoBuf;
using System;
using VRage.Sync;
using VRage.Utils;

namespace Invalid.ShieldProjector
{
[ProtoContract]
internal class ShieldProjectorSettings
{
ShieldProjectorSettings() { }
public ShieldProjectorSettings(ShieldProjector owner)
{
Owner = owner;
}

[ProtoMember(1)] // Ensure this property is serialized by assigning it a unique ProtoMember tag.
private float squareSize = 5.0f; // Default value of 5 meters

ShieldProjector Owner;


public float SquareSize
{
get { return squareSize; }
set { squareSize = value; }
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using ProtoBuf;
using Starcore.ResistBlock;

namespace Starcore.ResistBlock
{
[ProtoContract]
internal class ResistSettings
{
public ResistSettings() { }

public ResistSettings(ResistBlock owner)
{
Owner = owner;
Owner.IsActiveSync.ValueChanged += (obj) => { IsActive = Owner.IsActiveSync.Value; };
Owner.ResistanceStrengthSync.ValueChanged += (obj) => { ResistanceStrength = Owner.ResistanceStrengthSync.Value; };
}

[ProtoMember(1)]
private bool isActive;

[ProtoMember(2)]
private float resistanceStrength;

ResistBlock Owner;

public bool IsActive
{
get { return isActive; }
set
{
isActive = value;
if (Owner != null && Owner.IsActiveSync != null)
Owner.IsActiveSync.Value = value;
}
}

public float ResistanceStrength
{
get { return resistanceStrength; }
set
{
resistanceStrength = value;
if (Owner != null && Owner.ResistanceStrengthSync != null)
Owner.ResistanceStrengthSync.Value = value;
}
}
}
}
Loading