mirror of
https://github.com/wassname/phaser.git
synced 2026-07-01 16:50:43 +08:00
Working gravity + contacts + platformer demo :)
This commit is contained in:
@@ -0,0 +1,157 @@
|
||||
|
||||
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render });
|
||||
|
||||
function preload() {
|
||||
|
||||
game.load.spritesheet('dude', 'assets/games/starstruck/dude.png', 32, 48);
|
||||
game.load.image('background', 'assets/games/starstruck/background2.png');
|
||||
game.load.image('box', 'assets/sprites/block.png');
|
||||
|
||||
}
|
||||
|
||||
var player;
|
||||
var facing = 'left';
|
||||
var jumpTimer = 0;
|
||||
var cursors;
|
||||
var jumpButton;
|
||||
var box1;
|
||||
var box2;
|
||||
|
||||
function create() {
|
||||
|
||||
game.stage.backgroundColor = '#000000';
|
||||
|
||||
bg = game.add.tileSprite(0, 0, 800, 600, 'background');
|
||||
bg.fixedToCamera = true;
|
||||
|
||||
// map = game.add.tilemap('level1');
|
||||
|
||||
// map.addTilesetImage('tiles-1');
|
||||
|
||||
// map.setCollisionByExclusion([ 13, 14, 15, 16, 46, 47, 48, 49, 50, 51 ]);
|
||||
|
||||
// layer = map.createLayer('Tile Layer 1');
|
||||
|
||||
// Un-comment this on to see the collision tiles
|
||||
// layer.debug = true;
|
||||
|
||||
// layer.resizeWorld();
|
||||
|
||||
// game.physics.setBoundsToWorld();
|
||||
|
||||
game.physics.gravity.y = 20;
|
||||
game.physics.friction = 0.5;
|
||||
|
||||
// Materials
|
||||
var groundMaterial = game.physics.createMaterial('ground');
|
||||
var characterMaterial = game.physics.createMaterial('character');
|
||||
var boxMaterial = game.physics.createMaterial('box');
|
||||
|
||||
player = game.add.sprite(32, 320, 'dude');
|
||||
player.physicsEnabled = true;
|
||||
player.body.fixedRotation = true;
|
||||
player.body.setMaterial(characterMaterial);
|
||||
|
||||
player.animations.add('left', [0, 1, 2, 3], 10, true);
|
||||
player.animations.add('turn', [4], 20, true);
|
||||
player.animations.add('right', [5, 6, 7, 8], 10, true);
|
||||
|
||||
box1 = game.add.sprite(200, 300, 'box');
|
||||
box1.physicsEnabled = true;
|
||||
// box1.body.fixedRotation = true;
|
||||
box1.body.setMaterial(boxMaterial);
|
||||
|
||||
box2 = game.add.sprite(400, 300, 'box');
|
||||
box2.physicsEnabled = true;
|
||||
// box2.body.fixedRotation = true;
|
||||
box2.body.setMaterial(boxMaterial);
|
||||
|
||||
// Set the material along the ground
|
||||
game.physics.setWorldMaterial(groundMaterial, false, false, false, true);
|
||||
|
||||
var groundCharacterCM = game.physics.createContactMaterial(groundMaterial, characterMaterial, { friction: 0.0 }); // no friction between character and ground
|
||||
var boxCharacterCM = game.physics.createContactMaterial(boxMaterial, characterMaterial, { friction: 0.0 }); // No friction between character and boxes
|
||||
var boxGroundCM = game.physics.createContactMaterial(boxMaterial, groundMaterial, { friction: 0.6 }); // Between boxes and ground
|
||||
|
||||
// game.camera.follow(player);
|
||||
|
||||
cursors = game.input.keyboard.createCursorKeys();
|
||||
jumpButton = game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR);
|
||||
|
||||
}
|
||||
|
||||
function update() {
|
||||
|
||||
player.body.velocity.x = 0;
|
||||
|
||||
if (cursors.left.isDown)
|
||||
{
|
||||
player.body.moveLeft(150);
|
||||
|
||||
if (facing != 'left')
|
||||
{
|
||||
player.animations.play('left');
|
||||
facing = 'left';
|
||||
}
|
||||
}
|
||||
else if (cursors.right.isDown)
|
||||
{
|
||||
player.body.moveRight(150);
|
||||
|
||||
if (facing != 'right')
|
||||
{
|
||||
player.animations.play('right');
|
||||
facing = 'right';
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (facing != 'idle')
|
||||
{
|
||||
player.animations.stop();
|
||||
|
||||
if (facing == 'left')
|
||||
{
|
||||
player.frame = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
player.frame = 5;
|
||||
}
|
||||
|
||||
facing = 'idle';
|
||||
}
|
||||
}
|
||||
|
||||
if (jumpButton.isDown && game.time.now > jumpTimer && checkIfCanJump())
|
||||
{
|
||||
player.body.moveUp(300);
|
||||
jumpTimer = game.time.now + 750;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
function checkIfCanJump(){
|
||||
var yAxis = p2.vec2.fromValues(0,1);
|
||||
var result = false;
|
||||
for(var i=0; i<game.physics.world.narrowphase.contactEquations.length; i++){
|
||||
var c = game.physics.world.narrowphase.contactEquations[i];
|
||||
if(c.bi === player.body.data || c.bj === player.body.data){
|
||||
var d = p2.vec2.dot(c.ni,yAxis); // Normal dot Y-axis
|
||||
if(c.bi === player.body.data) d *= -1;
|
||||
if(d > 0.5) result = true;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
function render () {
|
||||
|
||||
// if (player.debug)
|
||||
// {
|
||||
// game.debug.renderPhysicsBody(player.body);
|
||||
// game.debug.renderBodyInfo(player, 16, 24);
|
||||
// }
|
||||
|
||||
}
|
||||
@@ -34,7 +34,7 @@ Object.defineProperty(Phaser.Physics.InversePointProxy.prototype, "x", {
|
||||
|
||||
set: function (value) {
|
||||
|
||||
this.destination[0] *= -value;
|
||||
this.destination[0] = -value;
|
||||
|
||||
}
|
||||
|
||||
@@ -54,7 +54,7 @@ Object.defineProperty(Phaser.Physics.InversePointProxy.prototype, "y", {
|
||||
|
||||
set: function (value) {
|
||||
|
||||
this.destination[1] *= -value;
|
||||
this.destination[1] = -value;
|
||||
|
||||
}
|
||||
|
||||
|
||||
+55
-5
@@ -54,6 +54,12 @@ Phaser.Physics.World = function (game) {
|
||||
*/
|
||||
this.bounds = null;
|
||||
|
||||
/**
|
||||
* @property {array} _wallShapes - The wall bounds shapes.
|
||||
* @private
|
||||
*/
|
||||
this._wallShapes = [ null, null, null, null ];
|
||||
|
||||
/**
|
||||
* @property {Phaser.Signal} onBodyAdded - Dispatched when a new Body is added to the World.
|
||||
*/
|
||||
@@ -216,6 +222,45 @@ Phaser.Physics.World.prototype = {
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Sets the given material against the 4 bounds of this World.
|
||||
*
|
||||
* @method Phaser.Physics#setWorldMaterial
|
||||
* @param {Phaser.Physics.Material} material - The material to set.
|
||||
* @param {boolean} [left=true] - If true will set the material on the left bounds wall.
|
||||
* @param {boolean} [right=true] - If true will set the material on the right bounds wall.
|
||||
* @param {boolean} [top=true] - If true will set the material on the top bounds wall.
|
||||
* @param {boolean} [bottom=true] - If true will set the material on the bottom bounds wall.
|
||||
*/
|
||||
setWorldMaterial: function (material, left, right, top, bottom) {
|
||||
|
||||
if (typeof left === 'undefined') { left = true; }
|
||||
if (typeof right === 'undefined') { right = true; }
|
||||
if (typeof top === 'undefined') { top = true; }
|
||||
if (typeof bottom === 'undefined') { bottom = true; }
|
||||
|
||||
if (left && this._wallShapes[0])
|
||||
{
|
||||
this._wallShapes[0].material = material;
|
||||
}
|
||||
|
||||
if (right && this._wallShapes[1])
|
||||
{
|
||||
this._wallShapes[1].material = material;
|
||||
}
|
||||
|
||||
if (top && this._wallShapes[2])
|
||||
{
|
||||
this._wallShapes[2].material = material;
|
||||
}
|
||||
|
||||
if (bottom && this._wallShapes[3])
|
||||
{
|
||||
this._wallShapes[3].material = material;
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Sets the bounds of the Physics world to match the given world pixel dimensions.
|
||||
* You can optionally set which 'walls' to create: left, right, top or bottom.
|
||||
@@ -264,22 +309,26 @@ Phaser.Physics.World.prototype = {
|
||||
|
||||
if (left)
|
||||
{
|
||||
this.bounds.addShape(new p2.Plane(), [this.game.math.px2p(-hw), 0], 1.5707963267948966 );
|
||||
this._wallShapes[0] = new p2.Plane();
|
||||
this.bounds.addShape(this._wallShapes[0], [this.game.math.px2p(-hw), 0], 1.5707963267948966 );
|
||||
}
|
||||
|
||||
if (right)
|
||||
{
|
||||
this.bounds.addShape(new p2.Plane(), [this.game.math.px2p(hw), 0], -1.5707963267948966 );
|
||||
this._wallShapes[1] = new p2.Plane();
|
||||
this.bounds.addShape(this._wallShapes[1], [this.game.math.px2p(hw), 0], -1.5707963267948966 );
|
||||
}
|
||||
|
||||
if (top)
|
||||
{
|
||||
this.bounds.addShape(new p2.Plane(), [0, this.game.math.px2p(-hh)], -3.141592653589793 );
|
||||
this._wallShapes[2] = new p2.Plane();
|
||||
this.bounds.addShape(this._wallShapes[2], [0, this.game.math.px2p(-hh)], -3.141592653589793 );
|
||||
}
|
||||
|
||||
if (bottom)
|
||||
{
|
||||
this.bounds.addShape(new p2.Plane(), [0, this.game.math.px2p(hh)] );
|
||||
this._wallShapes[3] = new p2.Plane();
|
||||
this.bounds.addShape(this._wallShapes[3], [0, this.game.math.px2p(hh)] );
|
||||
}
|
||||
|
||||
this.world.addBody(this.bounds);
|
||||
@@ -514,9 +563,10 @@ Phaser.Physics.World.prototype = {
|
||||
* @method Phaser.Physics.World#createContactMaterial
|
||||
* @param {Phaser.Physics.Material} [materialA] - The first Material to create the ContactMaterial from. If undefined it will create a new Material object first.
|
||||
* @param {Phaser.Physics.Material} [materialB] - The second Material to create the ContactMaterial from. If undefined it will create a new Material object first.
|
||||
* @param {object} [options] - Material options object.
|
||||
* @return {Phaser.Physics.ContactMaterial} The Contact Material that was created.
|
||||
*/
|
||||
createContactMaterial: function (materialA, materialB) {
|
||||
createContactMaterial: function (materialA, materialB, options) {
|
||||
|
||||
if (typeof materialA === 'undefined') { materialA = this.createMaterial(); }
|
||||
if (typeof materialB === 'undefined') { materialB = this.createMaterial(); }
|
||||
|
||||
Reference in New Issue
Block a user