我的游戏对象通常以pseudo-javascript
方式显示:
function Player(){
this.update = function() {
//logic that should be run per update frame
//physics code I really don't want here
}
}
Player.prototype = new gameObject;
Player.update
由实例化它的gameObjectManager
调用,它基本上是一个循环,通过在其中的所有对象上调用update()
来循环。很好,这对于特定于对象本身的更新函数非常有效。
我在该功能中不想要的是物理逻辑。它在我的代码中造成了不必要的膨胀,我发现我必须在我创建的每个唯一gameObject
中复制很多功能。找到合适的课程是很困难的,因为并非所有gameObjects
都需要它。
我玩过的一个解决方案是另一个类gameObjectManger
,巧妙地命名为physicsManger
,它将维护一个需要物理更新的对象数组,并直接对其进行操作而不是调用{{1}每个。
或者,也许该代码应该进入update()
本身,我不知道。
我的目标是将物理应用于gameObjectManager
中的任意对象,就像设置一样简单
gameworld
理想情况下,这些对象不应该知道或关心物理代码,只需要对它们采取行动即可。
答案 0 :(得分:0)
My recommendation is to just separate it to two functions. Have a dataSource
function and an updateLogic()
function. Then for physicsless objects leave the updatePhysics()
function empty. You could add a updatePhysics()
if you like, but I would just have your physicsObjectManager
call each function for each object.
I'm presuming that by 'bloating my code' you mean in terms of reading and maintaining it, not in terms of performance. I highly recommend using helper functions more often (subdividing a function into smaller, reusable component functions) if you find you are writing similar or identical code often.
I don't know what language you are developing in, but this also sounds like a perfect time to use Object Inheritance. This will make your classes much less redundant, and more structured. You may also find that using inheritance also makes the project easier for you to understand and work effectively with.
答案 1 :(得分:0)
好吧,我放弃了我需要原型继承来实现我的物理代码的想法。我最终得到的是全局命名空间中的一个简单.fileupload
{
font-family: inherit;
font-size: inherit;
line-height: inherit;
width: 100%;
height: 32px;
pointer-events: none;
}
函数,它希望对包含任何Physics
坐标和其他物理属性进行操作。
我可以在其他代码的任何地方调用它,以便使用GameObject
这给了我所寻求的灵活性,所以我将使用这种方法而不是原型继承。