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

Add hit manager and modify functions for plugin functionality #2

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Expand Up @@ -44,3 +44,6 @@ TDx*
UnityEngine.dll
*.pdb
*.jar

# obj folder
BahaTurret/obj/
Expand Down
2 changes: 1 addition & 1 deletion BahaTurret/BDExplosivePart.cs
Expand Up @@ -52,7 +52,7 @@ public void Detonate()
hasDetonated = true;
if(part!=null) part.temperature = part.maxTemp + 100;
Vector3 position = transform.position+rigidbody.velocity*Time.fixedDeltaTime;
ExplosionFX.CreateExplosion(position, blastRadius, blastPower, vessel, FlightGlobals.getUpAxis(), "BDArmory/Models/explosion/explosionLarge", "BDArmory/Sounds/explode1");
ExplosionFX.CreateExplosion(position, blastRadius, blastPower, vessel, FlightGlobals.getUpAxis(), "BDArmory/Models/explosion/explosionLarge", "BDArmory/Sounds/explode1", true);
}
}
}
Expand Down
7 changes: 4 additions & 3 deletions BahaTurret/BahaTurret.cs
Expand Up @@ -1131,18 +1131,19 @@ private bool FireLaser()
lr.SetPosition(1, hit.point + (physStepFix));
if(Time.time-timeCheck > 60/1200 && BDArmorySettings.BULLET_HITS)
{
BulletHitFX.CreateBulletHit(hit.point, hit.normal, false);
BulletHitFX.CreateBulletHit(hit.point, hit.normal, false, true);
}
try
{
Part p = Part.FromGO(hit.rigidbody.gameObject);
if(p.vessel!=this.vessel)
{
if(p.vessel!=this.vessel && HitManager.ShouldAllowDamageHooks(p.vessel.id))
{
float distance = hit.distance;
p.temperature += laserDamage/(float)(Math.PI*Math.Pow(tanAngle*distance,2))*TimeWarp.fixedDeltaTime; //distance modifier: 1/(PI*Pow(Dist*tan(angle),

if(BDArmorySettings.INSTAKILL) p.temperature += p.maxTemp;

HitManager.FireHitHooks(p);
}
}
catch(NullReferenceException){}
Expand Down
2 changes: 2 additions & 0 deletions BahaTurret/BahaTurret.csproj
Expand Up @@ -40,6 +40,7 @@
<Reference Include="UnityEngine">
<HintPath>C:\Games\KSP 1.01 - Mod Dev\KSP_Data\Managed\UnityEngine.dll</HintPath>
</Reference>
<Reference Include="System.Core" />
</ItemGroup>
<ItemGroup>
<Compile Include="BahaTurret.cs" />
Expand Down Expand Up @@ -71,6 +72,7 @@
<Compile Include="TargetInfo.cs" />
<Compile Include="BDATargetManager.cs" />
<Compile Include="BDModulePilotAI.cs" />
<Compile Include="HitManager.cs" />
</ItemGroup>
<ItemGroup>
<Folder Include="Animation\" />
Expand Down
28 changes: 24 additions & 4 deletions BahaTurret/BahaTurret.userprefs
@@ -1,8 +1,28 @@
<Properties>
<MonoDevelop.Ide.Workspace ActiveConfiguration="Debug|x86" PreferredExecutionTarget="MonoDevelop.Default" />
<MonoDevelop.Ide.Workbench ActiveDocument="BDModulePilotAI.cs">
<Properties StartupItem="BahaTurret.csproj">
<MonoDevelop.Ide.Workspace ActiveConfiguration="Debug|x86" />
<MonoDevelop.Ide.Workbench>
<Files>
<File FileName="BDModulePilotAI.cs" Line="368" Column="25" />
<File FileName="MissileLauncher.cs" Line="1" Column="1" />
<File FileName="MissileFire.cs" Line="1" Column="1" />
<File FileName="BahaTurretBullet.cs" Line="241" Column="36" />
<File FileName="ExplosionFX.cs" Line="130" Column="38" />
<File FileName="BDArmorySettings.cs" Line="1" Column="1" />
<File FileName="MissileGuidance.cs" Line="1" Column="1" />
<File FileName="BDModularGuidance.cs" Line="1" Column="1" />
<File FileName="BDModulePilotAI.cs" Line="1" Column="1" />
<File FileName="BahaTurret.cs" Line="1041" Column="46" />
<File FileName="BDATargetManager.cs" Line="1" Column="1" />
<File FileName="TargetInfo.cs" Line="1" Column="1" />
<File FileName="CMFlare.cs" Line="1" Column="1" />
<File FileName="ParticleTurbulence.cs" Line="1" Column="1" />
<File FileName="BDAGaplessParticleEmitter.cs" Line="1" Column="1" />
<File FileName="BDExplosivePart.cs" Line="1" Column="1" />
<File FileName="CMDropper.cs" Line="1" Column="1" />
<File FileName="Misc.cs" Line="1" Column="1" />
<File FileName="HitManager.cs" Line="17" Column="40" />
<File FileName="BulletHitFX.cs" Line="67" Column="45" />
<File FileName="RocketLauncher.cs" Line="1" Column="1" />
<File FileName="ClusterBomb.cs" Line="1" Column="1" />
</Files>
</MonoDevelop.Ide.Workbench>
<MonoDevelop.Ide.DebuggingService.Breakpoints>
Expand Down
17 changes: 12 additions & 5 deletions BahaTurret/BahaTurretBullet.cs
Expand Up @@ -132,9 +132,12 @@ void FixedUpdate()


currPosition = gameObject.transform.position;

HitManager.FireTracerHooks (this.bulletTrail);

if((currPosition-startPosition).sqrMagnitude > maxDistance*maxDistance)
{
HitManager.FireTracerDestroyHooks (this.bulletTrail);
GameObject.Destroy(gameObject);
return;
}
Expand Down Expand Up @@ -177,7 +180,7 @@ void FixedUpdate()
}


