Skip to content

Commit

Permalink
improved warning msgs for SpriteBase, set limit of warnings in logger.
Browse files Browse the repository at this point in the history
  • Loading branch information
jjppof committed Jun 7, 2023
1 parent e8f68ca commit 499979e
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 21 deletions.
19 changes: 19 additions & 0 deletions base/Logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,14 @@ export enum msg_types {
* a log file inside logs directory.
*/
export class Logger {
private static readonly MESSAGE_NUMBER_LIMIT = 15;

private data: GoldenSun;
private msgs_count: {[msg: string]: number};

constructor(data: GoldenSun) {
this.data = data;
this.msgs_count = {};
this.log_message("Engine started.", msg_types.INFO);
}

Expand All @@ -24,6 +28,21 @@ export class Logger {
return;
}
const now = new Date().toLocaleString();
if (message in this.msgs_count) {
++this.msgs_count[message];
if (this.msgs_count[message] === Logger.MESSAGE_NUMBER_LIMIT) {
message = `GSHTML5 - [${now}] [WARNING]: limit for '${message}' message reached.`;
console.warn(message);
if (this.data.electron_app) {
this.data.ipcRenderer.send("register-log", message);
}
return;
} else if (this.msgs_count[message] > Logger.MESSAGE_NUMBER_LIMIT) {
return;
}
} else {
this.msgs_count[message] = 0;
}
switch (type) {
case msg_types.INFO:
message = `GSHTML5 - [${now}] [INFO]: ${message}`;
Expand Down
58 changes: 42 additions & 16 deletions base/SpriteBase.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import {GoldenSun} from "GoldenSun";

/**
* This class holds all the info related to spritesheet data, animations, actions
* and the appropriate methods to generate this info.
*/
export class SpriteBase {
public static readonly ACTION_ANIM_SEPARATOR = "/";

private data: GoldenSun;
public key_name: string;
private actions: {
[action: string]: {
Expand All @@ -17,7 +20,8 @@ export class SpriteBase {
};
};

constructor(key_name, actions) {
constructor(data, key_name, actions) {
this.data = data;
this.key_name = key_name;
this.actions = {};
for (let i = 0; i < actions.length; ++i) {
Expand Down Expand Up @@ -108,19 +112,23 @@ export class SpriteBase {
}

setAnimation(sprite: Phaser.Sprite, action) {
const animations = this.actions[action].animations;
const loop = this.actions[action].loop ?? true;
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);
sprite.animations.add(
anim_key,
this.actions[action].frame_names[animation],
frame_rate,
Array.isArray(loop) ? loop[i] : loop,
false
);
if (action in this.actions) {
const animations = this.actions[action].animations;
const loop = this.actions[action].loop ?? true;
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);
sprite.animations.add(
anim_key,
this.actions[action].frame_names[animation],
frame_rate,
Array.isArray(loop) ? loop[i] : loop,
false
);
}
} else {
this.data.logger.log_message(`Action '${action}' not available for '${this.key_name}'.`);
}
}

Expand All @@ -141,11 +149,29 @@ export class SpriteBase {
}

getFrameRate(action, animation) {
return this.actions[action].frame_rate[animation];
if (action in this.actions) {
if (animation in this.actions[action].frame_rate) {
return this.actions[action].frame_rate[animation];
} else {
this.data.logger.log_message(`Animation '${animation}' not available for '${this.key_name}'.`);
}
} else {
this.data.logger.log_message(`Action '${action}' not available for '${this.key_name}'.`);
}
return null;
}

getFrameNumber(action, animation) {
return this.actions[action].frame_names[animation].length;
if (action in this.actions) {
if (animation in this.actions[action].frame_names) {
return this.actions[action].frame_names[animation].length;
} else {
this.data.logger.log_message(`Animation '${animation}' not available for '${this.key_name}'.`);
}
} else {
this.data.logger.log_message(`Action '${action}' not available for '${this.key_name}'.`);
}
return null;
}

getSpriteKey(action) {
Expand Down
2 changes: 1 addition & 1 deletion base/initializers/enemies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export function initialize_enemies(
data: enemies_db[i],
sprite_base: null,
};
info.sprite_base = new SpriteBase(info.data.key_name, [base_actions.BATTLE]);
info.sprite_base = new SpriteBase(data, info.data.key_name, [base_actions.BATTLE]);

const battle_spritesheet = info.data.battle_spritesheet;
if (battle_spritesheet !== undefined) {
Expand Down
2 changes: 1 addition & 1 deletion base/initializers/interactable_objects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export function initialize_interactable_objs_data(
}
if (iter_obj_data.actions) {
const actions = Object.keys(iter_obj_data.actions);
const sprite_base = new SpriteBase(iter_obj_data.key_name, actions);
const sprite_base = new SpriteBase(data, iter_obj_data.key_name, actions);
iter_obj_sprite_base_list[iter_obj_data.key_name] = sprite_base;
for (let i = 0; i < actions.length; ++i) {
const action_key = actions[i];
Expand Down
3 changes: 2 additions & 1 deletion base/initializers/main_chars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,12 @@ export function initialize_main_chars(
continue;
}
const char_db = npc_db[char_data.key_name];
const sprite_base = new SpriteBase(char_data.key_name, Object.keys(char_db.actions));
const sprite_base = new SpriteBase(data, char_data.key_name, Object.keys(char_db.actions));
const char_snapshot_data = snapshot?.main_chars.find(
member_info => member_info.key_name === char_data.key_name
);
const weapons_sprite_base = new SpriteBase(
data,
`${char_data.key_name}_weapons`,
Object.keys(char_data.weapons_sprites)
);
Expand Down
2 changes: 1 addition & 1 deletion base/initializers/misc_data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export function initialize_misc_data(
const misc_sprite_base_list: GameInfo["misc_sprite_base_list"] = {};
for (let misc_key in misc_db) {
const misc_data = misc_db[misc_key];
const sprite_base = new SpriteBase(misc_data.key_name, Object.keys(misc_data.actions));
const sprite_base = new SpriteBase(data, misc_data.key_name, Object.keys(misc_data.actions));
misc_sprite_base_list[misc_data.key_name] = sprite_base;
for (let action_key in misc_data.actions) {
const action = misc_data.actions[action_key];
Expand Down
2 changes: 1 addition & 1 deletion base/initializers/npcs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export function initialize_npcs_data(
const npc_info = npc_db[npc_key];
if (npc_info.actions === undefined) continue;
const actions = Object.keys(npc_info.actions);
const npc_sprite_info = new SpriteBase(npc_key, actions);
const npc_sprite_info = new SpriteBase(data, npc_key, actions);
npc_sprite_base_list[npc_key] = npc_sprite_info;
for (let i = 0; i < actions.length; ++i) {
const action_key = actions[i];
Expand Down

0 comments on commit 499979e

Please sign in to comment.