From ecc2f2dd847abc49c31eec1d34e90c503df145a5 Mon Sep 17 00:00:00 2001 From: IbanPlay <56191483+IbanPlay@users.noreply.github.com> Date: Wed, 26 Jan 2022 17:17:59 +0100 Subject: [PATCH] player.CheckSolidGround util --- Items/Weapons/Melee/BiomeBlade.cs | 8 ++----- Items/Weapons/Melee/TrueBiomeBlade.cs | 9 ++------ Utilities/PlayerUtils.cs | 32 +++++++++++++++++++++++++++ 3 files changed, 36 insertions(+), 13 deletions(-) diff --git a/Items/Weapons/Melee/BiomeBlade.cs b/Items/Weapons/Melee/BiomeBlade.cs index e16eb1a10e..df44233543 100644 --- a/Items/Weapons/Melee/BiomeBlade.cs +++ b/Items/Weapons/Melee/BiomeBlade.cs @@ -259,11 +259,8 @@ namespace CalamityMod.Items.Weapons.Melee if (Main.projectile.Any(n => n.active && n.type == ProjectileType() && n.owner == player.whoAmI)) return; - int x = (int)player.Center.X / 16; - int y = (int)(player.position.Y + (float)player.height - 1f) / 16; - Tile tileStandingOn = Main.tile[x, y + 1]; - bool mayAttune = player.StandingStill() && !player.mount.Active && tileStandingOn.IsTileSolidGround(); + bool mayAttune = player.StandingStill() && !player.mount.Active && player.CheckSolidGround(1, 3); Vector2 displace = new Vector2(18f, 0f); Projectile.NewProjectile(player.Top + displace, Vector2.Zero, ProjectileType(), 0, 0, player.whoAmI, mayAttune ? 0f : 1f); } @@ -326,9 +323,8 @@ namespace CalamityMod.Items.Weapons.Melee { private Player Owner => Main.player[projectile.owner]; - public bool OwnerOnGround => Main.tile[(int)Owner.Center.X / 16, (int)(Owner.position.Y + (float)Owner.height - 1f) / 16 + 1].IsTileSolidGround() && Main.tile[(int)Owner.Center.X / 16 + Owner.direction, (int)(Owner.position.Y + (float)Owner.height - 1f) / 16 + 1].IsTileSolidGround(); public bool OwnerCanUseItem => Owner.HeldItem == associatedItem ? (Owner.HeldItem.modItem as BiomeBlade).CanUseItem(Owner) : false; - public bool OwnerMayChannel => OwnerCanUseItem && Owner.Calamity().mouseRight && Owner.active && !Owner.dead && Owner.StandingStill() && !Owner.mount.Active && OwnerOnGround; + public bool OwnerMayChannel => OwnerCanUseItem && Owner.Calamity().mouseRight && Owner.active && !Owner.dead && Owner.StandingStill() && !Owner.mount.Active && Owner.CheckSolidGround(1, 3); public ref float ChanneledState => ref projectile.ai[0]; public ref float ChannelTimer => ref projectile.ai[1]; public ref float Initialized => ref projectile.localAI[0]; diff --git a/Items/Weapons/Melee/TrueBiomeBlade.cs b/Items/Weapons/Melee/TrueBiomeBlade.cs index 18e585f5a8..7caa304417 100644 --- a/Items/Weapons/Melee/TrueBiomeBlade.cs +++ b/Items/Weapons/Melee/TrueBiomeBlade.cs @@ -281,11 +281,7 @@ namespace CalamityMod.Items.Weapons.Melee if (Main.projectile.Any(n => n.active && n.type == ProjectileType() && n.owner == player.whoAmI)) return; - int x = (int)player.Center.X / 16; - int y = (int)(player.position.Y + (float)player.height - 1f) / 16; - Tile tileStandingOn = Main.tile[x, y + 1]; - - bool mayAttune = player.StandingStill() && !player.mount.Active && tileStandingOn.IsTileSolidGround(); + bool mayAttune = player.StandingStill() && !player.mount.Active && player.CheckSolidGround(1, 3); Vector2 displace = new Vector2(18f, 0f); Projectile.NewProjectile(player.Top + displace, Vector2.Zero, ProjectileType(), 0, 0, player.whoAmI, mayAttune ? 0f : 1f); } @@ -381,9 +377,8 @@ namespace CalamityMod.Items.Weapons.Melee public class BiomeBladeHoldout : ModProjectile //Visuals { private Player Owner => Main.player[projectile.owner]; - public bool OwnerOnGround => Main.tile[(int)Owner.Center.X / 16, (int)(Owner.position.Y + (float)Owner.height - 1f) / 16 + 1].IsTileSolidGround() && Main.tile[(int)Owner.Center.X / 16 + Owner.direction, (int)(Owner.position.Y + (float)Owner.height - 1f) / 16 + 1].IsTileSolidGround(); public bool OwnerCanUseItem => Owner.HeldItem == associatedItem ? (Owner.HeldItem.modItem as TrueBiomeBlade).CanUseItem(Owner) : false; - public bool OwnerMayChannel => OwnerCanUseItem && Owner.Calamity().mouseRight && Owner.active && !Owner.dead && Owner.StandingStill() && !Owner.mount.Active && OwnerOnGround; + public bool OwnerMayChannel => OwnerCanUseItem && Owner.Calamity().mouseRight && Owner.active && !Owner.dead && Owner.StandingStill() && !Owner.mount.Active && Owner.CheckSolidGround(1, 3); public ref float ChanneledState => ref projectile.ai[0]; public ref float ChannelTimer => ref projectile.ai[1]; public ref float Initialized => ref projectile.localAI[0]; diff --git a/Utilities/PlayerUtils.cs b/Utilities/PlayerUtils.cs index 8ac53f212f..d5604785f4 100644 --- a/Utilities/PlayerUtils.cs +++ b/Utilities/PlayerUtils.cs @@ -187,6 +187,38 @@ namespace CalamityMod target.AddBuff(BuffID.OnFire, (int)(180 * multiplier)); } + /// + /// Checks if the player is ontop of solid ground. May also check for solid ground for X tiles in front of them + /// + /// The Player whose position is being checked + /// How many tiles in front of the player to check + /// How many tiles above every checked tile are checked for non-solid ground + public static bool CheckSolidGround(this Player player, int solidGroundAhead = 0, int airExposureNeeded = 0) + { + if (player.velocity.Y != 0) //Player gotta be standing still in any case + return false; + + Tile checkedTile; + bool ConditionMet = true; + + for (int i = 0; i <= solidGroundAhead; i++) //Check i tiles in front of the player + { + ConditionMet = Main.tile[(int)player.Center.X / 16 + player.direction * i, (int)(player.position.Y + (float)player.height - 1f) / 16 + 1].IsTileSolidGround(); + if (!ConditionMet) + return ConditionMet; + + for (int j = 1; j <= airExposureNeeded; j++) //Check j tiles ontop of each checked tiles for non-solid ground + { + checkedTile = Main.tile[(int)player.Center.X / 16 + player.direction * i, (int)(player.position.Y + (float)player.height - 1f) / 16 + 1 - j]; + + ConditionMet = !(checkedTile != null && checkedTile.nactive() && Main.tileSolid[checkedTile.type]); //IsTileSolidGround minus the ground part, to avoid platforms and other half solid tiles messing it up + if (!ConditionMet) + return ConditionMet; + } + } + return ConditionMet; + } + /// /// Makes the given player send the given packet to all appropriate receivers.
/// If server is false, the packet is sent only to the multiplayer host.