UDBScript: the methods of the Pen class now return the instance of the Pen class to allow method chaining. Resolves #662

This commit is contained in:
biwa 2021-12-10 11:54:22 +01:00
parent d68f41a2a3
commit ebd7d0c01c
5 changed files with 74 additions and 27 deletions

View file

@ -11,6 +11,8 @@ class Pen
moveTo(pos)
{
this.curpos = new Vector2D(pos);
return this;
}
moveForward(distance)
@ -19,6 +21,8 @@ class Pen
this.curpos.x + Math.cos(this.angle) * distance,
this.curpos.y + Math.sin(this.angle) * distance
);
return this;
}
turnRightRadians(radians = Math.PI / 2)
@ -26,7 +30,9 @@ class Pen
this.angle -= radians;
while(this.angle < 0)
this.angle += Math.PI * 2;
this.angle += Math.PI * 2;
return this;
}
turnLeftRadians(radians = Math.PI / 2)
@ -34,7 +40,9 @@ class Pen
this.angle += radians;
while(this.angle > Math.PI * 2)
this.angle -= Math.PI * 2;
this.angle -= Math.PI * 2;
return this;
}
turnRight(degrees = 90.0)
@ -43,6 +51,8 @@ class Pen
while(this.angle < 0)
this.angle += Math.PI * 2;
return this;
}
turnLeft(degrees = 90.0)
@ -50,22 +60,30 @@ class Pen
this.angle += degrees * Math.PI / 180.0;
while(this.angle > Math.PI * 2)
this.angle -= Math.PI * 2;
this.angle -= Math.PI * 2;
return this;
}
setAngleRadians(radians)
{
this.angle = Math.PI / 2 - radians;
this.angle = Math.PI / 2 - radians;
return this;
}
setAngle(degrees)
{
this.angle = (90 - degrees) * Math.PI / 180.0;
return this;
}
drawVertex()
{
this.vertices.push(this.curpos);
return this;
}
finishDrawing(close = false)

View file

