CPSceneObject

abstract class CPSceneObject(id: String, tags: Set[String]) extends CPGameObject with CPLifecycle

Root type for all CosPlay scene objects.

Scene objects can be visible or invisible, have rectangular shape, have XY-coordinates of its left-top corner and Z-index. On each game frame and for each scene object the engine calls update method and, if object is visible and in frame, calls render method followed by calling all of its shaders.

Scene is a container for for scene objects. Scene objects can be added or removed from the scene dynamically via CPSceneObjectContext instance passed to update and render methods. Note that all scenes should have at least one scene object. In CosPlay there is no hierarchy of scene objects in the scene - all scene objects form a simple flat structure inside of the scene. Just like with any other game objects, you can use tags to organize scenes and scene objects.

Generally, you would use one of many existing scene object subclasses like the various sprites provided out-of-the-box. You can, however, just as easily subclass this class directly and provide the necessary implementation. At the minimum you need to implement all abstract methods and at least one of either render or update methods.

Here's a basic minimal scene object implementation that prints "Hello CosPlay!" text at (0,0) coordinate in black color:

object HelloSprite extends CPSceneObject("id"):
   override def getX: Int = 0
   override def getY: Int = 0
   override def getZ: Int = 0
   override def getDim: CPDim = CPDim(15, 1)
   override def render(ctx: CPSceneObjectContext): Unit =
       ctx.getCanvas.drawString(0, 0, 0, "Hello CosPlay!", CPColor.C_BLACK)

If scene object is invisible than only update method will be called on each frame. If object is visible and in camera frame - method render will be called as well to render itself. Note that shaders are called always, regardless of whether the object visible, in camera frame or invisible.

Note that shader pass consists of two phases:

  • On the 1st phase shaders for all visible scene objects are processed
  • On the 2nd phase shaders for all invisible scene objects are processed. This allows to minimize the interference between object shaders and full-screen shaders that are typically attached to the off-screen sprite that is invisible. In other words, full-screen shaders will be execute after all object shaders in a given scene.
Value parameters:
id

Optional ID of this scene object. By default, the random 6-character ID will be used.

tags

Optional set of organizational or grouping tags. By default, the empty set is used.

Source:
CPSceneObject.scala
class Object
trait Matchable
class Any

Value members

Abstract methods

Gets current dimension (width and height) of this object.

Gets current dimension (width and height) of this object.

See also:
Source:
CPSceneObject.scala
def getX: Int

Gets current X-coordinate of this object within dimensions of its scene. Note that returned value is allowed to be outside scene's dimension (e.g. negative value). In such cases, the clipping of the scene rendering will result in showing only portion or none of the object.

Gets current X-coordinate of this object within dimensions of its scene. Note that returned value is allowed to be outside scene's dimension (e.g. negative value). In such cases, the clipping of the scene rendering will result in showing only portion or none of the object.

Source:
CPSceneObject.scala
def getY: Int

Gets current Y-coordinate of this object within dimensions of its scene. Note that returned value is allowed to be outside scene's dimension (e.g. negative value). In such cases, the clipping of the scene rendering will result in showing only portion or none of the object.

Gets current Y-coordinate of this object within dimensions of its scene. Note that returned value is allowed to be outside scene's dimension (e.g. negative value). In such cases, the clipping of the scene rendering will result in showing only portion or none of the object.

Source:
CPSceneObject.scala
def getZ: Int

Gets Z-index or order to use in rendering of this object. A pixel with higher Z-index visually overrides the overlapping pixel in the same XY-coordinate with equal or smaller Z-index.

Gets Z-index or order to use in rendering of this object. A pixel with higher Z-index visually overrides the overlapping pixel in the same XY-coordinate with equal or smaller Z-index.

Source:
CPSceneObject.scala

Concrete methods

Gets optional collision shape or hit box for this sprite.

Gets optional collision shape or hit box for this sprite.

Returns:

Optional collision shape, None if this sprite does not support collisions.

See also:
Source:
CPSceneObject.scala
inline def getHeight: Int

Gets current height of this object.

Gets current height of this object.

See also:
Source:
CPSceneObject.scala

Gets rectangular shape of this sprite. It is basically a combination of its top-left corner XY-coordinate and sprite's dimension.

Gets rectangular shape of this sprite. It is basically a combination of its top-left corner XY-coordinate and sprite's dimension.

See also:
Source:
CPSceneObject.scala

Gets the optional list of shaders attached to this scene object. By default, returns an empty list. Note that shaders are called regardless of whether the object visible, in camera frame or invisible.