if(hitPart!=null && !hitPart.partInfo.name.Contains("Strut")) //when a part is hit, execute damage code (ignores struts to keep those from being abused as armor)(no, because they caused weird bugs :) -BahamutoD)
if(hitPart!=null && !hitPart.partInfo.name.Contains("Strut") && HitManager.ShouldAllowDamageHooks(hitPart.vessel.id)) //when a part is hit, execute damage code (ignores struts to keep those from being abused as armor)(no, because they caused weird bugs :) -BahamutoD)
{
float heatDamage = (rigidbody.mass/hitPart.crashTolerance) * rigidbody.velocity.magnitude * 50 * BDArmorySettings.DMG_MULTIPLIER; //how much heat damage will be applied based on bullet mass, velocity, and part's impact tolerance
if(!penetrated)
Expand All @@ -199,6 +202,8 @@ void FixedUpdate()
if (hitPart.vessel != sourceVessel) hitPart.temperature += heatDamage; //apply heat damage to the hit part.
}

HitManager.FireHitHooks (hitPart);

}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -235,7 +240,7 @@ void FixedUpdate()
hasBounced = true;
if(BDArmorySettings.BULLET_HITS)
{
BulletHitFX.CreateBulletHit(hit.point, hit.normal, true);
BulletHitFX.CreateBulletHit(hit.point, hit.normal, true, true);
}

transform.position = hit.point;
Expand All @@ -250,14 +255,15 @@ void FixedUpdate()
{
if(BDArmorySettings.BULLET_HITS)
{
BulletHitFX.CreateBulletHit(hit.point, hit.normal, false);
BulletHitFX.CreateBulletHit(hit.point, hit.normal, false, true);
}

if(bulletType == BulletTypes.Explosive)
{
ExplosionFX.CreateExplosion(hit.point, radius, blastPower, sourceVessel, rigidbody.velocity.normalized, explModelPath, explSoundPath);
ExplosionFX.CreateExplosion(hit.point, radius, blastPower, sourceVessel, rigidbody.velocity.normalized, explModelPath, explSoundPath, true);
}

HitManager.FireTracerDestroyHooks (this.bulletTrail);
GameObject.Destroy(gameObject); //destroy bullet on collision
}
}
Expand Down Expand Up @@ -290,7 +296,8 @@ void FixedUpdate()
if(airDetonation && (transform.position-startPosition).sqrMagnitude > Mathf.Pow(detonationRange, 2))
{
//detonate
ExplosionFX.CreateExplosion(transform.position, radius, blastPower, sourceVessel, rigidbody.velocity.normalized, explModelPath, explSoundPath);
ExplosionFX.CreateExplosion(transform.position, radius, blastPower, sourceVessel, rigidbody.velocity.normalized, explModelPath, explSoundPath, true);
HitManager.FireTracerDestroyHooks (this.bulletTrail);
GameObject.Destroy(gameObject); //destroy bullet on collision
}

