kaplay icon indicating copy to clipboard operation
kaplay copied to clipboard

refactor: rewrite loop to use update directly

Open niceEli opened this issue 1 year ago • 7 comments

make it less cursed. add a choice to wait for execute or just run right away. might be breaking change but may not depending on implementation

niceEli avatar May 23 '24 02:05 niceEli

I don't like that name

I think kaboom style is like

Hey kaboom, add a game object destroy a game object tween this make an object loop this function

lajbel avatar May 23 '24 03:05 lajbel

This is not about renaming loop, it is about refactoring it to not use wait. Instead it should use an update event directly.

mflerackers avatar May 23 '24 03:05 mflerackers

Lol, my bad

lajbel avatar May 23 '24 03:05 lajbel

@niceEli something like this

loop(this: GameObj<TimerComp>, time: number, action: () => void, count: number = -1): EventController {
        let t:number = 0
        const ev = this.onUpdate(() => {
	        t += dt()
		if (t >= time) {
			action()
                        t -= time
                        if (count != -1 && --count === 0) {
				ev.cancel()
			}
                }
	})
	return {
		get paused() {
			return ev.paused
		},
		set paused(p) {
			ev.paused = p
		},
		cancel: ev.cancel,
	}
},
wait(this: GameObj<TimerComp>, time: number, action?: () => void): EventController {
        return this.loop(time, action, 1)
}

mflerackers avatar May 23 '24 04:05 mflerackers

BTW, does anyone really use the then or onEnd stuff of wait? It only adds complexity I think. It makes more sense to add an onEnd to loop, since you might want to do something when it stops looping.

mflerackers avatar May 23 '24 04:05 mflerackers

loop(this: GameObj<TimerComp>, time: number, action: () => void, count: number = -1): EventController { let t:number = 0 const ev = this.onUpdate(() => { t += dt() if (t >= time) { action() t -= time if (count != -1 && --count === 0) { ev.cancel() } } }) return { get paused() { return ev.paused }, set paused(p) { ev.paused = p }, cancel: ev.cancel, } }, wait(this: GameObj<TimerComp>, time: number, action?: () => void): EventController { return this.loop(time, action, 1) }

should i add?

niceEli avatar May 23 '24 13:05 niceEli

Replace you mean? You can.

mflerackers avatar May 23 '24 14:05 mflerackers