Gets the optional list of shaders attached to this scene object. By default, returns an empty list. Note that shaders are called regardless of whether the object visible, in camera frame or invisible.

Source:
CPSceneObject.scala
inline def getWidth: Int

Gets current width of this object.

Gets current width of this object.

See also:
Source:
CPSceneObject.scala
def hide(): Unit

Shortcut method for hiding this object. Note that by default all scene objects are visible.

Shortcut method for hiding this object. Note that by default all scene objects are visible.

See also:
Source:
CPSceneObject.scala
def isVisible: Boolean

Checks the visibility flag.

Checks the visibility flag.

If object is invisible than only update method will be called on each frame. If object is visible and in camera frame - method render will be called as well to render itself. Note that shaders are called regardless of whether the object visible, in camera frame or invisible.

See also:
Source:
CPSceneObject.scala

Called to render this scene object. Only visible and in camera frame objects will receive this callback. This callback is called on scene object after all scene objects received update callback. Note that unlike update callbacks and shaders that are called for all scene objects on each frame, this callback is only called for scene objects that are visible and, at least partially, in camera frame.

Called to render this scene object. Only visible and in camera frame objects will receive this callback. This callback is called on scene object after all scene objects received update callback. Note that unlike update callbacks and shaders that are called for all scene objects on each frame, this callback is only called for scene objects that are visible and, at least partially, in camera frame.

Value parameters:
ctx

Frame context. This context provides bulk of functionality that a scene object can do in a game, e.g. interact with other scene objects, check collisions, read input events and manage input focus, add or remove scene objects, add new and switch between scenes, etc.

See also:
Source:
CPSceneObject.scala
def setVisible(vis: Boolean): Unit

Sets visibility flag. Note that by default all scene objects are visible.

Sets visibility flag. Note that by default all scene objects are visible.

Value parameters:
vis

true to make this object visible, false otherwise.

See also:
Source:
CPSceneObject.scala
def show(): Unit

Shortcut method for showing this object. Note that by default all scene objects are visible.

Shortcut method for showing this object. Note that by default all scene objects are visible.

See also:
Source:
CPSceneObject.scala

Called to update the internal state of this scene object. This callback is called each frame on every object in the scene and it is called before any render callback. Note that all scene object will receive this callback before first render callback.

Called to update the internal state of this scene object. This callback is called each frame on every object in the scene and it is called before any render callback. Note that all scene object will receive this callback before first render callback.

Value parameters:
ctx

Frame context. This context provides bulk of functionality that a scene object can do in a game, e.g. interact with other scene objects, check collisions, read input events and manage input focus, add or remove scene objects, add new and switch between scenes, etc.

See also:
Source:
CPSceneObject.scala

Inherited methods

override def equals(obj: Any): Boolean
Definition Classes
Inherited from:
CPGameObject
Source:
CPGameObject.scala
def getId: String

Gets unique ID of this game object.

Gets unique ID of this game object.

Inherited from:
CPGameObject
Source:
CPGameObject.scala
final def getState: State

Gets current lifecycle state.

Gets current lifecycle state.

Inherited from:
CPLifecycle
Source:
CPLifecycle.scala
def getTags: Set[String]

Gets optional set of organizational tags. Note that by default the set of tags is empty.

Gets optional set of organizational tags. Note that by default the set of tags is empty.

See also:
Inherited from:
CPGameObject
Source:
CPGameObject.scala
def onActivate(): Unit

Callback on lifecycle object activation. Default implementation is no-op.

Callback on lifecycle object activation. Default implementation is no-op.

See also:

CPLifecycle.State.LF_ACTIVE

Inherited from:
CPLifecycle
Source:
CPLifecycle.scala
def onDeactivate(): Unit

Callback on lifecycle object deactivation. Default implementation is no-op.

Callback on lifecycle object deactivation. Default implementation is no-op.

See also:

CPLifecycle.State.LF_INACTIVE

Inherited from:
CPLifecycle
Source:
CPLifecycle.scala
def onStart(): Unit

Callback on lifecycle object start. Default implementation is no-op.

Callback on lifecycle object start. Default implementation is no-op.

See also:

CPLifecycle.State.LF_STARTED

Inherited from:
CPLifecycle
Source:
CPLifecycle.scala
def onStop(): Unit

Callback on lifecycle object stop. Default implementation is no-op.

Callback on lifecycle object stop. Default implementation is no-op.

See also:

CPLifecycle.State.LF_STOPPED

Inherited from:
CPLifecycle
Source:
CPLifecycle.scala