示例#1
0
    public override void OnInspectorGUI()
    {
        DrawDefaultInspector();

        WM_Player P = (WM_Player)target;

        // SETBASE
        //Debug.Log("heresy " + FBase?.name);
        int size = CG.Graph.Count;

        string[] options = new string[size];
        if (FBase == null) // if new graph was created then lost previous base link, so give a new base
        {
            FBase      = CG.Graph[0];
            FBaseIndex = 0;
        }
        for (int i = 0; i < size; i++)
        {
            if (FBase.name == CG.Graph[i].name)
            {
                FBaseIndex = i;                                 // so that the popup shows the current base
            }
            options[i] = CG.Graph[i].name;
        }
        FBaseIndex = EditorGUILayout.Popup("Select Initial Base", FBaseIndex, options);
        FBase      = CG.Graph[FBaseIndex];
        if (GUILayout.Button("Set Base"))
        {
            P.SetInitialNode(FBase); // force the base initialisation if new graph
        }
    }
示例#2
0
    public IEnumerator MovePartyTo()
    {
        // select a random node from current node's neighbour
        currentNode.partyList.Remove(this);
        WM_Node        chosenNode;
        List <WM_Node> possibleNodes = new List <WM_Node>();

        for (int i = 0; i < currentNode.neighbours.Count; i++)
        {
            if (currentNode.neighbours[i].faction == faction)
            {
                possibleNodes.Add(currentNode.neighbours[i]);
            }
        }

        if (possibleNodes.Count > 0)
        {
            chosenNode = possibleNodes[Random.Range(0, possibleNodes.Count)];
        }
        else
        {
            chosenNode = currentNode.neighbours[Random.Range(0, currentNode.neighbours.Count)];
        }
        yield return(SetDestination(chosenNode));

        currentNode = chosenNode;
        currentNode.partyList.Add(this);
        UpdatePosOnNode(currentNode.PartyPositionOnNode(this));
        yield return(null);
    }
示例#3
0
    public IEnumerator Move(WM_Node dest)
    {
        isMoving = true;
        // movement animation
        Vector3 dir = dest.transform.position - transform.position;

        //Debug.Log("Starting to move from " + currentNode.gameObject.name + " " + currentNode.transform.position + " to " + dest.gameObject.name + " " + dest.transform.position);

        while (transform.position != dest.transform.position)
        {
            //Debug.Log("Current pos : " + transform.position);
            transform.Translate(dir.normalized * movSpeed * Time.deltaTime, Space.World);
            yield return(new WaitForFixedUpdate());

            dir = dest.transform.position - transform.position;
            if (Vector3.Distance(transform.position, dest.transform.position) < 0.1)
            {
                break;
            }
        }

        //transform.position = dest.transform.position;
        currentNode = dest;
        isMoving    = false;
        futureNode  = null;
        yield return(null);
    }
示例#4
0
 IEnumerator SetDestination(WM_Node dest)
 {
     foreach (WM_Unit unit in party)
     {
         //Debug.Log("Starting to move in party function");
         yield return(StartCoroutine(unit.Move(dest)));
     }
 }
示例#5
0
 /// <summary>
 /// funtion to move on the worldmap
 /// </summary>
 void ClickToMove()
 {
     if (futureNode != null && !GameManager.inCombat && !isMoving && currentNode.neighbours.Contains(futureNode))
     {
         Debug.Log("in WM_Player : move to node " + futureNode.name);
         StartCoroutine(Move(futureNode));
     }
     futureNode = null;
 }
示例#6
0
 public void Execute()
 {
     nodeToDefend = faction.nodeToDefend;
     if (faction.roamingUnits.Count == 0 || nodeToDefend == null)
     {
         return;
     }
     for (int i = faction.roamingUnits.Count - 1; i >= 0; i--)
     {
         faction.roamingUnits[i].Defend(nodeToDefend);
     }
 }
示例#7
0
    public void SetBase(WM_Node n)
    {
        if (Base != null)
        {
            Debug.Log("changing from " + Base.name + " to " + n.name);
        }
        //else Debug.Log("setting Base to " + n.name);
        Base = n;
        transform.position = n.transform.position;


        if (nearVicinity?.Count >= 0)
        {
            Debug.Log(nearVicinity.Count);
            for (int i = 0; i < nearVicinity.Count; i++)
            {
                Debug.Log(nearVicinity[i].name);
                nearVicinity[i].Liberate();
            }
        }

        // get all nodes in a certain node radius
        nearVicinity = new List <WM_Node>();
        nearVicinity.Add(Base);
        for (int i = 0; i < initialNodeRadius; i++)
        {
            int count = nearVicinity.Count;
            for (int j = 0; j < count; j++)
            {
                for (int k = 0; k < nearVicinity[j].neighbours.Count; k++)
                {
                    if (!nearVicinity.Contains(nearVicinity[j].neighbours[k]))
                    {
                        nearVicinity.Add(nearVicinity[j].neighbours[k]);
                    }
                }
            }
        }

        for (int i = 0; i < nearVicinity.Count; i++)
        {
            nearVicinity[i].ConquerNode(this);
        }

        /*
         * Debug.Log("Nodes in the nearVicinity : ");
         * foreach (Node item in nearVicinity)
         * {
         *  Debug.Log(item.name);
         * }
         */
    }
