Skip to content

Commit

Permalink
char rotation and pos tween events
Browse files Browse the repository at this point in the history
  • Loading branch information
jjppof committed Oct 13, 2023
1 parent 43a45ad commit 333cfbc
Show file tree
Hide file tree
Showing 5 changed files with 198 additions and 2 deletions.
20 changes: 18 additions & 2 deletions base/ControllableChar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -309,14 +309,30 @@ export abstract class ControllableChar {
};
}

/** The current x position in px. */
/** Gets the current x position in px. */
get x(): number {
return this.sprite.body ? this.sprite.body.x : this.sprite.x;
}
/** The current y position in px. */
/** Gets the current y position in px. */
get y(): number {
return this.sprite.body ? this.sprite.body.y : this.sprite.y;
}
/** Sets the current x position in px. */
set x(x: number) {
if (this.sprite.body) {
this.sprite.body.x = x;
} else if (this.sprite) {
this.sprite.x = x;
}
}
/** Sets the current y position in px. */
set y(y: number) {
if (this.sprite.body) {
this.sprite.body.y = y;
} else if (this.sprite) {
this.sprite.y = y;
}
}

/** This char width. */
get width() {
Expand Down
43 changes: 43 additions & 0 deletions base/game_events/CharRotationEvent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import {GameEvent, event_types} from "./GameEvent";

export class CharRotationEvent extends GameEvent {
private is_npc: boolean;
private npc_label: string;
private rotate: boolean;
private interframe_interval: number;
private frame_index: number;

constructor(
game,
data,
active,
key_name,
keep_reveal,
is_npc,
npc_label,
rotate,
interframe_interval,
frame_index
) {
super(game, data, event_types.CHAR_ROTATION, active, key_name, keep_reveal);
this.is_npc = is_npc;
this.npc_label = npc_label;
this.rotate = rotate;
this.interframe_interval = interframe_interval;
this.frame_index = frame_index;
}

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;
}
target_char.set_rotation(this.rotate, this.interframe_interval, this.frame_index);
}

_destroy() {}
}
102 changes: 102 additions & 0 deletions base/game_events/CharTweenPositionEvent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import {get_centered_pos_in_px} from "../utils";
import {GameEvent, event_types} from "./GameEvent";

export class CharTweenPositionEvent extends GameEvent {
private is_npc: boolean;
private npc_label: string;
private duration: number;
private ease: Function;
private repeat: number;
private yoyo: boolean;
private position: {x?: number; y?: number};
private incremental: boolean;
private is_px: boolean;
private finish_events: GameEvent[];

constructor(
game,
data,
active,
key_name,
keep_reveal,
is_npc,
npc_label,
duration,
ease,
repeat,
yoyo,
position,
incremental,
is_px,
finish_events
) {
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.repeat = repeat ?? 0;
this.yoyo = yoyo ?? false;
this.position = position ?? {};
this.incremental = incremental ?? false;
this.is_px = is_px ?? true;
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);
});
}
}

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 || !target_char.sprite) {
return;
}
++this.data.game_event_manager.events_running_count;
const tween_pos: CharTweenPositionEvent["position"] = {};
if (this.position.hasOwnProperty("x")) {
tween_pos.x = this.is_px
? this.position.x
: get_centered_pos_in_px(this.position.x, this.data.map.tile_width);
tween_pos.x = this.incremental ? target_char.x + tween_pos.x : tween_pos.x;
}
if (this.position.hasOwnProperty("y")) {
tween_pos.y = this.is_px
? this.position.y
: 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;
}
const target = target_char.body ?? target_char.sprite;
if (this.duration >= 30) {
this.game.add
.tween(target)
.to(tween_pos, this.duration, this.ease, true, undefined, this.repeat, this.yoyo)
.onComplete.addOnce(() => {
this.finish();
});
} 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();
}
}

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

_destroy() {
this.finish_events.forEach(event => event?.destroy());
}
}
2 changes: 2 additions & 0 deletions base/game_events/GameEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ export enum event_types {
SET_IO_ACTIVATION = "set_io_activation",
TELEPORT = "teleport",
SET_NPC_COLLISION = "set_npc_collision",
CHAR_ROTATION = "char_rotation",
CHAR_TWEEN_POSITION = "char_tween_position",
}

export enum game_event_misc_origin {
Expand Down
33 changes: 33 additions & 0 deletions base/game_events/GameEventManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ import {SetIoVisibilityEvent} from "./SetIoVisibilityEvent";
import {SetIoActivationEvent} from "./SetIoActivationEvent";
import {TeleportEvent} from "./TeleportEvent";
import {SetNpcCollisionEvent} from "./SetNpcCollisionEvent";
import {CharRotationEvent} from "./CharRotationEvent";
import {CharTweenPositionEvent} from "./CharTweenPositionEvent";

export enum interaction_patterns {
NO_INTERACTION = "no_interaction",
Expand Down Expand Up @@ -1140,6 +1142,37 @@ export class GameEventManager {
info.keep_reveal,
info.collision_active
);
case event_types.CHAR_ROTATION:
return new CharRotationEvent(
this.game,
this.data,
info.active,
info.key_name,
info.keep_reveal,
info.is_npc,
info.npc_label,
info.rotate,
info.interframe_interval,
info.frame_index
);
case event_types.CHAR_TWEEN_POSITION:
return new CharTweenPositionEvent(
this.game,
this.data,
info.active,
info.key_name,
info.keep_reveal,
info.is_npc,
info.npc_label,
info.duration,
info.ease,
info.repeat,
info.yoyo,
info.position,
info.incremental,
info.is_px,
info.finish_events
);
default:
const origin = `Event origin: ${event_origin}. ${
entity_origin?.label
Expand Down

0 comments on commit 333cfbc

Please sign in to comment.