Giter Club home page Giter Club logo

phaser-arcade-slopes's Introduction

Phaser Arcade Slopes Plugin

Arcade Slopes brings sloped tile collision handling to Phaser's Arcade Physics engine.

Check out the demo!

Phaser Arcade Slopes

Features

  • 24 new tile types ๐ŸŽ‰
  • SAT-driven collision handling ๐Ÿ‘Œ
  • Unobtrusive and cooperative integration with Arcade Physics โœŒ๏ธ
  • Heuristic SAT restraints that prevent AABBs catching on hidden edges ๐Ÿ‘
  • Supports sprites ๐Ÿš€, groups ๐Ÿ‘ฅ, particle emitters โœจ and circular physics bodies โšช

Compatibility

Phaser Version Arcade Slopes Version
v2.4.1 - v2.4.8 v0.1.0
v2.5.0 - v2.7.3 v0.1.1 - v0.2.0

Installation

Grab a copy of the latest release from the dist directory in this repository and include it after Phaser.

<script src="phaser.min.js"></script>
<script src="phaser-arcade-slopes.min.js"></script>

Usage

Enabling the plugin

Enable the plugin in the create() method of your Phaser state.

game.plugins.add(Phaser.Plugin.ArcadeSlopes);

Mapping tiles

The plugin provides a couple of built in tile slope mappings:

After you've created a tilemap with a collision layer, you'll need to convert that layer to work with Arcade Slopes.

map = game.add.tilemap('tilemap');
map.addTilesetImage('collision', 'arcade-slopes-32');

ground = map.createLayer('collision');

// Convert the collision layer to work with Arcade Slopes
game.slopes.convertTilemapLayer(ground, 'arcadeslopes');

In the case that the first tile ID of the collision tileset in your tilemap is not 1 (the default), you can provide a third argument to specify it.

game.slopes.convertTilemapLayer(ground, 'ninja', 16);

Enabling physics bodies

Now you need to enable slopes for any game entities you want to collide against the tilemap.

game.physics.arcade.enable(player);

game.slopes.enable(player);
game.slopes.enable(emitter);

You don't need to do anything special for circular physics bodies, just the usual sprite.body.setCircle(radius).

Make sure you call game.slopes.enable(object) after making any changes to the size or shape of the physics body.

Collision

Now you can collide your sprite against the tilemap in the update() method of your Phaser state, as you normally would, using Arcade Physics. Voila!

// Collide the player with the collision layer
game.physics.arcade.collide(player, ground);

// Collide the particles with the collision layer
game.physics.arcade.collide(emitter, ground);

Debug rendering

To debug your collision layer, set its debug property to true.

This will overlay the collision shapes of each tile when the layer is rendered.

ground.debug = true;

Extras

Minimum Y offset

This feature separates rectangular physics bodies on the Y axis only, in the right situations.

// Prefer the minimum Y offset for this physics body
player.body.slopes.preferY = true;

// Prefer the minimum Y offset globally
game.slopes.solvers.sat.options.preferY = true;

If you're making a platformer, your player has drag on the X axis, and you don't want it to slide down slopes, this should solve your problem.

Collision pulling

To attempt to keep objects on a surface, you can use collision pulling.

This will pull physics bodies into a collision by a set velocity, if it matches the set direction.

// Pull the player into downwards collisions with a velocity of 50
player.body.slopes.pullDown = 50;

Here are the available properties for collision pulling:

body.slopes.pullUp
body.slopes.pullDown
body.slopes.pullLeft
body.slopes.pullRight
body.slopes.pullTopLeft
body.slopes.pullTopRight
body.slopes.pullBottomLeft
body.slopes.pullBottomRight

Building

If you want to build the plugin yourself from source, install Bower, clone the repository and run NPM, Bower and Gulp like so.

npm i -g bower
npm install
bower install
gulp build

There's also a watch task that builds the plugin whenever you make changes to the source.

gulp watch

Thanks

My thanks go out to those who made this Plugin possible.

And to contributors who were generous with their time, talents and support:

phaser-arcade-slopes's People

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.