Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ESM Script Documentation #644

Open
wants to merge 74 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
74 commits
Select commit Hold shift + click to select a range
28ecc52
Upgrading Script examples to include ESM variants
marklundin Mar 27, 2024
7dd39e0
Add esm variants for tutorials
marklundin Mar 27, 2024
36f094f
linting
marklundin Mar 27, 2024
fa3a447
linting
marklundin Mar 27, 2024
58c6e7b
Merge branch 'main' into esm-script-examples
willeastcott Apr 5, 2024
dbf459a
Update docs/tutorials/collision-and-triggers.md
marklundin Apr 8, 2024
4ea970d
Update docs/tutorials/collision-and-triggers.md
marklundin Apr 8, 2024
34bc4db
Update docs/tutorials/Using-forces-on-rigid-bodies.md
marklundin Apr 8, 2024
a9709a3
Update docs/tutorials/entity-picking.md
marklundin Apr 8, 2024
441f8c6
Update docs/tutorials/facebook-api.md
marklundin Apr 8, 2024
492543d
Update docs/tutorials/facebook-api.md
marklundin Apr 8, 2024
577ac0e
Update docs/tutorials/facebook-api.md
marklundin Apr 8, 2024
3ef4a68
Update docs/tutorials/facebook-api.md
marklundin Apr 8, 2024
e183493
Update docs/tutorials/facebook-api.md
marklundin Apr 8, 2024
77f40e3
Update docs/tutorials/first-person-movement.md
marklundin Apr 8, 2024
67360a7
Update docs/tutorials/keepyup-part-four.md
marklundin Apr 8, 2024
e6fae27
Update docs/tutorials/keepyup-part-three.md
marklundin Apr 8, 2024
e1c53dd
Update docs/tutorials/google-ads-for-games.md
marklundin Apr 8, 2024
9ed44e7
Update docs/tutorials/keepyup-part-six.md
marklundin Apr 8, 2024
99e777b
Update docs/tutorials/facebook-api.md
marklundin Apr 8, 2024
57f0e73
Update docs/tutorials/facebook-api.md
marklundin Apr 8, 2024
ecb0387
Update docs/tutorials/facebook-api.md
marklundin Apr 8, 2024
728b9bf
replaced attributesDefinition with attributes
marklundin Apr 10, 2024
0123222
Add ESM Scripts user manual page
marklundin Apr 11, 2024
302f7ab
fix link
marklundin Apr 11, 2024
42ce655
fixed admonition
marklundin Apr 11, 2024
3af9af9
content update
marklundin Apr 11, 2024
3903927
updates
marklundin Apr 12, 2024
d9dfedf
CDN
marklundin Apr 12, 2024
90493a7
warning
marklundin Apr 12, 2024
b9087bc
esm-scripts typos (#651)
LeXXik Apr 13, 2024
bf138a1
Update docs/user-manual/publishing/web/communicating-webpage.md
marklundin Apr 15, 2024
1663a93
Remove ScriptType reference
marklundin Apr 15, 2024
4127e46
Merge branch 'esm-script-examples' of https://github.com/playcanvas/d…
marklundin Apr 15, 2024
f976527
Update docs/user-manual/scripting/script-attributes.md
marklundin Apr 15, 2024
5e290e6
Update docs/user-manual/scripting/migration-guide.md
marklundin Apr 15, 2024
385873b
Update docs/user-manual/scripting/communication.md
marklundin Apr 15, 2024
e11d4b9
arrow function
marklundin Apr 15, 2024
7e11268
Update docs/user-manual/scripting/script-attributes.md
marklundin Apr 15, 2024
99201ad
Update docs/user-manual/scripting/script-attributes.md
marklundin Apr 15, 2024
fe5cd7c
linting fixes
marklundin Apr 15, 2024
86a9b67
add version flag
marklundin Apr 15, 2024
71e7789
renamed Classic -> Legacy. Script Attributes docs
marklundin Jun 17, 2024
0957290
Completed Script Attribute docs
marklundin Jun 17, 2024
755b744
Merge branch 'main' into esm-script-examples
marklundin Jun 17, 2024
30641fc
replicate legacy in ja, to fix build
marklundin Jun 17, 2024
5552740
Refactored attribute grouping and added interface attributes
marklundin Jul 1, 2024
979a114
Update legacy scripting system documentation
marklundin Jul 1, 2024
4c495f2
Update script attributes and loading order documentation
marklundin Sep 2, 2024
f1ba724
Refactor script creation and import statements
marklundin Sep 2, 2024
43e3664
Refactor script attributes documentation
marklundin Sep 2, 2024
b421e09
Fix linting issues and update script documentation
marklundin Sep 2, 2024
2aa64f2
Update linting command in package.json
marklundin Sep 2, 2024
1adbabd
Merge branch 'main' into esm-script-examples
marklundin Sep 2, 2024
a0b1a24
Update migration guide and add legacy loading order documentation
marklundin Sep 2, 2024
7f3cca7
Merge branch 'esm-script-examples' of https://github.com/playcanvas/d…
marklundin Sep 2, 2024
793a4f8
Update loading order title and add redirect for legacy loading order
marklundin Sep 2, 2024
cedc5d0
Remove unnecessary blank line in esm-scripts.md
marklundin Sep 2, 2024
9466eb2
Refactor watch function to use Object.defineProperty
marklundin Sep 5, 2024
3c6530f
reverting tutorials back to main
marklundin Sep 17, 2024
e32a1a7
revert animation docs
marklundin Sep 17, 2024
0606a53
revert getting started + asset types
marklundin Sep 17, 2024
a3d165b
revert physics
marklundin Sep 17, 2024
75cac59
revert publishing
marklundin Sep 17, 2024
6087ea8
revert user interface
marklundin Sep 17, 2024
1e266db
revert package-lock.json to main
marklundin Sep 17, 2024
437712c
Merge branch 'main' into esm-script-examples
marklundin Sep 17, 2024
574964c
package-lock fix
marklundin Sep 17, 2024
587f807
Update docs/user-manual/scripting/anatomy.md
marklundin Sep 17, 2024
e2dbbe4
legacy -> classic
marklundin Sep 26, 2024
154785d
fix links
marklundin Sep 26, 2024
57fa097
Update ESM Scripts description
marklundin Sep 26, 2024
00f67df
fix linting
marklundin Sep 26, 2024
4bbc962
fixed i18n link
marklundin Sep 26, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 87 additions & 0 deletions docs/tutorials/Using-forces-on-rigid-bodies.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,90 @@ We include a reset function that brings the cube to its original position and, a

## Full code listing

import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';

<Tabs defaultValue="classic" groupId='script-code'>
<TabItem value="esm" label="ESM">

```javascript
import { ScriptType, Vec3, EVENT_KEYDOWN } from 'playcanvas';
import { KEY_LEFT, KEY_RIGHT, KEY_UP, KEY_A, KEY_D, KEY_W, KEY_S, KEY_F, KEY_R } from 'playcanvas';
marklundin marked this conversation as resolved.
Show resolved Hide resolved

export class Movement extends ScriptType {

// initialize code called once per entity
initialize() {
this.torque = 7;
this.app.keyboard.on(EVENT_KEYDOWN, this.onKeyDown, this);

this.on('destroy', function() {
this.app.keyboard.off(EVENT_KEYDOWN, this.onKeyDown, this);
}, this);
}

onKeyDown(event) {
event.event.preventDefault();
}

// update code called every frame
update(dt) {
//update player's position
this.playerPos = this.entity.getLocalPosition();

const app = this.app;

//keyboard controls and applying forces and moments.
if (app.keyboard.isPressed(KEY_LEFT) ) {
this.entity.rigidbody.applyImpulse(-1, 0, 0);
}
if (app.keyboard.isPressed(KEY_RIGHT) ) {
this.entity.rigidbody.applyImpulse(1, 0, 0);
}
if (app.keyboard.isPressed(KEY_UP) ) {
this.entity.rigidbody.applyImpulse(0, 1, 0);
}
if (app.keyboard.isPressed(KEY_A) ) {
this.entity.rigidbody.applyTorque(0, this.torque, 0);
}
if (app.keyboard.isPressed(KEY_D) ) {
this.entity.rigidbody.applyTorque(0, -this.torque, 0);
}
if (app.keyboard.isPressed(KEY_W) ) {
this.entity.rigidbody.applyTorque(-this.torque, 0, 0);
}
if (app.keyboard.isPressed(KEY_S) ) {
this.entity.rigidbody.applyTorque(this.torque, 0, 0);
}
if (app.keyboard.isPressed(KEY_F) ) {
this.entity.rigidbody.applyForce(0, 9.8, 0);
}

// Keeping the cube on screen - cube moves off of one screen edge then appears from the opposite edge.
if (this.playerPos.x < -9.0) {
this.entity.rigidbody.teleport(8.8, this.playerPos.y, this.playerPos.z);
}
if (this.playerPos.x > 9.0) {
this.entity.rigidbody.teleport(-8.8, this.playerPos.y, this.playerPos.z);
}

// cube reset control
if (app.keyboard.wasPressed(KEY_R) ) {
this.reset();
}
}

reset() {
this.entity.rigidbody.teleport(0, 2, 0);
this.entity.rigidbody.linearVelocity = Vec3.ZERO;
this.entity.rigidbody.angularVelocity = Vec3.ZERO;
}
}
```

</TabItem>
<TabItem value="classic" label="Classic">

```javascript
var DynamicBody = pc.createScript('dynamicBody');

Expand Down Expand Up @@ -184,6 +268,9 @@ DynamicBody.prototype.reset = function () {
};
```

</TabItem>
</Tabs>

[1]: https://api.playcanvas.com/classes/Engine.RigidBodyComponent.html#applyForce
[2]: https://api.playcanvas.com/classes/Engine.RigidBodyComponent.html#applyImpulse
[3]: https://api.playcanvas.com/classes/Engine.RigidBodyComponent.html#applyTorque
Expand Down
35 changes: 35 additions & 0 deletions docs/tutorials/anim-blending.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,38 @@ With the animations fully set up, we now need to make it possible for users to i

Remember that parameter `punch` that we set up before? This script will simply toggle that parameter on and off depending on whether the ‘P’ key is currently pressed and whether the character is currently punching:

import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';

<Tabs defaultValue="classic" groupId='script-code'>
<TabItem value="esm" label="ESM">

```javascript
import { ScriptType, EVENT_KEYDOWN, EVENT_KEYUP, KEY_P } from 'playcanvas';

export class KeyboardControls extends ScriptType {

// initialize code called once per entity
initialize() {
this.app.keyboard.on(EVENT_KEYDOWN, this.keyDown, this);
this.app.keyboard.on(EVENT_KEYUP, this.keyUp, this);
}

update(dt) {
if (this.app.keyboard.wasPressed(KEY_P) && (this.entity.anim.baseLayer.activeState !== 'Punch')) {
this.entity.anim.setBoolean('punch', true);
}

if (this.app.keyboard.wasReleased(KEY_P) && (this.entity.anim.baseLayer.activeState === 'Punch')) {
this.entity.anim.setBoolean('punch', false);
}
}
}
```

</TabItem>
<TabItem value="classic" label="Classic">

```javascript
var KeyboardControls = pc.createScript('keyboardControls');

Expand All @@ -95,6 +127,9 @@ KeyboardControls.prototype.update = function(dt) {
};
```

</TabItem>
</Tabs>

From this point, you are able to add more and more animations to the animstategraph asset and start building much more complex animation state graphs!

See the full Scene [here](https://playcanvas.com/editor/scene/1065029)
53 changes: 53 additions & 0 deletions docs/tutorials/animation-blending.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,56 @@ In the image you can see the animation component in the Inspector. There are 2 a

So this kind of functionality goes beyond the abilities of the humble animation component. A script component is required to cook up this additional behavior. You can see the script component in the above screenshot of the skinned character entity in Editor and it refers to a JS file called animation_blending.js. The contents of this file is:

import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';

<Tabs defaultValue="classic" groupId='script-code'>
<TabItem value="esm" label="ESM">

```javascript
import { ScriptType, KEY_P } from 'playcanvas';

export class AnimationBlending extends ScriptType {
static states = {
idle: {
animation: 'male.json'
},
punch: {
animation: 'male_uppercut_jab.json'
}
}

// initialize code called once per entity
initialize() {
this.blendTime = 0.2;

this.setState('idle');
};

update(dt) {
if (this.app.keyboard.wasPressed(KEY_P)) {
this.setState('punch');
}

if (this.app.keyboard.wasReleased(KEY_P)) {
this.setState('idle');
}
};

setState(state) {
const states = AnimationBlending.states;

this.state = state;
// Set the current animation, taking 0.2 seconds to blend from
// the current animation state to the start of the target animation.
this.entity.animation.play(states[state].animation, this.blendTime);
};
}
```

</TabItem>
<TabItem value="classic" label="Classic">

```javascript
var AnimationBlending = pc.createScript('animationBlending');

Expand Down Expand Up @@ -79,6 +129,9 @@ AnimationBlending.prototype.setState = function (state) {
};
```

</TabItem>
</Tabs>

From this point, you are able to add more and more animations to the animation component and start scripting much more complex animation state charts.

See [the full Scene here][2]
Expand Down
57 changes: 57 additions & 0 deletions docs/tutorials/collision-and-triggers.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,34 @@ The next Entity we'll need is the trigger.

With this Entity we have a *collision* component but no *rigidbody* so it acts as a trigger. The trigger Entity also has a *script* component with some code attached. Triggers are only useful if something happens when they are triggered, so we need to add some code to fire and listen for events when the trigger is activated.

import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';

<Tabs defaultValue="classic" groupId='script-code'>
<TabItem value="esm" label="ESM">

```javascript
import { ScriptType } from 'playcanvas';
marklundin marked this conversation as resolved.
Show resolved Hide resolved

export class Trigger extends ScriptType {
// initialize code called once per entity
initialize() {
this.entity.collision.on('triggerenter', this.onTriggerEnter, this);
};

onTriggerEnter(entity) {
this.entity.rigidbody.linearVelocity = pc.Vec3.ZERO;
this.entity.rigidbody.angularVelocity = pc.Vec3.ZERO;
marklundin marked this conversation as resolved.
Show resolved Hide resolved
// Reset back to roughly the position the entity started in.
const position = entity.getPosition();
this/entity.rigidbody.teleport(position.x, 10, 0);
};
}
```

</TabItem>
<TabItem value="classic" label="Classic">

```javascript
var Trigger = pc.createScript('trigger');

Expand All @@ -80,6 +108,9 @@ Trigger.prototype.onTriggerEnter = function(entity) {
};
```

</TabItem>
</Tabs>

There two significant parts to the code above.

First in the ```initialize``` method we start listening to the **triggerenter** event. This event fires once when a rigid body enters a trigger volume (where a trigger volume is an entity that has a collision component but no rigidbody component). The companion event is **triggerleave** which is fired once the penetrating rigid body leaves the trigger.
Expand Down Expand Up @@ -114,6 +145,29 @@ The difference between **contact** and **collisionstart** is subtle but importan

Both events are useful, but in this demo we'll use the **collisionstart** event to trigger a sound effect that plays when the objects hit the ground. Here's the code:

<Tabs defaultValue="classic" groupId='script-code'>
<TabItem value="esm" label="ESM">

```javascript
import { ScriptType } from 'playcanvas';

export class Collider extends ScriptType {
// initialize code called once per entity
initialize() {
this.entity.collision.on('collisionstart', this.onCollisionStart, this);
}

onCollisionStart(result) {
if (result.other.rigidbody) {
this.entity.sound.play("hit");
}
}
}
```

</TabItem>
<TabItem value="classic" label="Classic">

```javascript
var Collider = pc.createScript('collider');

Expand All @@ -129,6 +183,9 @@ Collider.prototype.onCollisionStart = function (result) {
};
```

</TabItem>
</Tabs>

In the ```initialize``` method we set up the event listener, and then in the event handler we check to see if the other entity has a **rigidbody** component (this is to avoid playing a sound when we enter a trigger volume) and then we play the "hit" sound effect. So now, every time an Entity with the collider script attached collides with another rigid body, it will play the hit sound.

And that's all there is to handling Collisions and Triggers in PlayCanvas.
Expand Down
Loading