Search Unity

"Neighbors" problem

Discussion in 'Scripting' started by Xoroxoxoxoxoso, Aug 26, 2012.

  1. Xoroxoxoxoxoso

    Xoroxoxoxoxoso

    Joined:
    Jul 26, 2012
    Posts:
    80
    Hi guys,

    I am making turn based strategy game and right know I am working on AI for the nations. But I am stucked. To introduce you to my problem. Picture a europe map where I need to recognize a neighbors let`s say for Germany. And what my problem is that I don`t know how to add more neighbors to this country if Germany conquer for example France. So how can I add neighbors of France and at the same time I need to remove France from Germany neighbors because it is already conquered.

    Thank you guys!
     
  2. diegzumillo

    diegzumillo

    Joined:
    Jul 26, 2010
    Posts:
    418
    You're basically merging the two countries in to a single..er.. Frarmany.

    OK, here's my suggestion. Stop thinking in terms of countries and think in terms of teams and territories. For example blue, red and yellow teams and the countries as territories. So the blue team occupies Germany and france, from there you can simply check all neighbors of blue territories that are not blue, these are all attainable territories for the blue team.

    Don't worry about "blue team" sounding silly, it's just an internal system. For the user, the blue team is called Germany because that's the blue team's first territory, for example.
     
  3. Xoroxoxoxoxoso

    Xoroxoxoxoxoso

    Joined:
    Jul 26, 2012
    Posts:
    80
    Well basically yes, but my main problem is the mapping of neighbors and checking it. Create an array of all neighbors in all territories and when blue captures it add it to blue array list ? Not sure if it`s possible and there might be better solutions too.
     
  4. roger0

    roger0

    Joined:
    Feb 3, 2012
    Posts:
    1,208
    Are you trying to figure out how to take away from one side and give to the other? In that case, you could have all of the territories of one side stored in a generic list. The contents of generic list can be added and removed easily. When one side conqures anothers territory, remove the territory from that sides list and add it to the others list.

    However, if your trying to do something more advanced, such as having one territory recognize the other territories around it organically, it might help to take a look at this tutorial series. http://www.digitaltutors.com/11/training.php?pid=641&autoplay=1. Its for AI and waypoints but the waypoints need to recognize the other waypoints around it as neighbors. The idea could work for you, although what your doing might not need the amount of depth.
     
  5. Himani_123

    Himani_123

    Joined:
    May 12, 2014
    Posts:
    1
    Hi guys,

    i have a problem with Addneighbour and removeNeighbour functions..,on triggerEnter i get all the neighbours and position of neighbour is also exchanging but ontriggerExit the two gems whose position got change don't remain each others neighbor as a result on again click they donot change their position.,
    and if donot call removeNeighbour function onTriggerExit object's previous and new neighbours get add. Hope i am able to make u understand my question.



    feeler.cs


    void OnTriggerEnter(Collider c)
    {
    //owner.RemoveNeighbor(c.GetComponent<Gem>());
    if (c.tag == "Gem") {

    owner.AddNeighbor(c.GetComponent<Gem> ());

    }

    }
    void OnTriggerExit(Collider c)
    {
    if(c.tag =="Gem")
    {
    owner.RemoveNeighbor(c.GetComponent<Gem>());


    }


    }


    board.cs


    public void SwapGem(Gem gem1)
    {

    gem1start = gem1.transform.position;
    gem1end = gemComponent.transform.position;
    if (gemComponent.IsNeighbourWith(gem1)) {

    gem2start = gem1end;
    gem2end = gem1start;
    // gem1start=new Vector3(0,0,0);
    // gem1end = new Vector3 (0,0,0);
    gem1.transform.position = gem2start;
    gemComponent.transform.position = gem2end;

    }


    }


    Gem.cs


    public void AddNeighbor(Gem g)
    {
    if (!Neighbors.Contains (g)) {
    Neighbors.Add (g);
    }
    }




    public void OnMouseDown()
    {

    if (!GameObject.Find ("board").GetComponent<board> ().isSwapping) {
    GameObject.Find ("board").GetComponent<board> ().SwapGem (this);

    }

    }
    public void RemoveNeighbor(Gem g)
    {
    Neighbors.Remove(g);
    }


    what am i doing wrong...?? can you guys help me in to solve it
     
    Last edited: May 21, 2014
  6. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    firstly: [code ] [/code ] tags (without spaces) when you are pasting in code

    secondly: why did you necro a thread from 2 years ago about something completely different??

    thirdly: changing a transform's position using translate of setting the position property does so without interacting with the physics engine, so those triggers shouldn't be firing (being part of the physics stuff). Where else are you using "RemoveNeighbour()"?