kaplay icon indicating copy to clipboard operation
kaplay copied to clipboard

feat: A random noise component

Open hinum opened this issue 10 months ago • 2 comments

Most games need a way to generate number in a smooth manner. while you can do this with an external solutions like noisejs and others, i think a native solution would be nice.

explation

some thing like this


type NoiseType = "perlin"
type NoiseOpt = {
  type?: NoiseType
  seed?: number
  step?: number

  roughness?: number
}

function noise(opts?: NoiseOpt): NoiseComp

interface NoiseComp extends Comp {
  rand(stepBy?: number): number
  randVec(stepBy?: number): vec2
  randColor(stepBy?: number): Color
}

it keep a private state of the position. and the steps are how much to move it after a noise is generated.

example

an example of how this might be used

randomly changing an object's color:

const obj = k.add([
  k.rect(16, 16),
  k.color(),
  k.noise()
])

k.onUpdate(() => obj.color = obj.randColor(k.dt()))

alternatives

using components might not be the most ideal so maybe making it just a normal class might be more appropriate.

const noise = new k.NoiseGenerator()
noise.rand()

or maybe make it a global function like

k.perlinRand(somePosition)

hinum avatar Feb 25 '25 12:02 hinum

this would be pretty cool

niceEli avatar Feb 26 '25 14:02 niceEli

this is probably good plugin material

dragoncoder047 avatar Apr 02 '25 14:04 dragoncoder047