示例#8
0
    public IEnumerator MovePartyTo(WM_Node endNode)
    {
        currentNode.partyList.Remove(this);

        //Debug.Log("MovePartyTo " + path[i].gameObject.name);
        yield return(SetDestination(endNode));

        currentNode = endNode;

        currentNode.partyList.Add(this);
        UpdatePosOnNode(currentNode.PartyPositionOnNode(this));
        yield return(null);
    }
示例#9
0
 public void Execute()
 {
     nodeToConquer = SelectNode();
     if (nodeToConquer == null)
     {
         return;
     }
     party = SelectParty();
     if (party == null)
     {
         return;
     }
     party.Conquer(nodeToConquer);
 }
示例#10
0
    public static List <WM_Node> PathToNode(WM_Node current, WM_Node endNode)
    {
        //Debug.Log("Starting in pathToNode");
        List <WM_Node>  path     = new List <WM_Node>();
        List <WM_Node>  marked   = new List <WM_Node>();
        List <WM_Node>  visiting = new List <WM_Node>();  // nodes to visit
        List <pathfind> pathprev = new List <pathfind>(); // contains the historic of the previous nodes

        pathprev.Add(new pathfind(current, null));
        visiting.Add(current);
        while (visiting.Count != 0)
        {
            for (int i = 0; i < visiting[0].neighbours.Count; i++)
            {
                if (!marked.Contains(visiting[0].neighbours[i]))
                {
                    visiting.Add(visiting[0].neighbours[i]);
                    pathprev.Add(new pathfind(visiting[0].neighbours[i], visiting[0]));
                }
                if (visiting[0].neighbours[i] == endNode)
                {
                    //Debug.Log("Found endnode : " + visiting[0].neighbours[i].name);
                    break;
                }
            }
            if (visiting[visiting.Count - 1] == endNode)
            {
                break;
            }
            marked.Add(visiting[0]);
            visiting.RemoveAt(0);
        }

        path.Add(pathprev[pathprev.Count - 1].node);
        while (path[path.Count - 1] != current)
        {
            for (int i = 0; i < pathprev.Count; i++)
            {
                if (path[path.Count - 1] == pathprev[i].node)
                {
                    path.Add(pathprev[i].prev);
                }
            }
        }
        path.Reverse();
        path.RemoveAt(0);
        return(path);
    }
示例#11
0
    public void Defend(WM_Node nodeToDef)
    {
        if (!available)
        {
            return;
        }
        float distToBase = Vector3.Distance(faction.GetBase().transform.position, nodeToDef.transform.position);
        float dist       = Vector3.Distance(transform.position, nodeToDef.transform.position);
        float alpha      = dist / distToBase;

        if (Random.Range(0, 1) >= alpha)
        {
            Debug.Log(this.name + " defending " + nodeToDef.name + " chance : " + alpha * 100 + "%.");
            nodeToTravel = nodeToDef;
        }
    }
示例#12
0
    List <WM_Node> ConvexNodes(WM_Node givenNode)
    {
        List <WM_Node> marked      = new List <WM_Node>();
        List <WM_Node> convexNodes = new List <WM_Node>();

        convexNodes.Add(givenNode);
        while (convexNodes.Count != 0)
        {
            foreach (WM_Node node in convexNodes[0].neighbours)
            {
                if (marked.Contains(node))
                {
                    continue;
                }
                convexNodes.Add(node);
            }
            marked.Add(convexNodes[0]);
            convexNodes.RemoveAt(0);
        }
        return(marked);
    }
示例#13
0
    public IEnumerator TravelTo(WM_Node endNode)
    {
        List <WM_Node> path = new List <WM_Node>();

        path = CreateGraph.PathToNode(currentNode, endNode);
        //Debug.Log(this.name  + " path count : " + path.Count);

        for (int i = 0; i < path.Count; i++)
        {
            //Debug.Log(this.name + " going through " + path[i].name);
        }

        for (int i = 0; i < path.Count; i++)
        {
            yield return(StartCoroutine(MovePartyTo(path[i])));
        }
        //Debug.Log("arrived to destination");
        yield return(new WaitForEndOfFrame());

        yield return(null);
    }
