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