Skip to content

Commit

Permalink
Added ammo icon and projectile
Browse files Browse the repository at this point in the history
  • Loading branch information
Gauge committed Jul 29, 2024
1 parent 39b5cac commit 71897a1
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 16 deletions.
4 changes: 2 additions & 2 deletions GrappleHook/Data/AmmoMagazines.sbc
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
<SubtypeId>GrappleHookMagazine</SubtypeId>
</Id>
<DisplayName>Grapple Hook</DisplayName>
<Icon>Textures\GUI\Icons\ammo\LargeCalibreShell.dds</Icon>
<Icon>Textures\GUI\Icons\ammo\GrappleHookAmmo.dds</Icon>
<Size>
<X>1.2</X>
<Y>0.3</Y>
<Z>0.3</Z>
</Size>
<Mass>300</Mass>
<Volume>100</Volume>
<Model>Models\Weapons\LargeCalibreShell.mwm</Model>
<Model>Models\ammo\GrappleHookAmmo.mwm</Model>
<PhysicalMaterial>Ammo</PhysicalMaterial>
<Capacity>1</Capacity>
<AmmoDefinitionId Subtype="GrappleHookAmmo" />
Expand Down
2 changes: 1 addition & 1 deletion GrappleHook/Data/Blueprints.sbc
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<SubtypeId>GrappleHookBlueprint</SubtypeId>
</Id>
<DisplayName>Grapple Hook</DisplayName>
<Icon>Textures\GUI\Icons\ammo\LargeCalibreShell.dds</Icon>
<Icon>Textures\GUI\Icons\ammo\GrappleHookAmmo.dds</Icon>
<Prerequisites>
<Item Amount="60" TypeId="Ingot" SubtypeId="Iron" />
<Item Amount="8" TypeId="Ingot" SubtypeId="Nickel" />
Expand Down
55 changes: 42 additions & 13 deletions GrappleHook/Data/Scripts/hook/WeaponControlLayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using System.Text;
using VRage.Game;
using VRage.Game.Components;
using VRage.Game.Entity;
using VRage.Game.ModAPI;
using VRage.Input;
using VRage.ModAPI;
Expand All @@ -33,6 +34,8 @@ public class WeaponControlLayer : MyGameLogicComponent
private IMyLargeTurretBase Turret;
private IMyGunObject<MyGunBase> gun;

private MyEntity Projectile;

public enum States { idle, reloading, projectile, attached }

private NetSync<States> State;
Expand All @@ -54,8 +57,10 @@ public enum States { idle, reloading, projectile, attached }
private NetSync<ZiplineEntity> RequestZiplineDisconnect;
private NetSync<List<ZiplineEntity>> ZiplinePlayers;

private Vector3D GrapplePosition = Vector3D.Zero;
private Vector3 GrappleDirection = Vector3.Zero;
//private Vector3D GrapplePosition = Vector3D.Zero;
//private Vector3 GrappleDirection = Vector3.Zero;

private MatrixD GrappleMatrix = MatrixD.Zero;

private bool terminalShootOn = false;
private bool interactable = false;
Expand Down Expand Up @@ -107,6 +112,11 @@ public override void Init(MyObjectBuilder_EntityBase objectBuilder)
Turret = Entity as IMyLargeTurretBase;
Turret.Range = 0;

Projectile = new MyEntity();
Projectile.Init(null, ModContext.ModPath + "\\Models\\Ammo\\GrappleHookAmmo.mwm", null, null);

MyEntities.Add(Projectile, true);

NeedsUpdate |= MyEntityUpdateEnum.EACH_FRAME;

if (!Hijack)
Expand All @@ -121,8 +131,7 @@ private void Reset(bool o, bool n)
}
private void Reset()
{
GrapplePosition = Vector3D.Zero;
GrappleDirection = Vector3D.Zero;
HideProjectile();

if (ConnectedEntity != null)
ConnectedEntity.OnMarkForClose -= attachedEntityClosed;
Expand Down Expand Up @@ -173,11 +182,15 @@ public override void UpdateOnceBeforeFrame()
Hijack = true;
}



