Impact

This forum is read only and just serves as an archive. If you have any questions, please post them on github.com/phoboslab/impact

1 decade ago by FelipeBudinich

I've been trying without luck to implement debouncing on Impact in order to lighten the load of my AI (for a clear explanation and the place I got this idea and code snippet from: click here )

    Function.prototype.debounce = function (threshold, execAsap) {
        var func = this, // reference to original function
            timeout; // handle to setTimeout async task (detection period)
        // return the new debounced function which executes the original function only once
        // until the detection period expires
        return function debounced() {
            var obj = this, // reference to original context object
                args = arguments; // arguments at execution time
            // this is the detection function. it will be executed if/when the threshold expires
            function delayed() {
                // if we're executing at the end of the detection period
                if (!execAsap) { // execute now
                    func.apply(obj, args);
                }
                // clear timeout handle
                timeout = null;
            }
            // stop any current detection period
            if (timeout) { // otherwise, if we're not already waiting and we're executing at the beginning of the waiting period
                clearTimeout(timeout);
            } else if (execAsap) { // execute now
                func.apply(obj, args);
            }
            // reset the waiting period
            timeout = setTimeout(delayed, threshold || 100);
        };
    };

The idea is that I don't need each entity to know if it's on-screen every frame, or if the mouse is over it every frame, or if the win condition was met every frame, etc

Any ideas would be welcome ;)

1 decade ago by FelipeBudinich

Found something on Impact++

https://github.com/collinhover/impactplusplus/blob/master/lib/plusplus/helpers/utils.js#L346

Gonna test it :) (without the whole framework, it's overkill for my needs, and probably wouldn't be compatible out of the box with what I already have)

1 decade ago by FelipeBudinich

I managed to create something that resembles the function, but I guess it's too complex, and it will only be useful when the function being debounced is very complex:
//During update, we are gonna check every 300 ms 
// if the entity is currently on the screen
        update: function (entity) {
            if (!entity.isMoving) {
                return;
            }
            entity.onScreen = this.debounce(this.isOnScreen, [entity], 300);
        },
        isOnScreen: function (entity) {
            return (
                entity.pos.x + entity.size.x - 1 >= ig.game.screen.x &&
                entity.pos.x < ig.game.screen.x + ig.system.width &&
                entity.pos.y + entity.size.y - 1 >= ig.game.screen.y &&
                entity.pos.y < ig.game.screen.y + ig.system.height
            );
        },
 
        previousCall: 0,
        previousValue: "",
        debounce: function (fn, args, wait) {
            var value;
            if (Date.now() - this.previousCall > wait) {
                this.previousCall = Date.now();
                value = fn.apply(this, args);
                this.previousValue = value;
            } else {
                value = this.previousValue;
            }
            return value;
        },


If anyone figures out a more elegant way to do it, I'm all ears/eyes. (any kind of improvement is welcome).

1 decade ago by FelipeBudinich

Removed unnecessary assignments:

 debounce: function (fn, args, every) {

            if (Date.now() - this.previousCall >= every) {
                this.previousCall = Date.now();
                this.previousValue = fn.apply(this, args);
            }

            return this.previousValue;
        },

if I manage to get rid of the "previous call" hack, I'm calling it done.

1 decade ago by drhayes

Check out underscore's version. Could probably be adapted to Impact&039;s #Function.prototype modifications without too much trouble.
Page 1 of 1
« first « previous next › last »