02OPhone平台2D游戏引擎实现场景图层元素.docx
- 文档编号:5870125
- 上传时间:2023-01-01
- 格式:DOCX
- 页数:48
- 大小:44.54KB
02OPhone平台2D游戏引擎实现场景图层元素.docx
《02OPhone平台2D游戏引擎实现场景图层元素.docx》由会员分享,可在线阅读,更多相关《02OPhone平台2D游戏引擎实现场景图层元素.docx(48页珍藏版)》请在冰豆网上搜索。
02OPhone平台2D游戏引擎实现场景图层元素
上一篇我们对游戏引擎的框架进行了分析,同时也完成了一个基础的游戏引擎框架,那么这一篇我们将完成游戏引擎中的场景,图层,元素(节点)进行实现,但是在开始之前,我们可以对上一篇所介绍的框架进行完善,首先我们可以对引擎的渲染窗口(YFSGLSurfaceView)进行完善,使其具备事件处理能力和对渲染器(Renderer)进行控制。
首先在YFSGLSurfaceView中声明以下成员:
viewplaincopytoclipboardprint?
1.//渲染器
2.private Director mDirector;
3.//事件调度器
4.private EventDispatcher mDispatcher;
5.然后,需要在构造函数中实例化这些对象,代码如下:
6.//取得Director
7.this.mDirector = Director.getInstance();
8.//取得context
9.this.mDirector.context = context;
10.//取得EventDispatcher
11.this.mDispatcher = EventDispatcher.getInstance();
12.同时在退出函数surfaceDestroyed中,我们将调用Director的onSurfaceDestroyed函数来处理退出操作,释放资源等,代码如下:
13.public void surfaceDestroyed(SurfaceHolder holder) {
14. super.surfaceDestroyed(holder);
15. this.mDirector.onSurfaceDestroyed();
16.}
//渲染器privateDirectormDirector;//事件调度器privateEventDispatchermDispatcher;然后,需要在构造函数中实例化这些对象,代码如下:
//取得Directorthis.mDirector=Director.getInstance();//取得contextthis.mDirector.context=context;//取得EventDispatcherthis.mDispatcher=EventDispatcher.getInstance();同时在退出函数surfaceDestroyed中,我们将调用Director的onSurfaceDestroyed函数来处理退出操作,释放资源等,代码如下:
publicvoidsurfaceDestroyed(SurfaceHolderholder){super.surfaceDestroyed(holder);this.mDirector.onSurfaceDestroyed();}
最后就是事件处理部分,由于我们对事件的处理都是在EventDispatcher中进行统一管理调度的,所以我们只需要按照不同的事件类型,将事件传递给EventDispatcher即可,EventDispatcher便会为我们进行事件处理,如代码清单2-1所示。
代码清单2-1:
事件处理
viewplaincopytoclipboardprint?
1.//触笔事件处理
2.public boolean onTouchEvent(MotionEvent event) {
3. switch (event.getAction()) {
4. case MotionEvent.ACTION_CANCEL:
5. case MotionEvent.ACTION_OUTSIDE:
6. this.mDispatcher.touchesCancelled(event);
7. break;
8. case MotionEvent.ACTION_DOWN:
9. this.mDispatcher.touchesBegan(event);
10. break;
11. case MotionEvent.ACTION_MOVE:
12. this.mDispatcher.touchesMoved(event);
13. break;
14. case MotionEvent.ACTION_UP:
15. this.mDispatcher.touchesEnded(event);
16. }
17. return true;
18.}
19.//按键事件处理
20.public boolean onKeyDown(int keyCode, KeyEvent event) {
21. if (this.mDispatcher.keyDown(event)) {
22. return true;
23. }
24. return super.onKeyDown(keyCode, event);
25.}
26.public boolean onKeyUp(int keyCode, KeyEvent event) {
27. if (this.mDispatcher.keyUp(event)) {
28. return true;
29. }
30. return super.onKeyUp(keyCode, event);
31.}
//触笔事件处理publicbooleanonTouchEvent(MotionEventevent){switch(event.getAction()){caseMotionEvent.ACTION_CANCEL:
caseMotionEvent.ACTION_OUTSIDE:
this.mDispatcher.touchesCancelled(event);break;caseMotionEvent.ACTION_DOWN:
this.mDispatcher.touchesBegan(event);break;caseMotionEvent.ACTION_MOVE:
this.mDispatcher.touchesMoved(event);break;caseMotionEvent.ACTION_UP:
this.mDispatcher.touchesEnded(event);}returntrue;}//按键事件处理publicbooleanonKeyDown(intkeyCode,KeyEventevent){if(this.mDispatcher.keyDown(event)){returntrue;}returnsuper.onKeyDown(keyCode,event);}publicbooleanonKeyUp(intkeyCode,KeyEventevent){if(this.mDispatcher.keyUp(event)){returntrue;}returnsuper.onKeyUp(keyCode,event);}
好了,上面则是我们对上一篇引擎的框架进行完善,当然还有Director需要完善,我们会在后面组件进行完善。
这里我们先来分析最基础的场景元素,即前面说的节点。
场景元素
从名字大家可以看出来,所谓的场景元素,那么肯定就是能够放置在场景中的任何对象,比如一棵树、一个怪物、一个按钮,一个标签等都是场景中的元素,甚至场景、图层本身也是一个场景元素,即节点。
我们将在后面给大家分析,大家也可以跳到后面去看一下,这样更有助于理解场景元素的定义。
节点基类实现
我们可以为这所有的元素抽象一个基类Node,任何可以在场景中放置的元素都将继承自该类。
Node封装了各种元素的的通用逻辑和渲染过程,这里我们也把这些元素称之为“节点”,在后文中出现的节点就表示元素。
首先我们要确定一个通用节点需要哪些属性?
如代码清单2-2所示。
代码清单2-2:
节点的部分属性
viewplaincopytoclipboardprint?
1.public class Node {
2. protected static final int INVALID_TAG = -1;
3. //锚点比例
4. protected PointF mAnchorPercent;
5. //摄像头
6. private Camera mCamera;
7. //子节点
8. protected ArrayList
9. //尺寸大小
10. protected YFSSize mContentSize;
11. private BaseGrid mGrid;
12. //是否运行中...
13. private boolean mRunning;
14. //变换
15. protected boolean mTransformDirty;
16. protected boolean mInverseDirty;
17. //父节点
18. private Node mParent;
19. //位置
20. protected YFSPoint mPosition;
21. //锚点是否发生作用
22. private boolean mRelativeAnchorPoint;
23. //是否显示
24. private boolean mEnabled;
25. //是否选中
26. private boolean mSelected;
27. //旋转,缩放
28. private float mRotation;
29. private float mScaleX;
30. private float mScaleY;
31. //tag标记
32. protected int mTag;
33. //放射变换
34. private YFSAffineTransform mTransformMatrix;
35. private YFSAffineTransform mInverseMatrix;
36. //锚点位置
37. protected YFSPoint mAnchorPosition;
38. //其他附加数据
39. private Object mUserData;
40. //节点在OpenGL中的z order值
41. private float mVertexZ;
42. //是否可见
43. protected boolean mVisible;
44. //节点在父节点中的z order值
45. private int mZOrder;
46. //...
47.}
publicclassNode{protectedstaticfinalintINVALID_TAG=-1;//锚点比例protectedPointFmAnchorPercent;//摄像头privateCameramCamera;//子节点protectedArrayList
代码清单2-2列出了一个通用节点所需要的常用属性,另外每个节点还可以设置其动画,我们将在引擎的动画部分完成之后在来完善节点的动画,大家可以根据注解来理解,这里我们主要说明一下“锚点”,我们对每一个节点都设置了一个锚点,比如一个节点的大小尺寸为480*320,锚点被设置为(0.5,0.5),那么锚点实际上所表示的就是(240,160)点的位置,那么我们在设置节点的位置坐标时,就会和这个锚点相关,如果我们将其中mRelativeAnchorPoint值设置为true,那么我们所设置的节点的位置就是这么锚点的相对位置,可能比较南里理解,我们在后面具体的使用过程中将进一步给大家分析。
大家可以看都我们创建了一个子节点列表和一个父节点,因此说明我们的节点也是可以嵌套的。
另一个需要分析的就是节点的摄像机Camera了,稍后将给大家详细分析,现在我们先来分析节点类的具体实现过程,由于篇幅关系对于该类的set和get函数就不进行解析了。
节点列表操作
首先我们来分析整个节点链,即子节点的一些常用操作,如代码清单2-3所示。
代码清单2-3:
节点列表操作
viewplaincopytoclipboardprint?
1.//添加节点
2.public Node addChild(Node child) {
3. assert (child !
= null);
4. return addChild(child, child.mZOrder, child.mTag);
5.}
6.//添加节点
7.public Node addChild(Node child, int z) {
8. assert (child !
= null);
9. return addChild(child, z, child.mTag);
10.}
11.//(节点,z值,tag标记)
12.public Node addChild(Node child, int z, int tag) {
13. assert (child !
= null);
14. assert (child.mParent == null);
15. if (this.mChildren == null) {
16. childrenAlloc();
17. }
18. insertChild(child, z);
19. child.mTag = tag;
20. child.setParent(this);
21. if (this.mRunning) {
22. child.onEnter();
23. }
24. return this;
25.}
26.//新建列表
27.private void childrenAlloc() {
28. this.mChildren = new ArrayList(4);
29.}
30.//清理
31.public void cleanup() {
32. if (this.mChildren !
= null)
33. for (int i = 0; i < this.mChildren.size(); ++i)
34. ((Node) this.mChildren.get(i)).cleanup();
35.}
36.//裁剪节点(移除节点)
37.private void detachChild(Node child, boolean doCleanup) {
38. if (doCleanup) {//移除前判断是否需要清理
39. child.cleanup();
40. }
41. child.setParent(null);
42. this.mChildren.remove(child);
43.}
44.//得到节点
45.public Node getChild(int tag) {
46. assert (tag !
= INVALID_TAG) :
"Invalid tag";
47. if (this.mChildren !
= null) {
48. for (int i = 0; i < this.mChildren.size(); ++i) {
49. Node child = (Node) this.mChildren.get(i);
50. if (child.mTag == tag) {
51. return child;
52. }
53. }
54. }
55. return null;
56.}
57.public ArrayList
58. return this.mChildren;
59.}
60.//插入节点
61.private void insertChild(Node node, int z) {
62. boolean added = false;
63. for (int i = 0; i < this.mChildren.size(); ++i) {
64. Node child = (Node) this.mChildren.get(i);
65. if (child.getZOrder() > z) {
66. added = true;
67. this.mChildren.add(i, node);
68. break;
69. }
70. }
71. if (!
added)
72. this.mChildren.add(node);
73. node.setZOrder(z);
74.}
75.//移除所有节点
76.public void removeAllChildren(boolean cleanup) {
77. for (int i = 0; i < this.mChildren.size(); ++i) {
78. Node child = (Node) this.mChildren.get(i);
79. if (cleanup) {
80. child.cleanup();
81. }
82. child.setParent(null);
83. }
84. this.mChildren.clear();
85.}
86.//移除节点
87.public void removeChild(Node child, boolean cleanup) {
88. if (child == null) {
89. return;
90. }
91. if (this.mChildren.contains(child))
92. detachChild(child, cleanup);
93. }
94.public void removeChild(int tag, boolean cleanup) {
95. assert (tag !
= INVALID_TAG);
96. Node child = getChild(tag);
97. if (child == null)
98. Log.w("Engine", "removeC
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 02 OPhone 平台 游戏 引擎 实现 场景 元素