diff --git a/RimWorldOfMagic/RimWorldOfMagic/Building_LightningTrap.cs b/RimWorldOfMagic/RimWorldOfMagic/Building_LightningTrap.cs index 835089a5..14e9d69f 100644 --- a/RimWorldOfMagic/RimWorldOfMagic/Building_LightningTrap.cs +++ b/RimWorldOfMagic/RimWorldOfMagic/Building_LightningTrap.cs @@ -15,6 +15,8 @@ public class Building_LightningTrap : Building_ExplosiveProximityTrap public bool extendedTrap = false; public bool iceTrap = false; + private FlyingObject_LightningTrap flyingObject; // Used to prevent duplicate eye spawning only. Not saved. + public override void ExposeData() { base.ExposeData(); @@ -24,8 +26,9 @@ public override void ExposeData() public override void Spring(Pawn p) { + if (flyingObject != null) return; // Prevent duplicate lightning eyes from spawning. Very op. base.Spring(p); - IntVec3 targetPos = this.Position; + IntVec3 targetPos = Position; targetPos.z += 2; LocalTargetInfo t = targetPos; float speed = .8f; @@ -33,16 +36,14 @@ public override void Spring(Pawn p) { speed = .6f; } - if (t.Cell != default(IntVec3)) + if (t.Cell != default) { - Thing eyeThing = new Thing(); - eyeThing.def = TorannMagicDefOf.FlyingObject_LightningTrap; - FlyingObject_LightningTrap flyingObject = (FlyingObject_LightningTrap)GenSpawn.Spawn(TorannMagicDefOf.FlyingObject_LightningTrap, this.Position, this.Map); - flyingObject.Launch(p, this.Position.ToVector3Shifted(), t.Cell, eyeThing, this.Faction, null, speed); + flyingObject = (FlyingObject_LightningTrap)GenSpawn.Spawn(TorannMagicDefOf.FlyingObject_LightningTrap, Position, Map); + flyingObject.Launch(p, Position.ToVector3Shifted(), t.Cell, Faction, speed); } if(iceTrap) { - AddSnowRadial(this.Position, this.Map, 6, 1.1f); + AddSnowRadial(Position, Map, 6, 1.1f); } } @@ -73,4 +74,4 @@ public override void Destroy(DestroyMode mode = DestroyMode.Vanish) } } } -} \ No newline at end of file +} diff --git a/RimWorldOfMagic/RimWorldOfMagic/FlyingObject_LightningTrap.cs b/RimWorldOfMagic/RimWorldOfMagic/FlyingObject_LightningTrap.cs index e1118d25..3b370e1f 100644 --- a/RimWorldOfMagic/RimWorldOfMagic/FlyingObject_LightningTrap.cs +++ b/RimWorldOfMagic/RimWorldOfMagic/FlyingObject_LightningTrap.cs @@ -10,8 +10,6 @@ namespace TorannMagic [StaticConstructorOnStartup] public class FlyingObject_LightningTrap : Projectile { - - private static readonly Color lightningColor = new Color(160f, 160f, 160f); private static readonly Material lightningMat = MaterialPool.MatFrom("Spells/LightningBolt_w", false); private static readonly Material OrbMat = MaterialPool.MatFrom("Spells/eyeofthestorm", false); @@ -19,355 +17,180 @@ public class FlyingObject_LightningTrap : Projectile protected new Vector3 destination; private int searchDelay = 10; - private int maxStrikeDelay = 100; - private int maxStrikeDelayBldg = 60; - private int lastStrike = 0; - private int lastStrikeBldg = 0; - private int age = -1; - private float arcaneDmg = 1; - public Matrix4x4 drawingMatrix = default(Matrix4x4); - public Vector3 drawingScale; - public Vector3 drawingPosition; - IntVec3[] from = new IntVec3[10]; - Vector3[] to = new Vector3[10]; - int[] fadeTimer = new int[10]; + private const float arcaneDmg = 1; // Currently not used. Could be used for difficulty damage changes. + + private class Strike + { + public IntVec3 from; + public Vector3 to; + public int fadeTimer; - //private int pwrVal = 0; - //private int verVal = 0; + public void CountDown() + { + fadeTimer--; + if (fadeTimer > 0) return; + + from = default; + to = default; + } + } + private readonly Strike[] strikeArray = Enumerable.Range(0, 10).Select(i => new Strike()).ToArray(); public float speed = .8f; protected new int ticksToImpact; - protected Faction faction = null; - //protected new Thing launcher; - protected Thing assignedTarget; + protected Faction faction; protected Thing flyingThing; - Pawn pawn; - - public DamageInfo? impactDamage; - - public bool damageLaunched = true; - - public bool explosion = false; - - public int timesToDamage = 3; - - public int weaponDmg = 0; - private bool initialized = true; + protected new int StartingTicksToImpact => Math.Max(1, + Mathf.RoundToInt((origin - destination).magnitude / (speed / 100f))); - protected new int StartingTicksToImpact - { - get - { - int num = Mathf.RoundToInt((this.origin - this.destination).magnitude / (this.speed / 100f)); - bool flag = num < 1; - if (flag) - { - num = 1; - } - return num; - } - } - - protected new IntVec3 DestinationCell - { - get - { - return new IntVec3(this.destination); - } - } + protected new IntVec3 DestinationCell => new IntVec3(destination); public new Vector3 ExactPosition { get { - Vector3 b = (this.destination - this.origin) * (1f - (float)this.ticksToImpact / (float)this.StartingTicksToImpact); - return this.origin + b + Vector3.up * this.def.Altitude; - } - } - - public new Quaternion ExactRotation - { - get - { - return Quaternion.LookRotation(this.destination - this.origin); + Vector3 b = (destination - origin) * (1f - ticksToImpact / (float)StartingTicksToImpact); + return origin + b + Vector3.up * def.Altitude; } } - public override Vector3 DrawPos - { - get - { - return this.ExactPosition; - } - } + public override Vector3 DrawPos => ExactPosition; public override void ExposeData() { base.ExposeData(); - Scribe_Values.Look(ref this.origin, "origin", default(Vector3), false); - Scribe_Values.Look(ref this.destination, "destination", default(Vector3), false); - Scribe_Values.Look(ref this.ticksToImpact, "ticksToImpact", 0, false); - Scribe_Values.Look(ref this.timesToDamage, "timesToDamage", 0, false); - Scribe_Values.Look(ref this.searchDelay, "searchDelay", 210, false); - //Scribe_Values.Look(ref this.pwrVal, "pwrVal", 0, false); - //Scribe_Values.Look(ref this.verVal, "verVal", 0, false); - Scribe_Values.Look(ref this.damageLaunched, "damageLaunched", true, false); - Scribe_Values.Look(ref this.explosion, "explosion", false, false); - Scribe_Values.Look(ref this.initialized, "initialized", false, false); - Scribe_References.Look(ref this.assignedTarget, "assignedTarget", false); - //Scribe_References.Look(ref this.launcher, "launcher", false); - Scribe_References.Look(ref this.pawn, "pawn", false); - Scribe_Deep.Look(ref this.flyingThing, "flyingThing", new object[0]); + Scribe_Values.Look(ref origin, "origin"); + Scribe_Values.Look(ref destination, "destination"); + Scribe_Values.Look(ref ticksToImpact, "ticksToImpact"); + Scribe_Values.Look(ref searchDelay, "searchDelay", 210); + Scribe_Deep.Look(ref flyingThing, "flyingThing"); } private void Initialize() { - if (pawn != null) - { - FleckMaker.Static(this.origin, this.Map, FleckDefOf.ExplosionFlash, 12f); - SoundDefOf.Ambient_AltitudeWind.sustainFadeoutTime.Equals(30.0f); - FleckMaker.ThrowDustPuff(this.origin, this.Map, Rand.Range(1.2f, 1.8f)); - } - flyingThing.ThingID += Rand.Range(0, 214).ToString(); - this.initialized = false; - } - - public void Launch(Thing launcher, LocalTargetInfo targ, Thing flyingThing, DamageInfo? impactDamage) - { - this.Launch(launcher, base.Position.ToVector3Shifted(), targ, flyingThing, null, impactDamage); - } - - public void Launch(Thing launcher, LocalTargetInfo targ, Thing flyingThing) - { - this.Launch(launcher, base.Position.ToVector3Shifted(), targ, flyingThing, null, null); + FleckMaker.Static(origin, Map, FleckDefOf.ExplosionFlash, 12f); + FleckMaker.ThrowDustPuff(origin, Map, Rand.Range(1.2f, 1.8f)); + flyingThing.ThingID = flyingThing.ThingID; // Get a new thingIDNumber } - public void Launch(Thing launcher, Vector3 origin, LocalTargetInfo targ, Thing flyingThing, Faction faction, DamageInfo? newDamageInfo = null, float _speed = .8f) - { - bool spawned = flyingThing.Spawned; - pawn = launcher as Pawn; - this.speed = _speed; - //CompAbilityUserMagic comp = pawn.GetCompAbilityUserMagic(); - //this.arcaneDmg = comp.arcaneDmg; - //MagicPowerSkill pwr = pawn.GetCompAbilityUserMagic().MagicData.MagicPowerSkill_EyeOfTheStorm.FirstOrDefault((MagicPowerSkill x) => x.label == "TM_EyeOfTheStorm_pwr"); - //MagicPowerSkill ver = pawn.GetCompAbilityUserMagic().MagicData.MagicPowerSkill_EyeOfTheStorm.FirstOrDefault((MagicPowerSkill x) => x.label == "TM_EyeOfTheStorm_ver"); - //verVal = ver.level; - //pwrVal = pwr.level; - // - //if (ModOptions.Settings.Instance.AIHardMode && !pawn.IsColonist) - //{ - // pwrVal = 1; - // verVal = 1; - //} - if (spawned) - { - flyingThing.DeSpawn(); - } - this.launcher = launcher; - this.origin = origin; - this.faction = faction; - this.impactDamage = newDamageInfo; - this.flyingThing = flyingThing; - bool flag = targ.Thing != null; - if (flag) - { - this.assignedTarget = targ.Thing; - } - this.destination = targ.Cell.ToVector3Shifted(); - this.ticksToImpact = this.StartingTicksToImpact; - - this.Initialize(); + public void Launch( + Thing projectileLauncher, + Vector3 projectileOrigin, + LocalTargetInfo target, + Faction projectileFaction = null, + float _speed = .8f) + { + // This is functioning essentially as a constructor + speed = _speed; + launcher = projectileLauncher; + origin = projectileOrigin; + faction = projectileFaction ?? Faction.OfPlayer; + flyingThing = new Thing { def = TorannMagicDefOf.FlyingObject_LightningTrap }; + destination = target.Cell.ToVector3Shifted(); + ticksToImpact = StartingTicksToImpact; + + Initialize(); } public override void Tick() { //base.Tick(); - age++; - this.searchDelay--; - Vector3 exactPosition = this.ExactPosition; - this.ticksToImpact--; - bool flag = !this.ExactPosition.InBoundsWithNullCheck(base.Map); - if (flag) - { - this.ticksToImpact++; - base.Position = this.ExactPosition.ToIntVec3(); - this.Destroy(DestroyMode.Vanish); - } + searchDelay--; + Vector3 exactPosition = ExactPosition; + ticksToImpact--; + if (!ExactPosition.InBoundsWithNullCheck(Map)) Destroy(); else { - base.Position = this.ExactPosition.ToIntVec3(); - DrawOrb(exactPosition, base.Map); - if(this.searchDelay < 0) + Position = ExactPosition.ToIntVec3(); + DrawOrb(exactPosition, Map); + if(searchDelay < 0) { - this.searchDelay = Rand.Range(20, 35); - SearchForTargets(this.origin.ToIntVec3(), 6f); - } - bool flag2 = this.ticksToImpact <= 0; - if (flag2) + searchDelay = Rand.Range(20, 35); + SearchForTargets(origin.ToIntVec3(), 6f); + } + if (ticksToImpact <= 0) { - bool flag3 = this.DestinationCell.InBoundsWithNullCheck(base.Map); - if (flag3) + if (DestinationCell.InBoundsWithNullCheck(Map)) { - base.Position = this.DestinationCell; + Position = DestinationCell; } - this.ImpactSomething(); + Impact(); } } } public void DrawOrb(Vector3 orbVec, Map map) { - Vector3 vector = orbVec; - float xOffset = Rand.Range(-0.6f, 0.6f); - float zOffset = Rand.Range(-0.6f, 0.6f); - orbVec.x += xOffset; - orbVec.z += zOffset; + orbVec.x += Rand.Range(-0.6f, 0.6f); + orbVec.z += Rand.Range(-0.6f, 0.6f); FleckMaker.ThrowLightningGlow(orbVec, map, 0.4f); - float num = Mathf.Lerp(1.2f, 1.55f, 5f); - vector.y = Altitudes.AltitudeFor(AltitudeLayer.MoteOverhead); - float angle = (float)Rand.Range(0, 360); - Vector3 s = new Vector3(0.4f, 0.4f, 0.4f); + orbVec.y = AltitudeLayer.MoteOverhead.AltitudeFor(); Matrix4x4 matrix = default(Matrix4x4); - matrix.SetTRS(vector, Quaternion.AngleAxis(0f, Vector3.up), s); - Graphics.DrawMesh(MeshPool.plane10, matrix, FlyingObject_LightningTrap.OrbMat, 0); + matrix.SetTRS(orbVec, Quaternion.AngleAxis(0f, Vector3.up), new Vector3(0.4f, 0.4f, 0.4f)); + Graphics.DrawMesh(MeshPool.plane10, matrix, OrbMat, 0); } public void SearchForTargets(IntVec3 center, float radius) { - Pawn target = null; - if (faction == null) - { - faction = Faction.OfPlayer; - } - target = TM_Calc.FindNearbyEnemy(center, this.Map, this.faction, radius, 0f); + Pawn target = TM_Calc.FindNearbyEnemy(center, Map, faction, radius, 0f); if (target != null) { CellRect cellRect = CellRect.CenteredOn(target.Position, 2); - cellRect.ClipInsideMap(base.Map); + cellRect.ClipInsideMap(Map); DrawStrike(center, target.Position.ToVector3()); for (int k = 0; k < Rand.Range(1, 5); k++) { IntVec3 randomCell = cellRect.RandomCell; - GenExplosion.DoExplosion(randomCell, base.Map, Rand.Range(.4f, .8f), TMDamageDefOf.DamageDefOf.TM_Lightning, this.launcher, Mathf.RoundToInt(Rand.Range(4, 6)), 0, SoundDefOf.Thunder_OnMap, null, null, null, null, 0f, 1, null, false, null, 0f, 1, 0.1f, true); + GenExplosion.DoExplosion(randomCell, Map, Rand.Range(.4f, .8f), TMDamageDefOf.DamageDefOf.TM_Lightning, launcher, Mathf.RoundToInt(Rand.Range(4, 6)), 0, SoundDefOf.Thunder_OnMap, null, null, null, null, 0f, 1, false, null, 0f, 1, 0.1f, true); } - GenExplosion.DoExplosion(target.Position, base.Map, 1f, TMDamageDefOf.DamageDefOf.TM_Lightning, this.launcher, Mathf.RoundToInt(Rand.Range(5, 9) * this.arcaneDmg), 0, SoundDefOf.Thunder_OffMap, null, null, null, null, 0f, 1, null, false, null, 0f, 1, 0.1f, true); - this.lastStrike = this.age; + GenExplosion.DoExplosion(target.Position, Map, 1f, TMDamageDefOf.DamageDefOf.TM_Lightning, launcher, Mathf.RoundToInt(Rand.Range(5, 9) * arcaneDmg), 0, SoundDefOf.Thunder_OffMap, null, null, null, null, 0f, 1, false, null, 0f, 1, 0.1f, true); } DrawStrikeFading(); } public void DrawStrike(IntVec3 center, Vector3 dest) { - TM_MeshBolt meshBolt = new TM_MeshBolt(center, dest, FlyingObject_LightningTrap.lightningMat); - meshBolt.CreateBolt(); - for (int i = 0; i < 10; i++) - { - if (fadeTimer[i] <= 0) - { - from[i] = center; - to[i] = dest; - fadeTimer[i] = 30; - i = 10; - } - } - } + new TM_MeshBolt(center, dest, lightningMat).CreateBolt(); - public void DrawStrikeFading() - { - for(int i = 0; i < 10; i ++) - { - if (fadeTimer[i] > 0) - { - TM_MeshBolt meshBolt = new TM_MeshBolt(from[i], to[i], FlyingObject_LightningTrap.lightningMat); - meshBolt.CreateFadedBolt(fadeTimer[i]/30); - fadeTimer[i]--; - if (fadeTimer[i] == 0) - { - from[i] = default(IntVec3); - to[i] = default(Vector3); - } - } - } + Strike strike = strikeArray.FirstOrDefault(s => s.fadeTimer <= 0); + if (strike == default) return; + + strike.from = center; + strike.to = dest; + strike.fadeTimer = 30; } - private void ImpactSomething() + public void DrawStrikeFading() { - bool flag = this.assignedTarget != null; - if (flag) - { - Pawn pawn = this.assignedTarget as Pawn; - bool flag2 = pawn != null && pawn.GetPosture() != PawnPosture.Standing && (this.origin - this.destination).MagnitudeHorizontalSquared() >= 20.25f && Rand.Value > 0.2f; - if (flag2) - { - this.Impact(null); - } - else - { - this.Impact(this.assignedTarget); - } - } - else + foreach (Strike strike in strikeArray.Where(s => s.fadeTimer > 0)) { - this.Impact(null); + new TM_MeshBolt(strike.from, strike.to, lightningMat).CreateFadedBolt(strike.fadeTimer/30); + strike.CountDown(); } } - protected new void Impact(Thing hitThing) + protected void Impact() { - bool flag = hitThing == null; - if (flag) - { - Pawn hitPawn; - bool flag2 = (hitPawn = (base.Position.GetThingList(base.Map).FirstOrDefault((Thing x) => x == this.assignedTarget) as Pawn)) != null; - if (flag2) - { - hitThing = hitPawn; - } - } - bool hasValue = this.impactDamage.HasValue; - if (hasValue) - { - for (int i = 0; i < this.timesToDamage; i++) - { - bool flag3 = this.damageLaunched; - if (flag3) - { - this.flyingThing.TakeDamage(this.impactDamage.Value); - } - else - { - hitThing.TakeDamage(this.impactDamage.Value); - } - } - bool flag4 = this.explosion; - if (flag4) - { - GenExplosion.DoExplosion(this.origin.ToIntVec3(), base.Map, 0.9f, DamageDefOf.Stun, this, -1, 0, null, null, null, null, null, 0f, 1, null, false, null, 0f, 1, 0f, false); - } - } - - List dissipationList = GenRadial.RadialCellsAround(this.origin.ToIntVec3(), 5, false).ToList(); + List dissipationList = GenRadial.RadialCellsAround(origin.ToIntVec3(), 5, false).ToList(); for (int i = 0; i < 4; i++) { IntVec3 strikeCell = dissipationList.RandomElement(); - if (strikeCell.InBoundsWithNullCheck(base.Map) && strikeCell.IsValid && !strikeCell.Fogged(this.Map)) + if (!strikeCell.InBoundsWithNullCheck(Map) || !strikeCell.IsValid || strikeCell.Fogged(Map)) continue; + + DrawStrike(ExactPosition.ToIntVec3(), strikeCell.ToVector3Shifted()); + for (int k = 0; k < Rand.Range(1, 8); k++) { - DrawStrike(this.ExactPosition.ToIntVec3(), strikeCell.ToVector3Shifted()); - for (int k = 0; k < Rand.Range(1, 8); k++) - { - CellRect cellRect = CellRect.CenteredOn(strikeCell, 2); - cellRect.ClipInsideMap(base.Map); - IntVec3 randomCell = cellRect.RandomCell; - GenExplosion.DoExplosion(randomCell, base.Map, Rand.Range(.2f, .6f), TMDamageDefOf.DamageDefOf.TM_Lightning, this.launcher, Mathf.RoundToInt(Rand.Range(2, 6)), 0, SoundDefOf.Thunder_OffMap, null, null, null, null, 0f, 1, null, false, null, 0f, 1, 0.1f, true); - } + CellRect cellRect = CellRect.CenteredOn(strikeCell, 2); + cellRect.ClipInsideMap(Map); + IntVec3 randomCell = cellRect.RandomCell; + GenExplosion.DoExplosion(randomCell, Map, Rand.Range(.2f, .6f), TMDamageDefOf.DamageDefOf.TM_Lightning, launcher, Mathf.RoundToInt(Rand.Range(2, 6)), 0, SoundDefOf.Thunder_OffMap, null, null, null, null, 0f, 1, false, null, 0f, 1, 0.1f, true); } } - GenExplosion.DoExplosion(this.origin.ToIntVec3(), base.Map, 1f, TMDamageDefOf.DamageDefOf.TM_Lightning, this.launcher, Mathf.RoundToInt(Rand.Range(4, 8)), 0, SoundDefOf.Thunder_OffMap, null, null, null, null, 0f, 1, null, false, null, 0f, 1, 0.1f, true); - + GenExplosion.DoExplosion(origin.ToIntVec3(), Map, 1f, TMDamageDefOf.DamageDefOf.TM_Lightning, launcher, Mathf.RoundToInt(Rand.Range(4, 8)), 0, SoundDefOf.Thunder_OffMap, null, null, null, null, 0f, 1, false, null, 0f, 1, 0.1f, true); - this.Destroy(DestroyMode.Vanish); + Destroy(); } } }