views:

382

answers:

5

Hi SO,

I am programming a simple game in Java but I am trying to do it 'right' with a nice clean design and no hacks.

I have two classes GamePanel that receives clicks and keypresses and Model which contains all the Entities and is responsible for their updates. The Model needs to know where the user's mouse is but I can't decide on the 'right' way to do it.

Should the Model and every Entity within keep a reference to the GamePanel or just cache the last known mouse position and receive updates from the GamePanel regularly. With the first option when the model is created it will need to be given a reference to the GamePanel and with the second the last mouse position will be sent as a parameter to the World.update() method.

Neither of these solutions seem elegant so I was wondering if there is a 'right' way to do this that I have missed.

Thanks, Ben.

+2  A: 

You could always have the current coordinates be a public property of the GamePanel, so that the Entitys/Models can simply access it from there.

Jeff
+1  A: 

If you want to poll the current mouse position, you could run this code snipplet in the Event Dispatch Thread at any time you wish:

Point pt = MouseInfo.getPointerInfo().getLocation();
Point cpt = GamePanel.getLocationOnScreen();
Point rel = new Point(pt.x - cpt.x, pt.y - cpt.y);

And rel contains the relative position of the mouse.

Alternatively, you could have a look at JInput, as it is designed to cope with keyboard/mouse/whatever inputs.

kd304
MouseInfo since 1.5
kd304
+3  A: 

In my opinion, it would depend on how your classes are interacting. Is a change in the mouse position triggering the entities in the Model class? or Is the Model class independent of the GamePanel and only works on the current values of Mouse position?

If the later and in that case I agree with what jeff has said before me. Pass a handle on the GamePanel to the Model class when creating it and let every entity use the handle to access the mouse position whenever needed. That way the updated mouse position is always used.

If the former I would suggest use Observers to let the Model class know when the mouse position value has changed. Then Model class could use the same design (i.e let the Model class have an handle on the GamePanel class always) and access the current values in the GamePanel.

To sum up my answer to your question, I think it is only logical and conforming to OO concepts to let the GamePanel hold the values of the mouse position and let other classes access that information using a GamePanel instance.

Thanks, Rohan.

rohangter
+3  A: 

A common solution is to fire events. In this case GamePanel would fire an event informing all interested (subscribed to the event) objects (Entity) in Model of the new mouseposition.

In this way, GamePanel does not need to know explicitly which Entities to inform, and the Entities do not need to keep an instance of GamePanel around.

My advice: read up on event listeners. (Java.util)

NomeN
What do you mean by Java.util ? Mouse events and listeners are in java.awt.event package.
kd304
You are both correct. The java.awt.event.MouseListener extends java.util.EventListener (http://java.sun.com/javase/6/docs/api/java/util/EventListener.html).
Nelson
I imagined that the OP was not using awt or swing, so i pointed him to util.
NomeN
+1  A: 

Does the model always need to know where the mouse is. IE: Do you need the mouse position at every update, or only at a specific points.

If you need it at every point or during every update then either solution that you have documented, or anyone solution here should be solid.

If you only need it at specific points then only grab it then. (IE: if your game responds to mouse clicks then do something on click.) Meaning Read about Event Listeners.

Good luck! Hope that helps.

Bryan Hare
There is one case you could use the poll based approach - if you don't want to fight the mouse-dragged event when 'dragging' the mouse around (e.g. shoot and turn)
kd304