Skip to content

Commit 75e567d

Browse files
committed
2 parents 97c5d7c + 8c0778a commit 75e567d

File tree

4 files changed

+73
-10
lines changed

4 files changed

+73
-10
lines changed

base/ControllableChar.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -796,7 +796,7 @@ export abstract class ControllableChar {
796796
/**
797797
* Returns if this char is enough close to a target char.
798798
* @param target_char The target char.
799-
* @returns Returns whether it's close or not.
799+
* @returns If it's close enough, returns the distance, else returns null.
800800
*/
801801
is_close(target_char: NPC) {
802802
const reference_angle = (8 - this.transition_direction) * numbers.degree45;
@@ -816,10 +816,9 @@ export abstract class ControllableChar {
816816
upper_limit += angle_shift;
817817
const target_angle = range_360(-Math.atan2(relative_point.y, relative_point.x) + angle_shift);
818818
const angle_condition = target_angle >= lower_limit && target_angle <= upper_limit;
819-
const distance_condition =
820-
get_sqr_distance(0, relative_point.x, 0, relative_point.y) <=
821-
target_char.talk_range * target_char.talk_range;
822-
return angle_condition && distance_condition;
819+
const relative_dist = get_sqr_distance(0, relative_point.x, 0, relative_point.y);
820+
const distance_condition = relative_dist <= target_char.talk_range * target_char.talk_range;
821+
return angle_condition && distance_condition ? relative_dist : null;
823822
}
824823

825824
/**

base/SpriteBase.ts

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,15 @@ export class SpriteBase {
2020
frame_names?: {[animation: string]: string[]};
2121
};
2222
};
23+
private action_aliases: {
24+
[alias: string]: string;
25+
};
2326

2427
constructor(data, key_name, actions) {
2528
this.data = data;
2629
this.key_name = key_name;
2730
this.actions = {};
31+
this.action_aliases = {};
2832
for (let i = 0; i < actions.length; ++i) {
2933
this.actions[actions[i]] = {};
3034
}
@@ -35,10 +39,21 @@ export class SpriteBase {
3539
}
3640

3741
setActionAnimations(action, animations, frame_counts) {
42+
if (!this.actions.hasOwnProperty(action)) {
43+
this.data.logger.log_message(`Sprite '${this.key_name}' has no action '${action}'.`, msg_types.ERROR);
44+
return;
45+
}
3846
this.actions[action].animations = new Array(animations.length);
3947
this.actions[action].frame_counts = new Array(animations.length);
4048
this.actions[action].frame_names = {};
4149
const frame_count_is_array = Array.isArray(frame_counts);
50+
if (frame_count_is_array && frame_counts.length !== animations.length) {
51+
this.data.logger.log_message(
52+
`Frames count and animations size must match. Issue on sprite '${this.key_name}' for action action '${action}'.`,
53+
msg_types.ERROR
54+
);
55+
return;
56+
}
4257
for (let i = 0; i < animations.length; ++i) {
4358
const frame_count = frame_count_is_array ? frame_counts[i] : frame_counts;
4459
this.actions[action].animations[i] = animations[i];
@@ -47,6 +62,17 @@ export class SpriteBase {
4762
}
4863

4964
setActionFrameRate(action, frame_rate) {
65+
if (!this.actions.hasOwnProperty(action)) {
66+
this.data.logger.log_message(`Sprite '${this.key_name}' has no action '${action}'.`, msg_types.ERROR);
67+
return;
68+
}
69+
if (!Array.isArray(frame_rate) && !(typeof frame_rate === "number")) {
70+
this.data.logger.log_message(
71+
`Invalid frame rate set for action '${action}' for sprite '${this.key_name}'.`,
72+
msg_types.ERROR
73+
);
74+
return;
75+
}
5076
this.actions[action].frame_rate = {};
5177
for (let i = 0; i < this.actions[action].animations.length; ++i) {
5278
const animation = this.actions[action].animations[i];
@@ -60,6 +86,13 @@ export class SpriteBase {
6086
} else {
6187
this_frame_rate = frame_rate;
6288
}
89+
if (this_frame_rate === undefined) {
90+
this.data.logger.log_message(
91+
`Invalid frame rate set for action '${action}' for sprite '${this.key_name}'.`,
92+
msg_types.ERROR
93+
);
94+
continue;
95+
}
6396
this.actions[action].frame_rate[animation] = this_frame_rate;
6497
}
6598
}
@@ -79,9 +112,17 @@ export class SpriteBase {
79112
if (reference_action in this.actions) {
80113
this.actions[alias] = this.actions[reference_action];
81114
game.cache.setCacheAlias(this.getSpriteKey(alias), this.getSpriteKey(reference_action), Phaser.Cache.IMAGE);
115+
this.action_aliases[alias] = reference_action;
82116
}
83117
}
84118

119+
getActionRefFromAlias(alias) {
120+
if (alias in this.action_aliases) {
121+
return this.action_aliases[alias];
122+
}
123+
return null;
124+
}
125+
85126
hasAction(action: string) {
86127
return action in this.actions;
87128
}
@@ -96,7 +137,7 @@ export class SpriteBase {
96137
const action_key = this.getSpriteKey(action);
97138
if (game.cache.checkImageKey(action_key)) {
98139
this.data.logger.log_message(
99-
`Sprite key '<key_name>/<action>' '${action_key} is already registered in the engine. Please consider renaming it.'`,
140+
`Sprite key '<key_name>/<action>' '${action_key}' is already registered in the engine. Please consider renaming it.`,
100141
msg_types.ERROR
101142
);
102143
}
@@ -125,14 +166,24 @@ export class SpriteBase {
125166
for (let i = 0; i < animations.length; ++i) {
126167
const animation = animations[i];
127168
const frame_rate = this.actions[action].frame_rate[animation];
128-
const anim_key = this.getAnimationKey(action, animation);
169+
let anim_key = this.getAnimationKey(action, animation);
129170
sprite.animations.add(
130171
anim_key,
131172
this.actions[action].frame_names[animation],
132173
frame_rate,
133174
Array.isArray(loop) ? loop[i] : loop,
134175
false
135176
);
177+
const ref_action = this.getActionRefFromAlias(action);
178+
if (ref_action) {
179+
anim_key = this.getAnimationKey(ref_action, animation);
180+
}
181+
if (!sprite.animations.frameData.getFrameByName(`${anim_key}${SpriteBase.ACTION_ANIM_SEPARATOR}00`)) {
182+
this.data.logger.log_message(
183+
`Animation '${anim_key}' is not valid for action '${action}' for sprite '${this.key_name}'.`,
184+
msg_types.ERROR
185+
);
186+
}
136187
}
137188
} else {
138189
this.data.logger.log_message(`Action '${action}' not available for '${this.key_name}'.`);

base/game_events/GameEventManager.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,10 @@ export class GameEventManager {
161161
* Look for valid npcs in the nearby.
162162
*/
163163
private search_for_npc() {
164+
const close_npcs: {
165+
npc: NPC;
166+
distance: number;
167+
}[] = [];
164168
for (let i = 0; i < this.data.map.npcs.length; ++i) {
165169
const npc = this.data.map.npcs[i];
166170
if (
@@ -170,8 +174,17 @@ export class GameEventManager {
170174
) {
171175
continue;
172176
}
173-
const is_close_check = this.data.hero.is_close(npc);
174-
if (is_close_check) {
177+
const npc_distance = this.data.hero.is_close(npc);
178+
if (npc_distance) {
179+
close_npcs.push({
180+
npc: npc,
181+
distance: npc_distance,
182+
});
183+
}
184+
}
185+
if (close_npcs.length) {
186+
for (let close_npc_data of _.sortBy(close_npcs, close_npc_data => close_npc_data.distance)) {
187+
const npc = close_npc_data.npc;
175188
if (
176189
npc.allow_only_front_interaction &&
177190
(npc.tile_pos.y + 1 !== this.data.hero.tile_pos.y || npc.tile_pos.x !== this.data.hero.tile_pos.x)

static/phaser.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73458,7 +73458,7 @@ Phaser.FrameData.prototype = {
7345873458
*/
7345973459
checkFrameName: function (name)
7346073460
{
73461-
if (this._frameNames[name] == null)
73461+
if (this._frameNames[name] == null || this._frameNames[name] === undefined)
7346273462
{
7346373463
return false;
7346473464
}

0 commit comments

Comments
 (0)