Skip to content

Commit

Permalink
feat(script): handle dispose script component
Browse files Browse the repository at this point in the history
add "dispose" event function;
pass run test;
  • Loading branch information
yyc-git committed Mar 26, 2019
1 parent 7d6e9a1 commit 2c53649
Show file tree
Hide file tree
Showing 15 changed files with 223 additions and 53 deletions.
2 changes: 1 addition & 1 deletion examples/config/setting.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"is_debug": false,
"is_debug": true,
"context": {
"alpha": true,
"depth": true,
Expand Down
36 changes: 31 additions & 5 deletions examples/script/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,35 @@
transform, state
);

var state =
api.setTransformLocalPosition(
transform, [x >= 20 ? -20 : x + 1, y, z], state
);

return state

if (x >= 20) {
var state = api.disposeGameObject(gameObject, state);

return state;
}
else {
var state =
api.setTransformLocalPosition(
transform, [x >= 20 ? -20 : x + 1, y, z], state
);

return state;
}


// var state =
// api.setTransformLocalPosition(
// transform, [x >= 20 ? -20 : x + 1, y, z], state
// );

// return state
};

function _dispose1(script, api, state) {
console.log("script: ", script);

return state;
};

function _addScript(box, state) {
Expand All @@ -102,6 +125,9 @@
},
"update": (script, api, state) => {
return _update1(script, api, state);
},
"dispose": (script, api, state) => {
return _dispose1(script, api, state);
}
}
);
Expand Down
4 changes: 4 additions & 0 deletions src/Index.re
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,8 @@ let disposeGameObjectBasicCameraViewComponent = GameObjectAPI.disposeGameObjectB

let addGameObjectBasicCameraViewComponent = GameObjectAPI.addGameObjectBasicCameraViewComponent;

let disposeGameObjectScriptComponent = GameObjectAPI.disposeGameObjectScriptComponent;

let addGameObjectScriptComponent = GameObjectAPI.addGameObjectScriptComponent;

let createGameObject = GameObjectAPI.createGameObject;
Expand Down Expand Up @@ -1092,6 +1094,8 @@ let disposeGameObjectBasicCameraViewComponent = GameObjectAPI.disposeGameObjectB

let addGameObjectBasicCameraViewComponent = GameObjectAPI.addGameObjectBasicCameraViewComponent;

let disposeGameObjectScriptComponent = GameObjectAPI.disposeGameObjectScriptComponent;

let addGameObjectScriptComponent = GameObjectAPI.addGameObjectScriptComponent;

let createGameObject = GameObjectAPI.createGameObject;
Expand Down
32 changes: 16 additions & 16 deletions src/api/GameObjectAPI.re
Original file line number Diff line number Diff line change
Expand Up @@ -58,23 +58,23 @@ let addGameObjectScriptComponent =
addScriptComponent(gameObject, component, state);
};

/* TODO
let disposeGameObjectScriptComponent =
(
gameObject: GameObjectPrimitiveType.gameObject,
component: component,
state: StateDataMainType.state,
) => {
WonderLog.Contract.requireCheck(
() =>
WonderLog.(
Contract.(Operators.(_checkGameObjectShouldAlive(gameObject, state)))
),
IsDebugMainService.getIsDebug(StateDataMain.stateData),
);
deferDisposeScriptComponent(. gameObject, component, state);
};
let disposeGameObjectScriptComponent =
(
gameObject: GameObjectPrimitiveType.gameObject,
component: component,
state: StateDataMainType.state,
) => {
WonderLog.Contract.requireCheck(
() =>
WonderLog.(
Contract.(Operators.(_checkGameObjectShouldAlive(gameObject, state)))
),
IsDebugMainService.getIsDebug(StateDataMain.stateData),
);
deferDisposeScriptComponent(. gameObject, component, state);
};

/* TODO
let unsafeGetGameObjectScriptComponent =
(
gameObject: GameObjectPrimitiveType.gameObject,
Expand Down
1 change: 1 addition & 0 deletions src/api/script/ScriptEventFunctionAPI.re
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ let createScriptEventFunctionData =
(jsObj: eventFunctionDataJsObj): eventFunctionData => {
init: _createScriptEventFunction(jsObj##init),
update: _createScriptEventFunction(jsObj##update),
dispose: _createScriptEventFunction(jsObj##dispose),
};
9 changes: 9 additions & 0 deletions src/job/utils/DisposeJobUtils.re
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ let _disposeComponents =
disposedDirectionLightArray,
disposedPointLightArray,
disposedMeshRendererComponentArray,
/* TODO test */
disposedScriptArray,
} = gameObjectRecord;
let state =
disposedBasicCameraViewArray
Expand Down Expand Up @@ -91,6 +93,13 @@ let _disposeComponents =
|> DisposeComponentGameObjectMainService.batchDisposeMeshRendererComponent(
state,
);

