Skip to content

Commit e1159e3

Browse files
committed
jump algorithm update
1 parent 75e567d commit e1159e3

File tree

1 file changed

+37
-22
lines changed

1 file changed

+37
-22
lines changed

base/ControllableChar.ts

Lines changed: 37 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -882,7 +882,10 @@ export abstract class ControllableChar {
882882
async jump(options: {
883883
/** A time to wait on jump finish in ms. */
884884
time_on_finish?: number;
885-
/** The height of the jump in px. */
885+
/**
886+
* The height of the jump in px. This option
887+
* is not valid if it's a diagonal direction jumping.
888+
*/
886889
jump_height?: number;
887890
/** The duration of the jump in ms. */
888891
duration?: number;
@@ -898,7 +901,8 @@ export abstract class ControllableChar {
898901
y?: number;
899902
/**
900903
* The jump distance that this char will perform. If not given,
901-
* this char will jump into the center of the destination.
904+
* this char will jump into the center of the destination. This option
905+
* is not valid if it's a diagonal direction jumping.
902906
*/
903907
distance?: number;
904908
/** The distance multiplier that will be applied to the final jump distance. */
@@ -923,34 +927,44 @@ export abstract class ControllableChar {
923927
this.data.audio.play_se(options?.sfx_key ?? "actions/jump");
924928
if (options?.dest !== undefined) {
925929
//deals with jumps that have a different position from the current char position.
926-
const dest_char = {
930+
const dest_pos = {
927931
x: options.dest.x ?? get_centered_pos_in_px(options.dest.tile_x, this.data.map.tile_width) ?? this.x,
928932
y: options.dest.y ?? get_centered_pos_in_px(options.dest.tile_y, this.data.map.tile_height) ?? this.y,
929933
};
930-
const is_jump_over_x_axis =
931-
(options.dest.tile_x ?? get_tile_position(dest_char.x, this.data.map.tile_width)) === this.tile_x_pos;
932-
const axis: "x" | "y" = is_jump_over_x_axis ? "y" : "x";
934+
const same_x_axis =
935+
(options.dest.tile_x ?? get_tile_position(dest_pos.x, this.data.map.tile_width)) === this.tile_x_pos;
936+
const same_y_axis =
937+
(options.dest.tile_y ?? get_tile_position(dest_pos.y, this.data.map.tile_height)) === this.tile_y_pos;
938+
const diagonal_jump = !(same_x_axis || same_y_axis);
939+
const axis: "x" | "y" = same_x_axis ? "y" : "x";
933940
let distance: number;
934-
if (options.dest.distance !== undefined) {
935-
distance = options.dest.distance;
936-
} else {
937-
distance = get_distance(this.x, dest_char.x, this.y, dest_char.y);
938-
if (this.sprite[axis] > dest_char[axis]) {
939-
distance *= -1;
941+
if (!diagonal_jump) {
942+
if (options.dest.distance !== undefined) {
943+
distance = options.dest.distance;
944+
} else {
945+
distance = get_distance(this.x, dest_pos.x, this.y, dest_pos.y);
946+
if (this.sprite[axis] > dest_pos[axis]) {
947+
distance *= -1;
948+
}
940949
}
950+
distance *= options.dest.distance_multiplier ?? 1.0;
941951
}
942-
const distance_multiplier = options.dest.distance_multiplier ?? 1.0;
943-
const tween_obj: {x?: number; y?: number | number[]} = {
944-
[axis]: this.sprite[axis] + distance * distance_multiplier,
945-
};
946-
const jump_direction = options.jump_direction ?? this.current_direction;
947-
if (axis === "x") {
948-
const half_height = jump_height >> 1;
949-
const aux_height = dest_char.y - half_height;
950-
tween_obj.y = [aux_height, dest_char.y - jump_height, aux_height, dest_char.y];
952+
const tween_obj: {x?: number | number[]; y?: number | number[]} = {};
953+
if (diagonal_jump) {
954+
tween_obj.x = dest_pos.x;
955+
const middle_pt_y = Math.min(dest_pos.y, this.y) - 6;
956+
tween_obj.y = [middle_pt_y, dest_pos.y];
951957
} else {
952-
tween_obj.x = dest_char.x;
958+
tween_obj[axis] = this.sprite[axis] + distance;
959+
if (axis === "x") {
960+
const half_height = jump_height >> 1;
961+
const aux_height = dest_pos.y - half_height;
962+
tween_obj.y = [aux_height, dest_pos.y - jump_height, aux_height, dest_pos.y];
963+
} else {
964+
tween_obj.x = dest_pos.x;
965+
}
953966
}
967+
const jump_direction = options.jump_direction ?? this.current_direction;
954968
this.toggle_collision(false);
955969
this.jumping = true;
956970
if (this.sprite_info.hasAction(base_actions.JUMP) && !options.dont_play_jump_animation) {
@@ -1016,6 +1030,7 @@ export abstract class ControllableChar {
10161030
}
10171031
}
10181032
await promise;
1033+
this.update_tile_position();
10191034
if (options?.time_on_finish) {
10201035
const time_promise = new Promise(resolve => (promise_resolve = resolve));
10211036
this.game.time.events.add(options.time_on_finish, promise_resolve);

0 commit comments

Comments
 (0)