Skip to content

Commit 8afd9df

Browse files
committed
improved sounds, logging
1 parent 9e9cb91 commit 8afd9df

29 files changed

+485
-242
lines changed

Parts/AreaTrigger.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export class AreaTrigger extends Part {
1818
super.act(delta);
1919
const collider = this.sibling<Collider>("Collider");
2020
if (!collider) {
21-
console.warn(`AreaTrigger <${this.name}> requires a Collider sibling.`);
21+
this.top?.warn(`AreaTrigger <${this.name}> requires a Collider sibling.`);
2222
return;
2323
}
2424

Parts/Camera.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export class Camera extends Part {
4040
// Could be used in rendering context
4141
const transform = this.child<Transform>("Transform")
4242
if (!transform) {
43-
console.warn(`Camera <${this.name}> (${this.id}) does not have a Transform component. View matrix will not be calculated.`);
43+
this.top?.warn(`Camera <${this.name}> (${this.id}) does not have a Transform component. View matrix will not be calculated.`);
4444
return { offset: Vector.From(0), scale: this.zoom };
4545
}
4646
return {
@@ -55,7 +55,7 @@ export class Camera extends Part {
5555
if (transform) {
5656
this.zoom = transform.scale;
5757
} else {
58-
console.warn(`Camera <${this.name}> (${this.id}) does not have a Transform component. Camera zoom will not be updated.`);
58+
this.top?.warn(`Camera <${this.name}> (${this.id}) does not have a Transform component. Camera zoom will not be updated.`);
5959
}
6060
}
6161

Parts/Children/AnimatedSprite.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ export class AnimatedSprite extends Renderer {
8888
}
8989

9090
image.onerror = (err) => {
91-
console.error(`Failed to load spritesheet image <${spritesheetData.meta.image}>:`, err);
91+
this.top?.error(`Failed to load spritesheet image <${spritesheetData.meta.image}>:`, err);
9292
this.ready = false;
9393
};
9494
this.spritesheetData = spritesheetData; // Store the parsed spritesheet data
@@ -99,7 +99,7 @@ export class AnimatedSprite extends Renderer {
9999
resolve();
100100
};
101101
image.onerror = (err) => {
102-
console.error(`Failed to load spritesheet image <${spritesheetData.meta.image}>:`, err);
102+
this.top?.error(`Failed to load spritesheet image <${spritesheetData.meta.image}>:`, err);
103103
this.ready = false;
104104
reject(err);
105105
};
@@ -120,7 +120,7 @@ export class AnimatedSprite extends Renderer {
120120
resolve();
121121
};
122122
frame.onerror = (err) => {
123-
console.error(`Failed to load frame at index ${i} for animated sprite <${this.name}>:`, err);
123+
this.top?.error(`Failed to load frame at index ${i} for animated sprite <${this.name}>:`, err);
124124
reject(err);
125125
};
126126
}));
@@ -141,12 +141,12 @@ export class AnimatedSprite extends Renderer {
141141
}
142142
frame(index: number): HTMLImageElement | null {
143143
if (!this.loadedSheet || !this.spritesheetData) {
144-
console.warn("AnimatedSprite is not ready or spritesheet data is missing.");
144+
this.top?.warn("AnimatedSprite is not ready or spritesheet data is missing.");
145145
return null;
146146
}
147147
const frameData = this.spritesheetData.frames[index];
148148
if (!frameData) {
149-
console.warn(`${this.name} attached to ${this.parent?.name} frame at index ${index} was indexed but does not exist in spritesheet`);
149+
this.top?.warn(`${this.name} attached to ${this.parent?.name} frame at index ${index} was indexed but does not exist in spritesheet`);
150150
return null;
151151
}
152152
const { x, y, w, h } = frameData.frame;
@@ -159,7 +159,7 @@ export class AnimatedSprite extends Renderer {
159159
canvas.height = sourceSize!.h;
160160
const ctx = canvas.getContext("2d");
161161
if (!ctx) {
162-
console.error("Failed to get canvas context.");
162+
this.top?.error("Failed to get canvas context.");
163163
return null;
164164
}
165165

@@ -193,7 +193,7 @@ export class AnimatedSprite extends Renderer {
193193
this.spritesheetData.meta.animations[this.currentAnimation].loop = loop;
194194
}
195195
} else {
196-
console.warn(`Animation '${animationName}' does not exist in spritesheet for animated sprite <${this.name}> attached to ${this.parent?.name}.`);
196+
this.top?.warn(`Animation '${animationName}' does not exist in spritesheet for animated sprite <${this.name}> attached to ${this.parent?.name}.`);
197197
}
198198
}
199199
act(delta: number) {
@@ -241,7 +241,7 @@ export class AnimatedSprite extends Renderer {
241241
const transform = this.sibling<Transform>("Transform");
242242
if (!transform) {
243243
if (!this.hasWarnedAboutTransform) {
244-
console.warn(`AnimatedSprite <${this.name}> attached to ${this.parent?.name} does not have a Transform component. Skipping rendering. This will only show once.`);
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.`);
245245
this.hasWarnedAboutTransform = true;
246246
}
247247
return;
@@ -262,7 +262,7 @@ export class AnimatedSprite extends Renderer {
262262
this.top.context.drawImage(frame, -this.width / 2, -this.height / 2, this.width, this.height);
263263
this.top.context.restore();
264264
} else {
265-
console.warn(`Frame (${this.currentAnimation}) index ${this.currentFrameIndex} does not exist for animated sprite <${this.name}> attached to ${this.parent?.name}.`);
265+
this.top?.warn(`Frame (${this.currentAnimation}) index ${this.currentFrameIndex} does not exist for animated sprite <${this.name}> attached to ${this.parent?.name}.`);
266266
}
267267
} else {
268268
throw new Error(`AnimatedSprite <${this.name}> attached to ${this.parent?.name} does not have a context to render to. Ensure it is added to a Game, Scene, or Layer with a game ancestor.`);

Parts/Children/BoxCollider.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export class BoxCollider extends Collider {
2525
super.onMount(parent);
2626
const transform = this.sibling<Transform>("Transform")
2727
if (!transform) {
28-
console.warn(
28+
this.top?.warn(
2929
`BoxCollider <${this.name}> (${this.id}) does not have Transform sibling. Skipping`
3030
);
3131
return;
@@ -92,7 +92,7 @@ export class BoxCollider extends Collider {
9292
// Delegate to PolygonCollider's checkCollision method
9393
return other.checkCollision(this);
9494
}
95-
console.warn("Collision checks are only supported between BoxColliders and PolygonColliders.");
95+
this.top?.warn("Collision checks are only supported between BoxColliders and PolygonColliders.");
9696
return false;
9797
}
9898
get vertices(): Vector[] {

Parts/Children/Button.ts

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,11 @@ export class Button extends Renderer {
2424
this.clickSound = clickSound;
2525
this.hoverSound = hoverSound;
2626
this.activeSound = activeSound;
27+
console.log(clickSound, hoverSound, activeSound);
2728
this.type = "Button";
2829

2930
this.onclick = (event: MouseEvent, input: any) => {
31+
console.log(`Button <${this.name}> clicked!`);
3032
if (this.onClickHandler) {
3133
this.onClickHandler();
3234
}
@@ -39,7 +41,9 @@ export class Button extends Renderer {
3941

4042
this.onhover = () => {
4143
this.isHovered = true;
44+
console.log('hovered', this.hoverSound);
4245
if (this.hoverSound) {
46+
4347
this.hoverSound.play({ clone: true });
4448
}
4549
};
@@ -64,30 +68,37 @@ export class Button extends Renderer {
6468
}
6569
};
6670
}
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+
}
6786
onMount(parent: Part) {
6887
super.onMount(parent);
6988
if (!this.sibling("Transform")) {
70-
console.warn(
89+
this.top?.warn(
7190
`Button <${this.name}> (${this.id}) does not have Transform sibling. Please ensure you add a Transform component before adding a Button.`
7291
);
7392
}
7493

75-
// Ensure an Input instance is attached to the scene
76-
if (this.parent instanceof Scene && !this.parent.sibling("Input")) {
77-
this.parent.addChild(new Input({
78-
key: () => { },
79-
keyup: () => { },
80-
mousemove: () => { },
81-
click: () => { },
82-
}));
83-
}
84-
8594
// Set superficial dimensions based on default styles
8695
const defaultStyle = this.styles?.default;
8796
this.superficialWidth = defaultStyle?.width ?? 100;
8897
this.superficialHeight = defaultStyle?.height ?? 50;
8998
}
90-
99+
setOnClick(onClick: () => void) {
100+
this.onClickHandler = onClick;
101+
}
91102
act(delta: number) {
92103
super.act(delta);
93104

Parts/Children/Collider.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export class Collider extends Part {
1313
onMount(parent: Part) {
1414
super.onMount(parent);
1515
if (!this.sibling("Transform")) {
16-
console.warn(
16+
this.top?.warn(
1717
`Collider <${this.name}> (${this.id}) does not have Transform sibling. Please ensure you add a Transform component before adding a Collider. It will not technically effect functionality, but it is good practice.`
1818
);
1919
return;

Parts/Children/ColorRender.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export class ColorRender extends Renderer {
3636
onMount(parent: Part) {
3737
super.onMount(parent);
3838
if (!this.sibling("Transform")) {
39-
console.warn(`ColorRender <${this.name}> does not have a Transform sibling. Please ensure you add a Transform component before adding others.`);
39+
this.top?.warn(`ColorRender <${this.name}> does not have a Transform sibling. Please ensure you add a Transform component before adding others.`);
4040
return;
4141
}
4242

@@ -54,7 +54,7 @@ export class ColorRender extends Renderer {
5454
}
5555
const transform = this.sibling<Transform>("Transform");
5656
if (!transform) {
57-
console.warn(`ColorRender <${this.parent?.name}.${this.name}> does not have a Transform sibling. Skipping rendering.`);
57+
this.top?.warn(`ColorRender <${this.parent?.name}.${this.name}> does not have a Transform sibling. Skipping rendering.`);
5858
return;
5959
}
6060
const position = transform.worldPosition;

Parts/Children/PolygonCollider.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export class PolygonCollider extends Collider {
2525
super.onMount(parent);
2626

2727
if (!this.sibling("Transform")) {
28-
console.warn(
28+
this.top?.warn(
2929
`PolygonCollider <${this.name}> (${this.id}) is not attached to a parent with a Transform component. Please ensure it is mounted to a GameObject with a Transform`
3030
);
3131
return;
@@ -39,7 +39,7 @@ export class PolygonCollider extends Collider {
3939
super.act(delta);
4040

4141
if (!this.sibling("Transform")) {
42-
console.warn(
42+
this.top?.warn(
4343
`PolygonCollider <${this.name}> (${this.id}) is not attached to a parent with a Transform component. Skipping`
4444
);
4545
return;
@@ -121,7 +121,7 @@ export class PolygonCollider extends Collider {
121121
} else if (other instanceof PolygonCollider) {
122122
return this.checkPolygonVsPolygon(this, other);
123123
}
124-
console.warn("Collision checks are only supported between BoxColliders and PolygonColliders.");
124+
this.top?.warn("Collision checks are only supported between BoxColliders and PolygonColliders.");
125125
return false;
126126
}
127127

Parts/Children/SpriteRender.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export class SpriteRender extends Renderer {
1919
this.ready = true;
2020
};
2121
this.image.onerror = (err: any) => {
22-
console.error(`Failed to load image <${this.imageSource}>:`, err);
22+
this.top?.error(`Failed to load image <${this.imageSource}>:`, err);
2323
};
2424
this.image.src = imageSource;
2525

@@ -29,7 +29,7 @@ export class SpriteRender extends Renderer {
2929
onMount(parent: Part) {
3030
super.onMount(parent);
3131
if (!this.sibling("Transform")) {
32-
console.warn(`SpriteRender <${this.name}> requires a Transform component to be mounted to a parent GameObject.`);
32+
this.top?.warn(`SpriteRender <${this.name}> requires a Transform component to be mounted to a parent GameObject.`);
3333
}
3434
this.parent?.setSuperficialDimensions(this.width, this.height);
3535
}

Parts/Children/TextRender.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export class TextRender extends Renderer {
4242
super.act(delta);
4343
const transform = this.sibling<Transform>('Transform');
4444
if (!transform) {
45-
console.warn(`Text <${this.name}> (${this.id}) does not have a Transform sibling. Skipping rendering.`);
45+
this.top?.warn(`Text <${this.name}> (${this.id}) does not have a Transform sibling. Skipping rendering.`);
4646
return;
4747
}
4848

0 commit comments

Comments
 (0)