include("system.js"); include("mainmenu.js"); include("world.js"); function VeridianWander() { var theater = null; this.theater = null; this.canvas = null; this.system = null; this.state = ""; this.stage = null; this.stages = {}; this.playerSocket = null; this.isConnected = false; this.websocketConnectionString = "ws://"+window.location.hostname+":8080"; this.init = function(systemObject, canvas) { this.theater = this; theater = this; this.system = systemObject; this.canvas = canvas; var urlParams = new URLSearchParams(window.location.search); this.websocketConnectionString = urlParams.has("socket") ? urlParams.get("socket") : this.websocketConnectionString; this.stages["mainmenu"] = new MainMenu(); this.stages["world"] = new World(); this.changeStage("mainmenu"); }; this.loadAssets = function(callback) { var assetFiles = { "charsprite": "./images/roguelikeChar_transparent.png", "mapsprite":"./images/roguelikeSheet_transparent.png" }; system.assets.load(assetFiles, callback); } this.changeStage = function(newstage) { if(this.state == newstage) { return; } if(this.stage != null && this.stage.hasOwnProperty("end")) { this.stage.end(function() { theater.state = newstage; theater.stage = theater.stages[theater.state]; theater.stage.init(theater.system, theater.canvas); }); } else { theater.state = newstage; theater.stage = theater.stages[theater.state]; theater.stage.init(theater.system, theater.canvas); } } this.updateDelta = function(delta, canvas){ this.stage.updateDelta(delta, canvas); }; this.draw = function(context) { this.stage.draw(context); }; this.handleResize = function(canvas) { if(!this.stage.hasOwnProperty("handleResize")) { return; } this.stage.handleResize(canvas); }; this.handleServerMessage = function(event) { if(!this.stage.hasOwnProperty("handleServerMessage")) { return; } this.stage.handleServerMessage(event); } this.socketConnect = function(callback, failcallback, closecallback) { this.playerSocket = new WebSocket(this.websocketConnectionString); this.playerSocket.onmessage = function(event) { system.theater.handleServerMessage(JSON.parse(event.data)); }; this.playerSocket.onerror = function(event) { system.theater.isConnected = false; failcallback(); }; this.playerSocket.onopen = function(event) { system.theater.isConnected = true; callback(); }; this.playerSocket.onclose = function(event) { system.theater.isConnected = false; closecallback(); } } this.socketSend = function(data) { if(!this.isConnected || this.playerSocket == null) { return; } var sendData = data; if(typeof data == "object") { sendData = JSON.stringify(data); } this.playerSocket.send(sendData); } this.socketDisconnect = function() { if(this.playerSocket == null) { return; } this.playerSocket.close(); this.playerSocket = null; } }; VeridianWander.attach = function(domContainer, backgroundColor) { loader.finishedLoading(function() { window.system = new System(); system.create(domContainer, new VeridianWander(), backgroundColor); }); }