Skip to content

Commit

Permalink
Add on move script to trigger
Browse files Browse the repository at this point in the history
  • Loading branch information
pau-tomas committed Jul 26, 2022
1 parent 011faab commit 865811f
Show file tree
Hide file tree
Showing 13 changed files with 72 additions and 11 deletions.
1 change: 1 addition & 0 deletions appData/src/gb/include/gbs_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ typedef struct actor_t

#define TRIGGER_HAS_ENTER_SCRIPT 1
#define TRIGGER_HAS_LEAVE_SCRIPT 2
#define TRIGGER_HAS_UPDATE_SCRIPT 4

typedef struct trigger_t {
uint8_t x, y, width, height;
Expand Down
18 changes: 13 additions & 5 deletions appData/src/gb/src/core/trigger.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ UBYTE triggers_len = 0;
UBYTE last_trigger_tx;
UBYTE last_trigger_ty;
UBYTE last_trigger;
UWORD last_pos_tx;
UWORD last_pos_ty;

void trigger_reset() BANKED {
last_trigger_tx = 0;
Expand Down Expand Up @@ -70,14 +72,20 @@ UBYTE trigger_activate_at_intersection(bounding_box_t *bb, upoint16_t *offset, U
UBYTE hit_trigger = trigger_at_intersection(bb, offset);
UBYTE trigger_script_called = FALSE;

// Don't reactivate trigger if not changed tile
if (!force && (last_trigger == hit_trigger)) {
return FALSE;
if ((last_pos_tx != offset->x >> 3 || last_pos_ty != offset->y >> 3) && hit_trigger != NO_TRIGGER_COLLISON && triggers[hit_trigger].script_flags & TRIGGER_HAS_UPDATE_SCRIPT) {
last_pos_tx = offset->x >> 3;
last_pos_ty = offset->y >> 3;

script_execute(triggers[hit_trigger].script.bank, triggers[hit_trigger].script.ptr, 0, 1, 3);

return TRUE;
} else {
return FALSE;
}
}

if (last_trigger != NO_TRIGGER_COLLISON &&
(hit_trigger == NO_TRIGGER_COLLISON || hit_trigger != last_trigger)) {

if (last_trigger != NO_TRIGGER_COLLISON && hit_trigger != last_trigger) {
if (hit_trigger != NO_TRIGGER_COLLISON && triggers[hit_trigger].script_flags & TRIGGER_HAS_ENTER_SCRIPT) {
script_execute(triggers[hit_trigger].script.bank, triggers[hit_trigger].script.ptr, 0, 1, 1);
trigger_script_called = TRUE;
Expand Down
6 changes: 5 additions & 1 deletion src/components/editors/TriggerEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,12 @@ interface ScriptHandlers {
leave: ScriptHandler;
}

type TriggerScriptKey = "script" | "leaveScript";
type TriggerScriptKey = "script" | "leaveScript" | "updateScript";

const scriptTabs = {
trigger: l10n("SIDEBAR_ON_ENTER"),
leave: l10n("SIDEBAR_ON_LEAVE"),
update: l10n("SIDEBAR_ON_MOVE"),
} as const;

const pointNClickScriptTabs = {
Expand All @@ -68,6 +69,9 @@ const getScriptKey = (tab: keyof typeof scriptTabs): TriggerScriptKey => {
if (tab === "leave") {
return "leaveScript";
}
if (tab === "update") {
return "updateScript";
}
return "script";
};

Expand Down
1 change: 1 addition & 0 deletions src/lang/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
"SIDEBAR_ON_TRIGGER": "On Trigger",
"SIDEBAR_ON_ENTER": "On Enter",
"SIDEBAR_ON_LEAVE": "On Leave",
"SIDEBAR_ON_MOVE": "On Move",
"SIDEBAR_ON_INTERACT": "On Interact",
"SIDEBAR_ON_HIT": "On Hit",
"SIDEBAR_ON_PLAYER_HIT": "On Player Hit",
Expand Down
6 changes: 5 additions & 1 deletion src/lib/compiler/compileData.js
Original file line number Diff line number Diff line change
Expand Up @@ -965,7 +965,10 @@ export const precompileScenes = (
trigger.script[0].command !== EVENT_END) ||
(trigger.leaveScript &&
trigger.leaveScript.length >= 1 &&
trigger.leaveScript[0].command !== EVENT_END)
trigger.leaveScript[0].command !== EVENT_END) ||
(trigger.updateScript &&
trigger.updateScript.length >= 1 &&
trigger.updateScript[0].command !== EVENT_END)
);
}),
playerSprite,
Expand Down Expand Up @@ -1507,6 +1510,7 @@ VM_ACTOR_SET_SPRITESHEET_BY_REF .ARG2, .ARG1`,
[
{ parameter: 0, value: 1, script: entity.script },
{ parameter: 0, value: 2, script: entity.leaveScript },
{ parameter: 0, value: 3, script: entity.updateScript },
],
true
);
Expand Down
1 change: 1 addition & 0 deletions src/lib/compiler/compileData2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ export const toASMTriggerScriptFlags = (trigger: Trigger) => {

if (trigger.script.length > 0) flags.push("TRIGGER_HAS_ENTER_SCRIPT");
if (trigger.leaveScript.length > 0) flags.push("TRIGGER_HAS_LEAVE_SCRIPT");
if (trigger.updateScript.length > 0) flags.push("TRIGGER_HAS_UPDATE_SCRIPT");

return flags.length > 0 ? flags.join(" | ") : 0;
};
Expand Down
1 change: 1 addition & 0 deletions src/lib/helpers/eventHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ export const walkDenormalizedTriggerEvents = (
};
walk(trigger.script);
walk(trigger.leaveScript);
walk(trigger.updateScript);
};

export const walkDenormalizedSceneSpecificEvents = (
Expand Down
2 changes: 2 additions & 0 deletions src/lib/helpers/eventSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ const mapSceneEvents = (scene: any, callback: any) => {
...trigger,
script: mapEvents(trigger.script, callback),
leaveScript: mapEvents(trigger.leaveScript, callback),
updateScript: mapEvents(trigger.updateScript, callback),
};
}),
};
Expand Down Expand Up @@ -109,6 +110,7 @@ const walkActorEvents = (actor: any, callback: any) => {
const walkTriggerEvents = (trigger: any, callback: any) => {
walkEvents(trigger.script, callback);
walkEvents(trigger.leaveScript, callback);
walkEvents(trigger.updateScript, callback);
};

const walkSceneEvents = (scene: any, callback: any) => {
Expand Down
27 changes: 25 additions & 2 deletions src/lib/project/migrateProject.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { toValidSymbol } from "lib/helpers/symbols";
const indexById = indexBy("id");

export const LATEST_PROJECT_VERSION = "3.1.0";
export const LATEST_PROJECT_MINOR_VERSION = "2";
export const LATEST_PROJECT_MINOR_VERSION = "3";

const ensureProjectAssetSync = (relativePath, { projectRoot }) => {
const projectPath = `${projectRoot}/${relativePath}`;
Expand Down Expand Up @@ -1704,6 +1704,25 @@ const migrateFrom310r1To310r2Events = (data) => {
};
};

/* Version 3.1.0 r2 to r3 Triggers migration to include the on move script
*/
export const migrateFrom310r2To310r3Triggers = (data) => {
return {
...data,
scenes: data.scenes.map((scene) => {
return {
...scene,
triggers: scene.triggers.map((trigger) => {
return {
...trigger,
updateScript: trigger.updateScript || [],
};
}),
};
}),
};
};

const migrateProject = (project, projectRoot) => {
let data = { ...project };
let version = project._version || "1.0.0";
Expand Down Expand Up @@ -1805,7 +1824,6 @@ const migrateProject = (project, projectRoot) => {
release = "1";
}
}

if (version === "3.0.0") {
if (release === "1") {
data = migrateFrom300r1To300r2Events(data);
Expand All @@ -1827,6 +1845,11 @@ const migrateProject = (project, projectRoot) => {
data = migrateFrom310r1To310r2Events(data);
release = "2";
}

if (release === "2") {
data = migrateFrom310r2To310r3Triggers(data);
release = "3";
}
}

if (process.env.NODE_ENV !== "production") {
Expand Down
4 changes: 4 additions & 0 deletions src/store/features/electron/electronMiddleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,10 @@ const electronMiddleware: Middleware<Dispatch, RootState> =
triggersLookup[triggerId]?.leaveScript || [],
filter
),
updateScript: filterEvents(
triggersLookup[triggerId]?.updateScript || [],
filter
),
},
})
);
Expand Down
2 changes: 2 additions & 0 deletions src/store/features/entities/entitiesHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ const actorSchema = new schema.Entity("actors", {
const triggerSchema = new schema.Entity("triggers", {
script: [scriptEventSchema],
leaveScript: [scriptEventSchema],
updateScript: [scriptEventSchema],
});
const metaspriteTilesSchema = new schema.Entity("metaspriteTiles");
const metaspritesSchema = new schema.Entity("metasprites", {
Expand Down Expand Up @@ -432,6 +433,7 @@ export const walkNormalisedTriggerEvents = (
) => {
walkNormalisedScriptEvents(trigger.script, lookup, options, callback);
walkNormalisedScriptEvents(trigger.leaveScript, lookup, options, callback);
walkNormalisedScriptEvents(trigger.updateScript, lookup, options, callback);
};

export const walkNormalisedSceneEvents = (
Expand Down
1 change: 1 addition & 0 deletions src/store/features/entities/entitiesState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1030,6 +1030,7 @@ const addTrigger: CaseReducer<
height,
script: [],
leaveScript: [],
updateScript: [],
};

// Add to scene
Expand Down
13 changes: 11 additions & 2 deletions src/store/features/entities/entitiesTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,11 @@ export type ActorDenormalized = Omit<
hit3Script: ScriptEventDenormalized[];
};

export const triggerScriptKeys = ["script", "leaveScript"] as const;
export const triggerScriptKeys = [
"script",
"leaveScript",
"updateScript",
] as const;
export type TriggerScriptKey = typeof triggerScriptKeys[number];

export type Trigger = {
Expand All @@ -130,11 +134,16 @@ export type Trigger = {
height: number;
script: string[];
leaveScript: string[];
updateScript: string[];
};

export type TriggerDenormalized = Omit<Trigger, "script" | "leaveScript"> & {
export type TriggerDenormalized = Omit<
Trigger,
"script" | "leaveScript" | "updateScript"
> & {
script: ScriptEventDenormalized[];
leaveScript: ScriptEventDenormalized[];
updateScript: ScriptEventDenormalized[];
};

export type Background = {
Expand Down

0 comments on commit 865811f

Please sign in to comment.