package org.cloudbus.cloudsim.codes.mygame.leiting.src;

import javax.swing.*;
import java.awt.*;

/**
 * ClassName EnemyPlane
 * Description
 * Create by HW
 * Date 2022/4/5 22:58
 */
public class EnemyPlane  extends Thread{
    GameFrame gameFrame;

    int x,y;
    int width=30,length=40;
    int speed=4;
    Image img=new ImageIcon("E:\\研究生\\研究生\\论文\\cloudsim学习文档\\cloudsim-5 (2).0\\modules\\cloudsim\\src\\main\\java\\org\\cloudbus\\cloudsim\\codes\\mygame\\leiting\\image/10024.png").getImage();

    public EnemyPlane(GameFrame gameFrame, int x, int y) {
        this.gameFrame = gameFrame;
        this.x = x;
        this.y = y;
    }

    public EnemyPlane(GameFrame gameFrame, int x, int y, int width, int length) {
        this.gameFrame = gameFrame;
        this.x = x;
        this.y = y;
        this.width = width;
        this.length = length;
        //this.img = img;
    }
    //当飞机被击中，产生爆炸的效果
    @Override
    public void run() {
        while(true){
            if (hit()){
                this.speed=0;
                this.img=new ImageIcon("E:\\研究生\\研究生\\论文\\cloudsim学习文档\\cloudsim-5 (2).0\\modules\\cloudsim\\src\\main\\java\\org\\cloudbus\\cloudsim\\codes\\mygame\\leiting\\image/die.png").getImage();
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                //被子弹击中
                gameFrame.enemyPlanes.remove(this);
            }
            if (this.y >=1200){
                break;
            }
                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
        }
    }

    //检测碰撞
    public boolean hit(){
        //Swing技术中，人家已经提供了
        Rectangle myrectangle = new Rectangle(this.x, this.y, this.width, this.length);
        for (int i = 0; i <gameFrame.bullets.size(); i++) {
            Bullets bullet = gameFrame.bullets.get(i);
            Rectangle rectangle1 = new Rectangle(bullet.x, bullet.y, bullet.width, bullet.length);
            if (myrectangle.intersects(rectangle1)){
                return true;
            }
        }
        return false;
    }

}
