Marcin JOT.PE
Terrarian
Help How to Fix Head
Code:
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Terraria;
using Terraria.ID;
using Terraria.ModLoader;
namespace MarcinMod.NPCs.Boss
{
public class ForestDragon_Head : ModNPC
{
public override void SetDefaults()
{
npc.name = "ForestDragon";
npc.displayName = "Forest Dragon";
npc.soundHit = 1;
npc.soundKilled = 1;
npc.lifeMax = 125;
npc.damage = 20;
npc.defense = 17;
npc.knockBackResist = 0.5f;
npc.width = 45; //22
npc.height = 45; //18
npc.boss = true;
npc.lavaImmune = true;
npc.noGravity = true;
npc.noTileCollide = true;
Main.npcFrameCount[npc.type] = 1;
npc.value = Item.buyPrice(0, 0, 1, 45);
npc.npcSlots = 1f;
npc.netAlways = true;
}
public override bool PreAI()
{
if (Main.netMode != 1)
{
if (npc.ai[0] == 0)
{
npc.realLife = npc.whoAmI;
int latestNPC = npc.whoAmI;
int WormLength = 1;
for (int i = 0; i < WormLength; ++i)
{
latestNPC = NPC.NewNPC((int)npc.Center.X, (int)npc.Center.Y, mod.NPCType("ForestDragon_Body"), npc.whoAmI, 0, latestNPC);
Main.npc[(int)latestNPC].realLife = npc.whoAmI;
Main.npc[(int)latestNPC].ai[3] = npc.whoAmI;
}
int WormLength2 = 1;
for (int i = 0; i < WormLength2; ++i)
{
latestNPC = NPC.NewNPC((int)npc.Center.X, (int)npc.Center.Y, mod.NPCType("ForestDragon_Legs"), npc.whoAmI, 0, latestNPC);
Main.npc[(int)latestNPC].realLife = npc.whoAmI;
Main.npc[(int)latestNPC].ai[3] = npc.whoAmI;
}
int WormLength3 = 4;
for (int i = 0; i < WormLength3; ++i)
{
latestNPC = NPC.NewNPC((int)npc.Center.X, (int)npc.Center.Y, mod.NPCType("ForestDragon_Body"), npc.whoAmI, 0, latestNPC);
Main.npc[(int)latestNPC].realLife = npc.whoAmI;
Main.npc[(int)latestNPC].ai[3] = npc.whoAmI;
}
for (int i = 0; i < WormLength2; ++i)
{
latestNPC = NPC.NewNPC((int)npc.Center.X, (int)npc.Center.Y, mod.NPCType("ForestDragon_Legs"), npc.whoAmI, 0, latestNPC);
Main.npc[(int)latestNPC].realLife = npc.whoAmI;
Main.npc[(int)latestNPC].ai[3] = npc.whoAmI;
}
for (int i = 0; i < WormLength; ++i)
{
latestNPC = NPC.NewNPC((int)npc.Center.X, (int)npc.Center.Y, mod.NPCType("ForestDragon_Body2"), npc.whoAmI, 0, latestNPC);
Main.npc[(int)latestNPC].realLife = npc.whoAmI;
Main.npc[(int)latestNPC].ai[3] = npc.whoAmI;
}
for (int i = 0; i < WormLength; ++i)
{
latestNPC = NPC.NewNPC((int)npc.Center.X, (int)npc.Center.Y, mod.NPCType("ForestDragon_Body3"), npc.whoAmI, 0, latestNPC);
Main.npc[(int)latestNPC].realLife = npc.whoAmI;
Main.npc[(int)latestNPC].ai[3] = npc.whoAmI;
}
latestNPC = NPC.NewNPC((int)npc.Center.X, (int)npc.Center.Y, mod.NPCType("ForestDragon_Tail"), npc.whoAmI, 0, latestNPC);
Main.npc[(int)latestNPC].realLife = npc.whoAmI;
Main.npc[(int)latestNPC].ai[3] = npc.whoAmI;
npc.ai[0] = 1;
npc.netUpdate = true;
}
int minTilePosX = (int)(npc.position.X / 16.0) - 1;
int maxTilePosX = (int)((npc.position.X + npc.width) / 16.0) + 2;
int minTilePosY = (int)(npc.position.Y / 16.0) - 1;
int maxTilePosY = (int)((npc.position.Y + npc.height) / 16.0) + 2;
if (minTilePosX < 0)
minTilePosX = 0;
if (maxTilePosX > Main.maxTilesX)
maxTilePosX = Main.maxTilesX;
if (minTilePosY < 0)
minTilePosY = 0;
if (maxTilePosY > Main.maxTilesY)
maxTilePosY = Main.maxTilesY;
bool collision = false;
float speed = 8f;
float acceleration = 0.07f;
Vector2 npcCenter = new Vector2(npc.position.X + npc.width * 0.5f, npc.position.Y + npc.height * 0.5f);
float targetXPos = Main.player[npc.target].position.X + (Main.player[npc.target].width / 2);
float targetYPos = Main.player[npc.target].position.Y + (Main.player[npc.target].height / 2);
float targetRoundedPosX = (float)((int)(targetXPos / 16.0) * 16);
float targetRoundedPosY = (float)((int)(targetYPos / 16.0) * 16);
npcCenter.X = (float)((int)(npcCenter.X / 16.0) * 16);
npcCenter.Y = (float)((int)(npcCenter.Y / 16.0) * 16);
float dirX = targetRoundedPosX - npcCenter.X;
float dirY = targetRoundedPosY - npcCenter.Y;
float length = (float)Math.Sqrt(dirX * dirX + dirY * dirY);
if (!collision)
{
Rectangle rectangle1 = new Rectangle((int)npc.position.X, (int)npc.position.Y, npc.width, npc.height);
int maxDistance = 1000;
bool playerCollision = true;
for (int index = 0; index < 255; ++index)
{
if (Main.player[index].active)
{
Rectangle rectangle2 = new Rectangle((int)Main.player[index].position.X - maxDistance, (int)Main.player[index].position.Y - maxDistance, maxDistance * 2, maxDistance * 2);
if (rectangle1.Intersects(rectangle2))
{
playerCollision = false;
break;
}
}
}
if (!collision)
{
npc.TargetClosest(true);
float absDirX = Math.Abs(dirX);
float absDirY = Math.Abs(dirY);
float newSpeed = speed / length;
dirX = dirX * newSpeed;
dirY = dirY * newSpeed;
if (npc.velocity.X > 0.0 && dirX > 0.0 || npc.velocity.X < 0.0 && dirX < 0.0 || (npc.velocity.Y > 0.0 && dirY > 0.0 || npc.velocity.Y < 0.0 && dirY < 0.0))
{
if (npc.velocity.X < dirX)
npc.velocity.X = npc.velocity.X + acceleration;
else if (npc.velocity.X > dirX)
npc.velocity.X = npc.velocity.X - acceleration;
if (npc.velocity.Y < dirY)
npc.velocity.Y = npc.velocity.Y + acceleration;
else if (npc.velocity.Y > dirY)
npc.velocity.Y = npc.velocity.Y - acceleration;
if (Math.Abs(dirY) < speed * 0.2 && (npc.velocity.X > 0.0 && dirX < 0.0 || npc.velocity.X < 0.0 && dirX > 0.0))
{
if (npc.velocity.Y > 0.0)
npc.velocity.Y = npc.velocity.Y + acceleration * 2f;
else
npc.velocity.Y = npc.velocity.Y - acceleration * 2f;
}
if (Math.Abs(dirX) < speed * 0.2 && (npc.velocity.Y > 0.0 && dirY < 0.0 || npc.velocity.Y < 0.0 && dirY > 0.0))
{
if (npc.velocity.X > 0.0)
npc.velocity.X = npc.velocity.X + acceleration * 2f;
else
npc.velocity.X = npc.velocity.X - acceleration * 2f;
}
}
else if (absDirX > absDirY)
{
if (npc.velocity.X < dirX)
npc.velocity.X = npc.velocity.X + acceleration * 1.1f;
else if (npc.velocity.X > dirX)
npc.velocity.X = npc.velocity.X - acceleration * 1.1f;
if (Math.Abs(npc.velocity.X) + Math.Abs(npc.velocity.Y) < speed * 0.5)
{
if (npc.velocity.Y > 0.0)
npc.velocity.Y = npc.velocity.Y + acceleration;
else
npc.velocity.Y = npc.velocity.Y - acceleration;
}
}
else
{
if (npc.velocity.Y < dirY)
npc.velocity.Y = npc.velocity.Y + acceleration * 1.1f;
else if (npc.velocity.Y > dirY)
npc.velocity.Y = npc.velocity.Y - acceleration * 1.1f;
if (Math.Abs(npc.velocity.X) + Math.Abs(npc.velocity.Y) < speed * 0.5)
{
if (npc.velocity.X > 0.0)
npc.velocity.X = npc.velocity.X + acceleration;
else
npc.velocity.X = npc.velocity.X - acceleration;
}
}
}
npc.rotation = (float)Math.Atan2(npc.velocity.Y, npc.velocity.X) + 1.57f;
}
}
return false;
}
}
}