游戏开发中的面向对象应用程序问题 [英] Object Oriented application problems in game development

查看:72
本文介绍了游戏开发中的面向对象应用程序问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于这个问题,我会尽量做到直接,因为一定有一些我完全缺少的东西来自结构化编程背景.

I'll be as direct as I can concerning this problem, because there must be something I'm totally missing coming from a structured programming background.

说我有一个Player类.此类Player类负责更改其在游戏世界中的位置.我将此方法称为warp(),该方法将Position类实例作为参数来修改Player的内部位置.从面向对象的角度来讲,这对我来说是完全有意义的,因为我要玩家做"一些事情.

Say I have a Player class. This Player class does things like changing its position in a game world. I call this method warp() which takes a Position class instance as a parameter to modify the internal position of the Player. This makes total sense to me in OO terms because I'm asking the player "to do" something.

问题是,除了修改球员位置之外,我还需要做其他事情.例如,假设我需要将该扭曲事件发送给在线游戏中的其他玩家.该代码也应该在Player的warp()方法中吗?如果没有,那么我可以想象在诸如Server类中声明某种辅助方法,例如warpPlayer(player,position).这样做似乎会减少玩家作为一系列的获取和设定者所做的一切,还是我在这里只是错了?这是完全正常的事情吗?我已经读过无数次了,一个将所有内容都暴露为一系列getter/setter的类表示非常差的抽象性(用作数据结构而不是类).

The issue comes when I need to do other things in addition to just modifying the players position. For example, say I need to send that warp event to other players in an online game. Should that code also be within Player's warp() method? If not, then I would imagine declaring some kind of secondary method within say the Server class like warpPlayer(player, position). Doing this seems to reduce everything a player does to itself as a series of getters and setters, or am I just wrong here? Is this something that's totally normal? I've read countless times that a class that exposes everything as a series of getters/setters indicates a pretty poor abstraction (being used as a data structure instead of a class).

当您需要保留数据并将其保存到文件时,也会出现相同的问题.由于将播放器保存"到文件的抽象级别不同于Player类,因此在player类中使用save()方法有意义吗?如果不是这样,则像savePlayer(player)一样在外部声明它,则意味着savePlayer方法将需要一种从Player类中获取所需数据的方法,最终暴露出该类的整个私有实现.

The same problem comes when you need to persist data, saving it to a file. Since "saving" a player to a file is at a different level of abstraction than the Player class, does it make sense to have a save() method within the player class? If not, declaring it externally like savePlayer(player) means that the savePlayer method would need a way to get every piece of data it needs out of the Player class, which ends up exposing the entire private implementation of the class.

因为OOP是当今最常用的设计方法(我认为是?),所以在这些问题上我肯定会缺少一些东西.我已经与同样进行轻度开发的同龄人讨论了这一点,他们在OOP中也遇到了这些完全相同的问题.也许仅仅是结构化的编程背景使我们无法理解OOP的全部好处,而不仅仅是提供设置和获取私有数据的方法,以便可以从一个地方进行更改和检索.

Because OOP is the design methodology most used today (I assume?), there's got to be something I'm missing concerning these issues. I've discussed it with my peers who also do light development, and they too have also had these exact same issues with OOP. Maybe it's just that structured programming background that keeps us from understanding the full benefits of OOP as something more than providing methods to set and get private data so that it's changed and retrieved from one place.

先谢谢您了,希望我听起来不会像个白痴.对于真正需要了解此设计涉及的语言的人,服务器端是Java,客户端是ActionScript 3.

Thanks in advance, and hopefully I don't sound too much like an idiot. For those who really need to know the languages involved with this design, it's Java on the server side and ActionScript 3 on the client side.

推荐答案

不必担心Player类是一堆setter和getter. Player类是模型类,并且模型类倾向于这样.重要的是,您的模型类要小而干净,因为它们将在整个程序中重复使用.

Don't worry too much about the Player class being a bunch of setters and getters. The Player class is a model class, and model classes tend to be like that. It's important that your model classes are small and clean, because they will be reused all over the program.

我认为您应该使用建议的warpPlayer(player, position)方法.它使Player类保持干净.如果您不想将播放器传递给函数,则可能有一个PlayerController类,其中包含一个Player对象和一个warp(Position p)方法.这样,您可以将事件发布添加到控制器,并将其保留在模型之外.

I think you should use the warpPlayer(player, position) approach you suggested. It keeps the Player class clean. If you don't want to pass the player into a function, maybe you could have a PlayerController class that contains a Player object and a warp(Position p) method. That way you can add event posting to the controller, and keep it out of the model.

关于保存播放器,我可以通过使Player实现某种序列化接口来实现. Player类负责自身的序列化和反序列化,而其他一些类则负责将序列化的数据写入文件或从文件写入.

As for saving the player, I'd do it by making Player implement some sort of serialisation interface. The player class is responsible for serializing and unserializing itself, and some other class would be responsible for writing the serialised data to/from a file.

这篇关于游戏开发中的面向对象应用程序问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