Simulate cocos engine in nodejs


#1

Hi, follow @jare 's instruction on the topic : Cocos engine in node.js , i manage to simulate the cocos engine node’s action in nodejs server :

global.Editor = {
    isMainProcess: true,
	versions:{}
};
process.versions.electron = "";
require("./engine-master");

require("./engine-master/cocos2d");
require("./engine-master/cocos2d/actions");
require("./engine-master/cocos2d/core/CCScheduler");
require("./engine-master/cocos2d/core/CCNode");
require("./engine-master/cocos2d/core/renderer");

cc.director = new cc.Director();
cc.director.init();
cc.renderer = cc.rendererCanvas;

var new_node = new cc.Node();
new_node.setPosition(cc.p(1280,720));
new_node.runAction(new cc.MoveTo(0.5,cc.p(640,360)));


var game_time = (new Date()).getTime();
var game_elapse = 0;
cc.director.getActionManager().update(0);
var Timer = setInterval(function(){
	var d = (new Date()).getTime();
	var dt = (d-game_time)/1000;
	game_elapse+=dt;
	game_time = d;
	cc.director.getActionManager().update(dt);
	
},1000/60);

The node position is update correctly but i feel like i did the wrong way. All i did is pre-define some object all just to make the node’s action work. The code is so ugly and doesnt feeling right. I think i should instantiace a game scene, a scheduler , v.v… i mean the correct way in cocos creator is having right now to handle a game.

Sorry it’s too vaguely. But if u think i have do it wrong please correct me please.


#2

Great work :grin:


#3

Hi what is the properly way cocos use in a scene could u teach me that? I mean the minimum setup to handle a node?

Example, sorry for the word example, i mean i thought there is something like a diagram about the system like this:
A root node extend a cc.node and act as the game area, which contains many child node.
A director which control object such as scheduler, timer, action …

I dont know how to describe that clearly. I want to know how system work so i can code the server the correct way


#4

Please keep updating. It is a very interesting idea. Thanks for sharing!


#5

That would be very helpful for every Creator developer.


#6

I think it is bad idea to simulate cocos in node as it will require a lot of power for server? Better then to use simple javascript for development.


#7

That’s why i ask for the minimum setup to make it work. Otherwise u must learn the way cocos engine work to make your own system.


#8

Im now using this setup in my server, it can create node, run action , control node’s params, handle collider event, that’s all i need to run in server. It can also run on multiple “game room” if u create a node for each room, no cross collide event between room because each room have its own collider manager. The only thing i think i need but i cant do now is different game FPS.

Btw here it is, first i init the engine for global :

/** cocos **/
		global.Editor = {
			isMainProcess: true,
			versions:{},
			log:function(str){
				console.log(str);
			}
		};
		process.versions.electron = "";
		require("./engine-master");
		global.window = {
			addEventListener:function(str,cb,b){
				cb();
			},
			removeEventListener:function(){},
			setTimeout:function(cb,time_out){
				setTimeout(cb,time_out);
			}
		};
		//simulate document variable
		global.document = {};
		//predefine cc.sys.capabilities variable
		cc.sys.capabilities = {
				"canvas": false,
				"opengl": false,
				"webp": false
			};
		require("./engine-master/CCBoot");
		//console.log("cc.ENGINE_VERSION",cc.ENGINE_VERSION);
		cc.game._initRenderer = function(){};
		//Config here
		cc.initEngine({
			width: 1280,
			height: 720,
			debugMode: 0,
			exposeClassName: false,//faster class intantiate
			showFPS: false,
			frameRate: 60,
			//id: "kem_game_portal",//web only
			//renderMode: 0,//web only
			//registerSystemEvent: false,???
			//scenes: "scenes",?? maybe no need
			groupList : ['Bullet', 'Creep'],
			collisionMatrix:[[false, true], [true, false]]
		},function(){
			cc._engineLoaded = true;
		});
		cc._initDebugSetting(0);
		require("./engine-master/cocos2d");
		require("./engine-master/cocos2d/actions");
		require("./engine-master/cocos2d/core/CCScheduler");
		require("./engine-master/cocos2d/core/CCNode");
		require("./engine-master/cocos2d/core/components/CCComponent");
		require("./engine-master/cocos2d/core/renderer");
		require("./engine-master/cocos2d/core/collider");
		//No Render
		cc.renderer = {clear:function(){},rendering:function(){},pushDirtyNode:function(){}};
		//creating a director instance
		//cc.director = cc.Director._getInstance();
		cc.director = new cc.Director();
		cc.director.init();
		//Enable collision manager
		cc.director.getCollisionManager().enabled = true;
		//Rewrite Main Loop like in normal game ( not editor )
		cc.director.mainLoop = function () {
			this.calculateDeltaTime();
			if (!this._paused) {
				this.emit(cc.Director.EVENT_BEFORE_UPDATE);
				this._compScheduler.startPhase();
				this._compScheduler.updatePhase(this._deltaTime);
				this._scheduler.update(this._deltaTime);
				this._compScheduler.lateUpdatePhase(this._deltaTime);
				this.emit(cc.Director.EVENT_AFTER_UPDATE);
				cc.Object._deferredDestroy();
			}
		}
		//another defined variable in nodejs environment
		cc.engine = {isPlaying:true,emit:function(){},attachedObjsForEditor:[]};
		_Scene = window._Scene || {};
		beFalse = function(){return false;}
		_Scene.DetectConflict = {
			beforeAddChild: beFalse,
			afterAddChild: beFalse,
			checkConflict_Layout: beFalse,
			checkConflict_Widget: beFalse,
		};
		//Running cocos
		cc.game.run(); 

And then in each “game room” in server, i create it own collider manager and call schedule update that manager:

                    this.GameArea = new cc.Node();//i will add node to this node so can easily destroy all node if room close
			this.GameCollisionManager = new cc.CollisionManager();
			cc.director.getScheduler().scheduleUpdate(this.GameCollisionManager, cc.Scheduler.PRIORITY_SYSTEM, "game collision manager");		
			this.GameCollisionManager.enabled = true;