- 浏览: 312049 次
- 性别:
- 来自: 益阳
文章分类
最新评论
-
duckbit:
楼主是否能把共享layout的例子发给我,有点没明白 谢谢额! ...
Android换肤apk -
天涯海角262253:
...
Androidpn里的Xmpp的理解 -
lbstudy:
Activity之间的切换动画 -
dumbnesslys:
楼主可不可以给个源码 ,就build.xml的 772774 ...
Ant自动打包 -
finaljava:
build.xml 这么复杂,看看这个吧http://angr ...
Ant自动打包
I used to make games like this:
class RenderableMovingCollidableGameObject extends RenderableMovingGameObject { public void update() { super.update(); // Parent classes implement rendering and movement. // implement collision detection here } } class PlayerObject extends RenderableMovingCollidableGameObject { public void update() { super.update(); // Run rendering, movement, and collision. // update the player } } ... // Main loop! while (true) { InputSystem.update(); // poll for input. for (gameObject : ListOGameObjects) { gameObject.update(); gameObject.draw(); } }
This isn't a bad way to start making games, but it doesn't scale. Making a good game requires flexibility and the ability to iterate quickly, and this approach starts to break down when the game becomes medium-sized. What if we have more than one game mode? What if the behavior of a specific game object needs to change for a short period of time? What if Level 27's version of the player needs to be subtly different than Level 26's?
In a nutshell, the problem with this approach is that the structure of the code producing a single frame of the game is hard-coded into the program. What I want is something that can change its structure between levels, or between game modes, or even on the fly. I need something more dynamic than a hard-coded for loop.
Another Approach
Let's look at game objects as an example. Game objects are entities in the game world like coins, enemies, moving platforms, and the player. They often have similar functionality, so one way to go about implementing a set of similar objects is to use an inheritance tree. We can create a base GameObject class, and from that derive RenderableGameObject, and from that derive RenderableMovingGameObject, and from that derive RenderableMovingCollidableGameObject, etc, etc, etc. Each level of derivation can add some common functionality until the leafs of this tree are specific entities, like the player.
The real problem with inheritance trees is that features that don't need to be inter-related become dependent on each other because of the way the code itself is written. Given the example class structure above, it's not possible to make a GameObject that can move but doesn't need to render (short of littering the code with flags--don't do that). Because of inheritance, a dependency between movement and rendering has been created where none actually needs to exist. What if we could mix and match features on a per-instance basis rather than tying them all together in a class hierarchy? This is where object composition comes in.
Object composition (or "object aggregation" depending on which design patterns book you read) is the idea that an object "has a" feature instead of "is a" feature. Rather than using inheritance or some other code-side method of collecting functionality together in a single object, we make the object manage a list of separate feature objects; the contents of that list can be different per instance.
So for Replica Island, I have a GameObject class that contains a list of GameComponents. A game object with an empty list does nothing; it can't be drawn, or make noise, or hit things, or do anything else to affect the game. GameComponents implement all of those features, and they must be inserted into the GameObject for it to actually be able to act. Here's some psudeo-code of how GameObject and GameComponents work:
class GameObject { private Array<GameComponent> mComponents; public void addComponent(GameComponent component) { mComponents.push(component); } public void update(float time) { for (component : mComponents) { component.update(time, this); } } }
class GameComponent { public void update(float time, GameObject parent) { // ... functionality goes here } }
A GameObject just runs all the GameComponents it contains to produce its output each frame. GameComponents in Replica Island implement things like movement, physics, collision detection, sprite animation, rendering, animation selection, AI, player control, etc. Once a generic GameComponent is written it can be inserted in any number of objects; if a specific object requires special functionality, a new GameComponent can be written just for that object without disturbing anything else in the system.
The beauty of this approach is that individual components can be compartmentalized features, and brand new objects in the game world can be created just by sticking different pieces of pre-existing code together in new ways. The result is also dynamic: unlike an inheritance-based object, Replica Island game objects can change their structure at runtime. For example, one of the Android robot's powers is to possess other robots. When an enemy robot is possessed, his AI component is removed and a new component that lets the player control him is inserted. All of a sudden the robot is being driven by the player; all the rest of his code for animation selection, physics, and collision detection, continues to work without realizing that anything has changed. When the player releases the robot, the original component structure can be restored (actually, in this case the robot blows up, taking out anything else near it, but you get the idea).
I've made a couple of games now using components and I'm very happy with the result. In Replica Island I took a couple of shortcuts for speed that damage the goal of completely independent components, but I think it was worth it; the sacrifices I've made for frame rate haven't actually proved detrimental to the flexibility or extensibility of the system.
发表评论
-
Want to Change the Game Industry? Support the Xperia PLAY.
2011-12-07 21:05 0The problems with the console g ... -
Replica Island Updated
2011-12-07 21:04 0Yesterday I uploaded the first ... -
Hot Failure
2011-12-07 21:04 0An article I originally wrote f ... -
Building a Reflective Object System in C++
2011-12-07 21:03 0Every game engine I've worked w ... -
Leveraging Java and C++ for Hybrid Games
2011-12-07 21:02 0I've been thinking a lot lately ... -
Game Object Construction Rabbit Hole
2011-12-07 21:01 0Today I want to write a little ... -
Long Time, No See
2011-12-07 21:00 0I haven't written anything here ... -
Replica Island Passes a Million Installs
2011-12-07 20:59 0Holy crap!Replica Island passed ... -
Control Configuration and Abstraction
2011-12-07 20:58 0The #1 thing that I've learn ... -
Design Post-Mortem: Three Mistakes
2011-12-07 20:58 0While I'm pretty happy with Rep ... -
Replica Island: One Month On
2011-12-07 20:57 0As of today, Replica Island ... -
My New Favorite User Feedback
2011-12-07 20:56 0You can't write this stuff ... -
Replica Island User Comments Are Hilarious
2011-12-07 20:55 0Update: Blogger apparently isn' ... -
Design Post-Mortem: The Possession Orb
2011-12-07 20:55 0Rather than write a big lo ... -
Replica Island Released!
2011-12-07 20:54 0I released Replica Island almos ... -
Fragmentation? More like Fragmentawesome.
2011-12-07 20:53 0I'm lucky enough to have occasi ... -
The Elusive Perfect Platformer Camera
2011-12-07 20:52 0I've come to believe that platf ... -
Game Play Video
2011-12-07 20:51 0Here's some footage of an early ... -
Tuning With Metrics Redux
2011-12-07 20:50 0A while back I posted about the ... -
Some Screenshots
2011-12-07 20:49 0I've posted some screenshots of ...
相关推荐
mongodb group aggregate项目实战笔记 管道聚合 mongodb group按时间分组,用aggregate管道聚合 会比group的处理效率要高而且更灵活方便
matlab开发-aggregate。基于对另一个矩阵中的变量进行分组,对一个矩阵中的值进行分组(并对其应用函数)
THE COMPUTATION OF AGGREGATE LOSS DISTRIBUTIONS 作者:ROBERSON 用FFT计算总损失分布
2聚合感知函数@Aggregate_Aware1聚合感知函数@Aggregate_Aware1聚合感知函数@Aggregate_Aware
meteor-aggregate, 对 Meteor的适当的MongoDB聚合支持 meteorhacks:aggregate为 Meteor 添加适当聚合支持的简单软件包。 这个包在 Mongo.Collection 实例上公开了 .aggregate 方法。这只在服务器端工作,没有...
aggregate-v0.75.tgz cacti插件。
Delphi数据排序、Aggregate字段等dbExpress技巧实例.rar
Cacti Aggregate Sum 添加多端口流量汇集
a novel variant called aggregate channel features, make a full exploration of feature design, and discover a multi- scale version of features with better performance. To deal with poses of faces in ...
maven-site-aggregate-plugin-2.7.0.jar
maven-site-aggregate-plugin-2.10.0.jar
我的博客:www.wangs0622.com, mongoDB-aggregate pipeline 使用的文档集合。
jar包,亲测可用
maven-site-aggregate-plugin-2.0.0.jar
mongo聚合函数(aggregate)java版, 类似mysql的group。 对应查询语句可查看 https://blog.csdn.net/xu180/article/details/89175663
DynSketch: a spatio-temporal aggregate index for moving objects in road networks
主要介绍了使用aggregate在MongoDB中查询重复数据记录的方法的相关资料,需要的朋友可以参考下
mongo spring boot Aggregate 例子
最近一直在用mongodb,有时候会需要用到统计,在网上查了一些资料,最适合用的就是用aggregate,以下介绍一下自己运用的心得。。 MongoDB 聚合 MongoDB中聚合(aggregate)主要用于处理数据(诸如统计平均值,求和等),...
Aggregate