cocos2d-x开发: 场景实体(entity)管理
公司现在开新项目,主题的框架部分都是我自己在做,不用受到别人的牵制,所以还算是比较的自由,很好发挥. 游戏并不大,所以需要用到的地方并不多.今天花了一些时间写了场景entity管理的部分代码,还没有完全的完善.
我的思路是这样的, entity manager提供注册一个update( dt )的帧频回调事件, 在每一次回调的时候都会遍历管理的所有的entity,调用entity的update( dt )帧频回调.何为帧频回调?我解释一下,cocos2d-x在c++那边是可以重写onDraw()方法实现的.在lua这边的话,如果是从cocos2d::node派生的子类,也就是使用 node = class( "node", function() return cc.Node:create() end),然后也可以注册schedulexxxx系列的方法,具体的可以去参考源码.由于我的管理类和entity都不是node派生的,所以我在提供了管理类的update( dt )回调,通过cc.Director:getInstance():getScheduler(),然后调用scheduler:scheduleScripteFunc( function(dt) end,0,false )方式实现的.
管理类的源码如下:
local entity_manager = class( "entity_manager", nil ) function entity_manager:ctor()
self.entity_list_ = {}
self.entity_nums_ =
end function entity_manager:register_entity( entity )
if entity == nil then
return
end local entity_rd
entity_rd = self.entity_nums_ +
entity:set_runtime_id( entity_rd ) table.insert( self.entity_list_, entity )
self.entity_nums_ = entity_rd
end function entity_manager:remove_entity( entity )
if entity == nil then
return
end local entity_rd
entity_rd = entity:get_runtime_rd() if entity_rd > self.entity_nums_ then
return
end
entity:remove_from_node()
table.remove( self.entity_list_, entity_rd )
for index, v_t in ipairs( self.entity_list_ ) do
if index >= entity_rd then
v_t:set_runtime_id( index )
end
end
self.entity_nums_ = #self.entity_list_
end function entity_manager:update( dt )
for _, v_t in ipairs( self.entity_list_ ) do
v_t:update( dt )
end
end return entity_manager
提供一个概念就是runtime entity id, 我们不能保证一个类型只可以创建一个对象,这是不合理的,所以除了entity的uniqure_id之外,就提供了运行时候的id。由于这个运行时候的id是动态的,所以在remove的时候需要更新一下,也就是上面table.remove下面的操作。下面是entity的代码:
local entity = class( "entity", nil ) entity.debug_mode_ = true
entity.debug_color_ = cc.c4f( , , , )
entity.callback_list_ = {}
entity.runtime_id_ = nil function entity:set_debug_mode( mode )
self.debug_mode_ = mode
end function entity:get_debug_mode()
return self.debug_mode_
end function entity:set_debug_color( color )
self.debug_color_ = color
end function entity:get_debug_color()
return self.debug_color_
end function entity:set_runtime_id( runtime_id )
self.runtime_id_ = runtime_id
end function entity:get_runtime_id()
return self.runtime_id_
end function entity:register_callback( callback, target )
if callback == nil then
return
end for _, v_t in ipairs( self.callback_list_ ) do
if v_t[] == callback and v_t[] == target then
return
end
end table.insert( self.callback_list_, { callback, target } )
end function entity:remove_callback( callback, target )
if callback == nil then
return
end for index, v_t in ipairs( self.callback_list_ ) do
if v_t[] == callback and v_t[] == target then
table.remove( self.callback_list_, index )
end
end
end function entity:update( dt )
local callback
local target
for _, v_t in ipairs( self.callback_list_ ) do
callback = v_t[]
target = v_t[]
if target ~= nil then
callback( target, dt )
else
callback( dt )
end
end
end return entity
这是一个基类的实现,不算复杂,也很好理解,就没什么好说的了.为什么要写一个entity base类呢? 因为现在的项目可能会用序列帧,也可能会用骨骼动画. 如果是骨骼动画的话,那么所有的action都比较好处理, bounding_box也很好获得.相应的接口就是
armature:getAnimation():play( action_const_name ) local bounding_box = armature:getBoundingBox()
然后就可以很简单的使用AABB或者是OBB进行碰撞检测以及设置AI等这些杂七杂八的东西了. 我们都知道序列帧使用的时候就没有那么方便了,在面对.png和Plist这样的组合的时候,动作的处理需要自己去解析,而Boundingbox也需要根据当前执行的动作做状态监测.好吧,废话说的有点多了,我简单实现了部分骨骼实体的封装,代码如下:
local entity = require "src.firework.entity.entity" local skeleton_entity = class( "skeleton_entity", entity ) function skeleton_entity:ctor( armature_const_name )
self.skeleton_armature_ = nil
self.draw_debug_node_ = nil self.skeleton_armature_ = ccs.Armature:create( armature_const_name )
self.draw_debug_node_ = cc.DrawNode:create()
self.skeleton_armature_:addChild( self.draw_debug_node_ )
self:init_callbacks()
end function skeleton_entity:play( const_action_name )
self.skeleton_armature_:getAnimation():play( const_action_name )
end function skeleton_entity:init_callbacks()
self:register_callback( self.draw_debug_bounding_box, self )
end function skeleton_entity:set_anchor_point( anchor_point )
self.skeleton_armature_:setAnchorPoint( anchor_point )
end function skeleton_entity:get_anchor_point()
return self.skeleton_armature_:getAnchorPoint()
end function skeleton_entity:set_position( position )
self.skeleton_armature_:setPosition( position )
end function skeleton_entity:get_position()
return self.skeleton_armature_:getPosition()
end function skeleton_entity:add_to_node( node )
node:addChild( self.skeleton_armature_ )
end function skeleton_entity:remove_from_node()
self:remove_callback( self.draw_debug_bounding_box, self )
self.skeleton_armature_:getParent():removeChild( self.skeleton_armature_ )
end function skeleton_entity:get_bounding_box()
return self.skeleton_armature_:getBoundingBox()
end function skeleton_entity:draw_debug_bounding_box( dt )
local bounding_box = self:get_bounding_box()
local lb = self.skeleton_armature_:convertToNodeSpace( cc.p( bounding_box.x, bounding_box.y ) )
local lt = self.skeleton_armature_:convertToNodeSpace( cc.p( bounding_box.x, bounding_box.y + bounding_box.height ) )
local rt = self.skeleton_armature_:convertToNodeSpace( cc.p( bounding_box.x + bounding_box.width, bounding_box.y + bounding_box.height ) )
local rb = self.skeleton_armature_:convertToNodeSpace( cc.p( bounding_box.x + bounding_box.width, bounding_box.y ) ) self.draw_debug_node_:clear()
self.draw_debug_node_:drawLine( lb, lt, self:get_debug_color() )
self.draw_debug_node_:drawLine( lt, rt, self:get_debug_color() )
self.draw_debug_node_:drawLine( rt, rb, self:get_debug_color() )
self.draw_debug_node_:drawLine( rb, lb, self:get_debug_color() )
end return skeleton_entity
我只是单纯的去获取创建Armatrue骨骼,没有加载资源,因为资源加载部分肯定是单独做的,这里只是顺便提一下.现在实现的部分代码只是单纯的画出了Boundingbox的区域,其他的还都没做.下面给出我的unittest部分的源码:
local test_case = require "src.unittest.test_case"
local skeleton_entity = require "src.firework.entity.skeleton_entity"
local entity_manager = require "src.firework.entity.entity_manager"
local visible_rect = require "src.firework.visible_rect"
local test_entity_manager_case = class( "test_entity_manager_case", test_case ) local scene = cc.Scene:create()
if cc.Director:getInstance():getRunningScene() then
cc.Director:getInstance():replaceScene( scene )
else
cc.Director:getInstance():runWithScene( scene )
end function test_entity_manager_case:run_impl()
ccs.ArmatureDataManager:getInstance():addArmatureFileInfo( "Hero/Hero0.png", "Hero/Hero0.plist", "Hero/Hero.ExportJson" )
local entity_manager_ins = entity_manager.new()
local skeleton_entity_ins = skeleton_entity.new( "Hero" )
skeleton_entity_ins:play( "loading" )
skeleton_entity_ins:set_anchor_point( cc.p( 0.5, 0.5 ) )
skeleton_entity_ins:set_position( visible_rect:center() )
skeleton_entity_ins:add_to_node( scene ) ccs.ArmatureDataManager:getInstance():addArmatureFileInfo( "tauren/tauren0.png", "tauren/tauren0.plist", "tauren/tauren.ExportJson" )
local skeleton_entity_ins1 = skeleton_entity.new( "tauren" )
skeleton_entity_ins1:play( "loading" )
skeleton_entity_ins1:set_anchor_point( cc.p( 0.5, 0.5 ) )
skeleton_entity_ins1:set_position( visible_rect:right_center() )
skeleton_entity_ins1:add_to_node( scene ) entity_manager_ins:register_entity( skeleton_entity_ins )
entity_manager_ins:register_entity( skeleton_entity_ins1 ) local scheduler = cc.Director:getInstance():getScheduler()
scheduler:scheduleScriptFunc( function ( dt )
entity_manager_ins:update( dt )
end, , false )
end return test_entity_manager_case
把这些代码加到test_controller中就好了.代码如下:
local fmt_logger = require "src.firework.fmt_logger" local test_controller = class( "test_controller", nil ) function test_controller:ctor()
fmt_logger.trace("---------------------------------------------------------")
fmt_logger.info(" running mode: [" .. self.__cname .. "] ")
end function test_controller:run() require "src.unittest.test_case"
get_test_case_sample().new():run() local test_fmt_logger_case = require "src.unittest.firework.test_fmt_logger_case"
test_fmt_logger_case.new():run() local test_default_dispatcher_case = require "src.unittest.firework.test_default_dispatcher_case"
test_default_dispatcher_case.new():run() local test_g_firework_case = require "src.unittest.firework.test_g_firework_case"
test_g_firework_case.new():run() local test_event_dispatcher_case = require "src.unittest.firework.test_event_dispatcher_case"
test_event_dispatcher_case.new():run() local test_measure_manager_case = require "src.unittest.firework.test_measure_manager_case"
test_measure_manager_case.new():run() local test_layer_update_case = require "src.unittest.firework.test_layer_update_case"
--test_layer_update_case.new():run() local test_entity_manager_case = require "src.unittest.firework.test_entity_manager_case"
test_entity_manager_case.new():run() end return test_controller
unittest这一套是我自己写的,只是为了自己用着方便, 如果需要知道如何实现的,请去参考前面文章.我在写代码分离模块的时候写过这部分的代码.
cocos版本是cocos2d-x 3.3 final. 如果是用 <3.3版本或者是2.x版本,相信修改少量的代码就可以了.就到这里了。
2015-1-30 修正: 将entity.lua 属性初始化放到一个entity:_init()函数中.我不知道看过这篇文章的人有没有看出问题. 这里的问题是update调用的callback_list_将包含所有entity派生子类的callback函数。 导致在一次update过程中执行的速度非常的慢.我原本以为是gl渲染速度的问题.
虽然我开启debug模式打印boundingbox,但是跑到100就会很卡. 果断是发现entity:update()中执行有问题. 所以做了一次调整之后轻松跑到两百个骨骼同时在一个场景中渲染.而且我还开启了debug gl绘制模式.
cocos2d-x开发: 场景实体(entity)管理的更多相关文章
- SharePoint Server 2013开发之旅(一):新的开发平台和典型开发场景介绍
我终于开始写这个系列文章,实际上确实有一段时间没有动笔了.最近重新安装了一套SharePoint Server 2013的环境,计划利用工作之余的时间为大家写一点新的东西. SharePoint Se ...
- JavaScript应用于asp开发场景
JavaScript应用于asp开发场景 演示代码示例: <%Path="../"%> <!--#include file="../../Inc/Con ...
- EBS OAF开发中实体对象和视图对象的属性设置器
EBS OAF开发中实体对象和视图对象的属性设置器 (版权声明.本人原创或者翻译的文章如需转载,如转载用于个人学习,请注明出处:否则请与本人联系,违者必究) 源文: Home > Oracle ...
- winform快速开发平台 -> 通用权限管理之动态菜单
这几个月一直忙APP的项目,没来得及更新项目,想想该抽出时间整理一下开发思路,跟大家分享,同时也希望得到宝贵的建议. 先说一下我们的权限管理的的设计思路,首先一个企业信息化管理系统一定会用到权限管理, ...
- CozyRSS开发记录14-RSS源管理初步完工
CozyRSS开发记录14-RSS源管理初步完工 1.添加源的响应 DialogHost.Show有几个版本的重载,加一个DialogClosingEventHandler参数.我们让添加源对话框的添 ...
- CozyRSS开发记录10-RSS源管理
CozyRSS开发记录10-RSS源管理 1.RSS源树结构 做解析体力活很多,把RSS解析的优化先放放,先玩一玩RSS源的管理. 虽然在初步的设计中,RSS源是以一个列表的方式来展示,但是,我觉得如 ...
- RDIFramework.NET V2.8版本 ━ 开发实例之产品管理(WinForm)
RDIFramework.NET V2.8版本 ━ 开发实例之产品管理(WinForm) 现在,我们使用.NET快速开发整合框架(RDIFramework.NET)来开发一个应用,此应用皆在说明如何使 ...
- SNF开发平台WinForm之四-开发-主细表管理页面-SNF快速开发平台3.3-Spring.Net.Framework
4.1运行效果: 4.2开发实现: 4.2.1 有了第一个程序的开发,代码生成器的配置应该是没有问题了,我们只要在对应的数据库中创建我们需要的表结构就可以了,如下: 主表结构如下: ...
- SNF开发平台WinForm之二-开发-单表表单管理页面-SNF快速开发平台3.3-Spring.Net.Framework
2.1运行效果: 2.2开发实现: 2.2.1 这个开发与第一个开发操作步骤是一致的,不同之处就是在生成完代码之后,留下如下圈红程序,其它删除. 第一个开发地址:开发-单表表格编辑管理页面 http: ...
随机推荐
- iOS开发之静态库(一)—— 基本概念
在项目开发过程中,经常出现优秀代码重用现象,又或者提供给第三方功能模块却又不想让其看到源代码,这些时候,通常的做法是将代码封装成库或者框架,这些在Windows编程或Linux编程中非常容易实现的过程 ...
- iOS开发——高级技术&内购服务
内购服务 大家都知道做iOS开发本身的收入有三种来源:出售应用.内购和广告.国内用户通常很少直接 购买应用,因此对于开发者而言(特别是个人开发者),内购和广告收入就成了主要的收入来源.内购营销模式,通 ...
- forword/ sendRediect
res.sendRedirect(),是重定向,相当于两次请求,两次相应,地址栏会发生变化. 在实际使用中,重定向不能传指.也就是在requset中储存的值在跳转到另外一个页面后,在目标页面提取不出来 ...
- plsql无法连接64位oracle数据库的解决方法(图文解说)
oracle11g下载页面:http://www.oracle.com/technetwork/database/enterprise-edition/downloads/index.html,找到适 ...
- /etc/pam.d 与 /etc/security 密码策略
PAM(Pluggable Authentication Modules )是由Sun提出的一种认证机制.它通过提供一些动态链接库和一套统一的API,将系统提供的服务 和该服务的认证方式分开,使得系统 ...
- 混合使用Jquery Deferred和Angularjs的$timeout(转)
原文链接:http://my.oschina.net/gejiawen0913/blog/174826 <!DOCTYPE html> <html ng-app="Demo ...
- mac电脑如何不生成.DS_STORE文件
执行这个命令,然后重启 defaults write com.apple.desktopservices DSDontWriteNetworkStores true
- C++ 记事本: 变量
C++ 变量也许和其他语言的变量没有什么差别.就是用来存储一些可能会变值的容器. 当然 C++ 变量里又分为 原子类型 的(int , char ,bool 等等),复合类型 的(struct ,cl ...
- 分享自己写的一个小工具RGB转十六进制(高手勿喷)
由于工作经常美工给的颜色是rgb,而我们网页里面是16进制.网上也有很多类型的工具.不过似乎都用浏览器打开.没网就不爽了 实现也很简单.代码已经共享了 http://git.oschina.net/w ...
- Naked Search in service
public List<TplRelease> searchTplReleaseById(TplRelease tr)throws Exception{ DBOperator dbo = ...