Question

I'm developing a grid based sim game in java, and I was wondering if there is a standard way of doing the following. I have a panel which is the game panel, and there are many different things which could happen when the panel is clicked. For example, when building a room, there are several stages, where dragging the mouse and left clicking will have different actions. Right now, the way I have done it, is to use booleans to check what's being built, and then what stage it is at.

Is there any better or standard way of handling something like this? I had a quick google, but as I have said before, people on Stack Overflow always give a better, more relevant, up to date answer.

I consider myself still rather new to java.

Thanks in advance.

Was it helpful?

Solution

You might try looking into something similar to the strategy pattern.

Basically, you start by clicking the room button on your toolbar. The toolbar goes through and tells the grid to use the 'room place' actionlistener. Presumably removing any previous action listener that was listening

The room place actionlistener would in turn implement all the interesting bit of logic for left/right clicking, dragging, etc.

If you have multiple stages to building a room (say, placing doors, then windows, then trap doors); the action listeners would be responsible for handing control off to the next stage: a bit of a finite state machine.

So, start by clicking 'room' button, 'place room' listener is added. Drag out the area you want the room to be, 'place room' modifies the game state, then changes the actionlistener to the 'place windows' listener. Ad infinitum... until you finish.

One very simple (non compilable) example:

class GridPanel extends JPanel
{
    void SetMouseListener(MouseListener newListener)
    {
        for(MouseListener ml : getMouseListeners())
            removeMouseListener(ml);
        addMouseListener(newListener);
    }
}

class ControlPanel extends JPanel
{
    GridPanel gameGrid;

    void OnRectangleButtonClicked(some stuff)
    {
        gameGrid.SetMouseListener(new PlaceRoomListener(gameGrid));
    }
}

class PlaceRoomListener extends MouseAdapter
{
    GridPanel gameGrid;

    //constructor, etc

    void OnClick(mouse event)
    {
        gameGrid.doCoolStuff();
        gameGrid.SetMouseListener(new PlaceTrapDoorListener());
    }
}

//etc

Now, that non-compilable example aside, Pyrolistical does have a point: you generally don't want to combine your game model and graphic interface into one single glob of classes. You want the model separated from the GUI, and to manipulate it through a well defined interface.

(Also, there are probably better methods for going about removing the mouse listener then just randomly removing all listeners... I was in a bit of a rush, sorry)

OTHER TIPS

It sounds like you need to define your game model/state and keep it separate from your mouse actions.

Are you using MVC?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top