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:
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?
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:
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 );
}
