Skip to content

Commit

Permalink
layer tween and visibility events
Browse files Browse the repository at this point in the history
  • Loading branch information
jjppof committed Jun 24, 2023
1 parent ed017c6 commit 8f17993
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 1 deletion.
2 changes: 2 additions & 0 deletions base/game_events/GameEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ export enum event_types {
SET_CHAR_VISIBILITY = "set_char_visibility",
EVENTS_HOLDER = "events_holder",
EVENTS_LOOP = "events_loop",
LAYER_TWEEN = "layer_tween",
LAYER_VISIBILITY = "layer_visibility",
}

export enum game_event_misc_origin {
Expand Down
25 changes: 25 additions & 0 deletions base/game_events/GameEventManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ import {SetCharVisibilityEvent} from "./SetCharVisibilityEvent";
import {EventsHolderEvent} from "./EventsHolderEvent";
import {EventsLoopEvent} from "./EventsLoopEvent";
import {InteractableObjects} from "interactable_objects/InteractableObjects";
import {LayerTweenEvent} from "./LayerTweenEvent";
import {LayerVisibilityEvent} from "./LayerVisibilityEvent";

export enum interaction_patterns {
NO_INTERACTION = "no_interaction",
Expand Down Expand Up @@ -1043,6 +1045,29 @@ export class GameEventManager {
info.interval,
info.events
);
case event_types.LAYER_TWEEN:
return new LayerTweenEvent(
this.game,
this.data,
info.active,
info.key_name,
info.keep_reveal,
info.map_layer_name,
info.finish_events,
info.destination_offset,
info.duration,
info.easing
);
case event_types.LAYER_VISIBILITY:
return new LayerVisibilityEvent(
this.game,
this.data,
info.active,
info.key_name,
info.keep_reveal,
info.map_layer_name,
info.visible
);
default:
const origin = `Event origin: ${event_origin}. ${
entity_origin?.label
Expand Down
70 changes: 70 additions & 0 deletions base/game_events/LayerTweenEvent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import {GameEvent, event_types} from "./GameEvent";
import * as _ from "lodash";

export class LayerTweenEvent extends GameEvent {
private map_layer_name: string;
private finish_events: GameEvent[];
private duration: number;
private destination_offset: {
x?: number;
y?: number;
};
private easing: string;

constructor(
game,
data,
active,
key_name,
keep_reveal,
map_layer_name,
finish_events,
destination_offset,
duration,
easing
) {
super(game, data, event_types.LAYER_TWEEN, active, key_name, keep_reveal);
this.map_layer_name = map_layer_name;
this.finish_events = [];
if (finish_events !== undefined) {
finish_events.forEach(event_info => {
const event = this.data.game_event_manager.get_event_instance(event_info, this.type, this.origin_npc);
this.finish_events.push(event);
});
}
this.destination_offset = destination_offset ?? {};
this.duration = duration ?? 0;
this.easing = easing ?? "Linear.None";
}

_fire() {
const map_layer = this.data.map.get_layer(this.map_layer_name);

if (!map_layer || !map_layer.sprite) {
return;
}

++this.data.game_event_manager.events_running_count;

const finish = () => {
--this.data.game_event_manager.events_running_count;
this.finish_events.forEach(event => event.fire(this.origin_npc));
};

if (this.duration > 30) {
const easing = _.get(Phaser.Easing, this.easing);
this.game.add
.tween(map_layer.sprite.tileOffset)
.to(_.pick(this.destination_offset, "x", "y"), this.duration, easing, true)
.onComplete.addOnce(finish);
} else {
map_layer.sprite.tileOffset.x = this.destination_offset.x ?? map_layer.sprite.tileOffset.x;
map_layer.sprite.tileOffset.y = this.destination_offset.y ?? map_layer.sprite.tileOffset.y;
finish();
}
}

_destroy() {
this.finish_events.forEach(event => event?.destroy());
}
}
24 changes: 24 additions & 0 deletions base/game_events/LayerVisibilityEvent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import {GameEvent, event_types} from "./GameEvent";

export class LayerVisibilityEvent extends GameEvent {
private map_layer_name: string;
private visible: boolean;

constructor(game, data, active, key_name, keep_reveal, map_layer_name, visible) {
super(game, data, event_types.LAYER_VISIBILITY, active, key_name, keep_reveal);
this.map_layer_name = map_layer_name;
this.visible = visible ?? true;
}

_fire() {
const map_layer = this.data.map.get_layer(this.map_layer_name);

if (!map_layer || !map_layer.sprite) {
return;
}

map_layer.sprite.visible = this.visible;
}

_destroy() {}
}
4 changes: 3 additions & 1 deletion base/game_events/MapOpacityEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export class MapOpacityEvent extends GameEvent {
this.duration = duration ?? 0;
}

async _fire() {
_fire() {
this.map_layer = this.data.map.get_layer(this.map_layer_name);

if (!this.map_layer || !this.map_layer.sprite) {
Expand All @@ -30,6 +30,8 @@ export class MapOpacityEvent extends GameEvent {

++this.data.game_event_manager.events_running_count;

this.map_layer.visible = true;

if (this.duration > 30) {
this.game.add
.tween(this.map_layer.sprite)
Expand Down

0 comments on commit 8f17993

Please sign in to comment.