-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathArcadeSlopes.js
More file actions
129 lines (110 loc) · 4.85 KB
/
ArcadeSlopes.js
File metadata and controls
129 lines (110 loc) · 4.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/**
* @author Chris Andrew <chris@hexus.io>
* @copyright 2016-2021 Chris Andrew
* @license MIT
*/
/**
* Arcade Slopes provides sloped tile functionality for tilemaps that use
* Phaser's Arcade physics engine.
*
* @class Phaser.Plugin.ArcadeSlopes
* @constructor
* @extends Phaser.Plugin
* @param {Phaser.Game} game - A reference to the game using this plugin.
* @param {any} parent - The object that owns this plugin, usually a Phaser.PluginManager.
* @param {integer} defaultSolver - The default collision solver type to use for sloped tiles.
*/
Phaser.Plugin.ArcadeSlopes = function (game, parent, defaultSolver) {
Phaser.Plugin.call(this, game, parent);
/**
* The collision solvers provided by the plugin.
*
* Maps solver constants to their respective instances.
*
* @property {object} solvers
*/
var solvers = {};
solvers[Phaser.Plugin.ArcadeSlopes.SAT] = new Phaser.Plugin.ArcadeSlopes.SatSolver();
/**
* The Arcade Slopes facade.
*
* @property {Phaser.Plugin.ArcadeSlopes.Facade} facade
*/
this.facade = new Phaser.Plugin.ArcadeSlopes.Facade(
new Phaser.Plugin.ArcadeSlopes.TileSlopeFactory(),
solvers,
defaultSolver || Phaser.Plugin.ArcadeSlopes.SAT
);
// Give the facade a reference to the plugin; this makes it easier to remove
// it at runtime
this.facade.plugin = this;
};
Phaser.Plugin.ArcadeSlopes.prototype = Object.create(Phaser.Plugin.prototype);
Phaser.Plugin.ArcadeSlopes.prototype.constructor = Phaser.Plugin.ArcadeSlopes;
/**
* The Arcade Slopes plugin version number.
*
* @constant
* @type {string}
*/
Phaser.Plugin.ArcadeSlopes.VERSION = '0.3.2';
/**
* The Separating Axis Theorem collision solver type.
*
* Uses the excellent SAT.js library.
*
* @constant
* @type {string}
*/
Phaser.Plugin.ArcadeSlopes.SAT = 'sat';
/**
* Initializes the plugin.
*
* @method Phaser.Plugin.ArcadeSlopes#init
*/
Phaser.Plugin.ArcadeSlopes.prototype.init = function () {
// Give the game an Arcade Slopes facade
this.game.slopes = this.game.slopes || this.facade;
// Keep a reference to the original Arcade.collideSpriteVsTilemapLayer method
this.originalCollideSpriteVsTilemapLayer = Phaser.Physics.Arcade.prototype.collideSpriteVsTilemapLayer;
// Replace the original method with the Arcade Slopes override, along with
// some extra methods that break down the functionality a little more
Phaser.Physics.Arcade.prototype.collideSpriteVsTile = Phaser.Plugin.ArcadeSlopes.Overrides.collideSpriteVsTile;
Phaser.Physics.Arcade.prototype.collideSpriteVsTiles = Phaser.Plugin.ArcadeSlopes.Overrides.collideSpriteVsTiles;
Phaser.Physics.Arcade.prototype.collideSpriteVsTilemapLayer = Phaser.Plugin.ArcadeSlopes.Overrides.collideSpriteVsTilemapLayer;
// Add some extra neighbour methods to the Tilemap class
Phaser.Tilemap.prototype.getTileTopLeft = Phaser.Plugin.ArcadeSlopes.Overrides.getTileTopLeft;
Phaser.Tilemap.prototype.getTileTopRight = Phaser.Plugin.ArcadeSlopes.Overrides.getTileTopRight;
Phaser.Tilemap.prototype.getTileBottomLeft = Phaser.Plugin.ArcadeSlopes.Overrides.getTileBottomLeft;
Phaser.Tilemap.prototype.getTileBottomRight = Phaser.Plugin.ArcadeSlopes.Overrides.getTileBottomRight;
// Keep a reference to the original TilemapLayer.renderDebug method
this.originalRenderDebug = Phaser.TilemapLayer.prototype.renderDebug;
// Add some overrides and helper methods to the TilemapLayer class
Phaser.TilemapLayer.prototype.getCollisionOffsetX = Phaser.Plugin.ArcadeSlopes.Overrides.getCollisionOffsetX;
Phaser.TilemapLayer.prototype.getCollisionOffsetY = Phaser.Plugin.ArcadeSlopes.Overrides.getCollisionOffsetY;
Phaser.TilemapLayer.prototype.renderDebug = Phaser.Plugin.ArcadeSlopes.Overrides.renderDebug;
};
/**
* Destroys the plugin and nulls its references. Restores any overriden methods.
*
* @method Phaser.Plugin.ArcadeSlopes#destroy
*/
Phaser.Plugin.ArcadeSlopes.prototype.destroy = function () {
// Null the game's reference to the facade
this.game.slopes = null;
// Restore the original collideSpriteVsTilemapLayer method and null the rest
Phaser.Physics.Arcade.prototype.collideSpriteVsTile = null;
Phaser.Physics.Arcade.prototype.collideSpriteVsTiles = null;
Phaser.Physics.Arcade.prototype.collideSpriteVsTilemapLayer = this.originalCollideSpriteVsTilemapLayer;
// Remove the extra neighbour methods from the Tilemap class
Phaser.Tilemap.prototype.getTileTopLeft = null;
Phaser.Tilemap.prototype.getTileTopRight = null;
Phaser.Tilemap.prototype.getTileBottomLeft = null;
Phaser.Tilemap.prototype.getTileBottomRight = null;
// Remove the overrides and helper methods from the TilemapLayer class
Phaser.TilemapLayer.prototype.getCollisionOffsetX = null;
Phaser.TilemapLayer.prototype.getCollisionOffsetY = null;
Phaser.TilemapLayer.prototype.renderDebug = this.originalRenderDebug;
// Call the parent destroy method
Phaser.Plugin.prototype.destroy.call(this);
};