Facebook
From Colorant Plover, 1 Year ago, written in Java.
Embed
Download Paste or View Raw
Hits: 86
  1. import java.awt.*;
  2. import java.awt.image.BufferedImage;
  3. import java.io.*;
  4. import java.util.*;
  5.  
  6. import javax.imageio.ImageIO;
  7. public class Boss extends Enemy
  8. {
  9.         private int attackNum;
  10.         private int delay;
  11.         private int ticks;
  12.         private int moveTick1; //timer for atk 1
  13.         private int moveTick2; //timer for atk 2
  14.         private int moveTick3; //timer for atk 3
  15.         private int moveTick4;
  16.         private int frameTick; //tick for animation
  17.         private Image bossImg;
  18.         private Image laserCharge1;
  19.         private Image laserCharge2;
  20.         private Image laserCharge3;
  21.         private Image laserCharge4;
  22.         private BufferedImage laser1;
  23.         private BufferedImage laser2;
  24.         private BufferedImage projectile1;
  25.         private BufferedImage projectile2_L;
  26.         private BufferedImage projectile2_R;
  27.         private BufferedImage projectile3;
  28.         private boolean chain; //attack combo counter
  29.         public Boss(int x, int y, int hp, int dx, int dy, int w, int h, Color colour)
  30.         {
  31.                 super(x, y, hp, dx, dy, w, h, colour);
  32.                 attackNum=0;
  33.                 delay=3000;
  34.         }
  35.         public Boss()
  36.         {
  37.                 super(200, -100, 6000, 0, 2, 300, 200, Color.red);
  38.                 attackNum=0;
  39.                 delay=1000;
  40.                 ticks=0;
  41.                 moveTick1=0;
  42.                 moveTick2=0;
  43.                 moveTick3=0;
  44.                 moveTick4=0;
  45.                 frameTick=0;
  46.                 try
  47.                 {
  48.                         bossImg=ImageIO.read(new File("Sprite_Alien_Aircraft_Carrier.png"));
  49.                         laserCharge1=ImageIO.read(new File("laserCharge_frame1.png"));
  50.                         laserCharge2=ImageIO.read(new File("laserCharge_frame2.png"));
  51.                         laserCharge3=ImageIO.read(new File("laserCharge_frame3.png"));
  52.                         laserCharge4=ImageIO.read(new File("laserCharge_frame4.png"));
  53.                         laser1=ImageIO.read(new File("laser_frame1.png"));
  54.                         laser2=ImageIO.read(new File("laser_frame2.png"));
  55.                         projectile1=ImageIO.read(new File("bossProjectile1.png"));
  56.                         projectile2_L=ImageIO.read(new File("bossProjectile2_L.png"));
  57.                         projectile2_R=ImageIO.read(new File("bossProjectile2_R.png"));
  58.                         projectile3=ImageIO.read(new File("bossProjectile3.png"));
  59.                         chain=false;
  60.                 }
  61.                 catch (IOException e) {}
  62.         }
  63.         //mutator and accessors
  64.         public int getAttackNum() {return attackNum;}
  65.         public int getDelay() {return delay;}
  66.         public void setAttackNum(int a) {attackNum=a;}
  67.         public void setDelay(int d) {delay=d;}
  68.        
  69.         public void move(ArrayList<Projectile> pArr, Ship s, Graphics g)
  70.         {
  71.                 if (visible)
  72.                 {      
  73.                         g.drawImage(bossImg, x, y, width, height, null, null);
  74.                         //g.setColor(Color.red);
  75.                         //g.drawRect(x+20, y+5, width-40, height-10); hitbox testing
  76.                         if (attackNum==0)
  77.                         {
  78.                                 x+=dx;
  79.                                 y+=dy;
  80.                                 if (y>=150)
  81.                                 {
  82.                                         dy=0;
  83.                                         ticks=delay;
  84.                                 }
  85.                         }
  86.                         else if (ticks>=delay || moveTick1>0 || moveTick2>0 || moveTick3>0 || moveTick4>0)
  87.                         {
  88.                                 ticks=0;
  89.                                 delay=(int)(1000*Math.random()+200);
  90.                                 attackNum=(int)(3*Math.random()+1);
  91.                                 //attackNum=4;
  92.                                 if ((attackNum==1 || moveTick1>0) && moveTick2==0 && moveTick3==0 && moveTick4==0) //first attack pattern (fires 2 columns of 6 projectiles)
  93.                                 {
  94.                                         moveTick1+=10;
  95.                                         if (moveTick1%50==0)
  96.                                                 attack1(pArr);
  97.                                         if (moveTick1>=300 || moveTick2>0)
  98.                                                 moveTick1=0;
  99.                                 }
  100.                                 else if ((attackNum==2 || moveTick2>0) && moveTick1==0 && moveTick3==0 && moveTick4==0) //second attack pattern (fires 2 laser)
  101.                                 {
  102.                                         if (moveTick2==0)
  103.                                                 dx=-2;
  104.                                         moveTick2+=10;
  105.                                         frameTick = (frameTick + 10) % 200;
  106.                                         x+=dx;
  107.                                         if (x<50 || x>350)
  108.                                                 dx=-dx;
  109.                                         if (moveTick2<=1000)
  110.                                         {
  111.                                                 g.drawImage(laserCharge4, x-8, y+95, null);
  112.                                                 g.drawImage(laserCharge4, x+270, y+95, null);
  113.                                                 if (150<=frameTick && frameTick<200)
  114.                                                 {
  115.                                                         g.drawImage(laserCharge4, x-8, y+95, null);
  116.                                                         g.drawImage(laserCharge4, x+270, y+95, null);
  117.                                                 }
  118.                                                 else if (100<=frameTick)
  119.                                                 {
  120.                                                         g.drawImage(laserCharge3, x-8, y+95, null);
  121.                                                         g.drawImage(laserCharge3, x+270, y+95, null);
  122.                                                 }
  123.                                                 else if (50<=frameTick)
  124.                                                 {
  125.                                                         g.drawImage(laserCharge2, x-8, y+95, null);
  126.                                                         g.drawImage(laserCharge2, x+270, y+95, null);
  127.                                                 }
  128.                                                 else if (0<=frameTick)
  129.                                                 {
  130.                                                         g.drawImage(laserCharge1, x-8, y+95, null);
  131.                                                         g.drawImage(laserCharge1, x+270, y+95, null);
  132.                                                 }
  133.  
  134.                                         }
  135.                                         else if (moveTick2<=3000)
  136.                                         {
  137.                                                 attack2(s);
  138.                                                 if (frameTick<50 && frameTick>=0)
  139.                                                 {
  140.                                                         g.drawImage(laser1, x-12, y+55, 50, 1000, null, null);
  141.                                                         g.drawImage(laser1, x+260, y+55, 50, 1000, null, null);
  142.                                                 }
  143.                                                 else
  144.                                                 {
  145.                                                         g.drawImage(laser2, x-12, y+55, 50, 1000, null, null);
  146.                                                         g.drawImage(laser2, x+260, y+55, 50, 1000, null, null);
  147.                                                 }
  148.                                                 if (frameTick>=100)
  149.                                                         frameTick=0;
  150.                                         }
  151.                                         else
  152.                                         {
  153.                                                 moveTick2=0;
  154.                                                 frameTick=0;
  155.                                         }
  156.                                 }
  157.                                 else if ((attackNum==3 || moveTick3>0) && moveTick1==0 && moveTick2==0 && moveTick4==0) //third attack pattern (tries to ram the player)
  158.                                 {
  159.                                         moveTick3+=10;
  160.                                         x+=dx;
  161.                                         y+=dy;
  162.                                         if (moveTick3<2500)
  163.                                         {
  164.                                                 dy=0;
  165.                                                 if (s.getX()+s.getWidth()/2 < x+width/2)
  166.                                                         dx=-2;
  167.                                                 else if (s.getX()+s.getWidth()/2 > x+width/2)
  168.                                                         dx=2;
  169.                                                 else
  170.                                                         dx=0;
  171.                                         }
  172.                                         else if (moveTick3<3500)
  173.                                         {
  174.                                                 dx=0;
  175.                                                 dy=0;
  176.                                                 frameTick=(frameTick+10)%200;
  177.                                                 if (frameTick<=100)
  178.                                                         dx=-1;
  179.                                                 if (frameTick>100)
  180.                                                         dx=1;
  181.                                                 if (moveTick3>5800)
  182.                                                         frameTick=0;
  183.                                         }
  184.                                         else if (moveTick3<6000 && y<1000)
  185.                                         {
  186.                                                 dx=0;
  187.                                                 dy=8;
  188.                                                 if (frameTick==0)
  189.                                                         attack3(pArr);
  190.                                                 frameTick=(frameTick+10)%100;  
  191.                                         }
  192.                                         else
  193.                                         {
  194.                                                 x=200;
  195.                                                 y=-100;
  196.                                                 dx=0;
  197.                                                 dy=2;
  198.                                                 attackNum=0;
  199.                                                 moveTick3=0;
  200.                                                 frameTick=0;
  201.                                         }
  202.                                 }
  203.                                 /*else if ((attackNum==4 || moveTick4>0) && moveTick1==0 && moveTick2==0 && moveTick3==0) //fourth attack pattern (fires random projectiles and chain into attack 3)
  204.                                 {
  205.                                         moveTick4+=10;
  206.                                         x+=dx;
  207.                                         y+=dy;
  208.                                         if (y<300)
  209.                                                 dy=2;
  210.                                         else if (moveTick4<(int)(1500*Math.random()+2000)) //random duration of attack
  211.                                         {
  212.                                                 dy=0;
  213.                                                 attack4(pArr);
  214.                                         }
  215.                                         else
  216.                                         {
  217.                                                 attackNum=3;
  218.                                                 dx=0;
  219.                                                 dy=0;
  220.                                                 moveTick4=0;
  221.                                         }
  222.                                 }
  223.                                 */
  224.                         }
  225.                         else if (attackNum!=0)
  226.                         {
  227.                                 idle();
  228.                         }
  229.                 }
  230.                 ticks+=10;
  231.         }
  232.        
  233.         public void idle() //movement while not attacking
  234.         {
  235.                 x+=dx;
  236.                 y+=dy;
  237.                 if (dx!=1)
  238.                 {
  239.                         dx=-1;
  240.                         dy=0;
  241.                 }
  242.                 if (x<=150)
  243.                         dx=-dx;
  244.                 else if (x>250)
  245.                         dx=-dx;
  246.         }
  247.         public void attack1(ArrayList<Projectile> pArr)
  248.         {
  249.                
  250.                 pArr.add(new Projectile(x+100, y+105, 0, 12, 20, 20, Color.magenta, 20, false, projectile1));
  251.                 pArr.add(new Projectile(x+200, y+105, 0, 12, 20, 20, Color.magenta, 20, false, projectile1));
  252.         }
  253.         public void attack2(Ship s)
  254.         {
  255.                 Rectangle ship=new Rectangle(s.getX(), s.getY(), s.getWidth(), s.getHeight());
  256.                 Rectangle l1=new Rectangle(x-12, y, 50, 1000);
  257.                 Rectangle l2=new Rectangle(x+260, y, 50, 1000);
  258.                 if (ship.intersects(l1) || ship.intersects(l2))
  259.                         s.damage(100);
  260.         }
  261.         public void attack3(ArrayList<Projectile> pArr)
  262.         {
  263.                 pArr.add(new Projectile(x-20, y, -6, 0, 15, 15, Color.red, 30, false, projectile2_L));
  264.                 pArr.add(new Projectile(x+width+20, y, 6, 0, 15, 15, Color.red, 30, false, projectile2_R));
  265.         }
  266.         public void attack4(ArrayList<Projectile> pArr)
  267.         {
  268.                 int projectileCount=(int)(6*Math.random()+1);
  269.                 for (int i=1;i<=projectileCount;i++)
  270.                 {
  271.                         int directionX=(int)(12*Math.random()-6);
  272.                         int directionY=(int)(12*Math.random()-6);
  273.                         pArr.add(new Projectile(x+width/2, y+height/2, directionX, directionY, 8, 8, Color.red, 20, false, projectile3));
  274.                 }
  275.                
  276.         }
  277.         public void collide(Ship s)
  278.         {
  279.                 Rectangle e=new Rectangle(x+20, y+5, width-40, height-10);
  280.                 Rectangle ship=new Rectangle(s.getX(), s.getY(), s.getWidth(), s.getHeight());
  281.                 if (e.intersects(ship))
  282.                 {
  283.                         s.damage(50);
  284.                         if (s.getITick()==0)
  285.                                 s.setITick(500);
  286.                 }
  287.         }
  288. }