123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- 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);
-
- });
- }
|