Skip to content

Commit f8a7e62

Browse files
committed
Adding more settings to IO storage keys. Allow to check IO storage key values on "set_value" event. Other minor enhancements.
1 parent d010788 commit f8a7e62

File tree

9 files changed

+158
-25
lines changed

9 files changed

+158
-25
lines changed

base/Hero.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,19 @@ export class Hero extends ControllableChar {
264264
}
265265
}
266266

267+
/**
268+
* Sets the hero visibility.
269+
* @param visible whether to be visible or not.
270+
*/
271+
set_visible(visible: boolean) {
272+
if (this.sprite) {
273+
this.sprite.visible = visible;
274+
if (this.shadow) {
275+
this.shadow.visible = this.sprite.visible;
276+
}
277+
}
278+
}
279+
267280
/**
268281
* Gets the hero battle encounter factor that depends on the type of the map (if
269282
* it's world map or not) and whether it's dashing or not.

base/NPC.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -341,10 +341,7 @@ export class NPC extends ControllableChar {
341341
if (this.storage_keys.visible !== undefined) {
342342
const storage_value = this.data.storage.get(this.storage_keys.visible);
343343
if (this.sprite?.visible !== storage_value) {
344-
this.sprite.visible = storage_value as boolean;
345-
if (this.shadow) {
346-
this.shadow.visible = storage_value as boolean;
347-
}
344+
this.set_visible(storage_value as boolean);
348345
}
349346
}
350347
if (this.storage_keys.movement_type !== undefined) {
@@ -654,9 +651,9 @@ export class NPC extends ControllableChar {
654651
} else {
655652
this.sprite.visible = true;
656653
}
657-
}
658-
if (this.shadow) {
659-
this.shadow.visible = this.sprite.visible;
654+
if (this.shadow) {
655+
this.shadow.visible = this.sprite.visible;
656+
}
660657
}
661658
this._active = true;
662659
} else {
@@ -669,6 +666,9 @@ export class NPC extends ControllableChar {
669666
}
670667
this._active = false;
671668
}
669+
if (this.storage_keys.active !== undefined) {
670+
this.data.storage.set(this.storage_keys.active, this._active);
671+
}
672672
}
673673

674674
/**
@@ -678,9 +678,12 @@ export class NPC extends ControllableChar {
678678
set_visible(visible: boolean) {
679679
if (this.sprite) {
680680
this.sprite.visible = visible;
681+
if (this.shadow) {
682+
this.shadow.visible = this.sprite.visible;
683+
}
681684
}
682-
if (this.shadow) {
683-
this.shadow.visible = visible;
685+
if (this.storage_keys.visible !== undefined) {
686+
this.data.storage.set(this.storage_keys.visible, visible);
684687
}
685688
}
686689

base/Storage.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,18 @@ export class Storage {
168168
this.data.logger.log_message(`There's no storage value with key '${key_name}'.`);
169169
return;
170170
}
171+
const prev_value = storage[key_name].value;
171172
storage[key_name].value = value;
173+
if (storage[key_name].type === storage_types.POSITION) {
174+
if (
175+
(prev_value as StoragePosition).x === (value as StoragePosition).x &&
176+
(prev_value as StoragePosition).y === (value as StoragePosition).y
177+
) {
178+
return;
179+
}
180+
} else if (prev_value === value) {
181+
return;
182+
}
172183
for (let id in storage[key_name].callbacks) {
173184
const callback_obj = storage[key_name].callbacks[id];
174185
callback_obj.callback();

base/game_events/GameEventManager.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,8 +376,10 @@ export class GameEventManager {
376376
info.keep_reveal,
377377
info.event_value,
378378
info.check_npc_storage_values,
379+
info.check_io_storage_values,
379380
info.check_collision_structures,
380381
info.check_layers_visibility,
382+
info.io_label,
381383
info.npc_label,
382384
info.npc_index,
383385
info.increment,

base/game_events/SetCharActivationEvent.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export class SetCharActivationEvent extends GameEvent {
1919
npc_label: this.npc_label,
2020
}) ?? this.origin_npc;
2121

22-
target_char.toggle_active(this.activate);
22+
target_char?.toggle_active(this.activate);
2323
}
2424

2525
_destroy() {}

base/game_events/SetCharVisibilityEvent.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,7 @@ export class SetCharVisibilityEvent extends GameEvent {
1818
is_npc: this.is_npc,
1919
npc_label: this.npc_label,
2020
}) ?? this.origin_npc;
21-
22-
target_char.sprite.visible = this.visible;
23-
if (target_char.shadow) {
24-
target_char.shadow.visible = this.visible;
25-
}
21+
target_char.set_visible(this.visible);
2622
}
2723

2824
_destroy() {}

base/game_events/SetIoVisibilityEvent.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@ export class SetIoVisibilityEvent extends GameEvent {
1616
return;
1717
}
1818
const interactable_object = this.data.map.interactable_objects_label_map[this.io_label];
19-
if (interactable_object.sprite) {
20-
interactable_object.sprite.visible = this.visible;
21-
}
19+
interactable_object.set_visible(this.visible);
2220
}
2321

2422
_destroy() {}

base/game_events/SetValueEvent.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@ import {storage_types} from "../Storage";
77
export class SetValueEvent extends GameEvent {
88
private event_value: EventValue;
99
private check_npc_storage_values: boolean;
10+
private check_io_storage_values: boolean;
1011
private check_collision_structures: boolean;
1112
private check_layers_visibility: boolean;
13+
private io_label: string;
1214
private npc_label: string;
1315
private npc_index: number;
1416
private increment: boolean;
@@ -22,8 +24,10 @@ export class SetValueEvent extends GameEvent {
2224
keep_reveal,
2325
event_value,
2426
check_npc_storage_values,
27+
check_io_storage_values,
2528
check_collision_structures,
2629
check_layers_visibility,
30+
io_label,
2731
npc_label,
2832
npc_index,
2933
increment,
@@ -32,8 +36,10 @@ export class SetValueEvent extends GameEvent {
3236
super(game, data, event_types.SET_VALUE, active, key_name, keep_reveal);
3337
this.event_value = event_value;
3438
this.check_npc_storage_values = check_npc_storage_values ?? false;
39+
this.check_io_storage_values = check_io_storage_values ?? false;
3540
this.check_collision_structures = check_collision_structures ?? false;
3641
this.check_layers_visibility = check_layers_visibility ?? false;
42+
this.io_label = io_label;
3743
this.npc_label = npc_label;
3844
this.npc_index = npc_index;
3945
this.increment = increment ?? false;
@@ -167,6 +173,17 @@ export class SetValueEvent extends GameEvent {
167173
);
168174
}
169175
}
176+
if (this.check_io_storage_values) {
177+
if (!(this.io_label in this.data.map.interactable_objects_label_map)) {
178+
this.data.logger.log_message(
179+
`IO label not passed to "set_value" event when "check_io_storage_values" was set true.`
180+
);
181+
return;
182+
} else {
183+
const interactable_object = this.data.map.interactable_objects_label_map[this.io_label];
184+
interactable_object.check_storage_keys();
185+
}
186+
}
170187
if (this.check_collision_structures) {
171188
this.data.map.collision_sprite.body.data.shapes.forEach(shape => {
172189
if (shape.properties?.controller_variable) {

base/interactable_objects/InteractableObjects.ts

Lines changed: 100 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,10 @@ export class InteractableObjects {
6666
scale?: string;
6767
base_collision_layer?: string;
6868
enable?: string;
69+
active?: string;
70+
visible?: string;
6971
entangled_by_bush?: string;
72+
action?: string;
7073
animation?: string;
7174
affected_by_reveal?: string;
7275
};
@@ -238,6 +241,9 @@ export class InteractableObjects {
238241
this.scale_y = scale_y;
239242
this._psynergy_casted = {};
240243
this.block_climb_collision_layer_shift = block_climb_collision_layer_shift;
244+
if (this.storage_keys.active !== undefined) {
245+
active = this.data.storage.get(this.storage_keys.active);
246+
}
241247
this._active = active ?? true;
242248
if (this.snapshot_info) {
243249
this._active = this.snapshot_info.active;
@@ -279,6 +285,9 @@ export class InteractableObjects {
279285
animation = this.data.storage.get(this.storage_keys.animation);
280286
}
281287
this._current_animation = animation;
288+
if (this.storage_keys.action !== undefined) {
289+
action = this.data.storage.get(this.storage_keys.action);
290+
}
282291
this._current_action = action;
283292
this._shapes_collision_active = false;
284293
this._active_filters = {
@@ -292,6 +301,9 @@ export class InteractableObjects {
292301
[engine_filters.FLAME]: false,
293302
};
294303
this._map_index = map_index;
304+
if (this.storage_keys.visible !== undefined) {
305+
initially_visible = this.data.storage.get(this.storage_keys.visible);
306+
}
295307
this._initially_visible = initially_visible ?? true;
296308
}
297309

@@ -568,6 +580,22 @@ export class InteractableObjects {
568580
});
569581
}
570582

583+
/**
584+
* Sets this IO visibility.
585+
* @param visible whether to be visible or not.
586+
*/
587+
set_visible(visible: boolean) {
588+
if (this.sprite) {
589+
this.sprite.visible = visible;
590+
if (this.shadow) {
591+
this.shadow.visible = this.sprite.visible;
592+
}
593+
}
594+
if (this.storage_keys.visible !== undefined) {
595+
this.data.storage.set(this.storage_keys.visible, visible);
596+
}
597+
}
598+
571599
set_entangled_by_bush(entangled_by_bush: boolean) {
572600
this._entangled_by_bush = entangled_by_bush;
573601
if (this.storage_keys.entangled_by_bush !== undefined) {
@@ -1003,8 +1031,8 @@ export class InteractableObjects {
10031031
}
10041032
}
10051033

1006-
play(animation: string, action?: string, start: boolean = true, frame_rate?: number, loop?: boolean) {
1007-
this._current_animation = animation;
1034+
play(animation?: string, action?: string, start: boolean = true, frame_rate?: number, loop?: boolean) {
1035+
this._current_animation = animation ?? this._current_animation;
10081036
this._current_action = action ?? this._current_action;
10091037

10101038
if (SpriteBase.getSpriteAction(this.sprite) !== this.current_action) {
@@ -1300,6 +1328,64 @@ export class InteractableObjects {
13001328
});
13011329
}
13021330

1331+
/**
1332+
* Updates this IO properties according to current storage values.
1333+
*/
1334+
check_storage_keys() {
1335+
if (this.storage_keys.active !== undefined) {
1336+
const storage_value = this.data.storage.get(this.storage_keys.active);
1337+
if (this.active !== storage_value) {
1338+
this.toggle_active(storage_value as boolean);
1339+
}
1340+
}
1341+
if (this.storage_keys.base_collision_layer !== undefined) {
1342+
const storage_value = this.data.storage.get(this.storage_keys.base_collision_layer);
1343+
if (this.base_collision_layer !== storage_value) {
1344+
this._base_collision_layer = storage_value as number;
1345+
}
1346+
}
1347+
if (this.storage_keys.affected_by_reveal !== undefined) {
1348+
const storage_value = this.data.storage.get(this.storage_keys.affected_by_reveal);
1349+
if (this.affected_by_reveal !== storage_value) {
1350+
this._affected_by_reveal = storage_value as boolean;
1351+
}
1352+
}
1353+
if (this.storage_keys.visible !== undefined) {
1354+
const storage_value = this.data.storage.get(this.storage_keys.visible);
1355+
if (this.sprite?.visible !== storage_value) {
1356+
this.set_visible(storage_value as boolean);
1357+
}
1358+
}
1359+
if (this.storage_keys.enable !== undefined) {
1360+
const storage_value = this.data.storage.get(this.storage_keys.enable);
1361+
if (this.enable !== storage_value) {
1362+
this.set_enable(storage_value as boolean);
1363+
}
1364+
}
1365+
let action_or_animation_changed = false;
1366+
if (this.storage_keys.action !== undefined) {
1367+
const storage_value = this.data.storage.get(this.storage_keys.action);
1368+
if (this.current_action !== storage_value) {
1369+
this._current_action = storage_value as string;
1370+
action_or_animation_changed = true;
1371+
}
1372+
}
1373+
if (this.storage_keys.animation !== undefined) {
1374+
const storage_value = this.data.storage.get(this.storage_keys.animation);
1375+
if (this.current_animation !== storage_value) {
1376+
this._current_animation = storage_value as string;
1377+
action_or_animation_changed = true;
1378+
}
1379+
}
1380+
if (action_or_animation_changed) {
1381+
this.play();
1382+
}
1383+
}
1384+
1385+
/**
1386+
* Activates or deactivates this IO.
1387+
* @param active true, if you want to activate it.
1388+
*/
13031389
toggle_active(active: boolean) {
13041390
if (active) {
13051391
this.sprite?.body?.collides(this.data.collision.hero_collision_group);
@@ -1310,10 +1396,14 @@ export class InteractableObjects {
13101396
this._blocking_stair_block.collides(this.data.collision.hero_collision_group);
13111397
}
13121398
if (this.sprite) {
1313-
this.sprite.visible = true;
1314-
}
1315-
if (this.shadow) {
1316-
this.shadow.visible = true;
1399+
if (this.storage_keys.visible !== undefined) {
1400+
this.sprite.visible = this.data.storage.get(this.storage_keys.visible) as boolean;
1401+
} else {
1402+
this.sprite.visible = true;
1403+
}
1404+
if (this.shadow) {
1405+
this.shadow.visible = this.sprite.visible;
1406+
}
13171407
}
13181408
this._active = true;
13191409
} else {
@@ -1328,10 +1418,13 @@ export class InteractableObjects {
13281418
this.sprite.visible = false;
13291419
}
13301420
if (this.shadow) {
1331-
this.shadow.visible = false;
1421+
this.shadow.visible = this.sprite.visible;
13321422
}
13331423
this._active = false;
13341424
}
1425+
if (this.storage_keys.active !== undefined) {
1426+
this.data.storage.set(this.storage_keys.active, this._active);
1427+
}
13351428
}
13361429

13371430
config_body() {

0 commit comments

Comments
 (0)