Expand Down
13 changes: 10 additions & 3 deletions BahaTurret/BulletHitFX.cs
Expand Up @@ -14,8 +14,8 @@ public class BulletHitFX : MonoBehaviour

//static GameObject go = GameDatabase.Instance.GetModel("BDArmory/Models/bulletHit/bulletHit"); //===TODO: static object wont load after scene reload

void Start()
{
void Start()
{
startTime = Time.time;
pEmitters = gameObject.GetComponentsInChildren<KSPParticleEmitter>();
audioSource = gameObject.AddComponent<AudioSource>();
Expand Down Expand Up @@ -58,10 +58,11 @@ void Update()
}
}

public static void CreateBulletHit(Vector3 position, Vector3 normalDirection, bool ricochet)
public static void CreateBulletHit(Vector3 position, Vector3 normalDirection, bool ricochet, bool fireHooks)
{
GameObject go = GameDatabase.Instance.GetModel("BDArmory/Models/bulletHit/bulletHit");
GameObject newExplosion = (GameObject) GameObject.Instantiate(go, position, Quaternion.LookRotation(normalDirection));
//Debug.Log ("CreateBulletHit instantiating at position X: " + position.x + " Y: " + position.y + " Z: " + position.z);
newExplosion.SetActive(true);
newExplosion.AddComponent<BulletHitFX>();
newExplosion.GetComponent<BulletHitFX>().ricochet = ricochet;
Expand All @@ -70,6 +71,12 @@ public static void CreateBulletHit(Vector3 position, Vector3 normalDirection, bo
pe.emit = true;
pe.force = (4.49f * FlightGlobals.getGeeForceAtPosition(position));
}

if (fireHooks)
{
BulletObject bulletObj = new BulletObject (position, normalDirection, ricochet);
HitManager.FireBulletHooks (bulletObj);
}
}

}
Expand Down
2 changes: 1 addition & 1 deletion BahaTurret/ClusterBomb.cs
Expand Up @@ -213,7 +213,7 @@ void FixedUpdate()

void Detonate(Vector3 pos)
{
ExplosionFX.CreateExplosion(pos, blastRadius, blastForce, sourceVessel, FlightGlobals.getUpAxis(), subExplModelPath, subExplSoundPath);
ExplosionFX.CreateExplosion(pos, blastRadius, blastForce, sourceVessel, FlightGlobals.getUpAxis(), subExplModelPath, subExplSoundPath, true);
GameObject.Destroy(gameObject); //destroy bullet on collision
}

