Cocos2d-x development: entity Management

Source: Internet
Author: User

Cocos2d-x development: entity Management
The company is now launching new projects, and the framework of the theme is all done by myself, without being restrained by others, so it is still relatively free to play well. the game is not big, so there are not many places to use. today, I spent some time writing some code for scenario entity Management, which is not completely complete yet. my idea is this: entity manager registers an update (dt) Frame Rate callback event, and traverses all the entity managed during each callback, call the update (dt) Frame Rate callback of entity. what is frame rate callback? I will explain that the cocos2d-x can override the onDraw () method in c ++. on the lua side, if it is a subclass derived from cocos2d: node, that is, use node = class ("node", function () return cc. node: create () end), and then you can register the schedulexxxx series methods. For details, refer to the source code. because neither my management class nor entity is node-derived, I provide the update (dt) callback for the management class through cc. director: getInstance (): getScheduler (), and then calls scheduler: scheduleScripteFunc (function (dt) end, 0, false. the management class source code is as follows: 1 local entity_manager = class ("entity_manager", nil) 2 3 function entity_manager: ctor () 4 self. entity_list _ = {} 5 self. entity_nums _ = 0 6 end 7 8 function entity_manager: register_entity (entity) 9 if entity = nil then10 return11 end12 13 local entity_rd14 entity_rd = self. entity_nums _ + 115 entity: set_runtime_id (entity_rd) 16 17 table. insert (self. entity_list _, entity) 18 self. entity_nums _ = entity_rd19 end20 21 function entity_manager: remove _ Entity (entity) 22 if entity = nil then23 return24 end25 26 local entity_rd27 entity_rd = entity: get_runtime_rd () 28 29 if entity_rd> self. entity_nums _ then30 return31 end32 entity: remove_from_node () 33 table. remove (self. entity_list _, entity_rd) 34 for index, v_t in ipairs (self. entity_list _) do35 if index> = entity_rd then36 v_t: set_runtime_id (index) 37 end38 end39 self. entity_nums _ = # self. e Ntity_list_40 end41 42 function entity_manager: update (dt) 43 for _, v_t in ipairs (self. entity_list _) do44 v_t: update (dt) 45 end46 end47 48 return entity_manager provides a concept that is runtime entity id. We cannot guarantee that only one object can be created for a type, which is unreasonable, therefore, besides the uniqure_id of the entity, the runtime id is provided. Because the id during this operation is dynamic, you need to update it when removing it, that is, the operation under table. remove above. The following is the entity code: 1 local entity = class ("entity", nil) 2 3 entity. debug_mode _ = true 4 entity. debug_color _ = cc. c4f (0, 1, 0, 1) 5 entity. callback_list _ = {} 6 entity. runtime_id _ = nil 7 8 function entity: set_debug_mode (mode) 9 self. debug_mode _ = mode10 end11 12 function entity: get_debug_mode () 13 return self. debug_mode_14 end15 16 function entity: set_debug_color (color) 17 self. debug_color _ = col Or18 end19 20 function entity: get_debug_color () 21 return self. debug_color_22 end23 24 function entity: set_runtime_id (runtime_id) 25 self. runtime_id _ = runtime_id26 end27 28 function entity: get_runtime_id () 29 return self. runtime_id_30 end31 32 function entity: register_callback (callback, target) 33 if callback = nil then34 return35 end36 37 for _, v_t in ipairs (self. callback_list _) do38 if v_t [1] = callback and v_t [2] = target then39 return40 end41 end42 43 table. insert (self. callback_list _, {callback, target}) 44 end45 46 function entity: remove_callback (callback, target) 47 if callback = nil then48 return49 end50 51 for index, v_t in ipairs (self. callback_list _) do52 if v_t [1] = callback and v_t [2] = target then53 table. remove (self. callback_list _, index) 54 end55 end56 end57 58 function entity: update (dt) 59 local callback60 local target61 for _, v_t in ipairs (self. callback_list _) do62 callback = v_t [1] 63 target = v_t [2] 64 if target ~ = Nil then65 callback (target, dt) 66 else67 callback (dt) 68 end69 end70 end71 72 return entity this is a base class implementation that is not complex and well understood, there is nothing to say. why write an entity base class? Because the current project may use sequence frames or bone animations. if it is a skeleton animation, all actions are well processed and bounding_box is also well obtained. the corresponding interface is 1 armature: getAnimation (): play (action_const_name) 2 3 local bounding_box = armature: getBoundingBox () then we can easily use AABB or OBB for collision detection and set up AI and other miscellaneous things. we all know that the use of sequence frames is not so convenient. In the combination of .png and Plist, action processing needs to be resolved by ourselves, while Boundingbox also needs to perform status monitoring based on the currently executed action. well, it's a bit more nonsense. I have simply implemented the encapsulation of some skeleton entities. The Code is as follows: 1 local entity = require "src. firework. entity. enti Ty "2 3 local skeleton_entity = class (" skeleton_entity ", entity) 4 5 function skeleton_entity: ctor (armature_const_name) 6 self. skeleton_armature _ = nil 7 self. draw_debug_node _ = nil 8 9 self. skeleton_armature _ = ccs. armature: create (armature_const_name) 10 self. draw_debug_node _ = cc. drawNode: create () 11 self. skeleton_armature _: addChild (self. draw_debug_node _) 12 self: init_callbacks () 13 end 14 15 function skeleton_entity: play (const_action_name) 16 self. skeleton_armature _: getAnimation (): play (const_action_name) 17 end18 19 function skeleton_entity: init_callbacks () 20 self: register_callback (self. draw_debug_bounding_box, self) 21 end22 23 function skeleton_entity: set_anchor_point (anchor_point) 24 self. skeleton_armature _: setAnchorPoint (anchor_point) 25 end26 27 function skeleton_entity: g Et_anchor_point () 28 return self. skeleton_armature _: getAnchorPoint () 29 end30 31 function skeleton_entity: set_position (position) 32 self. skeleton_armature _: setPosition (position) 33 end34 35 function skeleton_entity: get_position () 36 return self. skeleton_armature _: getPosition () 37 end38 39 function skeleton_entity: add_to_node (node) 40 node: addChild (self. skeleton_armature _) 41 end42 43 function skel Eton_entity: remove_from_node () 44 self: remove_callback (self. draw_debug_bounding_box, self) 45 self. skeleton_armature _: getParent (): removeChild (self. skeleton_armature _) 46 end47 48 function skeleton_entity: get_bounding_box () 49 return self. skeleton_armature _: getBoundingBox () 50 end51 52 function skeleton_entity: draw_debug_bounding_box (dt) 53 local bounding_box = self: get_bounding_box () 54 local lb = Self. skeleton_armature _: convertToNodeSpace (cc. p (bounding_box.x, bounding_box.y) 55 local lt = self. skeleton_armature _: convertToNodeSpace (cc. p (bounding_box.x, bounding_box.y + bounding_box.height) 56 local rt = self. skeleton_armature _: convertToNodeSpace (cc. p (bounding_box.x + bounding_box.width, bounding_box.y + bounding_box.height) 57 local rb = self. skeleton_armature _: convertToNodeSpac E (cc. p (bounding_box.x + bounding_box.width, bounding_box.y) 58 59 self. draw_debug_node _: clear () 60 self. draw_debug_node _: drawLine (lb, lt, self: get_debug_color () 61 self. draw_debug_node _: drawLine (lt, rt, self: get_debug_color () 62 self. draw_debug_node _: drawLine (rt, rb, self: get_debug_color () 63 self. draw_debug_node _: drawLine (rb, lb, self: get_debug_color () 64 end65 66 return skeleton_entit Y: I just went to get the Armatrue skeleton and didn't load the resource. Because the resource loading part must be done separately, I just mentioned it here. some of the Code implemented now only draws the Boundingbox area, and nothing else has been done yet. the following is the source code of my unittest Section: 1 local test_case = require "src. unittest. test_case "2 local skeleton_entity = require" src. firework. entity. skeleton_entity "3 local entity_manager = require" src. firework. entity. entity_manager "4 local visible_rect = require" src. firework. visible_rect "5 local test_entity_manager _ Case = class ("test_entity_manager_case", test_case) 6 7 local scene = cc. scene: create () 8 if cc. director: getInstance (): getRunningScene () then 9 cc. director: getInstance (): replaceScene (scene) 10 else11 cc. director: getInstance (): runWithScene (scene) 12 end13 14 function test_entity_manager_case: run_impl () 15 ccs. armatureDataManager: getInstance (): addArmatureFileInfo ("Hero/Hero0.png", "Hero/Hero0.pl Ist "," Hero/Hero. exportJson ") 16 local entity_manager_ins = entity_manager.new () 17 local skeleton_entity_ins = primary (" Hero ") 18 primary: play (" loading ") 19 primary: set_anchor_point (cc. p (0.5, 0.5) 20 skeleton_entity_ins: set_position (visible_rect: center () 21 skeleton_entity_ins: add_to_node (scene) 22 23 ccs. armatureDataManager: getInstance (): addArmatureFi LeInfo ("tauren/tauren0.png", "tauren/tauren0.plist", "tauren/tauren. exportJson ") 24 local region = skeleton_entity.new (" tauren ") 25 skeleton_entity_ins1: play (" loading ") 26 skeleton_entity_ins1: set_anchor_point (cc. p (0.5, 0.5) 27 skeleton_entity_ins1: set_position (visible_rect: right_center () 28 rows: add_to_node (scene) 29 30 entity_manager_ins: register_entity (s) Keleton_entity_ins) 31 entity_manager_ins: register_entity (skeleton_entity_ins1) 32 33 local schedty = cc. director: getInstance (): getScheduler () 34 schedunc: scheduleScriptFunc (function (dt) 35 entity_manager_ins: update (dt) 36 end, 0, false) 37 end38 39 return test_entity_manager_case Add the code to test_controller. the Code is as follows: 1 local fmt_logger = require "src. firework. fmt_logger "2 3 local test_controll Er = class ("test_controller", nil) 4 5 function test_controller: ctor () 6 fmt_logger.trace ("unknown") 7 fmt_logger.info ("running mode :[".. self. _ cname .. "]") 8 end 9 10 function test_controller: run () 11 12 require "src. unittest. test_case "13 get_test_case_sample (). new (): run () 14 15 local test_fmt_logger_case = require "src. unittest. firework. tes T_fmt_logger_case "16 test_fmt_logger_case.new (): run () 17 18 local test_default_dispatcher_case = require" src. unittest. firework. test_default_dispatcher_case "19 test_default_dispatcher_case.new (): run () 20 21 local test_g_firework_case = require" src. unittest. firework. test_g_firework_case "22 test_g_firework_case.new (): run () 23 24 local test_event_dispatcher_case = require" src. unittest. firework. test_ev Ent_dispatcher_case "25 test_event_dispatcher_case.new (): run () 26 27 local test_measure_manager_case = require" src. unittest. firework. test_measure_manager_case "28 test_measure_manager_case.new (): run () 29 30 local test_layer_update_case = require" src. unittest. firework. test_layer_update_case "31 -- test_layer_update_case.new (): run () 32 33 local test_entity_manager_case = require" src. unittest. firework. te St_entity_manager_case "34 test_entity_manager_case.new (): run () 35 36 end37 38 return test_controller unittest This set is written by myself, just for your convenience, if you need to know how to implement it, please refer to the previous article. I wrote this part of code when writing the code separation module. cocos version is cocos2d-x 3.3 final. if you are using <3.3 or 2. version x, I believe you can modify a small amount of code. that's it.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.