diff --git a/NPCs/SunkenSea/SeaMinnow.cs b/NPCs/SunkenSea/SeaMinnow.cs index 5acb5b2eb5..90abec83da 100644 --- a/NPCs/SunkenSea/SeaMinnow.cs +++ b/NPCs/SunkenSea/SeaMinnow.cs @@ -82,113 +82,147 @@ namespace CalamityMod.NPCs.SunkenSea { if (owner == null || !owner.active || (owner.type != ModContent.NPCType() && owner.type != ModContent.NPCType())) { - CalamityRegularEnemyAI.PassiveSwimmingAI(NPC, Mod, 3, 150f, 0.25f, 0.15f, 6f, 6f, 0.05f); - NPC.spriteDirection = (NPC.direction > 0) ? 1 : -1; - NPC.noGravity = true; - bool shouldSwimAway = false; - if (NPC.direction == 0) + if (CurrentPredator == null) { - NPC.TargetClosest(true); - } - NPC.TargetClosest(false); - if (Main.player[NPC.target].wet && !Main.player[NPC.target].dead && - (Main.player[NPC.target].Center - NPC.Center).Length() < 150f) - { - shouldSwimAway = true; - } - if ((!Main.player[NPC.target].wet || Main.player[NPC.target].dead) && shouldSwimAway) - { - shouldSwimAway = false; - } - if (!shouldSwimAway) - { - if (NPC.collideX || NPC.velocity.X == 0f) + CalamityRegularEnemyAI.PassiveSwimmingAI(NPC, Mod, 3, 150f, 0.25f, 0.15f, 6f, 6f, 0.05f); + NPC.spriteDirection = (NPC.direction > 0) ? 1 : -1; + NPC.noGravity = true; + bool shouldSwimAway = false; + if (NPC.direction == 0) { - NPC.velocity.X = NPC.velocity.X * -3f; - NPC.direction *= -1; - NPC.netUpdate = true; + NPC.TargetClosest(true); } - if (NPC.collideY) + NPC.TargetClosest(false); + if (Main.player[NPC.target].wet && !Main.player[NPC.target].dead && + (Main.player[NPC.target].Center - NPC.Center).Length() < 150f) { - NPC.netUpdate = true; - if (NPC.velocity.Y > 0f) + shouldSwimAway = true; + } + if ((!Main.player[NPC.target].wet || Main.player[NPC.target].dead) && shouldSwimAway) + { + shouldSwimAway = false; + } + if (!shouldSwimAway) + { + if (NPC.collideX || NPC.velocity.X == 0f) + { + NPC.velocity.X = NPC.velocity.X * -3f; + NPC.direction *= -1; + NPC.netUpdate = true; + } + if (NPC.collideY) + { + NPC.netUpdate = true; + if (NPC.velocity.Y > 0f) + { + NPC.velocity.Y = Math.Abs(NPC.velocity.Y) * -3f; + NPC.directionY = -1; + NPC.ai[0] = -1f; + } + else if (NPC.velocity.Y < 0f) + { + NPC.velocity.Y = Math.Abs(NPC.velocity.Y); + NPC.directionY = 1; + NPC.ai[0] = 1f; + } + } + } + if (shouldSwimAway) + { + NPC.TargetClosest(true); + NPC.velocity.X = NPC.velocity.X - (float)NPC.direction * 0.25f; + NPC.velocity.Y = NPC.velocity.Y - (float)NPC.directionY * 0.15f; + if (NPC.velocity.X > 6f) + { + NPC.velocity.X = 6f; + } + if (NPC.velocity.X < -6f) + { + NPC.velocity.X = -6f; + } + if (NPC.velocity.Y > 6f) + { + NPC.velocity.Y = 6f; + } + if (NPC.velocity.Y < -6f) + { + NPC.velocity.Y = -6f; + } + NPC.direction *= -1; + } + else + { + NPC.velocity.X = NPC.velocity.X + (float)NPC.direction * 0.1f; + if (NPC.velocity.X < -2.5f || NPC.velocity.X > 2.5f) + { + NPC.velocity.X = NPC.velocity.X * 0.95f; + } + if (NPC.ai[0] == -1f) + { + NPC.velocity.Y = NPC.velocity.Y - 0.01f; + if ((double)NPC.velocity.Y < -0.3) + { + NPC.ai[0] = 1f; + } + } + else + { + NPC.velocity.Y = NPC.velocity.Y + 0.01f; + if ((double)NPC.velocity.Y > 0.3) + { + NPC.ai[0] = -1f; + } + } + } + int npcTileX = (int)(NPC.position.X + (float)(NPC.width / 2)) / 16; + int npcTileY = (int)(NPC.position.Y + (float)(NPC.height / 2)) / 16; + if (Main.tile[npcTileX, npcTileY - 1].LiquidAmount > 128) + { + if (Main.tile[npcTileX, npcTileY + 1].HasTile) { - NPC.velocity.Y = Math.Abs(NPC.velocity.Y) * -3f; - NPC.directionY = -1; NPC.ai[0] = -1f; } - else if (NPC.velocity.Y < 0f) + else if (Main.tile[npcTileX, npcTileY + 2].HasTile) { - NPC.velocity.Y = Math.Abs(NPC.velocity.Y); - NPC.directionY = 1; - NPC.ai[0] = 1f; + NPC.ai[0] = -1f; } } - } - if (shouldSwimAway) - { - NPC.TargetClosest(true); - NPC.velocity.X = NPC.velocity.X - (float)NPC.direction * 0.25f; - NPC.velocity.Y = NPC.velocity.Y - (float)NPC.directionY * 0.15f; - if (NPC.velocity.X > 6f) + if ((double)NPC.velocity.Y > 0.4 || (double)NPC.velocity.Y < -0.4) { - NPC.velocity.X = 6f; + NPC.velocity.Y = NPC.velocity.Y * 0.95f; } - if (NPC.velocity.X < -6f) - { - NPC.velocity.X = -6f; - } - if (NPC.velocity.Y > 6f) - { - NPC.velocity.Y = 6f; - } - if (NPC.velocity.Y < -6f) - { - NPC.velocity.Y = -6f; - } - NPC.direction *= -1; } else { - NPC.velocity.X = NPC.velocity.X + (float)NPC.direction * 0.1f; - if (NPC.velocity.X < -2.5f || NPC.velocity.X > 2.5f) + // Check who is the avoided entity specifically. + avoidedEntity = avoidedEntity is NPC ? CurrentPredator : CurrentPlayer; + + if (avoidedEntity is not null) { - NPC.velocity.X = NPC.velocity.X * 0.95f; - } - if (NPC.ai[0] == -1f) - { - NPC.velocity.Y = NPC.velocity.Y - 0.01f; - if ((double)NPC.velocity.Y < -0.3) + // While it doesn't have any obstacles in front of it, run away in a straight line. + // Try to manuever if there are any obstacles. + if (!Main.tile[(NPC.Center + NPC.DirectionFrom(avoidedEntity.Center) * 96).ToTileCoordinates()].IsTileSolid()) { - NPC.ai[0] = 1f; + NPC.velocity += NPC.DirectionFrom(avoidedEntity.Center) * pathfinding.Acceleration; + pathfinding.ClearResults(); + + // Cap the speed if MaxSpeed has been surpassed. + if (NPC.velocity.LengthSquared() > pathfinding.MaxSpeed * pathfinding.MaxSpeed) + NPC.velocity = Vector2.Normalize(NPC.velocity) * pathfinding.MaxSpeed; + } + else + { + float distanceFromAvoided = Vector2.Distance(NPC.Center, avoidedEntity.Center); + randomPathPoint = NPC.Center + Main.rand.NextVector2Unit() * Utils.Remap(distanceFromAvoided, 0f, 960f, 80f, 3200f); + NPC.netUpdate = true; + pathfinding.DoPathfinding(new(NPC.Center, randomPathPoint, SunkenSeaTileValidity)); } } else { - NPC.velocity.Y = NPC.velocity.Y + 0.01f; - if ((double)NPC.velocity.Y > 0.3) - { - NPC.ai[0] = -1f; - } + pathfinding.DoPathfinding(new(NPC.Center, Main.rand.NextVector2Unit() * Main.rand.Next(300, 1000), SunkenSeaTileValidity)); } } - int npcTileX = (int)(NPC.position.X + (float)(NPC.width / 2)) / 16; - int npcTileY = (int)(NPC.position.Y + (float)(NPC.height / 2)) / 16; - if (Main.tile[npcTileX, npcTileY - 1].LiquidAmount > 128) - { - if (Main.tile[npcTileX, npcTileY + 1].HasTile) - { - NPC.ai[0] = -1f; - } - else if (Main.tile[npcTileX, npcTileY + 2].HasTile) - { - NPC.ai[0] = -1f; - } - } - if ((double)NPC.velocity.Y > 0.4 || (double)NPC.velocity.Y < -0.4) - { - NPC.velocity.Y = NPC.velocity.Y * 0.95f; - } } else { @@ -227,33 +261,18 @@ namespace CalamityMod.NPCs.SunkenSea } else { - // Check who is the avoided entity specifically. - avoidedEntity = avoidedEntity is NPC ? CurrentPredator : CurrentPlayer; - - if (avoidedEntity is not null) + if (NPC.velocity.Y == 0f) { - // While it doesn't have any obstacles in front of it, run away in a straight line. - // Try to manuever if there are any obstacles. - if (!Main.tile[(NPC.Center + NPC.DirectionFrom(avoidedEntity.Center) * 96).ToTileCoordinates()].IsTileSolid()) + NPC.velocity.X = NPC.velocity.X * 0.94f; + if ((double)NPC.velocity.X > -0.2 && (double)NPC.velocity.X < 0.2) { - NPC.velocity += NPC.DirectionFrom(avoidedEntity.Center) * pathfinding.Acceleration; - pathfinding.ClearResults(); - - // Cap the speed if MaxSpeed has been surpassed. - if (NPC.velocity.LengthSquared() > pathfinding.MaxSpeed * pathfinding.MaxSpeed) - NPC.velocity = Vector2.Normalize(NPC.velocity) * pathfinding.MaxSpeed; - } - else - { - float distanceFromAvoided = Vector2.Distance(NPC.Center, avoidedEntity.Center); - randomPathPoint = NPC.Center + Main.rand.NextVector2Unit() * Utils.Remap(distanceFromAvoided, 0f, 960f, 80f, 3200f); - NPC.netUpdate = true; - pathfinding.DoPathfinding(new(NPC.Center, randomPathPoint, SunkenSeaTileValidity)); + NPC.velocity.X = 0f; } } - else + NPC.velocity.Y = NPC.velocity.Y + 0.3f; + if (NPC.velocity.Y > 10f) { - pathfinding.DoPathfinding(new(NPC.Center, Main.rand.NextVector2Unit() * Main.rand.Next(300, 1000), SunkenSeaTileValidity)); + NPC.velocity.Y = 10f; } }