示例#14
0
    List <WM_Node> ClosestNodes(int maxClosest, WM_Node current)
    {
        List <WM_Node> allNodes = new List <WM_Node>();

        for (int i = 0; i < graphSize; i++)
        {
            if (Graph[i] == current)
            {
                continue;
            }
            allNodes.Add(Graph[i]);
        }

        List <WM_Node> closest = new List <WM_Node>();

        for (int i = 0; i < allNodes.Count; i++)
        {
            float dist = Vector3.Distance(allNodes[i].transform.position, current.transform.position);
            if (closest.Count == 0)
            {
                closest.Add(allNodes[i]);
                continue;
            }

            for (int j = 0; j < closest.Count; j++)
            {
                if (dist < Vector3.Distance(closest[j].transform.position, current.transform.position))
                {
                    closest.Insert(j, allNodes[i]);
                    break;
                }
            }

            if (closest.Count > maxClosest)
            {
                closest.RemoveRange(maxClosest, closest.Count - maxClosest);
            }
        }
        return(closest);
    }
示例#15
0
    void ConnectConvex(List <WM_Node> convexA, List <WM_Node> convexB)
    {
        float   minDist = Mathf.Infinity;
        WM_Node connectingNodeConvexA = null, connectingNodeConvexB = null;

        foreach (WM_Node nodeA in convexA)
        {
            foreach (WM_Node nodeB in convexB)
            {
                float tmp = Vector3.Distance(nodeA.transform.position, nodeB.transform.position);
                if (tmp < minDist)
                {
                    minDist = tmp;
                    connectingNodeConvexA = nodeA;
                    connectingNodeConvexB = nodeB;
                }
            }
        }
        connectingNodeConvexA.neighbours.Add(connectingNodeConvexB);
        connectingNodeConvexB.neighbours.Add(connectingNodeConvexA);
        //Debug.Log(connectingNodeConvexA.name + " " + connectingNodeConvexB.name);
    }
示例#16
0
    public override void OnInspectorGUI()
    {
        DrawDefaultInspector();

        Faction F = (Faction)target;

        // SETBASE
        CreateGraph CG   = FindObjectOfType <CreateGraph>();
        int         size = CG.Graph.Count;

        string[] options = new string[size];
        if (FBase == null) // if new graph was created then lost previous base link, so give a new base
        {
            FBase      = CG.Graph[0];
            FBaseIndex = 0;
        }
        for (int i = 0; i < size; i++)
        {
            if (FBase.name == CG.Graph[i].name)
            {
                FBaseIndex = i;                                 // so that the popup shows the current base
            }
            options[i] = CG.Graph[i].name;
        }
        FBaseIndex = EditorGUILayout.Popup("Select Faction Base", FBaseIndex, options);
        FBase      = CG.Graph[FBaseIndex];
        if (GUILayout.Button("Set Base"))
        {
            F.SetBase(FBase); // force the base initialisation if new graph
        }
        // END SETBASE


        numUnits = EditorGUILayout.IntField("Number of units in sqad", numUnits);
        EditorGUILayout.LabelField("Select function for the new unit");
        GUILayout.BeginHorizontal();
        if (GUILayout.Button("Roam") && numUnits >= 1)
        {
            F.SpawnNewUnits(numUnits, F.roamingUnits);
        }
        GUILayout.EndHorizontal();

        // adding enemies
        Faction[] factions     = FindObjectsOfType <Faction>();
        string[]  factionsName = new string[factions.Length];
        int       fnIndex      = -1;

        for (int i = 0; i < factions.Length; i++)
        {
            if (factions[i] == F)
            {
                continue;
            }
            factionsName[i] = factions[i].name;
        }
        fnIndex = EditorGUILayout.Popup("Select Enemy", fnIndex, factionsName);
        if (fnIndex != -1)
        {
            F.enemy.Add(factions[fnIndex]);
        }
    }
示例#17
0
 public void NodeAttacked(WM_Node nodeAttacked)
 {
     nodeToDefend = nodeAttacked;
 }
示例#18
0
 public pathfind(WM_Node node, WM_Node prev)
 {
     this.node = node;
     this.prev = prev;
 }
示例#19
0
 public void Exit()
 {
     faction.nodeToDefend = null;
     nodeToDefend         = null;
 }
示例#20
0
 public static void SetFutureNode(WM_Node n)
 {
     futureNode = n;
 }
示例#21
0
 public void SetInitialNode(WM_Node startNode)
 {
     gameObject.transform.position = startNode.transform.position;
     currentNode = startNode;
 }
示例#22
0
 public void Conquer(WM_Node nodeToConquer)
 {
     this.nodeToConquer = nodeToConquer;
 }