MZ Script for implementing a directional radar system for events

I'm currently trying to implement a radar system that allows events to detect the presence of the player in a well-defined area (concept detailed in this thread).

I've tried it with region IDs, and it works great for static events without having to write code. I did the same for moving events, using the $gameMap.distance() function as described in this thread, and it works too. However, this is limited to an area within X tiles around the event.

I'd like to go a bit further, trying to make the detection area also depend on the orientation of the event, as shown below (10 is the warning area, 1 is the danger area). I still lack knowledge about all the basic functions and scripts that exist in RPG Maker (although the list here is very useful). In particular, to know the current orientation of the event, and to count the number of tiles around it. Reason for my call for help.

Moving to the left / top:

1629898203827.png
1629898345150.png


I know there are plugins that could do this. Nevertheless, I prefer a "home-made" solution to code to avoid having too many dependencies and multiple parameters to manage that would change the basic behavior of the system when I only need this feature. And it is by coding that one becomes a coder, right? :ewink:

Note: if the possible solution is too resource-intensive, I will limit myself to the solution using $gameMap.distance() (or a similar custom formula, like the one proposed by @ImaginaryVillain ).

Thanks for suggestions.

Edit 1:

After some further research, I managed to produce this script that would work only if the event is directed upwards (still need to adapt the coordinates for each direction). Unfortunately, it seems that the retrieval of the direction of the event does not work, so the condition fails and the coordinates are not calculated. Any idea?

Code:
eventRadar.playerIsDetected = function( eventId, rangeFront, rangeSide, rangeBack ) {

    // Objects.
    const pl = $gamePlayer;   
    const ev = $gameMap.event( eventId );
    
    // Event direction.
    const evDirection = ev.direction();
    
    // Event coordinates (direction Top).
    if ( evDirection === 8 ) {
        const evCoordTopLeftX     = ev.x - rangeSide;
        const evCoordTopLeftY     = ev.y - rangeFront;
        const evCoordTopRightX    = ev.x + rangeSide;
        const evCoordTopRightY    = ev.y - rangeFront;
        const evCoordBottomLeftX  = ev.x - rangeSide;
        const evCoordBottomLeftY  = ev.y + rangeBack;
        const evCoordBottomRightX = ev.x + rangeSide;
        const evCoordBottomRightY = ev.y + rangeBack;
    }
    
    // Check detection.
    return ( evCoordTopLeftX <= pl.x && pl.x <= evCoordTopRightX && evCoordTopLeftY <= pl.y && pl.y <= evCoordBottomLeftY );
    
}
I know there are plugins that could do this. Nevertheless, I prefer a "home-made" solution to code to avoid having too many dependencies and multiple parameters to manage that would change the basic behavior of the system when I only need this feature. And it is by coding that one becomes a coder, right? :ewink:
I like your way of thinking~

This one is my personal code to detect range:
1629913832343.png

Spoiler: code
Code:
var pl = $gamePlayer;
var ev = $gameMap.event(this._eventId);
var rx = pl.x - ev.x;
var ry = pl.y - ev.y;
var check = Math.abs(rx+ry)<3;
$gameSelfSwitches.setValue([this._mapId, this._eventId, 'A'], check);

But that was just a simple range detection.
Then I modify it so user can make their custom rectangle:
1629914048772.png

Spoiler: code
Code:
var rc = [-1, -1, 1, 3];
var pl = $gamePlayer;
var ev = $gameMap.event(this._eventId);
switch (ev.direction()){
 case 4: rc = [-rc[3], rc[0], -rc[1], rc[2]]; break;
 case 6: rc = [rc[1], -rc[2], rc[3], -rc[0]]; break;
 case 8: rc = [-rc[2], -rc[3], -rc[0], -rc[1]]; break;
}
var rx = pl.x - ev.x;
var ry = pl.y - ev.y;
var check = (rx >= rc[0]) && (ry >= rc[1]) && (rx <= rc[2]) && (ry <= rc[3]);
$gameSelfSwitches.setValue([this._mapId, this._eventId, 'A'], check);

It's matrix transformation you study at high school.
Works pretty well for me inside the event~
Well thank you @Kuro DCupu, you save my day! This script works like a charm, and I can easily adapt it to my needs because it is so simple!

I like your way of thinking~
I have nothing against the use of plugins, on the contrary (and there are some for all needs, and very good ones too). But sometimes, you have to keep it simple... and it makes debugging easier. And if, at the same time, I can understand what I'm doing.

It's matrix transformation you study at high school.
What if I told you that it's been so long since I was in high school that I've forgotten that matrices exist. Thanks for reminding me that I need to brush up on my math basics again (note that this can still be helpful).

:vxmegane:
Thread closed

Original thread: https://forums.rpgmakerweb.com/threads/script-for-implementing-a-directional-radar-system-for-events.139869/