Advertisement
Guest User

Main.JS

a guest
Jun 25th, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.42 KB | None | 0 0
  1. //this game will have only 1 state
  2. var GameState = {
  3.  
  4. //initiate game settings
  5. init: function() {
  6. //adapt to screen size, fit all the game
  7. this.scale.scaleMode = Phaser.ScaleManager.SHOW_ALL;
  8. this.scale.pageAlignHorizontally = true;
  9. this.scale.pageAlignVertically = true;
  10.  
  11. this.game.physics.startSystem(Phaser.Physics.ARCADE);
  12. this.game.physics.arcade.gravity.y = 1000
  13.  
  14. this.cursors = this.game.input.keyboard.createCursorKeys();
  15.  
  16. this.game.world.setBounds(0,0,360,700);
  17.  
  18. this.RUNNING_SPEED = 400;
  19. this.JUMPING_SPEED = 550;
  20.  
  21. },
  22.  
  23. //load the game assets before the game starts
  24. preload: function() {
  25. this.load.image('ground', 'assets/images/ground.png');
  26. this.load.image('platform', 'assets/images/platform.png');
  27. this.load.image('goal', 'assets/images/gorilla3.png');
  28. this.load.image('arrowButton', 'assets/images/arrowButton.png');
  29. this.load.image('actionButton', 'assets/images/actionButton.png');
  30. this.load.image('barrel', 'assets/images/barrel.png');
  31.  
  32. this.load.spritesheet('player', 'assets/images/player_spritesheet.png', 28, 30, 5, 1, 1);
  33. this.load.spritesheet('fire', 'assets/images/fire_spritesheet.png', 20, 21, 2, 1, 1);
  34. this.load.image('coin', 'assets/images/coin.png', 32, 32);
  35. this.load.text('level', 'assets/data/level.json');
  36. },
  37.  
  38.  
  39. //executed after everything is loaded
  40. create: function() {
  41.  
  42. this.ground = this.add.sprite(0, 638, 'ground');
  43. this.game.physics.arcade.enable(this.ground);
  44. this.ground.body.allowGravity = false;
  45. this.ground.body.immovable = true;
  46.  
  47. //parse the file
  48. this.levelData = JSON.parse(this.game.cache.getText('level'));
  49.  
  50. this.platforms = this.add.group();
  51. this.platforms.enableBody = true;
  52.  
  53. this.levelData.platformData.forEach(function(element){
  54. this.platforms.create(element.x, element.y, 'platform');
  55. }, this);
  56.  
  57. this.platforms.setAll('body.immovable', true);
  58. this.platforms.setAll('body.allowGravity', false);
  59.  
  60. //coins=====================
  61. // Here we create our coins group
  62. var coin
  63. coins = this.add.group();
  64.  
  65. // Now let's add 50 coins into it
  66. for (var i = 0; i < 20; i++)
  67. {
  68. coin = this.add.sprite(this.world.randomX, this.world.randomY, 'coin', 0);
  69. coins.enableBody = true;
  70.  
  71. coins.add(coin);
  72.  
  73. }
  74. //=================================
  75. // Now using the power of callAll we can add the same animation to all coins in the group:
  76. //coins.callAll('animations.add', 'animations', 'spin', [0, 1, 2, 3, 4, 5], 10, true);
  77.  
  78. // And play them
  79. //coins.callAll('animations.play', 'animations', 'spin');
  80.  
  81.  
  82.  
  83. //fires
  84. this.fires = this.add.group();
  85. this.fires.enableBody = true;
  86.  
  87. var fire;
  88. this.levelData.fireData.forEach(function(element){
  89. fire = this.fires.create(element.x, element.y, 'fire');
  90. fire.animations.add('fire', [0, 1], 4, true);
  91. fire.play('fire');
  92. }, this);
  93.  
  94. this.fires.setAll('body.allowGravity', false);
  95.  
  96. //goal
  97. this.goal = this.add.sprite(this.levelData.goal.x, this.levelData.goal.y, 'goal');
  98. this.game.physics.arcade.enable(this.goal);
  99. this.goal.body.allowGravity = false;
  100.  
  101. //create player
  102. this.player = this.add.sprite(this.levelData.playerStart.x, this.levelData.playerStart.y, 'player', 3);
  103. this.player.anchor.setTo(0.5);
  104. this.player.animations.add('walking', [0, 1, 2, 1], 6, true);
  105. this.game.physics.arcade.enable(this.player);
  106. this.player.customParams = {};
  107. this.player.body.collideWorldBounds = true;
  108.  
  109. this.game.camera.follow(this.player);
  110.  
  111. this.createOnscreenControls();
  112.  
  113. this.barrels = this.add.group();
  114. this.barrels.enableBody = true;
  115.  
  116. this.createBarrel();
  117. this.barrelCreator = this.game.time.events.loop(Phaser.Timer.SECOND * this.levelData.barrelFrequency, this.createBarrel, this)
  118. },
  119. update: function() {
  120. this.game.physics.arcade.collide(this.player, this.ground);
  121. this.game.physics.arcade.collide(this.player, this.platforms);
  122.  
  123. this.game.physics.arcade.collide(this.barrels, this.ground);
  124. this.game.physics.arcade.collide(this.barrels, this.platforms);
  125.  
  126. this.game.physics.arcade.overlap(this.player, this.fires, this.killPlayer);
  127. this.game.physics.arcade.overlap(this.player, this.barrels, this.killPlayer);
  128. this.game.physics.arcade.overlap(this.player, this.goal, this.win);
  129.  
  130. //coin physics======================
  131.  
  132. this.game.physics.arcade.collide(this.coin, this.player, this.platforms, this.ground, this.barrels);
  133. this.game.physics.arcade.collide(this.coin, this.player, this.collectCoin);
  134. this.game.physics.arcade.collide(this.coin, this.platforms);
  135. this.game.physics.arcade.collide(this.coin, this.ground);
  136.  
  137. function collectCoin(coin, player){
  138. coin.kill();
  139. };
  140. //=========================================
  141.  
  142. this.player.body.velocity.x = 0;
  143.  
  144. if(this.cursors.left.isDown || this.player.customParams.isMovingLeft) {
  145. this.player.body.velocity.x = -this.RUNNING_SPEED;
  146. this.player.scale.setTo(1, 1);
  147. this.player.play('walking');
  148. }
  149. else if(this.cursors.right.isDown || this.player.customParams.isMovingRight) {
  150. this.player.body.velocity.x = this.RUNNING_SPEED;
  151. this.player.scale.setTo(-1, 1);
  152. this.player.play('walking');
  153. }
  154. else {
  155. this.player.animations.stop();
  156. this.player.frame = 3;
  157.  
  158. }
  159.  
  160. if((this.cursors.up.isDown || this.player.customParams.mustJump) && this.player.body.touching.down) {
  161. this.player.body.velocity.y = -this.JUMPING_SPEED;
  162. this.player.customParams.mustJump = false;
  163. }
  164.  
  165. this.barrels.forEach(function(element){
  166. if(element.x < 10 && element.y > 600) {
  167. element.kill();
  168. }
  169. }, this);
  170. },
  171. createOnscreenControls: function(){
  172. this.leftArrow = this.add.button(20, 535, 'arrowButton');
  173. this.rightArrow = this.add.button(110, 535, 'arrowButton');
  174. this.actionButton = this.add.button(280, 535, 'actionButton');
  175.  
  176. this.leftArrow.alpha = 0.5;
  177. this.rightArrow.alpha = 0.5;
  178. this.actionButton.alpha = 0.5;
  179.  
  180. this.leftArrow.fixedToCamera = true;
  181. this.rightArrow.fixedToCamera = true;
  182. this.actionButton.fixedToCamera = true;
  183.  
  184. this.actionButton.events.onInputDown.add(function(){
  185. this.player.customParams.mustJump = true;
  186. }, this);
  187.  
  188. this.actionButton.events.onInputUp.add(function(){
  189. this.player.customParams.mustJump = false;
  190. }, this);
  191.  
  192. //left
  193. this.leftArrow.events.onInputDown.add(function(){
  194. this.player.customParams.isMovingLeft = true;
  195. }, this);
  196.  
  197. this.leftArrow.events.onInputUp.add(function(){
  198. this.player.customParams.isMovingLeft = false;
  199. }, this);
  200.  
  201. this.leftArrow.events.onInputOver.add(function(){
  202. this.player.customParams.isMovingLeft = true;
  203. }, this);
  204.  
  205. this.leftArrow.events.onInputOut.add(function(){
  206. this.player.customParams.isMovingLeft = false;
  207. }, this);
  208.  
  209. //right
  210. this.rightArrow.events.onInputDown.add(function(){
  211. this.player.customParams.isMovingRight = true;
  212. }, this);
  213.  
  214. this.rightArrow.events.onInputUp.add(function(){
  215. this.player.customParams.isMovingRight = false;
  216. }, this);
  217.  
  218. this.rightArrow.events.onInputOver.add(function(){
  219. this.player.customParams.isMovingRight = true;
  220. }, this);
  221.  
  222. this.rightArrow.events.onInputOut.add(function(){
  223. this.player.customParams.isMovingRight = false;
  224. }, this);
  225. },
  226. killPlayer: function(player, fire) {
  227. console.log('I died!');
  228. game.state.start('GameState');
  229. },
  230. win: function(player, goal) {
  231. alert('You have passed the first stage!');
  232. game.state.start('GameState');
  233. },
  234. createBarrel: function() {
  235. //give me the first dead sprite
  236. var barrel = this.barrels.getFirstExists(false);
  237.  
  238. if(!barrel) {
  239. barrel = this.barrels.create(0, 0, 'barrel');
  240. }
  241.  
  242. barrel.body.collideWorldBounds = true;
  243. barrel.body.bounce.set(1, 0);
  244.  
  245. barrel.reset(this.levelData.goal.x, this.levelData.goal.y);
  246. barrel.body.velocity.x = this.levelData.barrelSpeed;
  247. }
  248.  
  249. };
  250.  
  251.  
  252.  
  253. //initiate the Phaser framework
  254. var game = new Phaser.Game(360, 592, Phaser.AUTO);
  255.  
  256. game.state.add('GameState', GameState);
  257. game.state.start('GameState');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement