コード例 #1
0
    void FeedGridData(s_leveldat levelData)
    {
        foreach (s_nodedat block in levelData.nodes_blocks)
        {
            Grid.block_layer[block.x, block.y] = Grid.SpawnObject(block.objectstr, new Vector2(20 * block.x, 20 * block.y));

            //SpriteRenderer rend = Grid.block_layer[block.x, block.y].gameObject.GetComponent<SpriteRenderer>();

            /*
             * if (rend != null)
             *  if (block.spr != null)
             *      //rend.sprite = block.spr;
             *
             *      Grid.block_layer[block.x, block.y].transform.rotation = block.rot;
             */
        }
        foreach (s_nodedat cha in levelData.nodes_character)
        {
            o_character character =
                (o_character)Grid.SpawnObject(cha.objectstr, new Vector2(20 * cha.x, 20 * cha.y));
            if (character.GetComponent <ICharacter>() != null)
            {
                character.GetComponent <ICharacter>().Intialize();
            }
        }
        foreach (s_nodedat cha in levelData.nodes_items)
        {
            o_item it = (o_item)Grid.SpawnObject(cha.objectstr, new Vector2(20 * cha.x, 20 * cha.y));
            if (it != null)
            {
                Grid.item_layer[cha.x, cha.y] = it;
            }
        }

        for (int x = 0; x < Grid.gridworldsize.x; x++)
        {
            for (int y = 0; y < Grid.gridworldsize.y; y++)
            {
                s_nodedat block_nod     = levelData.nodes_blocks.Find(obj => obj.x == x && obj.y == y);
                s_nodedat character_nod = levelData.nodes_character.Find(obj => obj.x == x && obj.y == y);
                s_nodedat item_nod      = levelData.nodes_items.Find(obj => obj.x == x && obj.y == y);
            }
        }
        Grid.ResetNodes();
    }
コード例 #2
0
    public bool PathFind(Vector2 target, Vector2 goal)
    {
        List <o_node>    openlist  = new List <o_node>();
        HashSet <o_node> closelist = new HashSet <o_node>();

        o_node startnode = Grid.NodeFromWorld(target);
        o_node goalnode  = Grid.NodeFromWorld(goal);

        if (goalnode == null ||
            //!startnode.walkable ||
            !goalnode.walkable)
        {
            //print("You're not a bloody ghost!");
            return(false);
        }

        startnode.h = HerusticVal(startnode.position, goal);
        openlist.Add(startnode);

        while (openlist.Count > 0)
        {
            o_node currentnode = openlist[0];
            //print(currentnode.h);
            for (int i = 1; i < openlist.Count; i++)
            {
                if (openlist[i].f <= currentnode.f && openlist[i].h < currentnode.h)
                {
                    currentnode = openlist[i];
                }
            }

            openlist.Remove(currentnode);
            closelist.Add(currentnode);
            //closednodes.Add(currentnode);   //For visual

            if (currentnode == goalnode)
            {
                Grid.ResetNodes();
                return(true);
            }

            foreach (o_node neighbour in Grid.CheckAroundNode(currentnode, this))
            {
                float newcost = currentnode.g + HerusticVal(currentnode.position, neighbour.position);
                if (!neighbour.walkable || closelist.Contains(neighbour))
                {
                    continue;
                }

                if (newcost < neighbour.g || !openlist.Contains(neighbour))
                {
                    neighbour.g      = newcost;
                    neighbour.h      = HerusticVal(neighbour.position, goal);
                    neighbour.parent = currentnode;

                    if (!openlist.Contains(neighbour))
                    {
                        //opennodes.Add(neighbour);
                        openlist.Add(neighbour);
                    }
                }
            }
        }
        return(false);
    }