I am trying to make a Pokémon like encounter system. What I wanted to do was make a NPC cause the player to be unable to move when they are spotted in a straight line of a predetermined distance as the NPC walks towards them. I was also wondering if there was a way to make it so you could use the action button to initiate the battle like in Pokémon.
Pokemon Like Battle Encounters
Game Mechanics DesignYou could make that NPC run a parallel process which checks for the player location and so on
##Proximity Detection Script v1.5a##Usage: Functions mainly used to activate events when within a certain range!##Functions:# Proxy.inprox?(@event_id, distance, los)# -distance is the range of detection and los is whether to use line of sight,# both have default values# Proxy.inprox_d?(@event_id, distance, los)# -same as original but only checks in the direction the event is facing# Proxy.inprox_r?(@event_id, width, height)# -checks a rectangle of width/height around the event (odd values best)## All calls return true if within range and false when not##Examples:# Proxy.inprox?(@event_id)# Proxy.inprox?(@event_id,10)# Proxy.inprox?(@event_id,5,false)# Proxy.inprox_d?(@event_id,6)# Proxy.inprox_r?(@event_id,3,3)##------##-- Script by: V.M of D.T##- Questions or comments can be:# given by email: [email hidden]# provided on facebook: http://www.facebook.com/DaimoniousTailsGames# All my other scripts and projects can be found here: http://daimonioustails.weebly.com/##--- Free to use in any project, commercial or non-commercial, with credit given# - - Though a donation's always a nice way to say thank you~ (I also accept actual thank you's) module Proxy #Default radius of detection: PROXYRANGE = 1 #Region for transparent obstacles REGION = 20 #Switch to be turned on to pause Proximity PAUSE_SWITCH = 100 #----# def self.inprox?(id, distance = PROXYRANGE, los = true, second_id = nil) return if $game_switches[PAUSE_SWITCH] x = $game_map.events[id].x y = $game_map.events[id].y if !second_id x2 = $game_player.x; y2 = $game_player.y else x2 = $game_map.events[second_id].x;y2 = $game_map.events[second_id].y end x_d = x - x2; y_d = y - y2 x_d *= -1 if x_d < 0; y_d *= -1 if y_d < 0 t_d = x_d + y_d return false if t_d > distance return false if !line_of_sight($game_player.x,$game_player.y,x,y) and los return true end def self.inprox_d?(id, distance = PROXYRANGE, los = true) if self.inprox?(id, distance, los) then else return false end x1 = $game_player.x; x2 = $game_map.events[id].x y1 = $game_player.y; y2 = $game_map.events[id].y x1 > x2 ? xx = x1 - x2 : xx = x2 - x1 y1 > y2 ? yy = y1 - y2 : yy = y2 - y1 case $game_map.events[id].direction when 2 if $game_player.y > $game_map.events[id].y then if yy >= xx then return true end end when 4 if $game_player.x < $game_map.events[id].x then if xx >= yy then return true end end when 6 if $game_player.x > $game_map.events[id].x then if xx >= yy then return true end end when 8 if $game_player.y < $game_map.events[id].y then if yy >= xx then return true end end end return false end def self.inprox_r?(id, width, height) return if $game_switches[PAUSE_SWITCH] width % 2 == 0 ? hwidth = width / 2 : hwidth = (width - 1) / 2 height % 2 == 0 ? hheight = height / 2 : hheight = (height - 1) / 2 x = $game_map.events[id].x - hwidth y = $game_map.events[id].y - hheight if $game_player.x >= x and $game_player.x < (x + width) if $game_player.y >= y and $game_player.y < (y + height) return true end end return false end def self.line_of_sight(x,y,x2,y2) tile_array = [] x_d = x - x2; y_d = y - y2 x_d *= -1 if x_d < 0 y_d *= -1 if y_d < 0 t_d = x_d + y_d t_d.to_i.times do |i| x_distance = x - x2 y_distance = y - y2 x_distance *= -1 if x_distance < 0 y_distance *= -1 if y_distance < 0 if x_distance > y_distance or y_distance == 0 x < x2 ? x += 1 : x -= 1 elsif y < y2 ? y += 1 : y -= 1 or x_distance == 0 end tile_array.push([x,y]) end tile_array.each do |cord| next if $game_map.region_id(cord[0],cord[1]) == REGION return false if !$game_map.check_passage(cord[0], cord[1], 0x002) end return true endendI am using this script, but I can't seem to get it to only a straight line, or keep the player from moving while the NPC is approaching the player. Also is there anyway that it can be triggered by the action button as well as the line of sight?
If you want it to be just like in Pokemon, then it's easy to just use some simple events. Instead of calculating line distance and all that, just put an invisible event on every square you want to be "in range", and have it run the encounter event when the player steps on it - the only difference between each block would be how many steps the NPC takes to get to where the player would be. Players already can't move when an event is running, so there's your solution for that. Then, when the battle is over, flip a switch that disables all of that one NPC's trigger blocks. Meanwhile, make the NPC itself a talked-to event that does the exact same thing, just without the walking.
The upside is that I'm pretty sure this is exactly how Pokemon does it, since the NPCs' line of sight is actually a set of specific blocks they can see, so as to make puzzles out of it. The downside is you need to manually make events for every NPC in the whole game.
The upside is that I'm pretty sure this is exactly how Pokemon does it, since the NPCs' line of sight is actually a set of specific blocks they can see, so as to make puzzles out of it. The downside is you need to manually make events for every NPC in the whole game.
Here's how you can check to see, for example, if the player is in a Pokemon-like line of sight within four spaces to the LEFT of an event:
i = $game_playerj = $game_map.events[event_id]if ((i.x < j.x) && ((i.x + 4) >= j.x) && (i.y == j.y))$game_self_switches[[map_id, event_id, 'A']] = trueendAdd this script alongside a short (1 to 4) frame Wait inside a Parallel Process page, and after that page add an Autorun page with Condition 'Self Switch A is ON' where the event walks up to the player and starts a fight or whatever. Be sure to either erase the event or set it to a new page afterward so the Autorun page doesn't repeat endlessly.
If you successfully implement this but have trouble figuring out the correct conditions for checking to the Right, Up, and Down directions, let me know and I'll give them to you.
Raemyi's solution will also work well and is much simpler. For a small game or a test trial, it is undoubtedly the best way. However it will take some extra time to scale to hundreds of events and my instinct is that it would cause significantly more lag on large maps.
i = $game_playerj = $game_map.events[event_id]if ((i.x < j.x) && ((i.x + 4) >= j.x) && (i.y == j.y))$game_self_switches[[map_id, event_id, 'A']] = trueendAdd this script alongside a short (1 to 4) frame Wait inside a Parallel Process page, and after that page add an Autorun page with Condition 'Self Switch A is ON' where the event walks up to the player and starts a fight or whatever. Be sure to either erase the event or set it to a new page afterward so the Autorun page doesn't repeat endlessly.
If you successfully implement this but have trouble figuring out the correct conditions for checking to the Right, Up, and Down directions, let me know and I'll give them to you.
Raemyi's solution will also work well and is much simpler. For a small game or a test trial, it is undoubtedly the best way. However it will take some extra time to scale to hundreds of events and my instinct is that it would cause significantly more lag on large maps.
Thank you. I do agree that Raemyi's solution isn't really a good one for this situation, and it would be ineffective. Since I'm still very new to this ,and scripts all together, the other directions would be helpful to have. I'm not entirely sure that I would be able to figure out the other directions since I have very little knowledge of scripts. Also is the a way to have it so the player can use the action button on the NPC to activate the event while keeping the line of sight activation?
If you're not comfortable working with stuff like this then it might be smart to use a solution like Raemyi's for now and then change over to this later as you're building most of the content in your game. Gotta crawl before you can ball.
Nevertheless, here are the other directions, in case you do find your way:
Right: if ((i.x > j.x) && ((i.x - 4) <= j.x) && (i.y == j.y))
Up: if ((i.y < j.y) && ((i.y + 4) >= j.y) && (i.x == j.x))
Down: if ((i.y > j.y) && ((i.y - 4) <= j.y) && (i.x == j.x))
Yes. The best way is to move all checks for a map into a single Parallel Process event and have it loop through every other Trainer on the map. Then you can put the normal Action Button processing in the Trainer's Event page. For example, if Trainers 3, 12, and 28 are the ones looking Down, you might construct it as such:
down_seekers = [3, 12, 28]i = $game_playerfor n in down_seekersj = $game_map.events[n]if ((i.y > j.y) && ((i.y - 4) <= j.y) && (i.x == j.x))$game_self_switches[[map_id, n, 'A']] = trueendendThis is the simplest way to code it but it's not very scalable. A more scalable version would directly check things like the graphic name and direction so that you wouldn't need to manually enter the IDs of events on the map that have a line of sight.
But again, crawl before you ball. Try to get it working by any "hacky" means necessary, even if it can't do everything smoothly. Then move onto stuff like this - it will be a million times easier for you to work with once you've got a good sense of how the engine views events.
Ok, thank you for all of your help.
Once you get a grasp at Ace, you can try using this:
Place a comment (notetag) with "<spot X>" in your event, where X is how far the event should be able to see. Once the player steps into this range without anything blocking between, the script will turn on self-switch D on the event, which ideally should toggle an autorun page that handles the rest. Your first page can still be "Action Button" to allow for manual fighting.
Code:
module IAVRA module SPOT REGEX = /<[ ]*spot[ ]+(\d+)[ ]*>/i SWITCH = "D" def self.see?(event, distance) return false unless distance x, y, d = event.x, event.y, event.direction distance.times do x2, y2 = $game_map.round_x_with_direction(x, d), $game_map.round_y_with_direction(y, d) return true if $game_player.x == x2 && $game_player.y == y2 return false unless event.passable?(x, y, d) x, y = x2, y2 end return false end endendclass Game_Event < Game_Character alias :iavra_spot_setup_page :setup_page alias :iavra_spot_update :update def setup_page(*args) iavra_spot_setup_page(*args) comments = @page.list.reduce([]){|a, c| a << c.parameters[0] if [108, 408].include?(c.code); a} comments.each{|c| @iavra_spot_distance = $1.to_i if c[IAVRA::SPOT::REGEX]} end def update(*args) iavra_spot_update(*args) $game_self_switches[[$game_map.map_id, @id, IAVRA::SPOT::SWITCH]] = true if IAVRA::SPOT.see?(self, @iavra_spot_distance) end end
Original thread: https://forums.rpgmakerweb.com/threads/pokemon-like-battle-encounters.41121/