public override void UpdateBeforeSimulation()
{
switch (State.Value)
{
case States.idle:

MoveProjectileToTurretMuzzle();
VerifyAndRequestShoot();
break;
case States.reloading:
Expand Down Expand Up @@ -224,15 +237,27 @@ private void Shoot(ShootData o, ShootData n)
{
if (State.Value != States.attached && n.direction != Vector3.Zero)
{
GrapplePosition = Turret.PositionComp.WorldMatrixRef.Translation;
GrappleDirection = n.direction;
GrappleMatrix = gun.GunBase.GetMuzzleWorldMatrix();
State.Value = States.projectile;
ReloadTime.Value = (float)gun.GunBase.ReloadTime;
Shooting.SetValue(new ShootData());
Tools.Debug($"Shooting - State Change: {State.Value}");
}
}

private void MoveProjectileToTurretMuzzle()
{
GrappleMatrix = gun.GunBase.GetMuzzleWorldMatrix();
GrappleMatrix.Translation += GrappleMatrix.Forward * 1.1f;
Projectile.WorldMatrix = GrappleMatrix;
}

private void HideProjectile()
{
GrappleMatrix = gun.GunBase.GetMuzzleWorldMatrix();
GrappleMatrix.Translation = Vector3D.MaxValue;
Projectile.WorldMatrix = GrappleMatrix;
}
private void Reload()
{
if (ReloadTime.Value > 0)
Expand All @@ -244,10 +269,10 @@ private void Reload()
private void UpdateProjectile()
{
//Tools.Debug($"Projectile In Flight {(Turret.PositionComp.WorldMatrixRef.Translation - GrapplePosition).Length()}");
Vector3 delta = GrappleDirection * settings.Value.GrappleProjectileSpeed;
Vector3 delta = GrappleMatrix.Forward * settings.Value.GrappleProjectileSpeed;

IHitInfo hit = null;
MyAPIGateway.Physics.CastRay(GrapplePosition, GrapplePosition + delta, out hit);
MyAPIGateway.Physics.CastRay(GrappleMatrix.Translation, GrappleMatrix.Translation + delta, out hit);
if (hit != null && !hit.HitEntity.MarkedForClose)
{
ConnectedEntityId.Value = hit.HitEntity.EntityId;
Expand All @@ -259,29 +284,33 @@ private void UpdateProjectile()
MyCubeGrid grid = (ConnectedEntity as MyCubeGrid);
grid.OnBlockRemoved += Grid_OnBlockRemoved;

Vector3I pos = grid.WorldToGridInteger(hit.Position + GrappleDirection * 0.1f);
Vector3I pos = grid.WorldToGridInteger(hit.Position + GrappleMatrix.Forward * 0.1f);
IMySlimBlock block = grid.GetCubeBlock(pos);
if (block != null)
{
LocalGrapplePositionI.Value = block.Position;
}
}

LocalGrapplePosition.Value = Vector3D.Transform(hit.Position + GrappleDirection * 0.1f, MatrixD.Invert(ConnectedEntity.WorldMatrix));
GrappleMatrix.Translation = hit.Position + GrappleMatrix.Forward * 0.1f;
LocalGrapplePosition.Value = Vector3D.Transform(hit.Position + GrappleMatrix.Forward * 0.1f, MatrixD.Invert(ConnectedEntity.WorldMatrix));
GrappleLength.Value = (hit.Position - Turret.PositionComp.WorldMatrixRef.Translation).Length() + 1.25f;
State.Value = States.attached;
Tools.Debug($"Attached {hit.HitEntity.DisplayName} - State Change: {State.Value}");
}
else
{
GrapplePosition += delta;
GrappleMatrix.Translation += delta;


// if grapple length goes beyond max length
if ((Turret.PositionComp.WorldMatrixRef.Translation - GrapplePosition).LengthSquared() > settings.Value.ShootRopeLength * settings.Value.ShootRopeLength)
if ((Turret.PositionComp.WorldMatrixRef.Translation - GrappleMatrix.Translation).LengthSquared() > settings.Value.ShootRopeLength * settings.Value.ShootRopeLength)
{
ResetIndicator.Value = !ResetIndicator.Value;
}
}

Projectile.WorldMatrix = GrappleMatrix;
}

private void UpdateGrappleLength()
Expand Down Expand Up @@ -692,7 +721,7 @@ private void Draw()
Vector3D position;
if (State.Value == States.projectile)
{
position = GrapplePosition;
position = GrappleMatrix.Translation;
Vector3D[] points = ComputeCurvePoints(gunPosition, position, sagDirection, Vector3D.Distance(gunPosition, position) * 1.005f, settings.Value.RopeSegments);

for (int i = 0; i < points.Length - 1; i++)
Expand Down
Binary file added GrappleHook/Models/ammo/GrappleHookAmmo.mwm
Binary file not shown.
Binary file not shown.

0 comments on commit 71897a1

Please sign in to comment.