cocos2dx 3d开源项目 fantasyWarrior3D 从零走起 3 [BattleScene]

来源:互联网 发布:sybase数据库建表语句 编辑:程序博客网 时间:2024/06/05 03:36
还是从构造函数看起
function BattleScene.create()

 1. setCamera()

 (1) 创建相机

 
 camera = cc.Camera:createPerspective(60.0, size.width/size.height, 10.0, 4000.0)
 
 cpp中的声明:
     /**
    * Creates a perspective camera.
    *
    * @param fieldOfView The field of view for the perspective camera (normally in the range of 40-60 degrees).
    * @param aspectRatio The aspect ratio of the camera (normally the width of the viewport divided by the height of the viewport).
    * @param nearPlane The near plane distance.
    * @param farPlane The far plane distance.
    */
    static Camera*    createPerspective(float fieldOfView, float aspectRatio, float nearPlane, float farPlane);


参数:
[1] fieldOfView    相机视野 0~p (即 0~180 度) 之间
[2] aspectRatio  屏幕的高宽比
[3] nearPlane 第三个参数是与近平面的距离,请注意距离比与近平面还近的内容都不会呈现在游戏窗口中
[4] farPlane 第四个参数是与远平面的距离,请注意距离比与远平面还远的内容都不会呈现在游戏窗口中。


 把camera对象添加到scene但中即可替代默认的camera(方向向量与 x,y 平面垂直)
 currentLayer:addChild(camera)
 

(2) 在相机上面加了ui层

camera:addChild(uiLayer)
那么看看uilayer的实现  initUILayer()


uiLayer:setPositionZ(cc.Director:getInstance():getZEye()/4)
getZEye() 获取到近平面的距离
z坐标设为负值应该是因为: uiLayer被添加为了camera对象的子节点,它相对于"近平面"更靠近"摄像机"
 uiLayer:setGlobalZOrder(3000)  确保ui盖在最上面

2. gameController() 


每一帧的游戏状态更新

(1) moveCamera 摄像机的移动

getFocusPointOfHeros()  获取所有英雄的平均位置,即作为相机的焦点位置


[1] 自动随角色位移
local temp = cc.pLerp(cameraPosition, 
    cc.p(focusPoint.x+cameraOffset.x, cameraOffset.y + focusPoint.y-size.height*3/4), 
    2*dt)

让camera 和 FocusPoint 的y坐标保持一致

[2] 特写效果  
实际上是在specialCamera.valid被置为true的几秒内,临时改变了 camera位置的朝向(lookAt)的计算方式

特效的时候蒙一层深灰色 

currentLayer:setColor(cc.c3b(125, 125, 125))--deep grey

界面级联(Cascade)
currentLayer:setCascadeColorEnabled(true)
子节点能够随着父节点的颜色改变而改变


[3] 玩家滑动改变 摄像机的偏移量
在 onTouchMoved函数中设置cameraOffset的值来实现
cameraOffset = cc.pGetClampPoint(cc.pSub(cameraOffset, delta),cameraOffsetMin,cameraOffsetMax)
因为是像滑动的反方向,所以是sub
通过pGetClampPoint限制位移的max 和 min


(2) updateParticlePos() 

 让例子效果根据随角色移动
 

(3) gameMaster:update(dt)  

负责刷怪,刷新对话框,提示 等等,这里就不去深究了

(4) collisionDetect 碰撞检测

由Manager.lua 来维护

(5) solveAttacks 伤害计算

由attackCommand来维护

 3. createBackground()

 创建场景
 

(1) 地面  

spriteBg:setPosition3D(cc.V3(-2300,-1000,0))  ? 暂时没搞清怎么来的,作者又不给个c3b的原文件


(2) cc.Water:create 水的实现

在Water.cpp中

 

 4. 注册了一些消息处理函数


 逻辑对象层(骑士,法师,弓箭手)通过发送消息的方式来和UI层交互
(1) 扣血 BLOOD_MINUS
 MessageDispatchCenter:registerMessage(MessageDispatchCenter.MessageType.BLOOD_MINUS, bloodMinus)
(2) 怒气值  ANGRY_CHANGE
(3) 放技能的时候给个特写 SPECIAL_PERSPECTIVE 
(4)UIcontainsPoint(position)
这个函数描述了UI层与逻辑对象的交互

 
 参考:
 3D 游戏控制
 http://blog.csdn.net/minsenwu/article/details/17120495
unity3d camera
http://game.ceeger.com/Script/Camera/Camera.fieldOfView.html
分析《FantasyWarrior3D》结构 
http://cn.cocos2d-x.org/tutorial/show?id=1798
0 0
原创粉丝点击