Skip to content

Commit

Permalink
Avoid to iterate on inactive objects
Browse files Browse the repository at this point in the history
  • Loading branch information
D8H committed Nov 24, 2023
1 parent d34f1a6 commit 90fa872
Show file tree
Hide file tree
Showing 24 changed files with 1,021 additions and 794 deletions.
8 changes: 8 additions & 0 deletions Extensions/3D/Model3DRuntimeObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ namespace gdjs {
);
}

if (this.isNeedingLifecycleFunctions()) {
this.getLifecycleSleepState().wakeUp();
}

// *ALWAYS* call `this.onCreated()` at the very end of your object constructor.
this.onCreated();
}
Expand Down Expand Up @@ -194,6 +198,10 @@ namespace gdjs {
}
}

isNeedingLifecycleFunctions(): boolean {
return super.isNeedingLifecycleFunctions() || this._animations.length > 0;
}

update(instanceContainer: gdjs.RuntimeInstanceContainer): void {
const elapsedTime = this.getElapsedTime() / 1000;
this._renderer.updateAnimation(elapsedTime * this._animationSpeedScale);
Expand Down
20 changes: 8 additions & 12 deletions Extensions/Lighting/lightobstacleruntimebehavior.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
namespace gdjs {
declare var rbush: any;

export class LightObstaclesManager {
_obstacleRBush: any;
_obstacleRBush: RBush<LightObstacleRuntimeBehavior>;

constructor(instanceContainer: gdjs.RuntimeInstanceContainer) {
this._obstacleRBush = new rbush();
this._obstacleRBush = new RBush<LightObstacleRuntimeBehavior>();
}

/**
Expand Down Expand Up @@ -41,6 +39,9 @@ namespace gdjs {
* added before.
*/
removeObstacle(obstacle: gdjs.LightObstacleRuntimeBehavior) {
if (!obstacle.currentRBushAABB) {
return;
}
this._obstacleRBush.remove(obstacle.currentRBushAABB);
}

Expand All @@ -59,9 +60,9 @@ namespace gdjs {
// is not necessarily in the middle of the object (for sprites for example).
const x = object.getX();
const y = object.getY();
const searchArea = gdjs.staticObject(
const searchArea: SearchArea = gdjs.staticObject(
LightObstaclesManager.prototype.getAllObstaclesAround
);
) as SearchArea;
// @ts-ignore
searchArea.minX = x - radius;
// @ts-ignore
Expand All @@ -70,13 +71,8 @@ namespace gdjs {
searchArea.maxX = x + radius;
// @ts-ignore
searchArea.maxY = y + radius;
const nearbyObstacles: gdjs.BehaviorRBushAABB<
gdjs.LightObstacleRuntimeBehavior
>[] = this._obstacleRBush.search(searchArea);
result.length = 0;
nearbyObstacles.forEach((nearbyObstacle) =>
result.push(nearbyObstacle.behavior)
);
this._obstacleRBush.search(searchArea, result);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ namespace gdjs {
export interface RuntimeInstanceContainer {
pathfindingObstaclesManager: gdjs.PathfindingObstaclesManager;
}
declare var rbush: any;

/**
* PathfindingObstaclesManager manages the common objects shared by objects
Expand All @@ -18,10 +17,10 @@ namespace gdjs {
* `gdjs.PathfindingRuntimeBehavior.obstaclesManagers`).
*/
export class PathfindingObstaclesManager {
_obstaclesRBush: any;
_obstaclesRBush: RBush<PathfindingObstacleRuntimeBehavior>;

constructor(instanceContainer: gdjs.RuntimeInstanceContainer) {
this._obstaclesRBush = new rbush();
this._obstaclesRBush = new RBush<PathfindingObstacleRuntimeBehavior>();
}

/**
Expand Down Expand Up @@ -60,6 +59,9 @@ namespace gdjs {
removeObstacle(
pathfindingObstacleBehavior: PathfindingObstacleRuntimeBehavior
) {
if (!pathfindingObstacleBehavior.currentRBushAABB) {
return;
}
this._obstaclesRBush.remove(pathfindingObstacleBehavior.currentRBushAABB);
}

Expand All @@ -74,9 +76,9 @@ namespace gdjs {
radius: float,
result: gdjs.PathfindingObstacleRuntimeBehavior[]
): void {
const searchArea = gdjs.staticObject(
const searchArea: SearchArea = gdjs.staticObject(
PathfindingObstaclesManager.prototype.getAllObstaclesAround
);
) as SearchArea;
// @ts-ignore
searchArea.minX = x - radius;
// @ts-ignore
Expand All @@ -85,13 +87,8 @@ namespace gdjs {
searchArea.maxX = x + radius;
// @ts-ignore
searchArea.maxY = y + radius;
const nearbyObstacles: gdjs.BehaviorRBushAABB<
gdjs.PathfindingObstacleRuntimeBehavior
>[] = this._obstaclesRBush.search(searchArea);
result.length = 0;
nearbyObstacles.forEach((nearbyObstacle) =>
result.push(nearbyObstacle.behavior)
);
this._obstaclesRBush.search(searchArea, result);
}
}

Expand Down
18 changes: 11 additions & 7 deletions Extensions/Physics2Behavior/physics2tools.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
namespace gdjs {
export namespace physics2 {
export const objectsCollide = function (
objectsLists1: Hashtable<Array<gdjs.RuntimeObject>>,
objectsLists1: ObjectsLists,
behaviorName: string,
objectsLists2: Hashtable<Array<gdjs.RuntimeObject>>,
objectsLists2: ObjectsLists,
inverted: boolean
) {
return gdjs.evtTools.object.twoListsTest(
Expand All @@ -16,9 +16,9 @@ namespace gdjs {
};

export const haveObjectsStartedColliding = function (
objectsLists1: Hashtable<Array<gdjs.RuntimeObject>>,
objectsLists1: ObjectsLists,
behaviorName: string,
objectsLists2: Hashtable<Array<gdjs.RuntimeObject>>,
objectsLists2: ObjectsLists,
inverted: boolean
) {
return gdjs.evtTools.object.twoListsTest(
Expand All @@ -31,9 +31,9 @@ namespace gdjs {
};

export const haveObjectsStoppedColliding = function (
objectsLists1: Hashtable<Array<gdjs.RuntimeObject>>,
objectsLists1: ObjectsLists,
behaviorName: string,
objectsLists2: Hashtable<Array<gdjs.RuntimeObject>>,
objectsLists2: ObjectsLists,
inverted: boolean
) {
return gdjs.evtTools.object.twoListsTest(
Expand All @@ -45,7 +45,11 @@ namespace gdjs {
);
};

export const setTimeScale = function (objectsLists, behavior, timeScale) {
export const setTimeScale = function (
objectsLists: ObjectsLists,
behavior: gdjs.Physics2RuntimeBehavior,
timeScale: float
) {
const lists = gdjs.staticArray(gdjs.physics2.setTimeScale);
objectsLists.values(lists);
for (let i = 0, len = lists.length; i < len; i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,9 @@ namespace gdjs {
}

doStepPreEvents(instanceContainer: gdjs.RuntimeInstanceContainer) {
// Update platforms locations.
this._manager.doStepPreEvents();

const LEFTKEY = 37;
const UPKEY = 38;
const RIGHTKEY = 39;
Expand Down
Loading

0 comments on commit 90fa872

Please sign in to comment.