world.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. function World() {
  2. this.game = null;
  3. this.system = null;
  4. this.player = null;
  5. this.mouse = null;
  6. this.characters = [];
  7. this.places = [];
  8. this.camera = null;
  9. this.background = null;
  10. this.dialog = null;
  11. this.curtain = null;
  12. this.isStarted = false;
  13. this.init = function(systemObject, canvas) {
  14. this.game = this;
  15. this.system = systemObject;
  16. this.mouse = new MouseInput();
  17. this.mouse.attach(canvas, this);
  18. this.camera = new Camera();
  19. this.camera.init(canvas);
  20. this.background = new Background(this.camera);
  21. this.background.init();
  22. this.player = new Player(this.game, new HumanController());
  23. this.player.id = 1;
  24. this.player.init();
  25. var brain = function(self) {
  26. if(self.targetPosition.x == self.position.x && self.targetPosition.x == 300) {
  27. self.waitHere = 300;
  28. self.targetPosition.x = 340;
  29. }
  30. if(self.targetPosition.x == self.position.x && self.targetPosition.x == 340) {
  31. self.waitHere = 300;
  32. self.targetPosition.x = 300;
  33. }
  34. };
  35. var newchar = new Character(this.game);
  36. newchar.id = 2;
  37. newchar.init(brain);
  38. this.characters.push(newchar);
  39. brain = function(self) {
  40. if(self.facingAngle == null) {
  41. self.facingAngle = 0;
  42. self.facingLimit = Math.PI / 2;
  43. self.facingDirection = Math.PI / 64;
  44. }
  45. self.facingAngle += self.facingDirection;
  46. if(self.facingDirection > 0 && self.facingAngle > self.facingLimit) {
  47. self.facingLimit = 0;
  48. self.facingDirection *= -1;
  49. self.waitHere = 300;
  50. }
  51. if(self.facingDirection < 0 && self.facingAngle < self.facingLimit) {
  52. self.facingLimit = Math.PI / 2;
  53. self.facingDirection *= -1;
  54. self.waitHere = 300;
  55. }
  56. self.targetAngle = self.facingAngle;
  57. self.angleRadians = self.facingAngle;
  58. /*if(self.facingAngle == Math.PI / 3 || self.facingAngle == null) {
  59. self.waitHere = 300;
  60. self.targetAngle = Math.PI / 4;
  61. self.angleRadians = Math.PI / 4;
  62. self.facingAngle = Math.PI / 4;
  63. }
  64. if(self.facingAngle == Math.PI / 4) {
  65. self.waitHere = 300;
  66. self.targetAngle = Math.PI / 3;
  67. self.angleRadians = Math.PI / 3;
  68. self.facingAngle = Math.PI / 3;
  69. }*/
  70. };
  71. newchar = new Character(this.game);
  72. newchar.id = 3;
  73. newchar.position.x = 540;
  74. newchar.position.y = 540;
  75. newchar.targetPosition.x = 540;
  76. newchar.targetPosition.y = 540;
  77. newchar.init(brain);
  78. this.characters.push(newchar);
  79. var newplace = new Place();
  80. newplace.init();
  81. this.places.push(newplace);
  82. this.dialog = new Dialog(this, this.camera);
  83. this.dialog.init(systemObject, canvas);
  84. this.ui = new HeadsUpDisplay(this.game, this.camera);
  85. this.ui.init(this.player, canvas);
  86. var stage = this;
  87. this.curtain = new Curtain();
  88. this.curtain.init(systemObject, canvas);
  89. this.curtain.open(700, function() {
  90. stage.isStarted = true;
  91. });
  92. };
  93. this.end = function() {
  94. this.mouse.detatch();
  95. }
  96. this.updateDelta = function(delta, canvas){
  97. if(system.keyboard.isDown(Keys.Escape)) {
  98. this.curtain.close(1000, function() {
  99. system.theater.changeStage("mainmenu");
  100. });
  101. }
  102. this.update(canvas);
  103. for(var index in this.places) {
  104. var place = this.places[index];
  105. place.update(delta);
  106. }
  107. this.player.update(delta);
  108. for(var index in this.characters) {
  109. var character = this.characters[index];
  110. character.update(delta);
  111. }
  112. this.dialog.update(delta);
  113. this.camera.update(canvas, this.player, delta);
  114. this.ui.update(delta);
  115. this.curtain.updateDelta(delta);
  116. };
  117. this.update = function(canvas) {
  118. this.player.control();
  119. this.background.update(canvas);
  120. if(this.player.position.x > 511 && this.player.position.x < 640 && this.player.position.y > 511 && this.player.position.y < 640 && this.player.trigger(1)) {
  121. var conversation = ["Hi! Welcome to grid 512:512, the hippest computer nightclub this quadrant!",
  122. "Oh, uh... hi. I'm here for some kind of party...",
  123. "Yes, the Bitwise convention. Right this way! Oh, and the wifi is free, just use the password 'l33tb0x'",
  124. "Y-yup! I will totally be connecting to that wifi, just like everyone, totally normal."
  125. ];
  126. var speakers = [1, 0, 1, 0];
  127. this.dialog.beginConversation(conversation, speakers);
  128. }
  129. if(this.player.position.x > 255 && this.player.position.x < 384 && this.player.position.y > 255 && this.player.position.y < 384 && this.player.trigger(0)) {
  130. var conversation = [
  131. "Now this is the story all about how my life got flipped, turned upside down and I'd like to take a minute just sit right there I'll tell you how I became the prince of a town called Bel-air.",
  132. "In west Philadelphia born and raised on the playground where I spent most of my days chilling out, maxing, relaxing all cool, and all shooting some b-ball outside of the school when a couple of guys, they were up to no good started making trouble in my neighbourhood I got in one little fight and my mom got scared and said \"You're moving with your auntie and uncle in Bel-air\"",
  133. "I whistled for a cab and when it came near the license plate said 'fresh' and had a dice in the mirror if anything I could say that this cab was rare but I thought nah, forget it, yo homes to Bel-air!",
  134. "I pulled up to a house about seven or eight and I yelled to the cabby \"Yo, homes smell you later!\" looked at my kingdom I was finally there to sit on my throne as the Prince of Bel-air.",
  135. "Is that some kind of pop-culture reference?"
  136. ];
  137. var speakers = [0,0,0,0,1];
  138. this.dialog.beginConversation(conversation, speakers);
  139. }
  140. };
  141. this.draw = function(context) {
  142. context.save();
  143. context.translate(this.camera.position.x, this.camera.position.y);
  144. this.background.draw(context);
  145. for(var index in this.places) {
  146. var place = this.places[index];
  147. place.draw(context);
  148. }
  149. for(var index in this.characters) {
  150. var character = this.characters[index];
  151. character.draw(context);
  152. }
  153. this.player.draw(context);
  154. context.restore();
  155. this.dialog.draw(context);
  156. this.ui.draw(context);
  157. this.curtain.draw(context);
  158. };
  159. this.handleResize = function(canvas) {
  160. this.camera.handleResize(canvas);
  161. }
  162. this.mouseMove = function(canvas, x, y) {
  163. this.player.controller.mouseMove(this.camera, x, y);
  164. };
  165. this.mouseDown = function(canvas, button, x, y) {
  166. if(!this.dialog.handleAdvance(button, x, y)) {
  167. this.player.controller.mouseDown(this.camera, button, x, y);
  168. }
  169. };
  170. this.mouseUp = function(canvas, button, x, y) {
  171. this.player.controller.mouseUp(this.camera, button, x, y);
  172. };
  173. this.touchStart = function(canvas, x, y) {
  174. if(!this.dialog.handleAdvance(null, x, y)) {
  175. this.player.controller.touchStart(this.camera, x, y);
  176. }
  177. };
  178. this.touchMove = function(canvas, x, y) {
  179. this.player.controller.touchMove(this.camera, x, y);
  180. };
  181. this.touchEnd = function(canvas, x, y) {
  182. this.player.controller.touchEnd(this.camera, x, y);
  183. };
  184. this.contextMenu = function(canvas, event) {
  185. this.player.controller.contextMenu(this.camera, event);
  186. };
  187. };