简体   繁体   English

OO游戏设计问题

[英]OO game design question

I am programming a simple game in Java but I am trying to do it 'right' with a nice clean design and no hacks. 我正在用Java编写一个简单的游戏,但我正在尝试用一个漂亮干净的设计做到'正确'而且没有黑客攻击。

I have two classes GamePanel that receives clicks and keypresses and Model which contains all the Entities and is responsible for their updates. 我有两个类GamePanel接收点击和按键, Model包含所有实体并负责更新。 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. Model和内部的每个Entity是否应该保持对GamePanel的引用,或者只是缓存最后已知的鼠标位置并定期从GamePanel接收更新。 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. 使用第一个选项创建模型时,需要为GamePanel提供引用,第二个鼠标位置将作为参数发送到World.update()方法。

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. 谢谢,本。

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? 鼠标位置的变化是否触发了Model类中的实体? or Is the Model class independent of the GamePanel and only works on the current values of Mouse position? 或者Model类是否独立于GamePanel并且仅适用于Mouse位置的当前值?

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. 在创建它时将GamePanel上的句柄传递给Model类,并让每个实体在需要时使用句柄来访问鼠标位置。 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. 如果前者我建议使用Observers让Model类知道鼠标位置值何时发生变化。 Then Model class could use the same design (ie let the Model class have an handle on the GamePanel class always) and access the current values in the GamePanel. 然后Model类可以使用相同的设计(即让Model类始终在GamePanel类上有句柄)并访问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. 总结我对你的问题的答案,我认为只有逻辑和符合OO概念才能让GamePanel保持鼠标位置的值,让其他类使用GamePanel实例访问该信息。

Thanks, Rohan. 谢谢,罗汉。

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. 在这种情况下,GamePanel将触发一个事件,通知新鼠标定位模型中所有感兴趣的(订阅事件)对象(实体)。

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. 通过这种方式,GamePanel不需要明确知道要通知哪些实体,并且实体不需要保留GamePanel的实例。

My advice: read up on event listeners. 我的建议:阅读事件监听器。 (Java.util) (java.util中)

您可以始终将当前坐标设置为GamePanel的公共属性,以便实体/模型可以从那里简单地访问它。

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: 如果要轮询当前鼠标位置,可以随时在Event Dispatch Thread中运行此代码snipplet:

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. 并且rel包含鼠标的相对位置。

Alternatively, you could have a look at JInput , as it is designed to cope with keyboard/mouse/whatever inputs. 或者,您可以查看JInput ,因为它旨在处理键盘/鼠标/任何输入。

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. IE:您是否需要在每次更新时使用鼠标位置,或仅在特定点处使用鼠标位置。

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. (IE:如果您的游戏响应鼠标点击,则点击一下。)含义阅读事件监听器。

Good luck! 祝好运! Hope that helps. 希望有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM