index.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. var game = new Phaser.Game(800, 240, Phaser.CANVAS, '', {
  2. preload: preload,
  3. create: create,
  4. update: update
  5. }, false, false);
  6. var score = 0;
  7. var scoreText;
  8. function preload() {
  9. game.load.spritesheet('tiles', 'tiled/tiles_dctsfk.png', 16, 16);
  10. game.load.spritesheet('goomba', 'assets/willie/dude-willie.png', 32, 48);
  11. //game.load.spritesheet('mario', 'http://res.cloudinary.com/harsay/image/upload/v1464614984/mario_wjlfy5.png', 16, 16);
  12. game.load.spritesheet('coin', 'tiled/coin_iormvy.png', 16, 16);
  13. game.load.spritesheet('mario', resource.dude, 32, 48);
  14. game.load.tilemap('level', 'tiled/world.json', null, Phaser.Tilemap.TILED_JSON);
  15. }
  16. function create() {
  17. Phaser.Canvas.setImageRenderingCrisp(game.canvas) //renderizzare meglio
  18. game.scale.pageAlignHorizontally = true; //per scalare
  19. game.scale.pageAlignVertically = true
  20. game.scale.scaleMode = Phaser.ScaleManager.SHOW_ALL;
  21. game.physics.startSystem(Phaser.Physics.ARCADE);
  22. game.stage.backgroundColor = '#5c94fc';
  23. map = game.add.tilemap('level');
  24. map.addTilesetImage('tiles', 'tiles');
  25. map.setCollisionBetween(3, 12, true, 'solid');
  26. map.createLayer('background');
  27. layer = map.createLayer('solid');
  28. layer.resizeWorld();
  29. coins = game.add.group();
  30. coins.enableBody = true;
  31. map.createFromTiles(2, null, 'coin', 'stuff', coins);
  32. coins.callAll('animations.add', 'animations', 'spin', [0, 0, 1, 2], 3, true);
  33. coins.callAll('animations.play', 'animations', 'spin');
  34. goombas = game.add.group();
  35. goombas.enableBody = true;
  36. map.createFromTiles(1, null, 'goomba', 'stuff', goombas);
  37. goombas.callAll('animations.add', 'animations', 'walkLeft', [0, 1, 2, 3], 7, true);
  38. goombas.callAll('animations.add', 'animations', 'walkRight', [5, 6, 7, 8], 7, true);
  39. goombas.callAll('animations.play', 'animations', 'walkLeft');
  40. goombas.setAll('body.bounce.x', 1);
  41. goombas.setAll('body.velocity.x', -80);
  42. goombas.setAll('body.gravity.y', 500);
  43. player = game.add.sprite(32, game.world.height - 150, 'mario');
  44. game.physics.arcade.enable(player);
  45. player.body.gravity.y = 370;
  46. player.body.collideWorldBounds = true;
  47. //player.animations.add('walkRight', [1, 2, 3], 10, true);
  48. //player.animations.add('walkLeft', [8, 9, 10], 10, true);
  49. player.animations.add('left', [0, 1, 2, 3], 7, true);
  50. player.animations.add('right', [5, 6, 7, 8], 7, true);
  51. player.goesRight = true;
  52. game.camera.follow(player);
  53. cursors = game.input.keyboard.createCursorKeys();
  54. scoreText = game.add.text(16, 16, 'pilla: 0', { fontSize: '32px', fill: '#000' });
  55. }
  56. function update() {
  57. game.physics.arcade.collide(player, layer);
  58. game.physics.arcade.collide(goombas, layer);
  59. game.physics.arcade.overlap(player, goombas, goombaOverlap);
  60. game.physics.arcade.overlap(player, coins, coinOverlap);
  61. game.physics.arcade.overlap(goombas, layer, goombaCollide);
  62. if (player.body.enable) {
  63. player.body.velocity.x = 0;
  64. if (cursors.left.isDown) {
  65. player.body.velocity.x = -90;
  66. player.animations.play('left');
  67. player.goesRight = false;
  68. } else if (cursors.right.isDown) {
  69. player.body.velocity.x = 90;
  70. player.animations.play('right');
  71. player.goesRight = true;
  72. } else {
  73. player.animations.stop();
  74. player.frame = 4;
  75. }
  76. if (cursors.up.isDown && player.body.onFloor()) {
  77. player.body.velocity.y = -190;
  78. player.animations.stop();
  79. }
  80. if (player.body.velocity.y != 0) {
  81. if (player.goesRight) player.frame = 8;
  82. else player.frame = 3;
  83. }
  84. }
  85. }
  86. function goombaCollide(goomba, layer){
  87. if (goomba.animations.name=='walkLeft' &&
  88. goomba.body.blocked.left) {
  89. goomba.animations.play('walkRight');
  90. goomba.animations.update;
  91. }
  92. else if (goomba.animations.name=='walkRight' &&
  93. goomba.body.blocked.right) {
  94. goomba.animations.play('walkLeft');
  95. goomba.animations.update;
  96. };
  97. }
  98. function coinOverlap(player, coin) {
  99. coin.kill();
  100. collectHeart();
  101. }
  102. function goombaOverlap(player, goomba) {
  103. if (player.body.touching.down) {
  104. goomba.animations.stop();
  105. goomba.frame = 4;
  106. goomba.body.enable = false;
  107. player.body.velocity.y = -80;
  108. game.time.events.add(Phaser.Timer.SECOND, function() {
  109. goomba.kill();
  110. });
  111. } else {
  112. player.frame = 6;
  113. player.body.enable = false;
  114. player.animations.stop();
  115. game.time.events.add(Phaser.Timer.SECOND * 3, function() {
  116. game.paused = true;
  117. });
  118. }
  119. }
  120. function collectHeart (player, star) {
  121. score += 1;
  122. scoreText.text = 'pilla: ' + score;
  123. }