kaplay icon indicating copy to clipboard operation
kaplay copied to clipboard

feat: shake() function for sprites

Open candycarmel opened this issue 1 year ago • 4 comments

there is camera shake however, i think a sprite shake would be useful aswell

a shake function for the (sprite component? really anything that can be drawn)

you would use the function with an intensity parameter, and an optional duration parameter, and the sprite would then shake back and forth without changing the transform of the area

giving all other objects a fixed() component and then doing camShake()

I'm not sure if the duration parameter would be necessary.

candycarmel avatar Jul 26 '24 02:07 candycarmel

this is a cool idea, could make it like only shake X axis or Y or both, about the duration it could use the same parameters as the improved shake i suggested

amyspark-ng avatar Jul 26 '24 02:07 amyspark-ng

something like this?

/**
* Shakes an object
* @param initialPos The position it initially was at 
* @param time How long will it shake for
* @param strength The strength of the shake (how much it will move in pixels)
* @param interval How long between each shake
*/
shakePanic(initialPos: Vec2, time?: number, strength?: number, interval?: number) : shakePanicReturnType {
	time = time ?? 1
	strength = strength ?? 10
	interval = interval ?? 0.05
	
	let shakeLoop = this.loop(interval, () => {
		const newPos = initialPos.add(rand(-strength, strength), rand(-strength, strength)) 
		this.pos = newPos
	})

	function cancelFunction() {
		shakeLoop.cancel()
		this.pos = initialPos
	}

	this.wait(time, cancelFunction)
	
	return {
		cancel: cancelFunction
	}
}

amyspark-ng avatar Sep 24 '24 01:09 amyspark-ng

Best to make it a component, so it works for any object with pos, not just sprites. Camera will become an object in 4000.

mflerackers avatar Sep 24 '24 01:09 mflerackers

yeah it's actually part of a component im writing for juice functions

amyspark-ng avatar Sep 24 '24 02:09 amyspark-ng