Magic movement in game tile by tile

2019-09-10 11:41发布

I have a 2D tile game and my hero can use magic(in this case fire), and the goal is to make the fireball move tile by tile until it finds either a wall or an enemy and to make the game stop while the fire is moving. I already have the fire moving and stopping if there is a wall or an enemy(and damaging the enemy). The problem is i can't seem to make the game show the fireball change from tile to tile, which means when i launch the fireball the game automatically shows me the fireball in its last position before collision, and then it disappears from the tiles. Anyone got any ideas as to what I am doing wrong or what i should do to make the game update the fire tile by tile? (Btw i thought it might have something to do with my observer but I've tried thread.sleep and wait() and it isn't quite working, maybe I am doing it the wrong way).Thank you for your help and if you guys need any code just ask.

public class ImageMatrixGUI extends Observable {

    private static final ImageMatrixGUI INSTANCE = new ImageMatrixGUI();

    private final String IMAGE_DIR = "images";
    private final int SQUARE_SIZE;
    private final int N_SQUARES_WIDTH;
    private final int N_SQUARES_HEIGHT;

    private JFrame frame;
    private JPanel panel;
    private JPanel info;

    private Map<String, ImageIcon> imageDB = new HashMap<String, ImageIcon>();

    private List<ImageTile> images = new ArrayList<ImageTile>();
    private List<ImageTile> statusImages = new ArrayList<ImageTile>();

    private int lastKeyPressed;
    private boolean keyPressed;

    private ImageMatrixGUI() {
        SQUARE_SIZE = 48;
        N_SQUARES_WIDTH = 10;
        N_SQUARES_HEIGHT = 10;
        init();
    }
    public static ImageMatrixGUI getInstance() {
        return INSTANCE;
    }
     public void setName(final String name) {
        frame.setTitle(name); 
    }

        private void init() {
        frame = new JFrame();
        panel = new RogueWindow();
        info = new InfoWindow();

        panel.setPreferredSize(new Dimension(N_SQUARES_WIDTH * SQUARE_SIZE, N_SQUARES_HEIGHT * SQUARE_SIZE));
        info.setPreferredSize(new Dimension(N_SQUARES_WIDTH * SQUARE_SIZE, SQUARE_SIZE));
        info.setBackground(Color.BLACK);
        frame.add(panel);
        frame.add(info, BorderLayout.NORTH);
        frame.pack();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        initImages();

        new KeyWatcher().start();

        frame.addKeyListener(new KeyListener() {

            @Override
            public void keyTyped(KeyEvent e) {

            }

            @Override
            public void keyReleased(KeyEvent e) {

            }

            @Override
            public void keyPressed(KeyEvent e) {
                lastKeyPressed = e.getKeyCode();
                keyPressed = true;
                releaseObserver();
            }
        });
    }

    synchronized void releaseObserver() {
        notify();
    }

    synchronized void waitForKey() throws InterruptedException {
        while (!keyPressed) {
            wait();
        }
        setChanged();
        notifyObservers(lastKeyPressed);
        keyPressed = false;
    }

    private void initImages() {
        File dir = new File(IMAGE_DIR);
        for (File f : dir.listFiles()) {
            assert (f.getName().lastIndexOf('.') != -1);
            imageDB.put(f.getName().substring(0, f.getName().lastIndexOf('.')),
                    new ImageIcon(IMAGE_DIR + "/" + f.getName()));
        }
    }
    public void go() {
        frame.setVisible(true);
    }
    public void newImages(final List<ImageTile> newImages) {
        synchronized (images) { // Added 16-Mar-2016
            if (newImages == null)
                return;
            if (newImages.size() == 0)
                return;
            for (ImageTile i : newImages) {
                if (!imageDB.containsKey(i.getName())) {
                    throw new IllegalArgumentException("No such image in DB " + i.getName());
                }
            }
            images.addAll(newImages);
        }
    }
     public void removeImage(final ImageTile image) {
        synchronized (images) { 
            images.remove(image);
        }
    }
        public void addImage(final ImageTile image) {
        synchronized (images) { 
            images.add(image);
        }
    }
        public void clearImages() {
        synchronized (images) {
            images.clear();
        }

        public void newStatusImages(final List<ImageTile> newImages) {
        synchronized (statusImages) { 
            if (newImages == null)
                return;
            if (newImages.size() == 0)
                return;
            for (ImageTile i : newImages) {
                if (!imageDB.containsKey(i.getName())) {
                    throw new IllegalArgumentException("No such image in DB " + i.getName());
                }
            }
            statusImages.addAll(newImages);
        }
    }

      public void removeStatusImage(final ImageTile image) {
        synchronized (statusImages) {
            statusImages.remove(image);
        }

     public void addStatusImage(final ImageTile image) {
        synchronized (statusImages) { 
            statusImages.add(image);
        }
    }
        public void clearStatus() {
        synchronized (statusImages) { 
            statusImages.clear();
        }
    }

    @SuppressWarnings("serial") 
    private class RogueWindow extends JPanel {
        @Override
        public void paintComponent(Graphics g) {
            // System.out.println("Thread " + Thread.currentThread() + "
            // repainting");
            synchronized (images) { 
                for (ImageTile i : images) {

                    g.drawImage(imageDB.get(i.getName()).getImage(), i.getPosition().getX() * SQUARE_SIZE,
                            i.getPosition().getY() * SQUARE_SIZE, SQUARE_SIZE, SQUARE_SIZE, frame);
                }
            }
        }
    }

     @SuppressWarnings("serial") 
      private class InfoWindow extends JPanel {
        @Override
        public void paintComponent(Graphics g) {
            synchronized (statusImages) { 
                for (ImageTile i : statusImages)
                    g.drawImage(imageDB.get(i.getName()).getImage(), i.getPosition().getX() * SQUARE_SIZE, 0,
                            SQUARE_SIZE, SQUARE_SIZE, frame);

            }
        }
    }

       private class KeyWatcher extends Thread {
        public void run() {
            try {
                while (true)
                    waitForKey();
            } catch (InterruptedException e) {
            }
        }
    }
        public void update() {
        frame.repaint();
    }

        public void dispose() {
        images.clear();
        statusImages.clear();
        imageDB.clear();
        frame.dispose();
    }
        public Dimension getGridDimension() {
        return new Dimension(N_SQUARES_WIDTH, N_SQUARES_HEIGHT);
    }

}

标签: java swing 2d tile
0条回答
登录 后发表回答