Java – OO game design issues
I'm writing a simple game in Java, but I'm trying to be 'right' with a beautiful and clean design without hacker attack
I have two classes gamepanel to receive clicks and keys. The model contains all entities and is responsible for updating The model needs to know the user's mouse position, but I can't decide to adopt the "correct" method
Whether the model and each entity inside should keep a reference to the gamepanel, or just cache the last known mouse position and receive updates from the gamepanel regularly When creating a model with the first option, you need to provide a reference to the gamepanel, and the second mouse position will be sent to the world as a parameter Update() method
These solutions don't seem elegant, so I wonder if there is a "right" way to do this, I've missed it
Thank you, Ben
Solution
In my opinion, it depends on how your course interacts Does the change of mouse position trigger the entities in the model class? Or is the model class independent of the gamepanel and only applicable to the current value of the mouse location?
If later and in that case, I agree with what Jeff said in front of me When creating it, pass the handle on the gamepanel to the model class, and let each entity use the handle to access the mouse position when needed This always uses the updated mouse position
If the former, I suggest using observers to let the model class know when the mouse position value has changed The model class can then use the same design (that is, let the model class always have a handle on the gamepanel class) and access the current value in the gamepanel
Summing up my answers to your questions, I think that only logic and OO concept can make gamepanel maintain the value of mouse position and let other classes use gamepanel instances to access this information
Thank you, Rohan