CardActor

CardActor

A card in Croquet Microverse is represented with a class called CardActor on the Croquet's Model side, and CardPawn on the Croquet's View side.

An instance of CardActor and CardPawn can have a list of behaviors. Each of such behavior can use the features provided by the CardActor or CardPawn to which it is attached. For example, A CardActor implements a method called createCard(), which takes a card spec and creates a new card and place it in the world. A behavior can simply call:

  this.createCard({...});

to invoke the feature of the CardActor.

This document shows all public methods on CardActor and CardPawn that can be used from your behaviors.

Also, properties of CardActor or CardPawn can be read and written in a simple form:

let a = this.foo;

or

this.foo = 42;

Note that multiple behaviors installed to the same CardActor (or CardPawn) share the same property.

Microverse uses the vector types defined in the Worldcore library. Those vectors are actually simple JavaScript Array. In the description below, the representation of Vector3 and Quatanion are:

type Vector3 = [<number>, <number, <number>]

type Quaternion = [<number>, <number, <number>, <number>]

Also, the Card can handle certain kinds of pointer events. The EventName type is defined as:

type EventName = "pointerDown"|"pointerUp"|pointerMove"|"pointerTap"|"pointerLeave"|"pointerEnter"|"wheel"|"doubleDown"|"click"|"keyUp"|"keyDown"

The pointerTap event, only one synthesized event type, is generated when a pointerUp event occurs close in time (< 300ms) and space (< 10 pixels) to the corresponding pointerDown event. Then first the pointerTap event is sent before the pointerUp.

Members

# _translation :Vector3

The [x, y, z] translation of the card.

Type:
  • Vector3

# _rotation :Quaternion

The rotation of the card in quaternion.

Type:
  • Quaternion

# _scale :Vector3

The scale of the card in three axes.

Type:
  • Vector3

# _layers :Array

The layers property specifies how the card is treated when a special action is taken. Typical value are as follows:

  • "walk": The avatar stays on the geometry of the card.
  • "pointer": The pointer action is enabled.
  • "portal": the avatar tests if the card it is going through needs to take the avatar to a connected world.
Type:
  • Array

# _parent :CardActor

The cards in the world are organized in a hierarchical parent-children structure. The _parent specifies its parent. Note that this is a "logical" structure. All cards are held as a direct child of the Three.JS scene, with automatic matrix composition for nested cards.

Type:

# _behaviorModules :Array

The list of behavior modules installed to the card.

Type:
  • Array

# _name :string

An informative string for the card.

Type:
  • string

# _cardData :Object

Any other values that the CardActor holds are stored in an object stored in the _cardData property. This is needed to mark the values to be stored in the persistent data.

Type:
  • Object

Methods

# createCard(data) → {CardActor}

This method creates a new card (a CardActor on the model side and a CardPawn on the view side), based on the cardSpec.

Parameters:
Name Type Description
data object

the spec for a card to be created.

Returns:

the CardActor created.

Type
CardActor

# destroy()

This method removes the card from the world. All teardown() method of installed pawn behaviors and actor behaviors are called before the CardActor is removed from the system.

# call(behaviorName, methodName) → {any}

This method invokes a method of another behavior. The behaviorName has to be in one of the form of:

  • "ModuleName$BehaviorName"
  • "BehaviorName"

When the first form is used, it specifies the globally known module name and the behavior with the name on the actor side of the module. When the second form is used, it specified the behavior in the same module as the calling behavior. The methodName is the name of the method, and values are variable-length arguments for the method.

Parameters:
Name Type Description
behaviorName string

name of the behavior

methodName string

name of the method

...arguments any

arguments for the method

Returns:

the return value from the method

Type
any

# future(time)

This method schedules a future call in the specified logical time in milliseconds. If it is used in this form:

this.future(20).mth();

mth of the same behavior will be invoked 20 milliseconds from logical now. If you would like to call a method of another module or behavior, you can use call():

this.future(20).call("Module$Behavior", "mth");

Parameters:
Name Type Description
time number

the delay in logical millisecond

Returns:

A proxy to invoke a method on

# setCardData(options)

This method updates some elements in the _cardData object. The current value and the new values are merged to create the new _cardData object. As a side effect, it publishes cardDataSet Croquet event that can be handled by the pawn or any other subscribers.

Parameters:
Name Type Description
options object

keys and values to specify new values

# addEventListener(eventType, listener)

This method adds a "listener" to be invoked when an event occurs on the card. When listener is a function, it has to have a form of this.mthName where mthName is an existing method name of CardActor or the behavior itself. When listener is a string, it has to be the name of a method at CardActor or the behavior itself. The listener added by this Actor-side addEventListener() is invoked when any user in the world causes the corresponding user pointer or key event.

Calling this method with the same arguments removes the previous listener before adding the new one. This semantics ensures that dynamically-modified method will be used.

Parameters:
Name Type Description
eventType EventName

the event type

listener string | function

the name of the handler in the calling behavior, or a function specified in the form of this.mth

# removeEventListener(eventName, listener)

This method removes the event listener that was added. You can call it when there is no matching event listener.

Parameters:
Name Type Description
eventName EventType

the event type

listener string | function

# subscribe(scope, eventName, listener)

This method adds a Croquet event subscription. Unlike the version in the Croquet Library, this version removes the subscription with the same scope and eventName if it exists before adding the new one. This semantics ensures that it is safe to call this from the setup() of a behavior.

Parameters:
Name Type Description
scope string

the scope of Croquet event

eventName string

the event name of Croquet event

listener string | function

the name of the handler in the calling behavior, or a function specified in the form of this.mth

# publish(scope, eventName, data)

This method publishes a Croquet event.

Parameters:
Name Type Description
scope string

the scope of Croquet event

eventName string

the event name of Croquet event

data any

serializable data to be published

# listen(eventName, listener)

This method adds a Croquet event subscription by calling the subscribe() method with this.id as the scope.

Parameters:
Name Type Description
eventName string

the event name of Croquet event

listener string | function

the name of the handler in the calling behavior, or a function specified in the form of this.mth

# say(eventName, data)

This method publishes a Croquet event with this.id as the scope. It is usually used to publish an event whose expect recipient is the corresponding CardPawn.

Parameters:
Name Type Description
eventName string

the event name of Croquet event

data any

serializable data to be published

# addLayer(newLayerName)

This method adds a new element to the layers array. If newLayerName is already in the layers array, the call does not have any effects.

Parameters:
Name Type Description
newLayerName string

the name of a later to be added

# removeLayer(layerName)

This method removes an element from the layers array. If layerName is not in the layers array, the call does not have any effects.

Parameters:
Name Type Description
layerName string

the name of a later to be removed

# translateTo(v)

This method moves the translation of the card to the specified [x, y, z] coordinates.

Parameters:
Name Type Description
v Vector3

the translation for the card

# rotateTo(q)

This method sets the translation of the card to the specified by a quaternion ([x, y, z, w]).

Parameters:
Name Type Description
q Quaternion

the rotation for the card

# scaleTo(s)

This method sets the scale of the card to the specified by scale factors in [x, y, z] axis.

Parameters:
Name Type Description
s Vector3

the scale for the card

# positionTo(v, q)

This method sets the translation and rotation of the card, making sure that those two values are used in the same logical time and used for the rendering.

Parameters:
Name Type Description
v Vector3

the translation for the card

q Quaternion

the rotation for the card

# nop()

This method is empty. It is used to have a way to get the tap to focus keyboard events but you don't need to take any particular action on tap.