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

docs: Add examples for opacity(), z(), follow(), onHoverUpdate() and onScroll() #462

Merged
merged 2 commits into from
Oct 17, 2024
Merged
Changes from all commits
Commits
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
109 changes: 101 additions & 8 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,20 @@ export interface KAPLAYCtx<
/**
* Sets the opacity of a Game Object (0.0 - 1.0).
*
* @example
* ```js
* const bean = add([
* sprite("bean"),
* opacity(0.5) // Make bean 50% transparent
* ])
*
* // Make bean invisible
* bean.opacity = 0
*
* // Make bean fully visible
* bean.opacity = 1
* ```
*
* @group Components
*/
opacity(o?: number): OpacityComp;
Expand Down Expand Up @@ -589,12 +603,50 @@ export interface KAPLAYCtx<
/**
* Determines the draw order for objects on the same layer. Object will be drawn on top if z value is bigger.
*
* @example
* ```js
* const bean = add([
* sprite("bean"),
* pos(100, 100),
* z(10), // Bean has a z value of 10
* ])
*
* // Mark has a z value of 20, so he will always be drawn on top of bean
* const mark = add([
* sprite("mark"),
* pos(100, 100),
* z(20),
* ])
*
* bean.z = 30 // Bean now has a higher z value, so it will be drawn on top of mark
* ```
*
* @group Components
*/
z(z: number): ZComp;
/**
* Determines the layer for objects. Object will be drawn on top if the layer index is higher.
*
* @example
* ```js
* // Define layers
* layers(["background", "game", "foreground"], "game")
*
* const bean = add([
* sprite("bean"),
* pos(100, 100),
* layer("background"),
* ])
*
* // Mark is in a higher layer, so he will be drawn on top of bean
* const mark = add([
* sprite("mark"),
* pos(100, 100),
* layer("game"),
* ])
*
* bean.layer("foreground") // Bean is now in the foreground layer and will be drawn on top of mark
*
* @group Components
*/
layer(name: string): LayerComp;
Expand All @@ -616,11 +668,11 @@ export interface KAPLAYCtx<
*
* @param popt The options for the particles.
* @param eopt The options for the emitter.
*
*
* @example
* ```js
* // beansplosion
*
*
* // create the emitter
* const emitter = add([
* pos(center()),
Expand Down Expand Up @@ -681,11 +733,11 @@ export interface KAPLAYCtx<
/**
* Applies a force on a colliding object in order to make it move along the collision tangent vector.
* Good for conveyor belts.
*
*
* @example
* ```js
* loadSprite("belt", "/sprites/jumpy.png")
*
*
* // conveyor belt
* add([
* pos(center()),
Expand Down Expand Up @@ -790,6 +842,30 @@ export interface KAPLAYCtx<
/**
* Follow another game obj's position.
*
* @example
* ```js
* const bean = add(...)
*
* add([
* sprite("bag"),
* pos(),
* follow(bean) // Follow bean's position
* ])
* ```
*
* @example
* ```js
* const target = add(...)
*
* const mark = add([
* sprite("mark"),
* pos(),
* follow(target, vec2(32, 32)) // Follow target's position with an offset
* ])
*
* mark.follow.offset = vec2(64, 64) // Change the offset
* ```
*
* @group Components
*/
follow(obj: GameObj | null, offset?: Vec2): FollowComp;
Expand Down Expand Up @@ -1012,19 +1088,19 @@ export interface KAPLAYCtx<
mask(maskType?: Mask): MaskComp;
/**
* Specifies the FrameBuffer the object should be drawn on.
*
*
* @example
* ```js
* // Draw on another canvas
* let canvas = makeCanvas(width(), height())
*
*
* let beanOnCanvas = add([
* sprite("bean"),
* drawon(canvas.fb),
* ])
* ```
*
* @param canvas
*
* @param canvas
*/
drawon(canvas: FrameBuffer): Comp;
/**
Expand Down Expand Up @@ -1354,6 +1430,14 @@ export interface KAPLAYCtx<
/**
* Register an event that runs every frame when game objs with certain tags are hovered (required to have area() component).
*
* @example
* ```js
* // Rotate bean 90 degrees per second when hovered
* onHoverUpdate("bean", (bean) => {
* bean.angle += dt() * 90
* })
* ```
*
* @since v3000.0
* @group Events
*/
Expand Down Expand Up @@ -1528,6 +1612,15 @@ export interface KAPLAYCtx<
/**
* Register an event that runs when mouse wheel scrolled.
*
* @example
* ```js
* // Zoom camera on scroll
* onScroll((delta) => {
* const zoom = delta.y / 500
* camScale(camScale().add(zoom))
* })
* ```
*
* @since v3000.0
* @group Input
*/
Expand Down