@ -71,11 +71,10 @@ let tags = Map.getMultipleNewTags(numnewtags);
var p = new Pen();
// Draw the closet
p.setAngle(90 * ScriptOptions.direction);
p.moveTo(basepos); p.drawVertex();
p.moveForward(ScriptOptions.length); p.drawVertex(); p.turnRight();
p.moveForward(closetwidth); p.drawVertex(); p.turnRight();
p.moveForward(ScriptOptions.length); p.drawVertex();
p.setAngle(90 * ScriptOptions.direction).moveTo(basepos).drawVertex()
.moveForward(ScriptOptions.length).drawVertex().turnRight()
.moveForward(closetwidth).drawVertex().turnRight()
.moveForward(ScriptOptions.length).drawVertex();
if(!p.finishDrawing(true))
throw "Something went wrong while drawing!";
@ -87,9 +86,8 @@ sector.floorHeight = 0;
sector.ceilingHeight = 56;
// Draw the carrying line
p.setAngle(90 * ScriptOptions.direction);
p.moveTo(basepos); p.drawVertex();
p.moveForward(32); p.drawVertex();
p.setAngle(90 * ScriptOptions.direction).moveTo(basepos).drawVertex()
.moveForward(32).drawVertex();
if(!p.finishDrawing())
throw 'Something went wrong while drawing!';
@ -106,11 +104,10 @@ newtagindex++;
if(ScriptOptions.inactive)
{
// Draw the blocking sector
p.setAngle(90 * ScriptOptions.direction);
p.moveTo(basepos);
p.moveForward(64); p.turnRight(); p.moveForward(16); p.drawVertex();
p.turnRight(); p.moveForward(8); p.drawVertex();
p.turnLeft(); p.moveForward(closetwidth - 32); p.drawVertex();
p.setAngle(90 * ScriptOptions.direction).moveTo(basepos)
.moveForward(64).turnRight().moveForward(16).drawVertex()
.turnRight().moveForward(8).drawVertex()
.turnLeft().moveForward(closetwidth - 32).drawVertex();
if(!p.finishDrawing(true))
throw "Something went wrong while drawing!";
@ -148,10 +145,9 @@ if(ScriptOptions.inactive)
if(ScriptOptions.looping)
{
// Create the teleport destination line
p.setAngle(90 * ScriptOptions.direction);
p.moveTo(basepos);
p.moveForward(32); p.turnRight(); p.moveForward(8); p.drawVertex();
p.moveForward(closetwidth - 16); p.drawVertex();
p.setAngle(90 * ScriptOptions.direction).moveTo(basepos)
.moveForward(32).turnRight().moveForward(8).drawVertex()
.moveForward(closetwidth - 16).drawVertex();
if(!p.finishDrawing())
throw 'Something went wrong while drawing!';
@ -161,10 +157,10 @@ if(ScriptOptions.looping)
line.tag = tags[newtagindex];
// Create the teleport line
p.setAngle(90 * ScriptOptions.direction);
p.moveTo(basepos);
p.moveForward(ScriptOptions.length - 32); p.turnRight(); p.moveForward(8); p.drawVertex();
p.moveForward(closetwidth - 16); p.drawVertex();
p.setAngle(90 * ScriptOptions.direction)
.moveTo(basepos)
.moveForward(ScriptOptions.length - 32).turnRight().moveForward(8).drawVertex()
.moveForward(closetwidth - 16).drawVertex();
if(!p.finishDrawing())
throw 'Something went wrong while drawing!';

View file

@ -70,7 +70,7 @@ namespace CodeImp.DoomBuilder.UDBScript
#region ================== Constants
private static readonly string SCRIPT_FOLDER = "udbscript";
public static readonly uint UDB_SCRIPT_VERSION = 1;
public static readonly uint UDB_SCRIPT_VERSION = 2;
#endregion

View file

@ -8,7 +8,7 @@ Example:
```js
// Draw a regular pentagon
let p = new Pen([0, 0]);
let p = new Pen([ 0, 0 ]);
for(let i=0; i < 5; i++)
{
@ -20,6 +20,18 @@ for(let i=0; i < 5; i++)
p.finishDrawing();
```
Example with method chaining (new in version 2):
```js
// Draw a regular pentagon
let p = new Pen([ 0, 0 ]);
for(let i=0; i < 5; i++)
p.drawVertex().moveForward(128).turnRight(72);
p.finishDrawing();
```
## Constructors
### Pen(pos=new Vector2D(0.0, 0.0))
@ -35,44 +47,62 @@ An instance of the `Pen` class
Moves the pen by a distance at the current angle.
#### Parameters
* distance: number of units to move
#### Return value
The instance of the `Pen` class (new in version 2)
### moveTo(pos)
Moves the pen to the given position. The position can be a `Vector2D` or an `Array` of two numbers.
#### Parameters
* pos: position to move the pen to
#### Return value
The instance of the `Pen` class (new in version 2)
### setAngle(degrees)
Sets the angle to the given degrees.
#### Parameters
* degrees: degrees to set the angle to
#### Return value
The instance of the `Pen` class (new in version 2)
### setAngleRadians(radians)
Sets the angle to the given radians.
#### Parameters
* radians: radians to set the angle to
#### Return value
The instance of the `Pen` class (new in version 2)
### turnLeft(degrees=90.0)
Turns the pen left by the given degrees.
#### Parameters
* degrees: degrees to turn left by. If omitted it will turn by 90°
#### Return value
The instance of the `Pen` class (new in version 2)
### turnLeftRadians(radians=Math.PI/2)
Turns the pen left by the given radians.
#### Parameters
* radians: radians to turn left by. If omitted it will turn by Pi/2
#### Return value
The instance of the `Pen` class (new in version 2)
### turnRight(degrees=90.0)
Turns the pen right by the given degrees.
#### Parameters
* degrees: degrees to turn right by. If omitted it will turn by 90°
#### Return value
The instance of the `Pen` class (new in version 2)
### turnRightRadians(radians=Math.PI/2)
Turns the pen right by the given radians.
#### Parameters
* radians: radians to turn right by. If omitted it will turn by Pi/2
#### Return value
The instance of the `Pen` class (new in version 2)
### drawVertex()
Draws a `Vertex` at the current position.
#### Return value
The instance of the `Pen` class (new in version 2)
### finishDrawing(close=false)
Finishes the drawing, actually creating the geometry. Also resets the vertices of this instance of `Pen`.

View file

@ -25,6 +25,9 @@ All files ending on .js in the `Scripts` directory (and its subdirectories) are
!!! tip
UDBScript does hot reloading, i.e. changes to the scripts, or copying new scripts into the directory structure will be shown immediately.
!!! warning
UDBScript comes with multiple example scripts in the `.\UDBScript\Scripts\Examples` directory. Do not modify those scripts, since they will be overwritten when UDB is updated. If you want to use one of the example scripts as a bas copy it to another directory!
## Setting hotkeys to execute scripts
Hotkeys to execute scripts can be set by going to `Tools` -> `Preferences` -> `Controls`, and then filtering by `execute`.