|
@@ -0,0 +1,145 @@
|
|
|
+var game = new Phaser.Game(800, 240, Phaser.CANVAS, '', {
|
|
|
+ preload: preload,
|
|
|
+ create: create,
|
|
|
+ update: update
|
|
|
+}, false, false);
|
|
|
+var score = 0;
|
|
|
+var scoreText;
|
|
|
+
|
|
|
+function preload() {
|
|
|
+ game.load.spritesheet('tiles', 'tiled/tiles_dctsfk.png', 16, 16);
|
|
|
+ game.load.spritesheet('goomba', 'assets/willie/dude-willie.png', 32, 48);
|
|
|
+ //game.load.spritesheet('mario', 'http://res.cloudinary.com/harsay/image/upload/v1464614984/mario_wjlfy5.png', 16, 16);
|
|
|
+ game.load.spritesheet('coin', 'tiled/coin_iormvy.png', 16, 16);
|
|
|
+ game.load.spritesheet('mario', resource.dude, 32, 48);
|
|
|
+ game.load.tilemap('level', 'tiled/world.json', null, Phaser.Tilemap.TILED_JSON);
|
|
|
+}
|
|
|
+
|
|
|
+function create() {
|
|
|
+ Phaser.Canvas.setImageRenderingCrisp(game.canvas) //renderizzare meglio
|
|
|
+ game.scale.pageAlignHorizontally = true; //per scalare
|
|
|
+ game.scale.pageAlignVertically = true
|
|
|
+ game.scale.scaleMode = Phaser.ScaleManager.SHOW_ALL;
|
|
|
+ game.physics.startSystem(Phaser.Physics.ARCADE);
|
|
|
+
|
|
|
+ game.stage.backgroundColor = '#5c94fc';
|
|
|
+
|
|
|
+ map = game.add.tilemap('level');
|
|
|
+ map.addTilesetImage('tiles', 'tiles');
|
|
|
+ map.setCollisionBetween(3, 12, true, 'solid');
|
|
|
+
|
|
|
+ map.createLayer('background');
|
|
|
+
|
|
|
+ layer = map.createLayer('solid');
|
|
|
+ layer.resizeWorld();
|
|
|
+
|
|
|
+ coins = game.add.group();
|
|
|
+ coins.enableBody = true;
|
|
|
+ map.createFromTiles(2, null, 'coin', 'stuff', coins);
|
|
|
+ coins.callAll('animations.add', 'animations', 'spin', [0, 0, 1, 2], 3, true);
|
|
|
+ coins.callAll('animations.play', 'animations', 'spin');
|
|
|
+
|
|
|
+ goombas = game.add.group();
|
|
|
+ goombas.enableBody = true;
|
|
|
+ map.createFromTiles(1, null, 'goomba', 'stuff', goombas);
|
|
|
+ goombas.callAll('animations.add', 'animations', 'walkLeft', [0, 1, 2, 3], 7, true);
|
|
|
+ goombas.callAll('animations.add', 'animations', 'walkRight', [5, 6, 7, 8], 7, true);
|
|
|
+ goombas.callAll('animations.play', 'animations', 'walkLeft');
|
|
|
+ goombas.setAll('body.bounce.x', 1);
|
|
|
+ goombas.setAll('body.velocity.x', -80);
|
|
|
+ goombas.setAll('body.gravity.y', 500);
|
|
|
+
|
|
|
+ player = game.add.sprite(32, game.world.height - 150, 'mario');
|
|
|
+ game.physics.arcade.enable(player);
|
|
|
+ player.body.gravity.y = 370;
|
|
|
+ player.body.collideWorldBounds = true;
|
|
|
+ //player.animations.add('walkRight', [1, 2, 3], 10, true);
|
|
|
+ //player.animations.add('walkLeft', [8, 9, 10], 10, true);
|
|
|
+ player.animations.add('left', [0, 1, 2, 3], 7, true);
|
|
|
+ player.animations.add('right', [5, 6, 7, 8], 7, true);
|
|
|
+ player.goesRight = true;
|
|
|
+
|
|
|
+ game.camera.follow(player);
|
|
|
+
|
|
|
+ cursors = game.input.keyboard.createCursorKeys();
|
|
|
+
|
|
|
+ scoreText = game.add.text(16, 16, 'pilla: 0', { fontSize: '32px', fill: '#000' });
|
|
|
+}
|
|
|
+
|
|
|
+function update() {
|
|
|
+ game.physics.arcade.collide(player, layer);
|
|
|
+ game.physics.arcade.collide(goombas, layer);
|
|
|
+ game.physics.arcade.overlap(player, goombas, goombaOverlap);
|
|
|
+ game.physics.arcade.overlap(player, coins, coinOverlap);
|
|
|
+ game.physics.arcade.overlap(goombas, layer, goombaCollide);
|
|
|
+
|
|
|
+
|
|
|
+ if (player.body.enable) {
|
|
|
+ player.body.velocity.x = 0;
|
|
|
+ if (cursors.left.isDown) {
|
|
|
+ player.body.velocity.x = -90;
|
|
|
+ player.animations.play('left');
|
|
|
+ player.goesRight = false;
|
|
|
+ } else if (cursors.right.isDown) {
|
|
|
+ player.body.velocity.x = 90;
|
|
|
+ player.animations.play('right');
|
|
|
+ player.goesRight = true;
|
|
|
+ } else {
|
|
|
+ player.animations.stop();
|
|
|
+ player.frame = 4;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (cursors.up.isDown && player.body.onFloor()) {
|
|
|
+ player.body.velocity.y = -190;
|
|
|
+ player.animations.stop();
|
|
|
+ }
|
|
|
+
|
|
|
+ if (player.body.velocity.y != 0) {
|
|
|
+ if (player.goesRight) player.frame = 8;
|
|
|
+ else player.frame = 3;
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+function goombaCollide(goomba, layer){
|
|
|
+ if (goomba.animations.name=='walkLeft' &&
|
|
|
+ goomba.body.blocked.left) {
|
|
|
+ goomba.animations.play('walkRight');
|
|
|
+ goomba.animations.update;
|
|
|
+ }
|
|
|
+ else if (goomba.animations.name=='walkRight' &&
|
|
|
+ goomba.body.blocked.right) {
|
|
|
+ goomba.animations.play('walkLeft');
|
|
|
+ goomba.animations.update;
|
|
|
+
|
|
|
+ };
|
|
|
+}
|
|
|
+
|
|
|
+function coinOverlap(player, coin) {
|
|
|
+ coin.kill();
|
|
|
+ collectHeart();
|
|
|
+}
|
|
|
+
|
|
|
+function goombaOverlap(player, goomba) {
|
|
|
+ if (player.body.touching.down) {
|
|
|
+ goomba.animations.stop();
|
|
|
+ goomba.frame = 4;
|
|
|
+ goomba.body.enable = false;
|
|
|
+ player.body.velocity.y = -80;
|
|
|
+ game.time.events.add(Phaser.Timer.SECOND, function() {
|
|
|
+ goomba.kill();
|
|
|
+ });
|
|
|
+ } else {
|
|
|
+ player.frame = 6;
|
|
|
+ player.body.enable = false;
|
|
|
+ player.animations.stop();
|
|
|
+ game.time.events.add(Phaser.Timer.SECOND * 3, function() {
|
|
|
+ game.paused = true;
|
|
|
+ });
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+function collectHeart (player, star) {
|
|
|
+ score += 1;
|
|
|
+ scoreText.text = 'pilla: ' + score;
|
|
|
+}
|