Click here to Skip to main content
15,745,973 members
Please Sign up or sign in to vote.
4.67/5 (3 votes)
See more:
I'm probably missing something incredibly obvious, but I can't for the life of me figure out what. I'm making an adventure game, where the character sees different things on his path through the world. I'm making it so, when he moves his X and Y co-ordinates change accordingly (so if he moves North, Y will be 1) That's working fine. My problem is getting something to show up when he moves into an area.

Making the character move is all happening in one class (Player) and in another class (Map) I have this line of code:


string comment;

public void PeopleAtLoc(Player ThePlayer)
{
    if (ThePlayer.XPos == 1)
    {
        comment = "You walked into a tree";
    }
}



What I'm aiming for is that if the player moves to a certain position (i.e. x=1) then it should print something that's there, and then allow the player to continue on their journey.

In another class, I have this block of code:

C#
public void eMove()
{
    Console.WriteLine("In which direction would you want to move?");
    Console.WriteLine("\n1) North \n 2) South \n 3) East \n 4) West");
    int nInput = int.Parse(Console.ReadLine());
    switch (nInput)
    {
        case 1:
            m_nYPos++;
            break;
        case 2:
            m_nYPos--;
            break;
        case 3:
            m_nXPos++;
            break;
        case 4:
            m_nXPos--;
            break;
    }
    Console.WriteLine("\nYour current position is X: " + XPos + " Y: " + YPos + "\n\n");
}


So, basically, I'm asking how I would go about printing that piece of code that says at point x1 for example, you would see a tree.

Thanks in advance.
Posted
Updated 4-Jan-11 23:59pm
v2
Comments
JOAT-MON 5-Jan-11 6:12am    
Questions:
1. Are you asking how to trigger the PeopleAtLoc()?
2. Is eMove() in Player? Or is it in an unmentioned class?

Well, the player object needs to be able to see the map object (maybe by passing the map as a parameter in the player object's constructor). At that point you could do this at the end of the eMove method:

C#
map.PeopleAtLoc(this);
 
Share this answer
 
How about having the Move method on the Map class

So the map would take Player object (which I'm assuming has a 'current location' field?!), and a parameter to indicate which direction the player is moving in

Another class here would probably be useful. An enum to handle direction and a class to hold the direction + movement in certain direction (e.g move North 1 \ 2 \ 3 etc)

C#
public enum Direction
{
    North = 1,
    South = 2 ,
    East = 3,
    West = 4
}


public class Movement
{
    public Direction Heading {get; set;}
    public int Advancement {get; set;}
}


So your Map object could now support a method like

map.Move (Player player, Movement movement)

Your map object would then raise an event such as PlayerMoved

You'd need to define some arguments for the event, e.g. PlayerMovedEventArguments. This should hold the new position the player has moved to and anything that happened at that location (such as walking into a tree)

You could pass that back to the controlling class to print out the info in some variable such as 'LocationInfo'


C#
private void Map_OnPlayerMoved(object sender, PlayerMovedEventArguments e)
{
     Console.WriteLine(e.LocationInfo);
}


HTH!
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900