|
| 1 | + |
| 2 | +var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render }); |
| 3 | + |
| 4 | +function preload() { |
| 5 | + |
| 6 | + game.load.spritesheet('dude', 'assets/games/starstruck/dude.png', 32, 48); |
| 7 | + game.load.image('background', 'assets/games/starstruck/background2.png'); |
| 8 | + game.load.image('box', 'assets/sprites/block.png'); |
| 9 | + |
| 10 | +} |
| 11 | + |
| 12 | +var player; |
| 13 | +var facing = 'left'; |
| 14 | +var jumpTimer = 0; |
| 15 | +var cursors; |
| 16 | +var jumpButton; |
| 17 | +var box1; |
| 18 | +var box2; |
| 19 | + |
| 20 | +function create() { |
| 21 | + |
| 22 | + game.stage.backgroundColor = '#000000'; |
| 23 | + |
| 24 | + bg = game.add.tileSprite(0, 0, 800, 600, 'background'); |
| 25 | + bg.fixedToCamera = true; |
| 26 | + |
| 27 | + // map = game.add.tilemap('level1'); |
| 28 | + |
| 29 | + // map.addTilesetImage('tiles-1'); |
| 30 | + |
| 31 | + // map.setCollisionByExclusion([ 13, 14, 15, 16, 46, 47, 48, 49, 50, 51 ]); |
| 32 | + |
| 33 | + // layer = map.createLayer('Tile Layer 1'); |
| 34 | + |
| 35 | + // Un-comment this on to see the collision tiles |
| 36 | + // layer.debug = true; |
| 37 | + |
| 38 | + // layer.resizeWorld(); |
| 39 | + |
| 40 | + // game.physics.setBoundsToWorld(); |
| 41 | + |
| 42 | + game.physics.gravity.y = 20; |
| 43 | + game.physics.friction = 0.5; |
| 44 | + |
| 45 | + // Materials |
| 46 | + var groundMaterial = game.physics.createMaterial('ground'); |
| 47 | + var characterMaterial = game.physics.createMaterial('character'); |
| 48 | + var boxMaterial = game.physics.createMaterial('box'); |
| 49 | + |
| 50 | + player = game.add.sprite(32, 320, 'dude'); |
| 51 | + player.physicsEnabled = true; |
| 52 | + player.body.fixedRotation = true; |
| 53 | + player.body.setMaterial(characterMaterial); |
| 54 | + |
| 55 | + player.animations.add('left', [0, 1, 2, 3], 10, true); |
| 56 | + player.animations.add('turn', [4], 20, true); |
| 57 | + player.animations.add('right', [5, 6, 7, 8], 10, true); |
| 58 | + |
| 59 | + box1 = game.add.sprite(200, 300, 'box'); |
| 60 | + box1.physicsEnabled = true; |
| 61 | + // box1.body.fixedRotation = true; |
| 62 | + box1.body.setMaterial(boxMaterial); |
| 63 | + |
| 64 | + box2 = game.add.sprite(400, 300, 'box'); |
| 65 | + box2.physicsEnabled = true; |
| 66 | + // box2.body.fixedRotation = true; |
| 67 | + box2.body.setMaterial(boxMaterial); |
| 68 | + |
| 69 | + // Set the material along the ground |
| 70 | + game.physics.setWorldMaterial(groundMaterial, false, false, false, true); |
| 71 | + |
| 72 | + var groundCharacterCM = game.physics.createContactMaterial(groundMaterial, characterMaterial, { friction: 0.0 }); // no friction between character and ground |
| 73 | + var boxCharacterCM = game.physics.createContactMaterial(boxMaterial, characterMaterial, { friction: 0.0 }); // No friction between character and boxes |
| 74 | + var boxGroundCM = game.physics.createContactMaterial(boxMaterial, groundMaterial, { friction: 0.6 }); // Between boxes and ground |
| 75 | + |
| 76 | + // game.camera.follow(player); |
| 77 | + |
| 78 | + cursors = game.input.keyboard.createCursorKeys(); |
| 79 | + jumpButton = game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR); |
| 80 | + |
| 81 | +} |
| 82 | + |
| 83 | +function update() { |
| 84 | + |
| 85 | + player.body.velocity.x = 0; |
| 86 | + |
| 87 | + if (cursors.left.isDown) |
| 88 | + { |
| 89 | + player.body.moveLeft(150); |
| 90 | + |
| 91 | + if (facing != 'left') |
| 92 | + { |
| 93 | + player.animations.play('left'); |
| 94 | + facing = 'left'; |
| 95 | + } |
| 96 | + } |
| 97 | + else if (cursors.right.isDown) |
| 98 | + { |
| 99 | + player.body.moveRight(150); |
| 100 | + |
| 101 | + if (facing != 'right') |
| 102 | + { |
| 103 | + player.animations.play('right'); |
| 104 | + facing = 'right'; |
| 105 | + } |
| 106 | + } |
| 107 | + else |
| 108 | + { |
| 109 | + if (facing != 'idle') |
| 110 | + { |
| 111 | + player.animations.stop(); |
| 112 | + |
| 113 | + if (facing == 'left') |
| 114 | + { |
| 115 | + player.frame = 0; |
| 116 | + } |
| 117 | + else |
| 118 | + { |
| 119 | + player.frame = 5; |
| 120 | + } |
| 121 | + |
| 122 | + facing = 'idle'; |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + if (jumpButton.isDown && game.time.now > jumpTimer && checkIfCanJump()) |
| 127 | + { |
| 128 | + player.body.moveUp(300); |
| 129 | + jumpTimer = game.time.now + 750; |
| 130 | + } |
| 131 | + |
| 132 | +} |
| 133 | + |
| 134 | +function checkIfCanJump(){ |
| 135 | +var yAxis = p2.vec2.fromValues(0,1); |
| 136 | +var result = false; |
| 137 | +for(var i=0; i<game.physics.world.narrowphase.contactEquations.length; i++){ |
| 138 | + var c = game.physics.world.narrowphase.contactEquations[i]; |
| 139 | + if(c.bi === player.body.data || c.bj === player.body.data){ |
| 140 | + var d = p2.vec2.dot(c.ni,yAxis); // Normal dot Y-axis |
| 141 | + if(c.bi === player.body.data) d *= -1; |
| 142 | + if(d > 0.5) result = true; |
| 143 | + } |
| 144 | +} |
| 145 | +return result; |
| 146 | +} |
| 147 | + |
| 148 | + |
| 149 | +function render () { |
| 150 | + |
| 151 | + // if (player.debug) |
| 152 | + // { |
| 153 | + // game.debug.renderPhysicsBody(player.body); |
| 154 | + // game.debug.renderBodyInfo(player, 16, 24); |
| 155 | + // } |
| 156 | + |
| 157 | +} |
0 commit comments