import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

public class GamePanel extends JPanel implements ActionListener {
    private Timer timer;
    private Player player;
    private ArrayList<Target> targets;

    public GamePanel() {
        timer = new Timer(30, this); // 每30ms更新一次
        player = new Player(100, 500); // 玩家初始位置
        targets = new ArrayList<>();
        targets.add(new Target(300, 300)); // 添加一个目标
        timer.start();
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        // 自动瞄准逻辑
        for (Target target : targets) {
            player.updateAim(target.getX(), target.getY());
        }
        repaint();
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.setColor(Color.BLUE);
        g.fillRect(player.getX(), player.getY(), 20, 20); // 绘制玩家
        g.setColor(Color.RED);
        g.fillRect(player.getAimX(), player.getAimY(), 5, 5); // 绘制瞄准位置
        g.setColor(Color.GREEN);
        for (Target target : targets) {
            g.fillRect(target.getX(), target.getY(), 20, 20); // 绘制目标
        }
    }
}