RPG - going from map screen to battle screen

OMG ANOTHER EDIT: Ok, ive nearly completed the “map screen” portion of my RPG. The next thing im going to try to do is make the battle system. The challenge im having now is figuring out how to go from the map screen to the battle screen. The layout of the battle screen is drastically different than that of the map screen, and the controls do different things on that screen to. Can i somehow fit this into my current Canvas class, or do i need to make a seperate class for it and somehow have the Main class switch between them?

current version of Main:

package projectrpg;

import java.awt.Color;
import java.io.IOException;
import javax.swing.JFrame;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPathExpressionException;
import org.xml.sax.SAXException;

/**
 *
 * @author Dennis Jr
 */
public class Main extends JFrame {

    public Main(String map, GameData data) {
        add(new Canvas("map1", data));
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //xAxis should be 10 pixles shorter than yAxis. For some reason the
        //xAxis seems to be naturally 10 pixels more than specified.
        setSize(490, 500);
        setLocationRelativeTo(null);
        setTitle("2D Adventure");
        setResizable(false);
        setVisible(true);
    }

    public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException, XPathExpressionException {
        GameData data = new GameData();
        String map = "map1";
        new Main(map, data);
    }
}

Current version of Canvas

package projectrpg;

import java.awt.Rectangle;

import java.awt.Graphics;

import java.util.ArrayList;

import java.awt.event.KeyAdapter;

import java.awt.event.KeyEvent;

import javax.swing.JPanel;

/**
 *
 * @author Dennis Jr
 */
public class Canvas extends JPanel implements Runnable {

    private GameData data;
    private Thread animator;
    private final int DELAY = 25;
    private Map levelMap;
    //Enable Test Map, delete or comment out before distribution.
    //private ArrayList<MapTile> mapTiles = new ArrayList<MapTile>();
    //Normal Map, enable before distribution.
    private ArrayList<MapTile> mapTiles;
    //Player's sprite and position on screen are the same throughout the game.
    //When the player moves, in reality the map moves around him.
    //Player should be positioned so x position = (xAxis / 2) - 15.
    //and y position = (yAxis / 2) - 30.
    private ArrayList<LinkTile> linkTiles;
    //This puts the center of the player in the center of the screen.
    private Player player;
    private int xShift;
    private int yShift;
    private Rectangle facing;
    private final int WIDTH = 20;
    private final int HEIGHT = 20;
    private final int PLAYERSTARTX = 230;
    private final int PLAYERSTARTY = 220;
    private final String PLAYERIMAGE = "player.png";
    private ArrayList<Interactable> interactables;
    private ArrayList<Collidable> collidables;

    public Canvas(String mapName, GameData data) {
        addKeyListener(new TAdapter());
        setFocusable(true);
        setDoubleBuffered(true);
        this.data = data;
        this.levelMap = this.data.findMap(mapName);
        this.player = new Player(PLAYERSTARTX, PLAYERSTARTY, PLAYERIMAGE);
        setFacing(player.getX(), player.getY() + HEIGHT, WIDTH, HEIGHT);
        this.xShift = 0;
        this.yShift = 0;
        this.interactables = this.levelMap.getInteractables();
        this.collidables = this.levelMap.getCollidables();
        this.linkTiles = this.levelMap.getLinkTiles();
    }

    public void drawMap(Graphics g) {
        int x = 0;
        int y = 0;

        for (Collidable a : getCollidables()) {
            x = a.getX();
            y = a.getY();

            g.drawImage(a.getImage(), x, y, this);
        }
        g.drawImage(player.getImage(), player.getX(), player.getY(), this);
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        drawMap(g);
    }

    @Override
    public void addNotify() {
        super.addNotify();
        animator = new Thread(this);
        animator.start();
    }

    public void run() {
        long beforeTime, timeDiff, sleep;

        beforeTime = System.currentTimeMillis();

        while (true) {

            repaint();

            timeDiff = System.currentTimeMillis() - beforeTime;
            sleep = DELAY - timeDiff;

            if (sleep < 0) {
                sleep = 2;
            }
            try {
                Thread.sleep(sleep);
            } catch (InterruptedException e) {
                System.out.println("interrupted");
            }

            beforeTime = System.currentTimeMillis();
        }
    }

    public void checkCollision(Mobile mobile) {
        for (Collidable a : getCollidables()) {
            if (a.isCollidable() == true) {
                Rectangle r2 = a.getBounds();
                if (r2.intersects(mobile.getLeftBound())) {
                    mobile.setLeftCollision(true);
                }
                if (r2.intersects(mobile.getRightBound())) {
                    mobile.setRightCollision(true);
                }
                if (r2.intersects(mobile.getUpBound())) {
                    mobile.setTopCollision(true);
                }
                if (r2.intersects(mobile.getDownBound())) {
                    mobile.setBottomCollision(true);
                }
            }
        }
    }

