veridianmeander.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. include("system.js");
  2. include("mainmenu.js");
  3. include("world.js");
  4. function VeridianWander() {
  5. var theater = null;
  6. this.theater = null;
  7. this.canvas = null;
  8. this.system = null;
  9. this.state = "";
  10. this.stage = null;
  11. this.stages = {};
  12. this.playerSocket = null;
  13. this.isConnected = false;
  14. this.websocketConnectionString = "ws://"+window.location.hostname+":8080";
  15. this.init = function(systemObject, canvas) {
  16. this.theater = this;
  17. theater = this;
  18. this.system = systemObject;
  19. this.canvas = canvas;
  20. var urlParams = new URLSearchParams(window.location.search);
  21. this.websocketConnectionString = urlParams.has("socket") ? urlParams.get("socket") : this.websocketConnectionString;
  22. this.stages["mainmenu"] = new MainMenu();
  23. this.stages["world"] = new World();
  24. this.changeStage("mainmenu");
  25. };
  26. this.loadAssets = function(callback) {
  27. var assetFiles = {
  28. "charsprite": "./images/roguelikeChar_transparent.png",
  29. "mapsprite":"./images/roguelikeSheet_transparent.png"
  30. };
  31. system.assets.load(assetFiles, callback);
  32. }
  33. this.changeStage = function(newstage) {
  34. if(this.state == newstage) {
  35. return;
  36. }
  37. if(this.stage != null && this.stage.hasOwnProperty("end")) {
  38. this.stage.end(function() {
  39. theater.state = newstage;
  40. theater.stage = theater.stages[theater.state];
  41. theater.stage.init(theater.system, theater.canvas);
  42. });
  43. } else {
  44. theater.state = newstage;
  45. theater.stage = theater.stages[theater.state];
  46. theater.stage.init(theater.system, theater.canvas);
  47. }
  48. }
  49. this.updateDelta = function(delta, canvas){
  50. this.stage.updateDelta(delta, canvas);
  51. };
  52. this.draw = function(context) {
  53. this.stage.draw(context);
  54. };
  55. this.handleResize = function(canvas) {
  56. if(!this.stage.hasOwnProperty("handleResize")) { return; }
  57. this.stage.handleResize(canvas);
  58. };
  59. this.handleServerMessage = function(event) {
  60. if(!this.stage.hasOwnProperty("handleServerMessage")) { return; }
  61. this.stage.handleServerMessage(event);
  62. }
  63. this.socketConnect = function(callback, failcallback, closecallback) {
  64. this.playerSocket = new WebSocket(this.websocketConnectionString);
  65. this.playerSocket.onmessage = function(event) {
  66. system.theater.handleServerMessage(JSON.parse(event.data));
  67. };
  68. this.playerSocket.onerror = function(event) {
  69. system.theater.isConnected = false;
  70. failcallback();
  71. };
  72. this.playerSocket.onopen = function(event) {
  73. system.theater.isConnected = true;
  74. callback();
  75. };
  76. this.playerSocket.onclose = function(event) {
  77. system.theater.isConnected = false;
  78. closecallback();
  79. }
  80. }
  81. this.socketSend = function(data) {
  82. if(!this.isConnected || this.playerSocket == null) {
  83. return;
  84. }
  85. var sendData = data;
  86. if(typeof data == "object") {
  87. sendData = JSON.stringify(data);
  88. }
  89. this.playerSocket.send(sendData);
  90. }
  91. this.socketDisconnect = function() {
  92. if(this.playerSocket == null) {
  93. return;
  94. }
  95. this.playerSocket.close();
  96. this.playerSocket = null;
  97. }
  98. };
  99. VeridianWander.attach = function(domContainer, backgroundColor) {
  100. loader.finishedLoading(function() {
  101. window.system = new System();
  102. system.create(domContainer, new VeridianWander(), backgroundColor);
  103. });
  104. }