views:

115

answers:

2

Hi all. 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.

A: 

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

Are you using MVC?

Pyrolistical
I'm afraid I do not know what MVC is...
Relequestual
Now's a good time to learn. http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller
Michael Myers
In that case I believe I am. I have just never heard it referred to as such.Mouse events are handled, data is manipulated, and the the change is displayed to the user. In essence, don't most desktop applications (especially games) work this way?
Relequestual
+3  A: 

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)

CoderTao
This sounds interesting. Can you link me to any examples where this patten is implemented? or is it common enough that I can find it?I recently got show another the factory method patten for some other logic, but that isn't really related to this question. It's interesting that there are so many established ways of doing things :)
Relequestual
http://en.wikipedia.org/wiki/Strategy_pattern . That said, but it basically boils down to is 'implement a common interface so that implementations that do different things can be swapped in and out'. My use of the term here may be invalid, but I think it's close enough in intent.
CoderTao
Thank you, this is interesting. It may be a way of doing it. Have you done it before with action listeners? or are you speculating? I'm trying to think how this would be implemented. I think I can work it out, but I'm rather sleep :). I think in essence, I was doing this in crude way using booleans, however using this strategy patten looks like a far better way to do it, and with less mess!
Relequestual
would anyone be kind enough to draw up a very very very simple example of how this would work? say with alternate drawing a square and then rectangle. Would help me dramatically. Cant quite see how you switch an action listener. The reason for this is because my code reads "addMouseActionListsner(new MouseEventListener());" when the panel is loaded. Is this bad practice in itself?Thanks again!
Relequestual
Thanks for the edit, adding the example.This seems like its exactly what I need to do.I think for simplicity's sake, I will have an action listener for each type rather than each stage, and then use boolean's and methods to separate the different stages, keeping it within the same class. I may have already got too many classes... I have so much to implement now! A lot of moving code around and re writing it! Probably won't be on Stack for a while now... unless I screw up :)Thanks once again CoderTao, and thanks all those at Stack Overflow for being fantastic!
Relequestual