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