コード例 #1
0
    public int MoveCost(Vector3 cursorpos)
    {
        Vector3 mousePositon = cursorpos;

        if (path.PathFind(transform.position, mousePositon))
        {
            o_node goal          = grid.NodeFromWorld(mousePositon);
            o_node thischaracter = grid.NodeFromWorld(transform.position);

            if (!grid.NodeFromWorld(mousePositon).walkable)
            {
                s_object objectm = grid.ObjectFromWorld(mousePositon);

                return(-1);
            }
            else
            {
                return(path.RetracePath(goal, thischaracter).Count);
            }
        }
        else
        {
            return(-1);
        }
    }
コード例 #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);
    }
コード例 #3
0
    public void CheckCharacterSurroundings(bool is_enemy)
    {
        //If an enemy or two appears enable the confirm attack button
        targets.Clear();
        HashSet <o_node> enemynodes = Grid.CheckAroundNode(Grid.NodeFromWorld(player.transform.position));

        foreach (o_node e in enemynodes)
        {
            s_object enemyObject = Grid.ObjectFromWorld(e);

            if (enemyObject == null)
            {
                continue;
            }

            if (enemyObject.GetType() == typeof(o_character))
            {
                o_character enemy = enemyObject.GetComponent <o_character>();

                if (PlayerPrefs.GetInt("MonsterMode") == 0)
                {
                    if (!is_enemy)
                    {
                        if (!Heroes.Contains(enemy.name))
                        {
                            targets.Add(enemy);
                        }
                    }
                    else
                    {
                        if (!Adversaries.Contains(enemy.name))
                        {
                            targets.Add(enemy);
                        }
                    }
                }
                else
                if (PlayerPrefs.GetInt("MonsterMode") == 1)
                {
                    if (!is_enemy)
                    {
                        if (!Adversaries.Contains(enemy.name))
                        {
                            targets.Add(enemy);
                        }
                    }
                    else
                    {
                        if (!Heroes.Contains(enemy.name))
                        {
                            targets.Add(enemy);
                        }
                    }
                }
            }
            else
            {
                continue;
            }
        }
    }
コード例 #4
0
    private void Update()
    {
        if (Grid.gridworldsize.y > 0 && Grid.gridworldsize.x > 0)
        {
            Vector3  mousePositon = Camera.main.ScreenToWorldPoint(Input.mousePosition);
            s_object selectedObj  = Grid.ObjectFromWorld(mousePositon);

            if (Input.GetKeyDown(KeyCode.W))
            {
                spritenum++;
            }
            if (Input.GetKeyDown(KeyCode.S))
            {
                spritenum--;
            }


            if (Input.GetKeyDown(KeyCode.E))
            {
                angle += 90;
            }

            if (angle > 360)
            {
                angle = 0;
            }

            examplerend.gameObject.transform.localRotation = Quaternion.Euler(0, 0, angle);

            spritenum = Mathf.Clamp(spritenum, 0, spriteArray.Length - 1);

            examplerend.sprite = spriteArray[spritenum];

            if (Grid.NodeFromWorld(mousePositon) != null)
            {
                Vector2 snap = Grid.SnapToGrid(mousePositon);
                if (Input.GetMouseButton(0))
                {
                    if (selectedObj != null)
                    {
                        if (selectedObj.GetComponent <s_object>().GetType() == typeof(o_tile))
                        {
                            SpriteRenderer ren = selectedObj.GetComponent <SpriteRenderer>();
                            ren.sprite = spriteArray[spritenum];
                            ren.gameObject.transform.localRotation = examplerend.gameObject.transform.localRotation;
                        }
                    }
                    //target.transform.position = mousePositon;
                    if (Grid.ObjectFromWorld(mousePositon) == null)
                    {
                        Vector2Int vecint = Grid.VectorPositionFromWorld(mousePositon);

                        if (selected_object != null)
                        {
                            s_object obj = Grid.SpawnObject(selected_object.name, snap);
                        }
                    }
                }
                if (Input.GetMouseButton(1))
                {
                    if (Grid.ObjectFromWorld(mousePositon) != null)
                    {
                        Vector2Int vecint = Grid.VectorPositionFromWorld(mousePositon);
                        if (selected_object != null)
                        {
                            Grid.DespawnObject(snap);
                        }
                    }
                }
            }
        }

        if (Grid.nodes != null)
        {
            for (int x = 0; x < Grid.gridworldsize.x; x++)
            {
                for (int y = 0; y < Grid.gridworldsize.y; y++)
                {
                    /*
                     * SpriteRenderer colourRender = Grid.nodegameobjects[x, y].GetComponent<SpriteRenderer>();
                     * colourRender.color = Color.white;
                     * if (Grid.CheckForNodeOnGrid(mousePositon, x, y))
                     * {
                     *  colourRender.color = Color.blue;
                     * }
                     */
                }
            }
        }
    }