Sure! Below is a C# code snippet for a Terraria tModLoader mod that creates a custom boss NPC with the specified AI behavior:
- Floats randomly around the player while maintaining a small distance.
- Shoots 5 arcing projectiles towards the player every 3-5 seconds.
- Spawns a
Drippler enemy every 10-12 seconds.
Please make sure you have tModLoader installed and set up for mod development before using this code.
using Terraria;
using Terraria.ID;
using Terraria.ModLoader;
using Microsoft.Xna.Framework;
namespace YourMod.NPCs
{
public class FloatingBoss : ModNPC
{
private Player player;
private float speed = 4f;
private float approachDistance = 200f;
private int shootTimer = 0;
private int spawnTimer = 0;
public override void SetStaticDefaults()
{
DisplayName.SetDefault("Floating Boss");
Main.npcFrameCount[NPC.type] = Main.npcFrameCount[NPCID.EyeofCthulhu]; // Use Eye of Cthulhu frames as an example
}
public override void SetDefaults()
{
NPC.width = 100;
NPC.height = 100;
NPC.damage = 40;
NPC.defense = 10;
NPC.lifeMax = 5000;
NPC.HitSound = SoundID.NPCHit1;
NPC.DeathSound = SoundID.NPCDeath14;
NPC.value = Item.buyPrice(gold: 10);
NPC.knockBackResist = 0f;
NPC.aiStyle = -1; // Custom AI
NPC.noGravity = true;
NPC.noTileCollide = true;
NPC.boss = true;
Music = MusicID.Boss1;
}
public override void ScaleExpertStats(int numPlayers, float bossLifeScale)
{
NPC.lifeMax = (int)(NPC.lifeMax * 0.6f * bossLifeScale);
NPC.damage = (int)(NPC.damage * 0.6f);
}
public override void AI()
{
// Target the player
if (NPC.target < 0 || NPC.target == 255 || player == null || !player.active || player.dead)
{
NPC.TargetClosest(faceTarget: true);
player = Main.player[NPC.target];
if (!player.active || player.dead)
{
// If no valid player is found, fly up and despawn
NPC.velocity = new Vector2(0f, -10f);
if (NPC.timeLeft > 10)
{
NPC.timeLeft = 10;
}
return;
}
}
Vector2 targetPosition = player.Center;
// Calculate desired position (floating around the player at a certain distance)
Vector2 direction = NPC.Center - player.Center;
if (direction.Length() < approachDistance)
{
// Move away if too close
NPC.velocity += direction.SafeNormalize(Vector2.Zero) * 0.1f;
}
else if (direction.Length() > approachDistance)
{
// Move closer if too far
direction = direction.SafeNormalize(Vector2.Zero);
NPC.velocity -= direction * 0.1f;
}
// Add some randomness to movement
NPC.ai[0] += 1f;
if (NPC.ai[0] > 100)
{
NPC.ai[0] = 0f;
Vector2 randomOffset = new Vector2(Main.rand.NextFloat(-1f, 1f), Main.rand.NextFloat(-1f, 1f));
NPC.velocity += randomOffset;
}
// Limit the speed
if (NPC.velocity.Length() > speed)
{
NPC.velocity = Vector2.Normalize(NPC.velocity) * speed;
}
// Shooting projectiles every 3-5 seconds
shootTimer++;
if (shootTimer >= 180) // 3 seconds * 60 ticks per second
{
shootTimer = 0;
ShootProjectiles();
}
// Spawning Drippler every 10-12 seconds
spawnTimer++;
if (spawnTimer >= 600) // 10 seconds * 60 ticks per second
{
spawnTimer = 0;
SpawnDrippler();
}
}
private void ShootProjectiles()
{
int numberProjectiles = 5;
for (int i = 0; i < numberProjectiles; i++)
{
// Calculate spread of projectiles
Vector2 shootDirection = player.Center - NPC.Center;
shootDirection += new Vector2(Main.rand.NextFloat(-100f, 100f), Main.rand.NextFloat(-100f, 100f));
shootDirection.Normalize();
shootDirection *= 10f;
int projectileID = ProjectileID.ShadowFlame; // Choose the projectile type you want
int damage = 20;
float knockBack = 1f;
Projectile.NewProjectile(NPC.GetSource_FromAI(), NPC.Center, shootDirection, projectileID, damage, knockBack, Main.myPlayer);
}
}
private void SpawnDrippler()
{
int npcID = NPCID.Drippler;
Vector2 spawnPosition = NPC.Center + new Vector2(Main.rand.Next(-200, 200), Main.rand.Next(-200, 200));
NPC.NewNPC(NPC.GetSource_FromAI(), (int)spawnPosition.X, (int)spawnPosition.Y, npcID);
}
public override void BossLoot(ref string name, ref int potionType)
{
potionType = ItemID.HealingPotion; // Healing potions dropped on death
// Add loot here
}
public override void FindFrame(int frameHeight)
{
NPC.spriteDirection = NPC.direction;
NPC.frameCounter += 1.0;
if (NPC.frameCounter >= 8.0)
{
NPC.frameCounter = 0.0;
NPC.frame.Y = NPC.frame.Y + frameHeight;
if (NPC.frame.Y >= Main.npcFrameCount[NPC.type] * frameHeight)
{
NPC.frame.Y = 0;
}
}
}
}
}
Explanation:
-
Floating Behavior: The boss maintains a set distance (approachDistance) from the player. It adjusts its velocity to either move closer or further away to maintain this distance. Randomness is added to the movement to make it less predictable.
-
Projectile Shooting: Every 3 seconds (shootTimer reaches 180 ticks), the boss shoots 5 projectiles towards the player. The projectiles have a spread due to random offsets in the shootDirection.
-
Spawning Dripplers: Every 10 seconds (spawnTimer reaches 600 ticks), the boss spawns a Drippler enemy at a random position near itself.
-
AI Style: The boss uses a custom AI (NPC.aiStyle = -1) since we are defining the movement and actions manually in the AI() method.
-
Visuals and Frames: FindFrame() manages the animation frames for the boss. Adjust Main.npcFrameCount[NPC.type] and related values to match your sprite sheet.
-
Loot and Drops: BossLoot() can be modified to add custom loot drops when the boss is defeated.
Notes:
- Replace
"YourMod" with the actual namespace of your mod.
- Ensure you have proper sprites for the boss NPC. Place them in the correct folder and reference them appropriately.
- You might need to adjust the hitbox size (
NPC.width and NPC.height), damage, defense, and other stats to fit your game's balance.
- The projectile ID (
ProjectileID.ShadowFlame) can be changed to any projectile you prefer or a custom one you've created.
- Remember to register the NPC in your mod's
Mod.cs file if necessary.
Testing:
- Build your mod using tModLoader's mod builder.
- Enable the mod in-game and summon the boss using a spawning item or command.
- Test the boss's behavior to ensure it moves and attacks as expected.
- Adjust timers and values as needed based on your observations.
Let me know if you need further assistance or modifications!