static bool Prefix(Projectile __instance)
            {
                if (!Enabled)
                {
                    return(true);
                }

                var projectile = __instance;

                var ticksToImpact         = (int)TicksToImpactField.GetValue(projectile);
                var startingTicksToImpact = (int)StartingTicksToImpactProperty.GetValue(projectile, null);

                var origin      = Common.ToVector2((Vector3)OriginField.GetValue(projectile));
                var destination = Common.ToVector2((Vector3)DestinationField.GetValue(projectile));
                var position    = Vector2.Lerp(origin, destination, 1.0f - ticksToImpact / (float)startingTicksToImpact);

                try
                {
                    if (projectile.def.projectile.flyOverhead)
                    {
                        // the shield has blocked the projectile - invert to get if harmony should allow the original block
                        return(!Mod.ShieldManager.ImpactShield(projectile.Map, position, origin, destination, (shield, vector2) =>
                        {
                            if (shield.Damage(projectile.def.projectile.GetDamageAmount(1f), position))
                            {
                                projectile.Destroy();
                                return true;
                            }
                            return false;
                        }));
                    }

                    var ray = new Ray2D(position, Vector2.Lerp(origin, destination, 1.0f - (ticksToImpact - 1) / (float)startingTicksToImpact));
                    Mod.ShieldManager.ImpactShield(projectile.Map, origin, ray, 1, (shield, point) =>
                    {
                        if (shield.Damage(projectile.def.projectile.GetDamageAmount(1f), point))
                        {
                            DestinationField.SetValue(projectile, Common.ToVector3(point, projectile.def.Altitude));
                            TicksToImpactField.SetValue(projectile, 0);
                            UsedTargetField.SetValue(projectile, null);
                            IntendedTargetField.SetValue(projectile, null);
                            return(true);
                        }
                        return(false);
                    });
                }
                catch (InvalidOperationException) {}
                return(true);
            }
Esempio n. 2
0
            static bool Prefix(ProjectileCE __instance)
            {
                if (!Enabled)
                {
                    return(true);
                }

                var projectile = __instance;

                var flightTicks           = (float)FTicksProperty.GetValue(projectile, null);
                var startingTicksToImpact = (float)StartingTicksToImpactProperty.GetValue(projectile, null);

                var origin       = (Vector2)OriginField.GetValue(projectile);
                var destination  = (Vector2)DestinationProperty.GetValue(projectile, null);
                var position3    = Common.ToVector3(Vector2.Lerp(origin, destination, flightTicks / startingTicksToImpact), projectile.Height);
                var origin3      = Common.ToVector3(origin);
                var destination3 = Common.ToVector3(destination);

                try
                {
                    if (projectile.def.projectile.flyOverhead)
                    {
                        if (projectile.def.projectile.flyOverhead)
                        {
                            // the shield has blocked the projectile - invert to get if harmony should allow the original block
                            return(!Mod.ShieldManager.ImpactShield(projectile.Map, position3, origin3, destination3, (shield, vector3) =>
                            {
                                if (shield.Damage(projectile.def.projectile.damageAmountBase, position3))
                                {
                                    projectile.Destroy();
                                    return true;
                                }
                                return false;
                            }));
                        }
                    }
                    else
                    {
                        var nextTick     = flightTicks + 1;
                        var nextHeight   = (float)GetHeightAtTicksMethod.Invoke(projectile, new object[] { (int)nextTick });
                        var nextPosition = Common.ToVector3(Vector2.Lerp(origin, destination, nextTick / startingTicksToImpact), nextHeight);
                        var ray          = new Ray(position3, nextPosition - position3);

                        // the shield has blocked the projectile - invert to get if harmony should allow the original block
                        return(!Mod.ShieldManager.ImpactShield(projectile.Map, origin3, ray, 1, (shield, point) =>
                        {
                            if (shield.Damage(projectile.def.projectile.damageAmountBase, point))
                            {
                                projectile.GetComp <CompExplosiveCE>()?.
                                Explode(
                                    (Thing)LauncherField.GetValue(projectile),
                                    new Vector3(point.x, 0, point.y),
                                    projectile.Map);
                                projectile.Destroy();
                                return true;
                            }
                            return false;
                        }));
                    }
                }
                catch (InvalidOperationException) {}
                return(true);
            }