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

import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.Random;
import java.util.Vector;

/**
 * ClassName GameFrame
 * Description
 * Create by HW
 * Date 2022/4/5 13:07
 */
public class GameFrame  extends JFrame {

    Plane plane;

    GameFrame gameFrame;
    //定义子弹的集合,线程安全的是Vector，而非list
    Vector<Bullets> bullets=new Vector<>();
    //定义敌机 的集合
    Vector<EnemyPlane> enemyPlanes=new Vector<>();
    
    
    public GameFrame(){
        gameFrame=this;
        plane=new Plane();
        plane.start();
        this.setSize(480,1200);
        this.setTitle("雷霆战机");
        this.isResizable(false);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.setLocationRelativeTo(null);
        this.setVisible(true);
        new Thread(new Runnable() {
            @Override
            public void run() {
                while(true){
                    repaint();
                    try {
                        Thread.sleep(10);
                    }catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                }

        }).start();

        //不断产生敌机
        new Thread(new Runnable() {
            @Override
            public void run() {
                Random random=new Random();
                while(true){
                    EnemyPlane enemyPlane = new EnemyPlane(gameFrame, random.nextInt(450), 0);
                    enemyPlane.start();
                    enemyPlanes.add(enemyPlane);
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
    }

    @Override
    public void paint(Graphics g){
        System.out.println("绘制画板！");
        BufferedImage image =(BufferedImage) this.createImage(this.getSize().width, this.getSize().height);
        Graphics pen = image.getGraphics();

        //绘制背景
        pen.drawImage(new ImageIcon("E:\\研究生\\研究生\\论文\\cloudsim学习文档\\cloudsim-5 (2).0\\modules\\cloudsim\\src\\main\\java\\org\\cloudbus\\cloudsim\\codes\\mygame\\leiting\\image/background.png").getImage(),0,0,null);

        //绘制飞机
        pen.drawImage(plane.img,plane.x,plane.y,null);
        //pen.drawImage(plane.img,plane.x,plane.y,null);

        for (int i=0; i<bullets.size();i++){
            Bullets bullet = bullets.get(i);
            if (bullet.y>0){
                pen.drawImage(bullet.img, bullet.x, bullet.y-=bullet.speed, bullet.width, bullet.length, null);
            }
            else{
                bullets.remove(bullet);
            }
        }

        //绘制敌机
        for (int i=0; i<enemyPlanes.size();i++){
            EnemyPlane ep = enemyPlanes.get(i);
            if (ep.y<1200){
                pen.drawImage(ep.img, ep.x, ep.y+=ep.speed, null);
            }
            else{
                enemyPlanes.remove(ep);
            }
        }
        //生效
        g.drawImage(image,0,0,null);
    }

    private void isResizable(boolean b) {
    }

    public static void main(String[] args) {
        GameFrame gameFrame = new GameFrame();
        Player player = new Player(gameFrame);
        gameFrame.addKeyListener(player);
    }
}
