Feedback on code?

So, I’ve been trying to figure out aggro, pathing and such the past few days, and I finally figured out how to get the enemy to break patrol, chase the player if they get to close and return to their patrolling pattern if they player escapes the aggro radius with tweens!

The only problem is that I feel like I could have better code for this, and was wanting some critique on what I am using. I’d appreciate any and all feedback / suggestions :slight_smile:

let distanceFromPlayer = Math.sqrt(Math.pow((scene.player.y-this.y) , 2) + Math.pow((scene.player.x-this.x), 2));
        if ((distanceFromPlayer < scene.player.aggroRadius))
        {
            //slime is aggroed            
            scene.tweens.add(        
            {
                targets: this,
                props: 
                {
                    y: { value: scene.player.y},
                    x: { value: scene.player.x}
                },
                yoyo: true,
                repeat: -1
            });
            this._aggroed = true;
            this.patrolling = false;
        }else{
            //slime is not aggroed            
            if (this.x == this.xSpawnCoordinate && this.y == this.ySpawnCoordinate && this.returningHome == true)
            {
                //slime has returned home
                scene.tweens.killTweensOf(this);
                scene.tweens.add(        
                    {
                        targets: this,
                        props: this.tweenXY,
                        yoyo: true,
                        repeat: -1
                    });
                    this.returningHome = false;
                    this.patrolling = true;
            }else{                
                //send slime home after losing aggro
                if (this.patrolling == false && this._aggroed == true)
                {
                    scene.tweens.add(        
                    {
                        targets: this,
                        props: 
                        {
                            y: { value: this.ySpawnCoordinate, duration: 2000},
                            x: { value: this.xSpawnCoordinate, duration: 2000}
                        },
                        yoyo: true,
                        repeat: -1
                    });   
                    this._aggroed = false;
                    this.returningHome = true;
                }
            }

Nice job! That’s real exciting that you got such a complex behavior working!

Couple things I noticed just skimming it:

There’s a distance function that Phaser provides you can use rather than having that real condensed line of math at the top of your code - https://photonstorm.github.io/phaser3-docs/Phaser.Math.Distance.html#.Between__anchor

This code is crying to be refactored into a finite state machine. Each condition of your if statement would be a different state that the slime is in. AGGROED, PATROLING, RETURNING_HOME (or something like that). It would also help you get rid of athe multiple boolean flags you’ve got. This is a real solid tutorial about it - https://www.mkelly.me/blog/phaser-finite-state-machine/

Otherwise it seems pretty solid. I don’t fully understand why all the tweens you are doing have yoyo: true and repeat: -1, but I don’t think that’s necessary to get the gist of the code.

1 Like