Skip to content

Commit 266f993

Browse files
committed
Added Line Shape
1 parent c5fa4ef commit 266f993

7 files changed

Lines changed: 267 additions & 2 deletions

File tree

src/gameobjects/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ var GameObjects = {
4242
Arc: require('./shape/arc/Arc'),
4343
Ellipse: require('./shape/ellipse/Ellipse'),
4444
IsoBox: require('./shape/isobox/IsoBox'),
45+
Line: require('./shape/line/Line'),
4546
Polygon: require('./shape/polygon/Polygon'),
4647
Rectangle: require('./shape/rectangle/Rectangle'),
4748
Star: require('./shape/star/Star'),
@@ -69,6 +70,7 @@ var GameObjects = {
6970
Arc: require('./shape/arc/ArcFactory'),
7071
Ellipse: require('./shape/ellipse/EllipseFactory'),
7172
IsoBox: require('./shape/isobox/IsoBoxFactory'),
73+
Line: require('./shape/line/LineFactory'),
7274
Polygon: require('./shape/polygon/PolygonFactory'),
7375
Rectangle: require('./shape/rectangle/RectangleFactory'),
7476
Star: require('./shape/star/StarFactory'),

src/gameobjects/shape/line/Line.js

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/**
2+
* @author Richard Davey <rich@photonstorm.com>
3+
* @copyright 2018 Photon Storm Ltd.
4+
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
5+
*/
6+
7+
var Class = require('../../../utils/Class');
8+
var Shape = require('../Shape');
9+
var GeomLine = require('../../../geom/line/Line');
10+
var LineRender = require('./LineRender');
11+
12+
/**
13+
* @classdesc
14+
*
15+
* @class Line
16+
* @extends Phaser.GameObjects.Shape
17+
* @memberOf Phaser.GameObjects
18+
* @constructor
19+
* @since 3.13.0
20+
*
21+
* @param {Phaser.Scene} scene - The Scene to which this Game Object belongs. A Game Object can only belong to one Scene at a time.
22+
* @param {number} x - The horizontal position of this Game Object in the world.
23+
* @param {number} y - The vertical position of this Game Object in the world.
24+
*/
25+
var Line = new Class({
26+
27+
Extends: Shape,
28+
29+
Mixins: [
30+
LineRender
31+
],
32+
33+
initialize:
34+
35+
function Line (scene, x, y, x1, y1, x2, y2, fillColor, fillAlpha)
36+
{
37+
if (x === undefined) { x = 0; }
38+
if (y === undefined) { y = 0; }
39+
if (x1 === undefined) { x1 = 0; }
40+
if (y1 === undefined) { y1 = 0; }
41+
if (x2 === undefined) { x2 = 128; }
42+
if (y2 === undefined) { y2 = 0; }
43+
44+
Shape.call(this, scene, 'Line', new GeomLine(x1, y1, x2, y2));
45+
46+
var width = this.data.right - this.data.left;
47+
var height = this.data.bottom - this.data.top;
48+
49+
this._startWidth = 1;
50+
this._endWidth = 1;
51+
52+
this.setPosition(x, y);
53+
this.setSize(width, height);
54+
55+
if (fillColor !== undefined)
56+
{
57+
this.setFillStyle(fillColor, fillAlpha);
58+
}
59+
60+
this.updateDisplayOrigin();
61+
},
62+
63+
setLineWidth: function (startWidth, endWidth)
64+
{
65+
if (endWidth === undefined) { endWidth = startWidth; }
66+
67+
this._startWidth = startWidth;
68+
this._endWidth = endWidth;
69+
70+
return this;
71+
},
72+
73+
/**
74+
* [description]
75+
*
76+
* @method Phaser.Geom.Line#setTo
77+
* @since 3.13.0
78+
*
79+
* @param {number} [x1=0] - [description]
80+
* @param {number} [y1=0] - [description]
81+
* @param {number} [x2=0] - [description]
82+
* @param {number} [y2=0] - [description]
83+
*
84+
* @return {Phaser.Geom.Line} This Line object.
85+
*/
86+
setTo: function (x1, y1, x2, y2)
87+
{
88+
this.data.setTo(x1, y1, x2, y2);
89+
90+
return this;
91+
}
92+
93+
});
94+
95+
module.exports = Line;
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* @author Richard Davey <rich@photonstorm.com>
3+
* @copyright 2018 Photon Storm Ltd.
4+
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
5+
*/
6+
7+
/**
8+
* Renders this Game Object with the Canvas Renderer to the given Camera.
9+
* The object will not render if any of its renderFlags are set or it is being actively filtered out by the Camera.
10+
* This method should not be called directly. It is a utility function of the Render module.
11+
*
12+
* @method Phaser.GameObjects.Line#renderCanvas
13+
* @since 3.13.0
14+
* @private
15+
*
16+
* @param {Phaser.Renderer.Canvas.CanvasRenderer} renderer - A reference to the current active Canvas renderer.
17+
* @param {Phaser.GameObjects.Line} src - The Game Object being rendered in this call.
18+
* @param {number} interpolationPercentage - Reserved for future use and custom pipelines.
19+
* @param {Phaser.Cameras.Scene2D.Camera} camera - The Camera that is rendering the Game Object.
20+
* @param {Phaser.GameObjects.Components.TransformMatrix} parentMatrix - This transform matrix is defined if the game object is nested
21+
*/
22+
var LineCanvasRenderer = function (renderer, src, interpolationPercentage, camera, parentMatrix)
23+
{
24+
};
25+
26+
module.exports = LineCanvasRenderer;
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* @author Richard Davey <rich@photonstorm.com>
3+
* @copyright 2018 Photon Storm Ltd.
4+
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
5+
*/
6+
7+
var GameObjectFactory = require('../../GameObjectFactory');
8+
var Line = require('./Line');
9+
10+
/**
11+
* Creates a new Line Shape Game Object and adds it to the Scene.
12+
*
13+
* Note: This method will only be available if the Line Game Object has been built into Phaser.
14+
*
15+
* @method Phaser.GameObjects.GameObjectFactory#line
16+
* @since 3.13.0
17+
*
18+
* @param {number} [x=0] - The horizontal position of this Game Object in the world.
19+
* @param {number} [y=0] - The vertical position of this Game Object in the world.
20+
* @param {number} [x1=0] - The horizontal position of the first point in the triangle.
21+
* @param {number} [y1=0] - The horizontal position of the first point in the triangle.
22+
* @param {number} [x2=128] - The horizontal position of the second point in the triangle.
23+
* @param {number} [y2=0] - The horizontal position of the second point in the triangle.
24+
* @param {number} [fillColor] - The color the triangle will be filled with, i.e. 0xff0000 for red.
25+
* @param {number} [fillAlpha] - The alpha the triangle will be filled with. You can also set the alpha of the overall Shape using its `alpha` property.
26+
*
27+
* @return {Phaser.GameObjects.Line} The Game Object that was created.
28+
*/
29+
GameObjectFactory.register('line', function (x, y, x1, y1, x2, y2, fillColor, fillAlpha)
30+
{
31+
return this.displayList.add(new Line(this.scene, x, y, x1, y1, x2, y2, fillColor, fillAlpha));
32+
});
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* @author Richard Davey <rich@photonstorm.com>
3+
* @copyright 2018 Photon Storm Ltd.
4+
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
5+
*/
6+
7+
var renderWebGL = require('../../../utils/NOOP');
8+
var renderCanvas = require('../../../utils/NOOP');
9+
10+
if (typeof WEBGL_RENDERER)
11+
{
12+
renderWebGL = require('./LineWebGLRenderer');
13+
}
14+
15+
if (typeof CANVAS_RENDERER)
16+
{
17+
renderCanvas = require('./LineCanvasRenderer');
18+
}
19+
20+
module.exports = {
21+
22+
renderWebGL: renderWebGL,
23+
renderCanvas: renderCanvas
24+
25+
};
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/**
2+
* @author Richard Davey <rich@photonstorm.com>
3+
* @copyright 2018 Photon Storm Ltd.
4+
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
5+
*/
6+
7+
var Utils = require('../../../renderer/webgl/Utils');
8+
9+
/**
10+
* Renders this Game Object with the WebGL Renderer to the given Camera.
11+
* The object will not render if any of its renderFlags are set or it is being actively filtered out by the Camera.
12+
* This method should not be called directly. It is a utility function of the Render module.
13+
*
14+
* @method Phaser.GameObjects.Line#renderWebGL
15+
* @since 3.13.0
16+
* @private
17+
*
18+
* @param {Phaser.Renderer.WebGL.WebGLRenderer} renderer - A reference to the current active WebGL renderer.
19+
* @param {Phaser.GameObjects.Line} src - The Game Object being rendered in this call.
20+
* @param {number} interpolationPercentage - Reserved for future use and custom pipelines.
21+
* @param {Phaser.Cameras.Scene2D.Camera} camera - The Camera that is rendering the Game Object.
22+
* @param {Phaser.GameObjects.Components.TransformMatrix} parentMatrix - This transform matrix is defined if the game object is nested
23+
*/
24+
var LineWebGLRenderer = function (renderer, src, interpolationPercentage, camera, parentMatrix)
25+
{
26+
var pipeline = this.pipeline;
27+
28+
var camMatrix = pipeline._tempMatrix1;
29+
var shapeMatrix = pipeline._tempMatrix2;
30+
31+
renderer.setPipeline(pipeline);
32+
33+
shapeMatrix.applyITRS(src.x, src.y, src.rotation, src.scaleX, src.scaleY);
34+
35+
camMatrix.copyFrom(camera.matrix);
36+
37+
if (parentMatrix)
38+
{
39+
// Multiply the camera by the parent matrix
40+
camMatrix.multiplyWithOffset(parentMatrix, -camera.scrollX * src.scrollFactorX, -camera.scrollY * src.scrollFactorY);
41+
42+
// Undo the camera scroll
43+
shapeMatrix.e = src.x;
44+
shapeMatrix.f = src.y;
45+
}
46+
else
47+
{
48+
shapeMatrix.e -= camera.scrollX * src.scrollFactorX;
49+
shapeMatrix.f -= camera.scrollY * src.scrollFactorY;
50+
}
51+
52+
var dx = src._displayOriginX;
53+
var dy = src._displayOriginY;
54+
var alpha = camera.alpha * src.alpha;
55+
56+
if (src.isFilled)
57+
{
58+
var strokeTint = pipeline.strokeTint;
59+
var color = Utils.getTintAppendFloatAlphaAndSwap(src.fillColor, src.fillAlpha * alpha);
60+
61+
strokeTint.TL = color;
62+
strokeTint.TR = color;
63+
strokeTint.BL = color;
64+
strokeTint.BR = color;
65+
66+
var startWidth = src._startWidth;
67+
var endWidth = src._endWidth;
68+
69+
pipeline.batchLine(
70+
src.data.x1 - dx,
71+
src.data.y1 - dy,
72+
src.data.x2 - dx,
73+
src.data.y2 - dy,
74+
startWidth,
75+
endWidth,
76+
1,
77+
0,
78+
false,
79+
shapeMatrix,
80+
camMatrix
81+
);
82+
}
83+
};
84+
85+
module.exports = LineWebGLRenderer;

src/gameobjects/shape/triangle/TriangleCanvasRenderer.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
* @param {Phaser.Cameras.Scene2D.Camera} camera - The Camera that is rendering the Game Object.
2020
* @param {Phaser.GameObjects.Components.TransformMatrix} parentMatrix - This transform matrix is defined if the game object is nested
2121
*/
22-
var RectangleCanvasRenderer = function (renderer, src, interpolationPercentage, camera, parentMatrix)
22+
var TriangleCanvasRenderer = function (renderer, src, interpolationPercentage, camera, parentMatrix)
2323
{
2424
};
2525

26-
module.exports = RectangleCanvasRenderer;
26+
module.exports = TriangleCanvasRenderer;

0 commit comments

Comments
 (0)