This sprite can be used to render static or moving image, the same image or changing images. For
static unchanging images see CPStaticImageSprite for even simpler API.
If a game is intended to work with adaptive scene sizing than all of its scene objects
must adapt to changing size of the scene. Image sprite provides simple and convenient way to support
this logic. Here's an example of using image sprite to display an image at the center of the screen
dynamically adjusting its position on each frame to the actual canvas size:
val img = ...
val spr = new CPImageSprite("logo", 0, 0, 0, img):
override def update(ctx: CPSceneObjectContext): Unit =
val canv = ctx.getCanvas
setX((canv.dim.width - getImage.getWidth) / 2)
setY((canv.dim.height - getImage.getHeight) / 2)
Notes:
All we have to do is override CPSceneObject.update method to update XY-coordinate on each frame.
Canvas instance will always have the current size of scene: whether it is defined explicitly or implicitly
as the current size of the terminal.
Initial (0,0) position is not used as we override the actual position on each frame.
We use methods setX and setY to set current XY-coordinates.
Note that we don't need to override any other methods. Specifically, we don't need to override render
method since it relies on getX and getY method in its implementation.
CosPlay provides number of built-in sprites. A sprite is a scene objects, visible or off-screen,
that is custom designed for a particular use case. Built-in sprites provide concrete
implementations for the abstract methods in the base CPSceneObject class. Most non-trivial games
will use combination of the built-in sprites and their own ones. Here's the list of the built-in
sprites:
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.
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.
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.
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.
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.
It is reflexive: for any instance x of type Any, x.equals(x) should return true.
It is symmetric: for any instances x and y of type Any, x.equals(y) should return true if and
only if y.equals(x) returns true.
It is transitive: for any instances x, y, and z of type Any if x.equals(y) returns true and
y.equals(z) returns true, then x.equals(z) should return true.
If you override this method, you should verify that your implementation remains an equivalence relation.
Additionally, when overriding this method it is usually necessary to override hashCode to ensure that
objects which are "equal" (o1.equals(o2) returns true) hash to the same scala.Int.
(o1.hashCode.equals(o2.hashCode)).
Attributes
that
the object to compare against this object for equality.
Returns:
true if the receiver object is equivalent to the argument; false otherwise.
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.
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.
Attributes
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.