Skip to content

Commit 616cc5d

Browse files
committed
add support for Games
1 parent 17b9550 commit 616cc5d

87 files changed

Lines changed: 23428 additions & 4 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

_layouts/post.html

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1515,10 +1515,6 @@ <h3>
15151515
{%- endif -%}
15161516
<a class="u-url" href="{{ page.url | relative_url }}" hidden></a>
15171517

1518-
<!-- Enhanced Analytics Tracking -->
1519-
<script src="{{ site.baseurl }}/assets/js/ocs-analytics-enhanced.js"></script>
1520-
<script src="{{ site.baseurl }}/assets/js/code-runner-analytics.js"></script>
1521-
<script src="{{ site.baseurl }}/assets/js/lesson-completion.js"></script>
15221518
</article>
15231519

15241520
{% endif %}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { Transform } from "./Transform";
2+
3+
export class Camera {
4+
constructor(x, y) {
5+
this.transform = new Transform(x, y);
6+
}
7+
8+
follow(target) {
9+
10+
}
11+
}
12+
13+
export const camera = new Camera(0, 0);
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
// combined game.js files from adventure and mansion games
2+
3+
class GameCore {
4+
constructor(environment, GameControlClass) {
5+
this.environment = environment;
6+
this.path = environment.path;
7+
this.gameContainer = environment.gameContainer;
8+
this.gameCanvas = environment.gameCanvas;
9+
this.pythonURI = environment.pythonURI;
10+
this.javaURI = environment.javaURI;
11+
this.fetchOptions = environment.fetchOptions;
12+
this.uid = null;
13+
this.id = null;
14+
this.gname = null;
15+
16+
// Snapshot the starting level list so we can reliably reset after the final level
17+
this.initialLevelClasses = [...(environment.gameLevelClasses || [])];
18+
19+
this.initUser();
20+
const gameLevelClasses = [...this.initialLevelClasses];
21+
// create GameControl using the engine-provided class
22+
this.gameControl = new GameControlClass(this, gameLevelClasses);
23+
this.gameControl.start();
24+
25+
// Try to dynamically load the centralized PauseMenu (prefer the shared one)
26+
import('./PauseMenu.js')
27+
.then(mod => {
28+
try {
29+
// Allow GameControl to specify PauseMenu options (like counterVar/counterLabel)
30+
const pmOptions = Object.assign({ parentId: 'gameContainer' }, this.gameControl.pauseMenuOptions || {});
31+
new mod.default(this.gameControl, pmOptions);
32+
} catch (e) { console.warn('PauseMenu init failed:', e); }
33+
})
34+
.catch(() => {
35+
// no-op: PauseMenu is optional
36+
});
37+
38+
// Try to dynamically load the Leaderboard
39+
import('./Leaderboard.js')
40+
.then(mod => {
41+
try {
42+
// Get the actual container element from gameContainer
43+
let parentId = 'gameContainer'; // default
44+
45+
// If gameContainer is a string ID, use it directly
46+
if (typeof this.gameContainer === 'string') {
47+
parentId = this.gameContainer;
48+
}
49+
// If gameContainer is an HTMLElement, get its ID
50+
else if (this.gameContainer instanceof HTMLElement) {
51+
parentId = this.gameContainer.id || 'gameContainer';
52+
}
53+
54+
new mod.default(this.gameControl, {
55+
gameName: 'AdventureGame', // Change to your game name
56+
parentId: parentId // Use the dynamic container ID
57+
});
58+
}
59+
catch (e) { console.warn('Leaderboard init failed:', e); }
60+
})
61+
.catch(() => {
62+
// no-op: Leaderboard is optional
63+
});
64+
}
65+
66+
static main(environment, GameControlClass) {
67+
return new GameCore(environment, GameControlClass);
68+
}
69+
70+
returnHome() {
71+
if (!this.gameControl || !this.initialLevelClasses.length) return;
72+
73+
try {
74+
if (this.gameControl.currentLevel && typeof this.gameControl.currentLevel.destroy === 'function') {
75+
this.gameControl.currentLevel.destroy();
76+
}
77+
this.gameControl.cleanupInteractionHandlers();
78+
} catch (error) {
79+
console.error("Error during cleanup when returning home:", error);
80+
}
81+
82+
// Restore the original level order and restart from the first one
83+
this.gameControl.levelClasses = [...this.initialLevelClasses];
84+
this.gameControl.currentLevelIndex = 0;
85+
this.gameControl.isPaused = false;
86+
this.gameControl.transitionToLevel();
87+
}
88+
89+
loadNextLevel() {
90+
if (this.gameControl && this.gameControl.currentLevel) {
91+
this.gameControl.currentLevel.continue = false;
92+
console.log("Loading next level...");
93+
} else {
94+
console.warn("GameControl or currentLevel not available");
95+
}
96+
}
97+
98+
loadPreviousLevel() {
99+
if (this.gameControl && this.gameControl.currentLevelIndex > 0) {
100+
try {
101+
this.gameControl.currentLevel.destroy();
102+
this.gameControl.cleanupInteractionHandlers();
103+
} catch (error) {
104+
console.error("Error during cleanup when returning home:", error);
105+
}
106+
this.gameControl.currentLevelIndex--;
107+
this.gameControl.transitionToLevel();
108+
} else {
109+
console.warn("No previous level to load");
110+
}
111+
}
112+
113+
initUser() {
114+
const pythonURL = this.pythonURI + '/api/id';
115+
fetch(pythonURL, this.fetchOptions)
116+
.then(response => {
117+
if (response.status !== 200) {
118+
console.error("HTTP status code: " + response.status);
119+
return null;
120+
}
121+
return response.json();
122+
})
123+
.then(data => {
124+
if (!data) return;
125+
this.uid = data.uid;
126+
127+
const javaURL = this.javaURI + '/rpg_answer/person/' + this.uid;
128+
return fetch(javaURL, this.fetchOptions);
129+
})
130+
.then(response => {
131+
if (!response || !response.ok) {
132+
throw new Error(`Spring server response: ${response?.status}`);
133+
}
134+
return response.json();
135+
})
136+
.then(data => {
137+
if (!data) return;
138+
this.id = data.id;
139+
})
140+
.catch(error => {
141+
console.error("Error:", error);
142+
});
143+
}
144+
}
145+
146+
export default {
147+
main: (environment, GameControlClass) => GameCore.main(environment, GameControlClass)
148+
};

0 commit comments

Comments
 (0)