forked from phaserjs/phaser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLine.js
More file actions
158 lines (136 loc) · 5.24 KB
/
Copy pathLine.js
File metadata and controls
158 lines (136 loc) · 5.24 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2019 Photon Storm Ltd.
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
var Class = require('../../../utils/Class');
var Shape = require('../Shape');
var GeomLine = require('../../../geom/line/Line');
var LineRender = require('./LineRender');
/**
* @classdesc
* The Line Shape is a Game Object that can be added to a Scene, Group or Container. You can
* treat it like any other Game Object in your game, such as tweening it, scaling it, or enabling
* it for input or physics. It provides a quick and easy way for you to render this shape in your
* game without using a texture, while still taking advantage of being fully batched in WebGL.
*
* This shape supports only stroke colors and cannot be filled.
*
* A Line Shape allows you to draw a line between two points in your game. You can control the
* stroke color and thickness of the line. In WebGL only you can also specify a different
* thickness for the start and end of the line, allowing you to render lines that taper-off.
*
* If you need to draw multiple lines in a sequence you may wish to use the Polygon Shape instead.
*
* @class Line
* @extends Phaser.GameObjects.Shape
* @memberof Phaser.GameObjects
* @constructor
* @since 3.13.0
*
* @param {Phaser.Scene} scene - The Scene to which this Game Object belongs. A Game Object can only belong to one Scene at a time.
* @param {number} [x=0] - The horizontal position of this Game Object in the world.
* @param {number} [y=0] - The vertical position of this Game Object in the world.
* @param {number} [x1=0] - The horizontal position of the start of the line.
* @param {number} [y1=0] - The vertical position of the start of the line.
* @param {number} [x2=128] - The horizontal position of the end of the line.
* @param {number} [y2=0] - The vertical position of the end of the line.
* @param {number} [strokeColor] - The color the line will be drawn in, i.e. 0xff0000 for red.
* @param {number} [strokeAlpha] - The alpha the line will be drawn in. You can also set the alpha of the overall Shape using its `alpha` property.
*/
var Line = new Class({
Extends: Shape,
Mixins: [
LineRender
],
initialize:
function Line (scene, x, y, x1, y1, x2, y2, strokeColor, strokeAlpha)
{
if (x === undefined) { x = 0; }
if (y === undefined) { y = 0; }
if (x1 === undefined) { x1 = 0; }
if (y1 === undefined) { y1 = 0; }
if (x2 === undefined) { x2 = 128; }
if (y2 === undefined) { y2 = 0; }
Shape.call(this, scene, 'Line', new GeomLine(x1, y1, x2, y2));
var width = this.geom.right - this.geom.left;
var height = this.geom.bottom - this.geom.top;
/**
* The width (or thickness) of the line.
* See the setLineWidth method for extra details on changing this on WebGL.
*
* @name Phaser.GameObjects.Line#lineWidth
* @type {number}
* @since 3.13.0
*/
this.lineWidth = 1;
/**
* Private internal value. Holds the start width of the line.
*
* @name Phaser.GameObjects.Line#_startWidth
* @type {number}
* @private
* @since 3.13.0
*/
this._startWidth = 1;
/**
* Private internal value. Holds the end width of the line.
*
* @name Phaser.GameObjects.Line#_endWidth
* @type {number}
* @private
* @since 3.13.0
*/
this._endWidth = 1;
this.setPosition(x, y);
this.setSize(width, height);
if (strokeColor !== undefined)
{
this.setStrokeStyle(1, strokeColor, strokeAlpha);
}
this.updateDisplayOrigin();
},
/**
* Sets the width of the line.
*
* When using the WebGL renderer you can have different start and end widths.
* When using the Canvas renderer only the `startWidth` value is used. The `endWidth` is ignored.
*
* This call can be chained.
*
* @method Phaser.GameObjects.Line#setLineWidth
* @since 3.13.0
*
* @param {number} startWidth - The start width of the line.
* @param {number} [endWidth] - The end width of the line. Only used in WebGL.
*
* @return {this} This Game Object instance.
*/
setLineWidth: function (startWidth, endWidth)
{
if (endWidth === undefined) { endWidth = startWidth; }
this._startWidth = startWidth;
this._endWidth = endWidth;
this.lineWidth = startWidth;
return this;
},
/**
* Sets the start and end coordinates of this Line.
*
* @method Phaser.GameObjects.Line#setTo
* @since 3.13.0
*
* @param {number} [x1=0] - The horizontal position of the start of the line.
* @param {number} [y1=0] - The vertical position of the start of the line.
* @param {number} [x2=0] - The horizontal position of the end of the line.
* @param {number} [y2=0] - The vertical position of the end of the line.
*
* @return {this} This Line object.
*/
setTo: function (x1, y1, x2, y2)
{
this.geom.setTo(x1, y1, x2, y2);
return this;
}
});
module.exports = Line;