Skip to content

Commit

Permalink
more controls to char tween, char shadow event
Browse files Browse the repository at this point in the history
  • Loading branch information
jjppof committed Oct 16, 2023
1 parent 333cfbc commit 160ffed
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 9 deletions.
30 changes: 30 additions & 0 deletions base/game_events/CharShadowVisibilityEvent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import {GameEvent, event_types} from "./GameEvent";

export class CharShadowVisibilityEvent extends GameEvent {
private is_npc: boolean;
private npc_label: string;
private visible: boolean;

constructor(game, data, active, key_name, keep_reveal, is_npc, npc_label, visible) {
super(game, data, event_types.CHAR_SHADOW_VISIBILITY, active, key_name, keep_reveal);
this.is_npc = is_npc;
this.npc_label = npc_label;
this.visible = visible;
}

async _fire() {
const target_char =
GameEvent.get_char(this.data, {
is_npc: this.is_npc,
npc_label: this.npc_label,
}) ?? this.origin_npc;
if (!target_char) {
return;
}
if (target_char?.shadow) {
target_char.shadow.visible = this.visible;
}
}

_destroy() {}
}
47 changes: 39 additions & 8 deletions base/game_events/CharTweenPositionEvent.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import {ControllableChar} from "ControllableChar";
import {get_centered_pos_in_px} from "../utils";
import {GameEvent, event_types} from "./GameEvent";
import * as _ from "lodash";

export class CharTweenPositionEvent extends GameEvent {
private is_npc: boolean;
Expand All @@ -12,6 +14,11 @@ export class CharTweenPositionEvent extends GameEvent {
private incremental: boolean;
private is_px: boolean;
private finish_events: GameEvent[];
private hide_shadow: boolean;
private keep_shadow_hidden: boolean;
private shadow_follow_char: boolean;
private keep_char_collision_disable: boolean;
private prev_collision_status: boolean;

constructor(
game,
Expand All @@ -28,18 +35,26 @@ export class CharTweenPositionEvent extends GameEvent {
position,
incremental,
is_px,
finish_events
finish_events,
hide_shadow,
keep_shadow_hidden,
shadow_follow_char,
keep_char_collision_disable
) {
super(game, data, event_types.CHAR_TWEEN_POSITION, active, key_name, keep_reveal);
this.is_npc = is_npc;
this.npc_label = npc_label;
this.duration = duration;
this.ease = ease ?? Phaser.Easing.Linear.None;
this.ease = ease ? _.get(Phaser.Easing, ease) : Phaser.Easing.Linear.None;
this.repeat = repeat ?? 0;
this.yoyo = yoyo ?? false;
this.position = position ?? {};
this.incremental = incremental ?? false;
this.is_px = is_px ?? true;
this.hide_shadow = hide_shadow ?? false;
this.keep_shadow_hidden = keep_shadow_hidden ?? false;
this.shadow_follow_char = shadow_follow_char ?? true;
this.keep_char_collision_disable = keep_char_collision_disable ?? false;
this.finish_events = [];
if (finish_events !== undefined) {
finish_events.forEach(event_info => {
Expand Down Expand Up @@ -72,26 +87,42 @@ export class CharTweenPositionEvent extends GameEvent {
: get_centered_pos_in_px(this.position.y, this.data.map.tile_height);
tween_pos.y = this.incremental ? target_char.y + tween_pos.y : tween_pos.y;
}
this.prev_collision_status = target_char.shapes_collision_active;
const target = target_char.body ?? target_char.sprite;
if (this.duration >= 30) {
this.game.add
target_char.toggle_collision(false);
if (this.hide_shadow && target_char.shadow) {
target_char.shadow.visible = false;
}
const tween = this.game.add
.tween(target)
.to(tween_pos, this.duration, this.ease, true, undefined, this.repeat, this.yoyo)
.onComplete.addOnce(() => {
this.finish();
.to(tween_pos, this.duration, this.ease, true, undefined, this.repeat, this.yoyo);
tween.onComplete.addOnce(() => {
this.finish(target_char);
});
if (this.shadow_follow_char) {
tween.onUpdateCallback(() => {
target_char.update_shadow();
});
}
} else {
if (tween_pos.hasOwnProperty("x")) {
target_char.x = tween_pos.x;
}
if (tween_pos.hasOwnProperty("y")) {
target_char.y = tween_pos.y;
}
this.finish();
this.finish(target_char);
}
}

finish() {
finish(target_char: ControllableChar) {
if (!this.keep_char_collision_disable) {
target_char.toggle_collision(this.prev_collision_status);
}
if (this.hide_shadow && target_char.shadow && !this.keep_shadow_hidden) {
target_char.shadow.visible = true;
}
--this.data.game_event_manager.events_running_count;
this.finish_events.forEach(event => event.fire(this.origin_npc));
}
Expand Down
1 change: 1 addition & 0 deletions base/game_events/GameEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ export enum event_types {
SET_NPC_COLLISION = "set_npc_collision",
CHAR_ROTATION = "char_rotation",
CHAR_TWEEN_POSITION = "char_tween_position",
CHAR_SHADOW_VISIBILITY = "char_shadow_visibility",
}

export enum game_event_misc_origin {
Expand Down
18 changes: 17 additions & 1 deletion base/game_events/GameEventManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ import {TeleportEvent} from "./TeleportEvent";
import {SetNpcCollisionEvent} from "./SetNpcCollisionEvent";
import {CharRotationEvent} from "./CharRotationEvent";
import {CharTweenPositionEvent} from "./CharTweenPositionEvent";
import {CharShadowVisibilityEvent} from "./CharShadowVisibilityEvent";

export enum interaction_patterns {
NO_INTERACTION = "no_interaction",
Expand Down Expand Up @@ -1171,7 +1172,22 @@ export class GameEventManager {
info.position,
info.incremental,
info.is_px,
info.finish_events
info.finish_events,
info.hide_shadow,
info.keep_shadow_hidden,
info.shadow_follow_char,
info.keep_char_collision_disable
);
case event_types.CHAR_SHADOW_VISIBILITY:
return new CharShadowVisibilityEvent(
this.game,
this.data,
info.active,
info.key_name,
info.keep_reveal,
info.is_npc,
info.npc_label,
info.visible
);
default:
const origin = `Event origin: ${event_origin}. ${
Expand Down

0 comments on commit 160ffed

Please sign in to comment.