Skip to content

Commit c94a05b

Browse files
committed
elephant pic and sounds
1 parent 8b075de commit c94a05b

File tree

5 files changed

+62
-13
lines changed

5 files changed

+62
-13
lines changed

public/img/Ele.jpg

5.95 MB
Loading

public/sound/elephant.m4a

127 KB
Binary file not shown.

src/gameComponent.ts

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,26 @@ export default class GameComponent extends BaseComponent {
2727
this.build();
2828
}
2929

30+
private _soundsrc = "";
31+
get soundsrc(): string {
32+
return this._soundsrc;
33+
}
34+
set soundsrc(value: string) {
35+
this._soundsrc = value;
36+
console.log("game: soundsrc set to: " + value);
37+
this.build();
38+
}
39+
40+
private _soundSpeed = 1.0;
41+
get soundSpeed(): number {
42+
return this._soundSpeed;
43+
}
44+
set soundSpeed(value: number) {
45+
this._soundSpeed = value;
46+
console.log("game: soundSpped set to: " + value);
47+
this.build();
48+
}
49+
3050
build = () => {
3151
if (!this.myShadow) {
3252
console.log("game called without shadow " + this.imagesrc);
@@ -87,15 +107,45 @@ export default class GameComponent extends BaseComponent {
87107
console.log("backtile clicked");
88108
window.location.reload(); // for now enough
89109
};
110+
111+
this.playSound();
90112
};
91113
connectedCallback() {
92114
this.myShadow = this.attachShadow({ mode: "open" }); // closed means it cannot be accessed from the outside
93115
this.build();
94116
}
117+
118+
119+
playSound = () => {
120+
try {
121+
const audio = document.getElementById("my-audio") as HTMLAudioElement;
122+
if(!audio){
123+
return;
124+
}
125+
126+
audio.pause();
127+
audio.muted = true;
128+
audio.playbackRate = 1.0;
129+
130+
if(!this.soundsrc){
131+
return;
132+
}
133+
134+
audio.src = this.soundsrc;
135+
if(this.soundSpeed){
136+
audio.playbackRate = this.soundSpeed;
137+
}
138+
audio.muted = false;
139+
audio.play();
140+
} catch (e: any) {
141+
console.log("error playing audio " + e.message); // will happen when user has not interacted with document (to be improved)
142+
}
143+
};
144+
95145
//#region static webcomponent methods
96146

97147
static get observedAttributes() {
98-
return [nameof<GameComponent>("name"), nameof<GameComponent>("imagesrc")];
148+
return [nameof<GameComponent>("name"), nameof<GameComponent>("imagesrc"), nameof<GameComponent>("soundsrc")];
99149
}
100150

101151
//#endregion

src/main.ts

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import StackComponent from "./stackComponent.js";
55
interface IGameInfo {
66
type?: string;
77
imageSrc?: string;
8+
soundSrc?: string;
9+
soundSpeed?: number;
810
}
911

1012
const init = () => {
@@ -16,16 +18,6 @@ const init = () => {
1618

1719
const tiles = getGameTiles();
1820
document.getElementById("tile-container")?.append(...tiles);
19-
20-
// start audio (will only work if user interacts with the page.. so initially no music)
21-
try {
22-
const audio = document.getElementById("my-audio") as HTMLAudioElement;
23-
audio.playbackRate = 2.0;
24-
audio.muted = false;
25-
audio.play();
26-
} catch (e: any) {
27-
console.log("error playing audio " + e.message); // will happen when user has not interacted with document (to be improved)
28-
}
2921
};
3022

3123
const getComponentContainer = () => {
@@ -41,7 +33,7 @@ const startGame = (tile?: TileComponent<IGameInfo>) => {
4133
);
4234
const componentContainer = getComponentContainer();
4335
if (tile?.data?.imageSrc) {
44-
componentContainer.innerHTML = `<a-game name="${tile.name}" imageSrc="${tile.data.imageSrc}"></a-game>`;
36+
componentContainer.innerHTML = `<a-game name="${tile.name}" imageSrc="${tile.data.imageSrc}" soundSrc="${tile.data.soundSrc}" soundSpeed="${tile.data.soundSpeed} ></a-game>`;
4537
}
4638
};
4739

@@ -85,7 +77,7 @@ const getGameTiles = () => {
8577

8678
tiles.push(
8779
new TileComponent<IGameInfo>("Die Familie", startGame, "green", "lime", {
88-
imageSrc: "img/almostEverybody.jpg",
80+
imageSrc: "img/almostEverybody.jpg", soundSrc: "sound/handflute.m4a", soundSpeed: 2.0
8981
})
9082
);
9183
tiles.push(
@@ -112,6 +104,13 @@ const getGameTiles = () => {
112104
})
113105
);
114106

107+
tiles.push(
108+
new TileComponent<IGameInfo>("Gross und grau", startGame, "gray", "silver", {
109+
imageSrc: "img/Ele.jpg", soundSrc: "sound/elephant.m4a"
110+
})
111+
);
112+
113+
115114
return tiles;
116115
};
117116

0 commit comments

Comments
 (0)