Skip to content

Commit 5069bdd

Browse files
committed
improve logging
1 parent 952d6a1 commit 5069bdd

File tree

13 files changed

+268
-188
lines changed

13 files changed

+268
-188
lines changed

Parts/Camera.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,11 @@ export class Camera extends Part {
4040
// Could be used in rendering context
4141
const transform = this.child<Transform>("Transform")
4242
if (!transform) {
43-
this.top?.warn(`Camera <${this.name}> (${this.id}) does not have a Transform component. View matrix will not be calculated.`);
43+
if (!this.warned.has("TransformMissing")) {
44+
// Warn only once about missing Transform component
45+
const seen = this.top?.warn(`Camera <${this.name}> (${this.id}) does not have a Transform component. View matrix will not be calculated.`);
46+
if (seen) this.warned.add("TransformMissing");
47+
}
4448
return { offset: Vector.From(0), scale: this.zoom };
4549
}
4650
return {
@@ -55,7 +59,9 @@ export class Camera extends Part {
5559
if (transform) {
5660
this.zoom = transform.scale;
5761
} else {
58-
this.top?.warn(`Camera <${this.name}> (${this.id}) does not have a Transform component. Camera zoom will not be updated.`);
62+
if (!this.warned.has("TransformMissing")) {
63+
this.top?.warn(`Camera <${this.name}> (${this.id}) does not have a Transform component. Camera zoom will not be updated.`) ? this.warned.add("TransformMissing") : null;
64+
}
5965
}
6066
}
6167

Parts/Children/AnimatedSprite.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ export class AnimatedSprite extends Renderer {
99
loadedSheet?: HTMLImageElement; // Loaded image for the spritesheet
1010
frames: Record<string, HTMLImageElement[]> = {}; // Object to hold individual frame images for each animation
1111
currentFrameIndex: number = 0; // Index of the current frame being displayed
12-
hasWarnedAboutTransform: boolean = false; // Flag to prevent multiple warnings about missing Transform part
1312
width: number; // Width of the animated sprite
1413
height: number; // Height of the animated sprite
1514
bouncing: boolean = false; // Flag to indicate if the sprite animation is in reverse (bouncing)
@@ -240,9 +239,9 @@ export class AnimatedSprite extends Renderer {
240239
}
241240
const transform = this.sibling<Transform>("Transform");
242241
if (!transform) {
243-
if (!this.hasWarnedAboutTransform) {
244-
this.top?.warn(`AnimatedSprite <${this.name}> attached to ${this.parent?.name} does not have a Transform component. Skipping rendering. This will only show once.`);
245-
this.hasWarnedAboutTransform = true;
242+
if (!this.warned.has("TransformMissing")) {
243+
const seen = this.top?.warn(`AnimatedSprite <${this.name}> attached to ${this.parent?.name} does not have a Transform component. Skipping rendering. This will only show once.`);
244+
if (seen) this.warned.add("TransformMissing");
246245
}
247246
return;
248247
}

Parts/Children/Button.ts

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { Scene } from "../Scene";
66
import { Renderer } from "./Renderer";
77
import { Sound } from "../Sound";
88
import { isNamedTupleMember } from "typescript";
9+
import type { Camera } from "../Camera";
910

1011
export class Button extends Renderer {
1112
styles?: ButtonStyles;
@@ -24,11 +25,9 @@ export class Button extends Renderer {
2425
this.clickSound = clickSound;
2526
this.hoverSound = hoverSound;
2627
this.activeSound = activeSound;
27-
console.log(clickSound, hoverSound, activeSound);
2828
this.type = "Button";
2929

3030
this.onclick = (event: MouseEvent, input: any) => {
31-
console.log(`Button <${this.name}> clicked!`);
3231
if (this.onClickHandler) {
3332
this.onClickHandler();
3433
}
@@ -41,9 +40,8 @@ export class Button extends Renderer {
4140

4241
this.onhover = () => {
4342
this.isHovered = true;
44-
console.log('hovered', this.hoverSound);
4543
if (this.hoverSound) {
46-
44+
4745
this.hoverSound.play({ clone: true });
4846
}
4947
};
@@ -68,29 +66,13 @@ export class Button extends Renderer {
6866
}
6967
};
7068
}
71-
onRegister(attribute: string, value: any): void {
72-
super.onRegister(attribute, value);
73-
if (attribute == "scene") {
74-
const scene = value as Scene;
75-
if (!scene.child<Input>("Input")) {
76-
const input = new Input({
77-
key: () => {},
78-
keyup: () => {},
79-
mousemove: () => {},
80-
click: () => {}
81-
});
82-
scene.addChild(input);
83-
}
84-
}
85-
}
8669
onMount(parent: Part) {
8770
super.onMount(parent);
8871
if (!this.sibling("Transform")) {
8972
this.top?.warn(
9073
`Button <${this.name}> (${this.id}) does not have Transform sibling. Please ensure you add a Transform component before adding a Button.`
9174
);
9275
}
93-
9476
// Set superficial dimensions based on default styles
9577
const defaultStyle = this.styles?.default;
9678
this.superficialWidth = defaultStyle?.width ?? 100;
@@ -109,7 +91,24 @@ export class Button extends Renderer {
10991
if (!transform) {
11092
throw new Error(`Button <${this.name}> does not have a Transform sibling. Ensure it is mounted to a GameObject with a Transform component.`);
11193
}
112-
94+
const scene = this.registrations["scene"] as Scene | undefined;
95+
if (scene) {
96+
if (!scene.child<Input>("Input")) {
97+
const input = new Input({
98+
key: () => { },
99+
keyup: () => { },
100+
mousemove: () => { },
101+
click: () => { }
102+
});
103+
scene.addChild(input);
104+
}
105+
if (!scene.child<Camera>("Camera") && !this.warned.has("Camera")) {
106+
const seen = this.top?.error(`Button <${this.name}> requires a Camera to function properly. Please add a Camera to the Scene.`);
107+
if (seen) {
108+
this.warned.add("Camera");
109+
}
110+
}
111+
}
113112
const position = transform.worldPosition;
114113
const rotation = transform.rotation;
115114
const scale = transform.scale;

Parts/Game.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ export class Game extends Part {
88
canvas: HTMLCanvasElement;
99
currentScene?: Scene;
1010
childrenArray: Scene[];
11-
hasWarnedActUsage: boolean = false;
1211
devmode: boolean;
1312
context: CanvasRenderingContext2D;
1413
showtoolTips: boolean = false;
@@ -38,6 +37,7 @@ export class Game extends Part {
3837
this.context.imageSmoothingEnabled = !disableAntiAliasing;
3938
this.debugEmoji = "🎮";
4039
this.tooltipLocked = false;
40+
this.top = this;
4141
if (this.devmode) {
4242
let tooltip = document.getElementById("debug-tooltip");
4343
if (!tooltip) {
@@ -170,11 +170,12 @@ export class Game extends Part {
170170

171171
pause() {
172172
this._isPaused = true;
173+
this.debug("Game paused");
173174
SoundManager.pauseGame();
174-
175175
}
176176

177177
resume() {
178+
this.debug("Game resumed");
178179
this._isPaused = false;
179180
SoundManager.resumeGame();
180181
}
@@ -203,9 +204,9 @@ export class Game extends Part {
203204
}
204205

205206
act(purposeful: boolean | number = false) {
206-
if (!this.hasWarnedActUsage && !purposeful) {
207-
this.warn(`Act called on Game <${this.name}>. Use start() to begin the game loop. Calling act() directly will run 1 frame of the current scene. This message will appear only once.`);
208-
this.hasWarnedActUsage = true;
207+
if (!this.warned.has("ActUsage") && !purposeful) {
208+
const seen = this.warn(`Act called on Game <${this.name}>. Use start() to begin the game loop. Calling act() directly will run 1 frame of the current scene. This message will appear only once.`);
209+
if (seen) this.warned.add("ActUsage");
209210
}
210211
if (this.currentScene) {
211212
this.currentScene.act(0);
@@ -229,22 +230,28 @@ export class Game extends Part {
229230
warn (...args: any[]) {
230231
if (this.messageHook && typeof this.messageHook === "function") {
231232
this.messageHook("warn", ...args);
233+
return true;
232234
} else {
233235
console.warn(`[${this.name}] - WARN`, ...args);
236+
return false;
234237
}
235238
}
236239
error (...args: any[]) {
237240
if (this.messageHook && typeof this.messageHook === "function") {
238241
this.messageHook("error", ...args);
242+
return true;
239243
} else {
240244
console.error(`[${this.name}] - ERROR`, ...args);
245+
return false;
241246
}
242247
}
243248
debug (...args: any[]) {
244249
if (this.messageHook && typeof this.messageHook === "function") {
245250
this.messageHook("debug", ...args);
251+
return true;
246252
} else {
247253
console.debug(`[${this.name}]`, ...args);
254+
return false;
248255
}
249256
}
250257
updateDebugToolTip() {

Parts/Input.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,9 @@ export class Input extends Part {
6464
const view = camera.getViewMatrix();
6565
const transform = camera.child<Transform>("Transform");
6666
if (!transform) {
67-
this.top?.warn("Camera does not have a Transform child.");
67+
if (!this.warned.has("TransformMissing")) {
68+
this.top?.warn("Camera does not have a Transform child.") ? this.warned.add("TransformMissing") : null;
69+
}
6870
return;
6971
}
7072
finalX = (mouseX - game.canvas.width / 2) / view.scale.x + transform.worldPosition.x;

Parts/Layer.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { GameObject } from "./GameObject";
22
import { generateUID } from "../helpers";
33
import { Part } from "./Part";
4+
import type { Game } from "./Game";
45

56
export class Layer extends Part {
67
constructor({ name }: { name: string }) {

Parts/Part.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export class Part {
3131
_superficialHeight: number = 0; // General height of object
3232
ties: Set<Tie> = new Set(); // Ties to other parts, allowing for dynamic attribute linking
3333
type: string;
34-
34+
warned: Set<string> = new Set(); // Set to track warnings for this part, preventing duplicate warnings
3535
private _childrenByName: { [name: string]: Part } = {}; // For quick access to children by name
3636
private _childrenByType: { [type: string]: Array<Part> } = {}; // For quick access to children by type
3737
constructor({ name }: { name?: string } = {}) {
@@ -120,9 +120,6 @@ export class Part {
120120
}
121121
onMount(parent: Part) {
122122
this.parent = parent;
123-
for (const [k, v] of Object.entries(parent.registrations)) {
124-
this.setAll(k, v);
125-
}
126123
}
127124
onRegister(attribute: string, value: any) {
128125
// This method can be overridden in subclasses to handle registration logic
@@ -167,6 +164,9 @@ export class Part {
167164
if (this.top) {
168165
child.setTop(this.top); // Set the top-level parent for the child
169166
}
167+
for (const [k, v] of Object.entries(this.registrations)) {
168+
child.setAll(k, v);
169+
}
170170
child.onMount(this);
171171
}
172172
addChildren(...children: Part[]) {

Parts/Scene.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,14 @@ export class Scene extends Part {
1515
addChild(part: Part) {
1616
part.setAll("scene", this);
1717
super.addChild(part);
18+
1819
}
1920
addChildren(...parts: Part[]) {
2021
parts.forEach((part) => this.addChild(part));
2122
}
2223
act(delta: number) {
2324
if (!this.top) {
24-
this.top?.warn(`Act called on Scene <${this.name}> without a top-level parent. Ensure this scene is added to a Game instance before calling act().`);
25+
throw new Error(`Act called on Scene <${this.name}> without a top-level parent. Ensure this scene is added to a Game instance before calling act().`);
2526
}
2627

2728
if (!this.top || !(this.top instanceof Game)) {

0 commit comments

Comments
 (0)