Exemple #1
0
    public List<Vector3> Get_Attack(List<Vector3> center, int reach)
    {
        MapData tmpmap=new MapData(sizex, sizey); 	// this is the distance to center map

        List<Vector3> lst = new List<Vector3>(); // list of avaliable squares
        List<Vector3> atk = new List<Vector3>(); // list of attack targets
        int[] occid = new int[center.Count];
        bool noblock = true;

        // tempororaily remove the character so it wont block itself
        for (int i=0; i<center.Count; i++) {
            //save the occupy id in map in center.z
            occid[i]= occupy.Check_At(center[i]);
            occupy.Set_At(center[i],0);
            lst.Add(center[i]);
            tmpmap.Set_At(center[i],1);
        }

        // build path
        int first = 0, last=lst.Count;
        for (int step=0; step< reach; step++)
        {
            for (int i=first; i< last; i++)
            {

                for (int di=0; di<8; di++)
                {
                    Vector3 tmpv = lst[i] + dirs[di];

                    // Empty square
                    if (Check_Avi(tmpv,noblock))
                    {
                        if (tmpmap.Check_At(tmpv) == 0)
                        {
                            tmpv.z = i;
                            int dist=tmpmap.Check_At(lst[i])+length[di];

                            if (dist <= reach * 10 + 5)
                            {
                                lst.Add(tmpv);
                                tmpmap.Set_At(tmpv, dist);
                            }
                        }
                        else
                        {
                            int dist = tmpmap.Check_At(lst[i]) + length[di];
                            if (dist < tmpmap.Check_At(tmpv))
                            {
                                tmpmap.Set_At(tmpv, dist);
                            }

                        }

                    }
                    // Square with enemies
                    if (Check_Avi(tmpv,noblock) && occupy.Check_At(tmpv) > 0)
                    {
                        if (!center.Contains(tmpv) && !atk.Contains(tmpv))
                        {
                            int dist = tmpmap.Check_At(lst[i]) + length[di];
                            if (dist <= ( reach) * 10 + 5)
                            {
                                tmpv.z = -1;
                                atk.Add(tmpv);
                                tmpmap.Add_At(tmpv, 1);
                            }
                        }
                    }

                }
            }

            first = last;
            last = lst.Count;
        }

        // put the character back
        for (int i=0; i<center.Count; i++) {
            occupy.Set_At(center[i], occid[i]);
        }

        //		lst.RemoveAt (0);
        lst.AddRange(atk);
        return lst;
    }
Exemple #2
0
    // Use this for initialization
    void Awake()
    {
        // directions and length

        dirs = new Vector3[]{
            new Vector3( 1, 0, 0),
            new Vector3(-1, 0, 0),
            new Vector3( 0, 1, 0),
            new Vector3( 0,-1, 0),
            new Vector3( 1, 1, 0),
            new Vector3(-1,-1, 0),
            new Vector3( 1,-1, 0),
            new Vector3(-1, 1, 0)};
        length = new int[8] {10,10,10,10,14,14,14,14};

        //////
        gameboard = new GameObject("Board").transform;
        // place tiles
        sizex = 20; sizey = 20;
        mapdata = new MapData(sizex, sizey);
        occupy = new MapData(sizex, sizey);
        charmap = new MapData(sizex, sizey);
        mapdata.Generate_Map();
        charmap.Set_All(-1);
        for (int i=0; i< sizex; i++)
        {
            for (int j=0; j< sizey; j++)
            {
                int t= mapdata.Check_At(i, j);
                GameObject toInstantiate = tiles[t];
                GameObject instance =Instantiate(toInstantiate, new Vector3(i, j, 0f), Quaternion.identity) as GameObject;
                instance.transform.SetParent(gameboard);
                SpriteRenderer sprite = instance.GetComponent<SpriteRenderer>();
                sprite.sortingLayerName = "map";
            }
        }
    }