Skip to content

Commit

Permalink
better npc is_close check & some sprite base fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
jjppof committed Apr 20, 2024
1 parent 060f0cc commit 8c0778a
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 13 deletions.
13 changes: 5 additions & 8 deletions base/ControllableChar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -773,9 +773,7 @@ export abstract class ControllableChar {
}
const animation_key = this.sprite_info.getAnimationKey(action, animation);
if (!this.sprite.animations.getAnimation(animation_key)) {
if (!this.sprite_info.setAnimation(this.sprite, action)) {
return null;
}
this.sprite_info.setAnimation(this.sprite, action);
}
const animation_obj = this.sprite.animations.getAnimation(animation_key);
if (!animation_obj) {
Expand All @@ -798,7 +796,7 @@ export abstract class ControllableChar {
/**
* Returns if this char is enough close to a target char.
* @param target_char The target char.
* @returns Returns whether it's close or not.
* @returns If it's close enough, returns the distance, else returns null.
*/
is_close(target_char: NPC) {
const reference_angle = (8 - this.transition_direction) * numbers.degree45;
Expand All @@ -818,10 +816,9 @@ export abstract class ControllableChar {
upper_limit += angle_shift;
const target_angle = range_360(-Math.atan2(relative_point.y, relative_point.x) + angle_shift);
const angle_condition = target_angle >= lower_limit && target_angle <= upper_limit;
const distance_condition =
get_sqr_distance(0, relative_point.x, 0, relative_point.y) <=
target_char.talk_range * target_char.talk_range;
return angle_condition && distance_condition;
const relative_dist = get_sqr_distance(0, relative_point.x, 0, relative_point.y);
const distance_condition = relative_dist <= target_char.talk_range * target_char.talk_range;
return angle_condition && distance_condition ? relative_dist : null;
}

/**
Expand Down
20 changes: 17 additions & 3 deletions base/SpriteBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,15 @@ export class SpriteBase {
frame_names?: {[animation: string]: string[]};
};
};
private action_aliases: {
[alias: string]: string;
};

constructor(data, key_name, actions) {
this.data = data;
this.key_name = key_name;
this.actions = {};
this.action_aliases = {};
for (let i = 0; i < actions.length; ++i) {
this.actions[actions[i]] = {};
}
Expand Down Expand Up @@ -108,7 +112,15 @@ export class SpriteBase {
if (reference_action in this.actions) {
this.actions[alias] = this.actions[reference_action];
game.cache.setCacheAlias(this.getSpriteKey(alias), this.getSpriteKey(reference_action), Phaser.Cache.IMAGE);
this.action_aliases[alias] = reference_action;
}
}

getActionRefFromAlias(alias) {
if (alias in this.action_aliases) {
return this.action_aliases[alias];
}
return null;
}

hasAction(action: string) {
Expand Down Expand Up @@ -154,26 +166,28 @@ export class SpriteBase {
for (let i = 0; i < animations.length; ++i) {
const animation = animations[i];
const frame_rate = this.actions[action].frame_rate[animation];
const anim_key = this.getAnimationKey(action, animation);
let anim_key = this.getAnimationKey(action, animation);
sprite.animations.add(
anim_key,
this.actions[action].frame_names[animation],
frame_rate,
Array.isArray(loop) ? loop[i] : loop,
false
);
const ref_action = this.getActionRefFromAlias(action);
if (ref_action) {
anim_key = this.getAnimationKey(ref_action, animation);
}
if (!sprite.animations.frameData.getFrameByName(`${anim_key}${SpriteBase.ACTION_ANIM_SEPARATOR}00`)) {
this.data.logger.log_message(
`Animation '${anim_key}' is not valid for action '${action}' for sprite '${this.key_name}'.`,
msg_types.ERROR
);
return false;
}
}
} else {
this.data.logger.log_message(`Action '${action}' not available for '${this.key_name}'.`);
}
return true;
}

generateAllFrames() {
Expand Down
17 changes: 15 additions & 2 deletions base/game_events/GameEventManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,10 @@ export class GameEventManager {
* Look for valid npcs in the nearby.
*/
private search_for_npc() {
const close_npcs: {
npc: NPC;
distance: number;
}[] = [];
for (let i = 0; i < this.data.map.npcs.length; ++i) {
const npc = this.data.map.npcs[i];
if (
Expand All @@ -170,8 +174,17 @@ export class GameEventManager {
) {
continue;
}
const is_close_check = this.data.hero.is_close(npc);
if (is_close_check) {
const npc_distance = this.data.hero.is_close(npc);
if (npc_distance) {
close_npcs.push({
npc: npc,
distance: npc_distance,
});
}
}
if (close_npcs.length) {
for (let close_npc_data of _.sortBy(close_npcs, close_npc_data => close_npc_data.distance)) {
const npc = close_npc_data.npc;
if (
npc.allow_only_front_interaction &&
(npc.tile_pos.y + 1 !== this.data.hero.tile_pos.y || npc.tile_pos.x !== this.data.hero.tile_pos.x)
Expand Down

0 comments on commit 8c0778a

Please sign in to comment.