ID:158894
 
So I am trying to create a rpg game where you get into a battle when you get near an enemy on the world map. If usr is 1 distance from the enemy, usr would thrown into a battlefield that would allow him to fight said enemy. How would I program this? I don't really know where to start.
Sandlight wrote:
If usr is 1 distance from the enemy, usr would thrown into a battlefield that would allow him to fight said enemy.

Don't use the word "usr" so much. It's not a valid word anyway and you're probably relying on the usr var too much and thinking it's suitable for every case. It very much isn't.

How would I program this? I don't really know where to start.

You want to initiate the battle when a player steps on a turf which is adjacent to an enemy. You can do something after every time a player steps into a turf by overriding turf/Entered() - that proc is called whenever something has entered a turf. The beginning of your system would look a little like this:
turf/Entered(mob/player/M)
if(istype(M,/mob/player)) //make sure what entered the turf is actually a player mob - it could be just a mob or even an obj
var/mob/enemy/E = locate() in range(1,src) //look for an enemy adjacent to the turf (or in the turf)
if(E) //if one was found
M.StartBattle(E) //start a battle between the player and that enemy

That's the basic premise. You could also of course look for multiple enemies and start a multiple-enemy battle if you want that. What exactly to do in the rest depends on multiple factors such as whether your game is single-player or multiplayer, what do you want to happen when a battle is initiated and how you want it to work, do you want the participants to be still visible on the map at the spot where the battle began, etc. Generally, you should handle battles tidily by using datums to keep track of the battle status. A short example:
mob/fighter
proc/StartBattle(list/enemies)
if(!istype(enemies,/list))
//in case we were only given one enemy
enemies = list(enemies)
var/player_side = src.party || list(src) //if src has a party, everyone fights
new /Battle(player_side,enemies)

//our battle datum
Battle
var
list
participants_A //a list for the participants of each side
participants_B
starting_locations //needed to move participants back afterward
//this could just be stored as associated value in the \
first 2 lists.

status
... //whatever else
New(list/side_A,list/side_B)
//do all the battle creation stuff...
src.participants_A = side_A
src.participants_B = side_B
//move everyone to the battle zone or whatever
...