let state =
disposedScriptArray
|> DisposeComponentGameObjectMainService.batchDisposeScriptComponent(
state,
);

(
state,
geometryNeedDisposeVboBufferArr,
Expand Down
1 change: 1 addition & 0 deletions src/service/record/main/data/GameObjectType.re
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ type gameObjectRecord = {
mutable disposedDirectionLightArray: array(int),
mutable disposedPointLightArray: array(int),
mutable disposedMeshRendererComponentArray: array(int),
mutable disposedScriptArray: array(int),
mutable aliveUidArray: gameObjectAliveUidArray,
mutable geometryMap: gameObjectGeometryMap,
mutable transformMap: gameObjectTransformMap,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ let create = () => {
WonderCommonlib.ArrayService.createEmpty(),
disposedUidArrayForDisposeGeometryRemoveMaterial:
WonderCommonlib.ArrayService.createEmpty(),
disposedScriptArray: WonderCommonlib.ArrayService.createEmpty(),
disposedBasicCameraViewArray: WonderCommonlib.ArrayService.createEmpty(),
disposedTransformArray: WonderCommonlib.ArrayService.createEmpty(),
disposedTransformArrayForKeepOrder:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ let create = () => {
"unsafeGetScriptAttributeFieldValue": OperateScriptAttributeDataMainService.unsafeGetScriptAttributeFieldValue,
"setScriptAttributeFieldValue": OperateScriptDataMainService.setScriptAttributeFieldValue,
"unsafeGetScriptGameObject": ScriptAPI.unsafeGetScriptGameObject,
"unsafeGetGameObjectTransformComponent": GameObjectAPI.unsafeGetGameObjectTransformComponent,
"getTransformLocalPosition": TransformAPI.getTransformLocalPosition,
"setTransformLocalPosition": TransformAPI.setTransformLocalPosition,
"unsafeGetGameObjectTransformComponent": GameObjectAPI.unsafeGetGameObjectTransformComponent,
"disposeGameObject": GameObjectAPI.disposeGameObject,
};
19 changes: 6 additions & 13 deletions src/service/state/main/data/StateDataMainType.re
Original file line number Diff line number Diff line change
Expand Up @@ -196,20 +196,8 @@ and scriptAPIJsObj = {
(string, ScriptAttributeType.scriptAttribute) =>
ScriptAttributeType.scriptAttributeValue,
"unsafeGetScriptGameObject": (int, state) => int,
"unsafeGetGameObjectTransformComponent":
(
GameObjectPrimitiveType.gameObject,
state
) =>
int,
"setScriptAttributeFieldValue":
(
int,
string,
string,
ScriptAttributeType.scriptAttributeValue,
state
) =>
(int, string, string, ScriptAttributeType.scriptAttributeValue, state) =>
state,
"getTransformLocalPosition":
(transform, state) =>
Expand All @@ -229,6 +217,9 @@ and scriptAPIJsObj = {
state
) =>
state,
"unsafeGetGameObjectTransformComponent":
(GameObjectPrimitiveType.gameObject, state) => int,
"disposeGameObject": (GameObjectPrimitiveType.gameObject, state) => state,
}
and apiRecord = {
scriptAPIJsObj,
Expand Down Expand Up @@ -353,10 +344,12 @@ and eventFunctionDataJsObj = {
.
"init": Js.Nullable.t(eventFunction),
"update": Js.Nullable.t(eventFunction),
"dispose": Js.Nullable.t(eventFunction),
}
and eventFunctionData = {
init: eventFunction,
update: eventFunction,
dispose: eventFunction,
}
and scriptRecord = {
index: int,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ open ComponentMapService;

open BasicCameraViewType;

let batchGetScriptComponent = (uidArray: array(int), {gameObjectRecord}) =>
batchGetComponent(uidArray, gameObjectRecord.scriptMap);

let batchGetBasicCameraViewComponent =
(uidArray: array(int), {gameObjectRecord}) =>
batchGetComponent(uidArray, gameObjectRecord.basicCameraViewMap);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,18 @@ open ComponentType;
let _removeComponent = (uid: int, componentMap) =>
componentMap |> ComponentMapService.removeComponent(uid) |> Obj.magic;

let deferDisposeScriptComponent =
(. uid, component: component, {gameObjectRecord} as state) => {
...state,
gameObjectRecord: {
...gameObjectRecord,
scriptMap: _removeComponent(uid, gameObjectRecord.scriptMap),
disposedScriptArray:
state.gameObjectRecord.disposedScriptArray
|> ArrayService.push(component),
},
};

let deferDisposeBasicCameraViewComponent =
(. uid, component: component, {gameObjectRecord} as state) => {
...state,
Expand Down Expand Up @@ -173,6 +185,16 @@ let deferDisposeMeshRendererComponent =
},
};

let batchDisposeScriptComponent = (state, componentArray: array(component)) =>
/* ...state,
scriptRecord:
ComponentMapService.batchDisposeComponent(
scriptRecord,
DisposeScriptMainService.handleBatchDisposeComponent,
componentArray,
), */
DisposeScriptMainService.handleBatchDisposeComponent(componentArray, state);

let batchDisposeBasicCameraViewComponent =
({basicCameraViewRecord} as state, componentArray: array(component)) => {
...state,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,15 @@ let batchDispose =
state,
);

let state =
state
|> BatchGetComponentGameObjectMainService.batchGetScriptComponent(
uidArray,
)
|> DisposeComponentGameObjectMainService.batchDisposeScriptComponent(
state,
);

let state =
state
|> BatchGetComponentGameObjectMainService.batchGetBasicCameraViewComponent(
Expand All @@ -120,6 +129,7 @@ let batchDispose =
|> DisposeComponentGameObjectMainService.batchDisposeBasicCameraViewComponent(
state,
);

let state =
state
|> BatchGetComponentGameObjectMainService.batchGetPerspectiveCameraProjectionComponent(
Expand Down Expand Up @@ -180,6 +190,7 @@ let batchDispose =
|> DisposeComponentGameObjectMainService.batchDisposeObjectInstanceComponent(
state,
);

(
state,
geometryNeedDisposeVboBufferArr,
Expand Down
70 changes: 70 additions & 0 deletions src/service/state/main/script/DisposeScriptMainService.re
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
open StateDataMainType;

let isAlive = (script, {disposedIndexArray}: scriptRecord) =>
DisposeComponentService.isAlive(script, disposedIndexArray);

let _disposeData = (script, {scriptRecord} as state) => {
let {gameObjectMap, scriptEventFunctionDataMap, scriptAttributeMap} = scriptRecord;

{
...state,
scriptRecord: {
...scriptRecord,
gameObjectMap:
DisposeComponentService.disposeSparseMapData(script, gameObjectMap),
scriptEventFunctionDataMap:
scriptEventFunctionDataMap
|> WonderCommonlib.ImmutableSparseMapService.deleteVal(script),
scriptAttributeMap:
scriptAttributeMap
|> WonderCommonlib.ImmutableSparseMapService.deleteVal(script),
},
};
};

let _batchExecDisposeEventFunction = (scriptArray, {scriptRecord} as state) =>
state
|> OperateScriptEventFunctionDataMainService.execAllEventFunction(
OperateScriptEventFunctionDataMainService.getScriptAllDisposeEventFunctionData(
scriptArray,
state,
),
);

/* TODO test */
let handleBatchDisposeComponent =
(scriptArray: array(ComponentType.component), {scriptRecord} as state) => {
WonderLog.Contract.requireCheck(
() =>
WonderLog.(
Contract.(
Operators.(
DisposeComponentService.checkComponentShouldAliveWithBatchDispose(
scriptArray,
isAlive,
scriptRecord,
)
)
)
),
IsDebugMainService.getIsDebug(StateDataMain.stateData),
);

let {scriptRecord} as state =
_batchExecDisposeEventFunction(scriptArray, state);

let {disposedIndexArray} = scriptRecord;

scriptArray
|> WonderCommonlib.ArrayService.reduceOneParam(
(. state, script) => state |> _disposeData(script),
{
...state,
scriptRecord: {
...scriptRecord,
disposedIndexArray:
disposedIndexArray |> Js.Array.concat(scriptArray),
},
},
);
};
Loading

0 comments on commit 2c53649

Please sign in to comment.