Expand Down
16 changes: 14 additions & 2 deletions BahaTurret/ExplosionFX.cs
Expand Up @@ -66,10 +66,16 @@ void FixedUpdate()
* 2: large, regular sound (like mk82 bomb)
* 3: small, pop sound (like cluster submunition)
*/
public static void CreateExplosion(Vector3 position, float radius, float power, Vessel sourceVessel, Vector3 direction, string explModelPath, string soundPath)
public static void CreateExplosion(Vector3 position, float radius, float power, Vessel sourceVessel, Vector3 direction, string explModelPath, string soundPath, bool fireExplosionHooks)
{
try
{
if (fireExplosionHooks)
{
ExplosionObject explosionObj = new ExplosionObject(position, radius, power, sourceVessel, direction, explModelPath, soundPath);
HitManager.FireExplosionHooks(explosionObj);
}

GameObject go;
AudioClip soundClip;

Expand Down Expand Up @@ -118,7 +124,13 @@ public static void CreateExplosion(Vector3 position, float radius, float power,
}catch(NullReferenceException){}
if(explodePart!=null && !explodePart.partInfo.name.Contains("Strut") && !explodePart.packed)
{

if (!HitManager.ShouldAllowDamageHooks(explodePart.vessel.id))
{
//Continue if not allowed to damage the part
continue;
}


if(!MissileLauncher.CheckIfMissile(explodePart) || ((explodePart.GetComponent<MissileLauncher>().sourceVessel != sourceVessel || explodePart.GetComponent<MissileLauncher>().sourceVessel==null) && explodePart.GetComponent<MissileLauncher>().hasFired))
{
//Debug.Log ("Explosion hit part from vessel: "+explodePart.vessel.vesselName);
Expand Down
163 changes: 163 additions & 0 deletions BahaTurret/HitManager.cs
@@ -0,0 +1,163 @@
using UnityEngine;
using System;
using System.Collections.Generic;


namespace BahaTurret
{
[KSPAddon(KSPAddon.Startup.Instantly, true)]
public class HitManager : MonoBehaviour
{
//Hook Registry
private static readonly List<Action<Part>> hitHooks = new List<Action<Part>> ();
private static readonly List<Action<ExplosionObject>> explosionHooks = new List<Action<ExplosionObject>> ();
private static readonly List<Action<BulletObject>> bulletHooks = new List<Action<BulletObject>> ();
private static readonly List<Action<LineRenderer>> tracerHooks = new List<Action<LineRenderer>> ();
private static readonly List<Action<LineRenderer>> tracerDestroyHooks = new List<Action<LineRenderer>> ();
private static readonly List<Func<Guid, bool>> allowDamageHooks = new List<Func<Guid, bool>> ();

public HitManager ()
{

}

public static void RegisterHitHook(Action<Part> hitHook)
{
if (!hitHooks.Contains (hitHook))
{
hitHooks.Add (hitHook);
}
}

public static void RegisterExplosionHook(Action<ExplosionObject> explosionHook)
{
if (!explosionHooks.Contains (explosionHook))
{
explosionHooks.Add (explosionHook);
}
}

public static void RegisterBulletHook(Action<BulletObject> bulletHook)
{
if (!bulletHooks.Contains (bulletHook))
{
bulletHooks.Add (bulletHook);
}
}

public static void RegisterTracerHook(Action<LineRenderer> tracerHook)
{
if (!tracerHooks.Contains (tracerHook))
{
tracerHooks.Add (tracerHook);
}
}

public static void RegisterTracerDestroyHook(Action<LineRenderer> tracerDestroyHook)
{
if (!tracerDestroyHooks.Contains (tracerDestroyHook))
{
tracerDestroyHooks.Add (tracerDestroyHook);
}
}

public static void RegisterAllowDamageHook(System.Func<Guid, bool> allowDamageHook)
{
if (!allowDamageHooks.Contains (allowDamageHook))
{
allowDamageHooks.Add (allowDamageHook);
}
}

public static void FireHitHooks(Part hitPart)
{
//Fire hitHooks
foreach (Action<Part> hitHook in hitHooks) {
hitHook (hitPart);
}
}

public static void FireExplosionHooks(ExplosionObject explosion)
{
foreach (Action<ExplosionObject> explosionHook in explosionHooks)
{
explosionHook (explosion);
}
}

public static void FireBulletHooks(BulletObject bullet)
{
foreach (Action<BulletObject> bulletHook in bulletHooks)
{
bulletHook (bullet);
}
}

public static void FireTracerHooks(LineRenderer tracer)
{
foreach (Action<LineRenderer> tracerHook in tracerHooks)
{
tracerHook (tracer);
}
}

public static void FireTracerDestroyHooks(LineRenderer tracer)
{
foreach (Action<LineRenderer> tracerHook in tracerDestroyHooks)
{
tracerHook (tracer);
}
}

public static bool ShouldAllowDamageHooks(Guid vesselID)
{
foreach (Func<Guid, bool> allowDamageHook in allowDamageHooks)
{
bool result;
result = allowDamageHook (vesselID);
if (!result) {
return false;
}
}
return true;
}
}

public class ExplosionObject
{
public readonly Vector3 position;
public readonly float raduis;
public readonly float power;
public readonly Vessel sourceVessel;
public readonly Vector3 direction;
public readonly string explModelPath;
public readonly string soundPath;

public ExplosionObject(Vector3 positionVal, float radiusVal, float powerVal, Vessel sourceVesselVal, Vector3 directionVal, string explModelPathVal, string soundPathVal)
{
position = positionVal;
raduis = radiusVal;
power = powerVal;
sourceVessel = sourceVesselVal;
direction = directionVal;
explModelPath = explModelPathVal;
soundPath = soundPathVal;
}

}

public class BulletObject
{
public readonly Vector3 position;
public readonly Vector3 normalDirection;
public readonly bool ricochet;

public BulletObject(Vector3 positionVal, Vector3 normalDirectionVal, bool ricochetVal)
{
position = positionVal;
normalDirection = normalDirectionVal;
ricochet = ricochetVal;
}
}
}