Advertisement
Ivyei

Main.JS

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