    /**
     * @return the interactables
     */
    public ArrayList<Interactable> getInteractables() {
        return interactables;
    }

    /**
     * @param interactables the interactables to set
     */
    public void setInteractables(ArrayList<Interactable> interactables) {
        this.interactables = interactables;
    }

    /**
     * @return the collidables
     */
    public ArrayList<Collidable> getCollidables() {
        return collidables;
    }

    /**
     * @param collidables the collidables to set
     */
    public void setCollidables(ArrayList<Collidable> collidables) {
        this.collidables = collidables;
    }

    class TAdapter extends KeyAdapter {

        @Override
        public void keyPressed(KeyEvent e) {

            int key = e.getKeyCode();
            player.setLeftCollision(false);
            player.setRightCollision(false);
            player.setTopCollision(false);
            player.setBottomCollision(false);

            if (key == KeyEvent.VK_S) {
                setFacing(player.getX() - WIDTH, player.getY(), WIDTH, HEIGHT);
                checkCollision(player);
                if (player.isLeftCollision()) {
                    return;
                }
                for (Collidable a : collidables) {
                    a.scrollX(WIDTH);
                }
                xShift = xShift + WIDTH;
            } else if (key == KeyEvent.VK_F) {
                setFacing(player.getX() + WIDTH, player.getY(), WIDTH, HEIGHT);
                checkCollision(player);
                if (player.isRightCollision()) {
                    return;
                }
                for (Collidable a : collidables) {
                    a.scrollX(-WIDTH);
                }
                xShift = xShift - WIDTH;
            } else if (key == KeyEvent.VK_E) {
                setFacing(player.getX(), player.getY() - HEIGHT, WIDTH, HEIGHT);
                checkCollision(player);
                if (player.isTopCollision()) {
                    return;
                }
                for (Collidable a : collidables) {
                    a.scrollY(HEIGHT);
                }
                yShift = yShift + HEIGHT;
            } else if (key == KeyEvent.VK_D) {
                setFacing(player.getX(), player.getY() + HEIGHT, WIDTH, HEIGHT);
                checkCollision(player);
                if (player.isBottomCollision()) {
                    return;
                }
                for (Collidable a : collidables) {
                    a.scrollY(-HEIGHT);
                }
                yShift = yShift - HEIGHT;
            } else if (key == KeyEvent.VK_J) {
                for (Interactable a : getInteractables()) {
                    if (facing.intersects(a.getBounds())) {
                        a.interact();
                    } else {
                        return;
                    }
                }
            }
            checkLink(player);
            repaint();
        }
    }

    /**
     * @return the levelMap
     */
    public Map getLevelMap() {
        return levelMap;
    }

    /**
     * @param levelMap the levelMap to set
     */
    public void setLevelMap(Map levelMap) {
        this.levelMap = levelMap;
    }

    public void checkLink(Mobile mobile) {
        for (LinkTile a : linkTiles) {
            Rectangle r = a.getBounds();
            if (r.intersects(mobile.getBounds())) {
                String toMap = a.getToMap();
                int newxShift = a.getxShift();
                int newyShift = a.getyShift();
                Map newMap = data.findMap(toMap);

                //Reset old map to original position
                for (MapTile d : mapTiles) {
                    d.setX(d.getX() - xShift);
                    d.setY(d.getY() - yShift);
                }

                for (LinkTile e : linkTiles) {
                    e.setX(e.getX() - xShift);
                    e.setY(e.getY() - yShift);
                }

                //load new map
                setLevelMap(newMap);
                setCollidables(levelMap.getCollidables());
                setInteractables(levelMap.getInteractables());

                //shift new map to starting position
                for (MapTile b : mapTiles) {
                    b.setX(b.getX() + newxShift);
                    b.setY(b.getY() + newyShift);
                }

                for (LinkTile c : linkTiles) {
                    c.setX(c.getX() + newxShift);
                    c.setY(c.getY() + newyShift);
                }

                //set shift values
                xShift = newxShift;
                yShift = newyShift;

            }
        }
    }

    public void setMapTiles(Map levelMap) {
        mapTiles = levelMap.getMapTiles();
    }

    public void setLinkTiles(Map levelMap) {
        linkTiles = levelMap.getLinkTiles();
    }

    public void setFacing(int x, int y, int width, int height) {
        facing = new Rectangle(x, y, width, height);
    }

    public Rectangle getFacing() {
        return facing;
    }
}

You could set the new map by using setLevelMap(Map)? :wink:

Also, do not use the paint(Graphics) method in JPanel. All Swing components that you extend must draw in paintComponent(Graphics). Then there is no need to call “super”.

Thanks ill try that.

Although to be more specific, the problem im having is that the map doesnt change when the player steps on the specified tile.

I don’t see any code that changes the map…