Files
GDevelop/GDJS/Runtime/oncetriggers.ts
Clément Pasteau e174136fb4 New Save & Load built-in actions (#7811)
* New actions are available to save & load the game state, making Saving & Loading as easy as adding 1 action to your game!
* While it will work in most cases, it has a few limitations and hasn't been thoroughly tested on all types of objects/behaviors and games, so it is for the moment tagged as **Experimental** while we gather feedback and improve it
* Check out the wiki for more info: https://wiki.gdevelop.io/gdevelop5/all-features/save-state
2025-09-29 15:38:37 +02:00

62 lines
2.0 KiB
TypeScript

/*
* GDevelop JS Platform
* Copyright 2013-2016 Florian Rival (Florian.Rival@gmail.com). All rights reserved.
* This project is released under the MIT License.
*/
namespace gdjs {
/**
* OnceTriggers is used to store the status of the conditions "Trigger once",
* that are used in events to have conditions that are only valid for one frame in a row.
*/
type OnceTriggersSyncData = {
onceTriggers: Record<integer, boolean>;
lastFrameOnceTriggers: Record<integer, boolean>;
};
export class OnceTriggers {
_onceTriggers: Record<integer, boolean> = {};
_lastFrameOnceTrigger: Record<integer, boolean> = {};
/**
* To be called when events begin so that "Trigger once" conditions
* are properly handled.
*/
startNewFrame(): void {
// Clear triggers from 2 frames ago
for (const k in this._lastFrameOnceTrigger)
if (this._lastFrameOnceTrigger.hasOwnProperty(k))
delete this._lastFrameOnceTrigger[k];
// Move triggers from this frame to last frame
for (const k in this._onceTriggers) {
if (this._onceTriggers.hasOwnProperty(k)) {
this._lastFrameOnceTrigger[k] = this._onceTriggers[k];
delete this._onceTriggers[k];
}
}
}
/**
* Used by "Trigger once" conditions: return true only if
* this method was not called with the same identifier during the last frame.
* @param triggerId The identifier of the "Trigger once" condition.
*/
triggerOnce(triggerId: integer): boolean {
this._onceTriggers[triggerId] = true;
return !this._lastFrameOnceTrigger.hasOwnProperty(triggerId);
}
getNetworkSyncData(): OnceTriggersSyncData {
return {
onceTriggers: this._onceTriggers,
lastFrameOnceTriggers: this._lastFrameOnceTrigger,
};
}
updateNetworkSyncData(data: OnceTriggersSyncData): void {
this._onceTriggers = data.onceTriggers;
this._lastFrameOnceTrigger = data.lastFrameOnceTriggers;
}
}
}