Advertisement
Guest User

MalaiseWorm.cs

a guest
Apr 6th, 2020
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.88 KB | None | 0 0
  1. using Terraria;
  2. using Terraria.ModLoader;
  3. using Terraria.ID;
  4. using static Terraria.ModLoader.ModContent;
  5. using Microsoft.Xna.Framework;
  6. using System;
  7. using System.Diagnostics;
  8. using Microsoft.Xna.Framework.Graphics;
  9. using System.IO;
  10.  
  11. namespace Malaise.NPCs.Bosses
  12. {
  13.  
  14.     [AutoloadBossHead]
  15.     public class MalaiseSoul : ModNPC
  16.     {
  17.         private int ai;
  18.         private int attackTimer = 0;
  19.         private bool fastSpeed = false;
  20.    
  21.         private bool stunned;
  22.         private int stunnedTimer;
  23.  
  24.         private int frame = 1;
  25.         private double counting;
  26.  
  27.         private const int Sphere = 50;
  28.         private bool Phase2;
  29.  
  30.         public override void SetStaticDefaults()
  31.         {
  32.             DisplayName.SetDefault("Malaise Soul");
  33.             Main.npcFrameCount[npc.type] = 4;
  34.         }
  35.  
  36.         public override void SetDefaults()
  37.         {
  38.             npc.width = 64;
  39.             npc.height = 64;
  40.  
  41.             npc.boss = true;
  42.             npc.aiStyle = -1;
  43.             npc.npcSlots = 5f;
  44.  
  45.             npc.lifeMax =  8500;
  46.             npc.damage = 15;
  47.             npc.defense = 4;
  48.             npc.knockBackResist = 0f;
  49.  
  50.             npc.value = Item.buyPrice(gold: 10);
  51.  
  52.             npc.lavaImmune = true;
  53.             npc.noTileCollide = true;
  54.             npc.noGravity = true;
  55.  
  56.             npc.HitSound = SoundID.NPCHit1;
  57.             npc.DeathSound = SoundID.NPCDeath1;
  58.             music = MusicID.Boss2;
  59.  
  60.             bossBag = ItemType<Items.BossBags.Unconfigured.MalaiseSoulBag>();
  61.         }
  62.  
  63.         public override void ScaleExpertStats(int numPlayers, float bossLifeScale)
  64.         {
  65.             npc.lifeMax = (int)(npc.lifeMax * bossLifeScale);
  66.             npc.damage = (int)(npc.damage * 1.3f);
  67.         }
  68.  
  69.         public override void AI()
  70.         {
  71.             npc.TargetClosest(true);
  72.             Player player = Main.player[npc.target];
  73.             Vector2 target = npc.HasPlayerTarget ? player.Center : Main.npc[npc.target].Center;
  74.             npc.rotation = 0.0f;
  75.             npc.netAlways = true;
  76.             npc.TargetClosest(true);
  77.             if(npc.life <= 4000)
  78.             {
  79.                 npc.defense = 100;
  80.                 npc.damage = 100000;
  81.             }
  82.             if (npc.target < 0 || npc.target == 255 || player.dead || !player.active)
  83.             {
  84.                 npc.TargetClosest(false);
  85.                 npc.direction = 1;
  86.                 npc.velocity.Y = npc.velocity.Y - 0.1f;
  87.                 if(npc.timeLeft > 20)
  88.                 {
  89.                     npc.timeLeft = 20;
  90.                     return;
  91.                 }
  92.             }
  93.             if(stunned)
  94.             {
  95.                 npc.velocity.X = 0.0f;
  96.                 npc.velocity.Y = 0.0f;
  97.                 stunnedTimer++;
  98.                 if(stunnedTimer >= 100)
  99.                 {
  100.                     stunned = false;
  101.                     stunnedTimer = 0;
  102.                 }
  103.             }
  104.  
  105.             ai++;
  106.             npc.ai[0] = (float)ai * 1f;
  107.             int distance = (int)Vector2.Distance(target, npc.Center);
  108.             if((double)npc.ai[0] < 300)
  109.             {
  110.                 frame = 0;
  111.                 MoveTowards(npc, target, (float)(distance > 300 ? 13f : 7f), 30f);
  112.                 npc.netUpdate = true;
  113.             }
  114.             else if ((double)npc.ai[0] >= 300 && (double)npc.ai[0] < 450.0)
  115.             {
  116.                 stunned = true;
  117.                 frame = 0;
  118.                 npc.defense = 15;
  119.                 npc.damage = 15;
  120.                 MoveTowards(npc, target, (float)(distance > 300 ? 13f : 7f), 30f);
  121.                 if(npc.life <= 4000)
  122.                 {
  123.                     npc.defense = 100;        // to be configured
  124.                     npc.damage = 100000;      // to be configured
  125.                 }
  126.                 npc.netUpdate = true;
  127.             }
  128.             else if((double)npc.ai[0] >= 450.0)
  129.             {
  130.                 frame = 0;
  131.                 stunned = false;
  132.                 npc.damage = (int)(15 * 1.3f);
  133.                 npc.defense = 5;
  134.                 if(npc.life <= 4000)
  135.                 {
  136.                     npc.defense = 1000;    // to be configured
  137.                     npc.damage = 1000000;  // to be configured
  138.                 }
  139.                 if(!fastSpeed)
  140.                 {
  141.                     fastSpeed = true;
  142.                 }
  143.                 else
  144.                 {
  145.                     if((double)npc.ai[0] % 50 == 0)
  146.                     {
  147.                         float speed = 12f;
  148.                         Vector2 vector = new Vector2(npc.position.X + (float)npc.width * 0.5f, npc.position.Y + (float)npc.height * 0.5f);
  149.                         float x = player.position.X + (float)(player.width / 2) - vector.X;
  150.                         float y = player.position.Y + (float)(player.height / 2) - vector.Y;
  151.                         float distance2 = (float)Math.Sqrt(x * x + y + y);
  152.                         float factor = speed / distance2;
  153.                         npc.velocity.X = x * factor;
  154.                         npc.velocity.Y = y * factor;
  155.                     }
  156.                 }
  157.                 npc.netUpdate = true;
  158.             }
  159.  
  160.             if((double)npc.ai[0] % (Main.expertMode ? 100 : 150) == 0 && !stunned && !fastSpeed)
  161.             {
  162.                 attackTimer++;
  163.                 if(attackTimer <= 2)
  164.                 {
  165.                     frame = 0;
  166.                     npc.velocity.X = 0f;
  167.                     npc.velocity.Y = 0f;
  168.                     Vector2 shootPos = npc.Center;
  169.                     float accuracy = 5f * (npc.life / npc.lifeMax);
  170.                     Vector2 shootVel = target - shootPos + new Vector2(Main.rand.NextFloat(-accuracy, accuracy), Main.rand.NextFloat(-accuracy, accuracy));
  171.                     shootVel.Normalize();
  172.                     shootVel *= 7.5f;
  173.                     for(int i = 0; i < (Main.expertMode ? 5 : 3); i++)
  174.                     {
  175.                         Projectile.NewProjectile(shootPos.X + (float)(-100 * npc.direction) + (float)Main.rand.Next(-40, 41), shootPos.Y - (float)Main.rand.Next(-50, 40), shootVel.X, shootVel.Y, ProjectileType<Projectiles.BossProjectiles.MalaiseSoulProjectile>(), npc.damage / 3, 5f);
  176.                     }
  177.                 }
  178.                 else
  179.                 {
  180.                     attackTimer = 0;
  181.                 }
  182.             }
  183.  
  184.             if((double)npc.ai[0] >= 650.0)
  185.             {
  186.                 ai = 0;
  187.                 npc.alpha = 0;
  188.                 fastSpeed = false;
  189.             }
  190.         }
  191.  
  192.         public override void FindFrame(int frameHeight)
  193.         {
  194.             if(frame == 0)
  195.             {
  196.                 counting += 1.0;
  197.                 if(counting < 8.0)
  198.                 {
  199.                     npc.frame.Y = 0;
  200.                 }
  201.                 else if(counting < 24.0)
  202.                 {
  203.                     npc.frame.Y = frameHeight;
  204.                 }
  205.                 else if(counting < 24.0)
  206.                 {
  207.                     npc.frame.Y = frameHeight * 2;
  208.                 }
  209.                 else if(counting <32.0)
  210.                 {
  211.                     npc.frame.Y = frameHeight * 3;
  212.                 }
  213.                 else
  214.                 {
  215.                     counting = 0.0;
  216.                 }
  217.             }
  218.             else if(frame == 1)
  219.             {
  220.                 npc.frame.Y = frameHeight * 4;
  221.             }
  222.             else
  223.             {
  224.                 npc.frame.Y = frameHeight * 5;
  225.             }
  226.         }
  227.  
  228.         private void MoveTowards(NPC npc, Vector2 playerTarget, float speed, float turnResistance)
  229.         {
  230.             var move = playerTarget - npc.Center;
  231.             float length = move.Length();
  232.             if(length > speed)
  233.             {
  234.                 move *= speed / length;
  235.             }
  236.             move = (npc.velocity * turnResistance + move) / (turnResistance + 1f);
  237.             length = move.Length();
  238.             if(length > speed)
  239.             {
  240.                 move *= speed / length;
  241.             }
  242.             npc.velocity = move;
  243.         }
  244.  
  245.         public override void NPCLoot()
  246.         {
  247.             MalaiseWorld.downedMalaiseSoul = true;
  248.             if(Main.expertMode)
  249.             {
  250.                 npc.DropBossBags();
  251.             }
  252.             else
  253.             {
  254.                 Item.NewItem((int)npc.position.X, (int)npc.position.Y, npc.width, npc.height, ItemID.LifeCrystal, Main.rand.Next(1, 3));
  255.                 Item.NewItem((int)npc.position.X, (int)npc.position.Y, npc.width, npc.height, ItemType<Items.Drops.Malaiseite>(), Main.rand.Next(1, 3));
  256.                 if(Main.rand.Next(7) == 0)
  257.                 {
  258.                     Item.NewItem((int)npc.position.X, (int)npc.position.Y, npc.width, npc.height, ItemType<Items.BossSummons.Unconfigured.MalaiseSoulSummon>());
  259.                 }
  260.             }
  261.         }
  262.  
  263.         public override void BossLoot(ref string name, ref int potionType)
  264.         {
  265.             potionType = ItemID.HealingPotion;
  266.         }
  267.  
  268.         public override bool PreDraw(SpriteBatch spriteBatch, Color drawColor)
  269.         {
  270.             if (npc.life <= 4000)
  271.             {
  272.                 spriteBatch.Draw(mod.GetTexture("NPCs/Bosses/Malaise1"), npc.Center - Main.screenPosition, null, Color.White * (70f / 255f), 0f, new Vector2(Sphere, Sphere), 3f, SpriteEffects.None, 0f);
  273.             }
  274.             return true;
  275.         }
  276.     }
  277. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement