Beispiel #1
0
 public void UpdateTargetInfo(string targetType)
 {
     if (Target != null)
     {
         if (targetType == "Enemy")
         {
             EnemyBehaviour  = Target.GetComponent <NpcBehaviour> ();
             enemyLevel.text = EnemyBehaviour.Level.ToString();
             enemyName.text  = Target.name;
             // targetPortrait = hit.transform.GetComponent<Image> ().sprite;
         }
         else if (targetType == "Friendly")
         {
             FriendlyBehaviour  = Target.GetComponent <NpcBehaviour> ();
             friendlyLevel.text = FriendlyBehaviour.Level.ToString();
             friendlyName.text  = Target.name;
             //friendlyPortrait = hit.transform.GetComponent<Image> ().sprite;
         }
         else
         {
             friendlyLevel.text = PlayerBehaviour.Level.ToString();
             friendlyName.text  = PlayerBehaviour.name;
         }
     }
 }
Beispiel #2
0
    // Constants

    // Use this for initialization
	void Start ()
	{
    	// Set up the character controller and movement
        m_npcController = GetComponentInParent<CharacterController>();
        m_finalMovement = Vector3.zero;
		behaviour = NpcBehaviour.Linear       ;
	}
Beispiel #3
0
    public void Initialize(NpcBehaviour actor)
    {
        camera = Camera.main;

        healthbar = Instantiate(Resources.Load <GameObject>("Prefabs/UI/enemyHealthbar"), FindObjectOfType <Canvas>().transform);
        hpText    = healthbar.GetComponentInChildren <Text>();
        hpImage   = healthbar.GetComponentInChildren <Image>();

        actor.OnHealthChanged += OnHealthChanged;
    }
Beispiel #4
0
    public Minimax(NpcBehaviour npc, MarbleScript marble, NewTileScript tileToMoveTo, GameManager gameManager, List <NewTileScript> previousTiles, bool jump)
    {
        this.bestNode      = this;
        score              = -Mathf.Infinity;
        this.npc           = npc;
        this.jump          = jump;
        this.marble        = marble;
        this.tileToMoveTo  = tileToMoveTo;
        this.gameManager   = gameManager;
        this.previousTiles = previousTiles;

        gameManager.MoveMarbleScript(marble.gameObject, tileToMoveTo.gameObject);

        previousTiles.Add(tileToMoveTo);
        if (jump && gameManager.playerList[(gameManager.playerList.IndexOf(marble.Player.PlayerColor) + 1) % gameManager.playerList.Count] + "Player" != gameManager.CurrentPlayer)
        {
            gameManager.MarblePicked(marble.gameObject, tileToMoveTo.gameObject, true, this, npc);
        }
        else
        {
            bestNode = this;
        }
        gameManager.StartMinimax(this);
    }
Beispiel #5
0
 public void SpClr(NpcBehaviour npc)
 {
     Destroy(npc);
 }
Beispiel #6
0
 public NpcBehaviour SpOfs(NpcBehaviour sprite, Vector2 pos)
 {
     sprite.transform.position = pos;
     return(sprite);
 }
Beispiel #7
0
 public NpcBehaviour SpChr(NpcBehaviour sprite, string animId)
 {
     sprite.ChangeSprite(animId);
     return(sprite);
 }
Beispiel #8
0
 public NpcBehaviour SpEvent(NpcBehaviour sprite, string label)
 {
     sprite.Label = label;
     return(sprite);
 }
Beispiel #9
0
    public IEnumerator CalculateValue()
    {
        int originalX, originalY, currentX, currentY, bestX, bestY, goalX, goalY;

        originalX = previousTiles[0].x;
        originalY = previousTiles[0].y;
        currentX  = tileToMoveTo.x;
        currentY  = tileToMoveTo.y;
        bestX     = bestNode.tileToMoveTo.x;
        bestY     = bestNode.tileToMoveTo.y;
        goalX     = marble.Player.GoalTile.x;
        goalY     = marble.Player.GoalTile.y;

        int originalXDistance = Mathf.Abs(goalX - originalX);
        int originalYDistance = Mathf.Abs(goalY - originalY);
        int originalDistance  = originalXDistance + originalYDistance;

        int currentXDistance = Mathf.Abs(currentX - originalX);
        int currentYDistance = Mathf.Abs(currentY - originalY);
        int currentDistance  = currentXDistance + currentYDistance;

        int improvement = originalDistance - currentYDistance;

        score = (float)(improvement * 7);

        //En if-sats för att kolla så att den spelare minimax kollar efter nu inte är den nuvarande spelaren
        //(dvs. den kollar på de återstående spelarnas potentiella drag):
        if (!jump && gameManager.playerList[(gameManager.playerList.IndexOf(marble.Player.PlayerColor) + 1) % gameManager.playerList.Count] != gameManager.CurrentPlayer)
        {
            gameManager.playerState = PlayerState.NpcMinimax;
            //Sätter vilken som är nästa spelare:
            GameObject   nextPlayer       = GameObject.FindGameObjectWithTag(gameManager.playerList[(gameManager.playerList.IndexOf(marble.Player.PlayerColor) + 1) % gameManager.playerList.Count] + "Player");
            NpcBehaviour nextPlayerScript = nextPlayer.GetComponent <NpcBehaviour>();

            float bestOppValue = -Mathf.Infinity;
            //För varje pjäs som nästa spelare har ...
            foreach (GameObject opponentMarble in nextPlayerScript.Marbles)
            {
                //Kolla vart den kan gå:
                gameManager.MarblePicked(opponentMarble, opponentMarble.GetComponent <MarbleScript>().myPosition, /* true,*/ false, this, nextPlayerScript);

                //GameManager kommer i sin tur att sätta värden i legalMoves, därför väntar vi tills legalMoves inte är null längre:
                yield return(new WaitUntil(() => legalMoves != null));

                if (legalMoves.Count < 1)
                {
                    legalMoves = null;
                    continue;
                }

                //Därefter kolla varje drag i legalMoves:
                foreach (NewTileScript move in legalMoves)
                {
                    List <NewTileScript> marblePath = new List <NewTileScript>();
                    marblePath.Add(opponentMarble.GetComponent <MarbleScript>().myPosition.GetComponent <NewTileScript>());
                    Minimax newNode = new Minimax(npc, opponentMarble.GetComponent <MarbleScript>(), move, gameManager, marblePath, move.jumpPosition);

                    yield return(new WaitUntil(() => newNode.Done == true));

                    if (newNode.BestNode.Score > bestOppValue)
                    {
                        bestOppValue = newNode.BestNode.Score;
                    }
                }
            }
            this.score -= bestOppValue;
        }
        done = true;
        gameManager.ResetValues(false, null, null);
    }
    // Följande startar hela uträkningen för vart en pjäs kan gå:
    public void MarblePicked(GameObject marble, GameObject position, bool jumpOnly, Minimax node, NpcBehaviour npcPlayerToLookAt)
    {
        currentMarble = marble;
        List <PossibleMove>  firstLegalMoves = new List <PossibleMove>();
        List <NewTileScript> legalMoves      = new List <NewTileScript>();

        marblePickedUp  = true;
        currentPosition = position;
        neighbours      = currentPosition.GetComponent <NewTileScript>().myNeighbours;

        for (int i = 0; i < neighbours.Count; i++)
        {
            if (!neighbours[i].GetComponent <NewTileScript>().taken)
            {
                if (!jumpOnly)
                {
                    if (playerState == PlayerState.Player)
                    {
                        neighbours[i].GetComponent <NewTileScript>().moveHere = true;
                        possibleMoves.Add(neighbours[i]);
                        Behaviour halo = (Behaviour)neighbours[i].GetComponent("Halo");
                        halo.enabled = true;
                    }
                    else if (playerState == PlayerState.Npc || playerState == PlayerState.NpcMinimax)
                    {
                        // Här sätts närliggande positioner som npc kan gå till:
                        legalMoves.Add(neighbours[i].GetComponent <NewTileScript>());
                        firstLegalMoves.Add(new PossibleMove(marble.GetComponent <MarbleScript>(), neighbours[i].GetComponent <NewTileScript>()));
                        possibleMoves.Add(neighbours[i]);

                        if (node != null && i == neighbours.Count - 1 && legalMoves != null && legalMoves.Count > 0)
                        {
                            node.AllMoves(legalMoves);
                        }
                        if (firstLegalMoves != null && firstLegalMoves.Count > 0)
                        {
                            npcPlayerToLookAt.FirstLegalMoves = firstLegalMoves;
                        }
                    }
                }
            }
            // Hoppa:
            else
            {
                string direction = currentPosition.GetComponent <NewTileScript>().directions[i];
                StartCoroutine(CalculateNeighbours(neighbours[i], false, direction, marble, node, legalMoves, jumpOnly, firstLegalMoves, npcPlayerToLookAt));
            }
        }
    }
    public IEnumerator CalculateNeighbours
        (GameObject tile, bool instantiateBoard, string direction, GameObject marble, Minimax node, List <NewTileScript> legalMoves, bool jumpOnly, List <PossibleMove> firstLegalMoves, NpcBehaviour npcPlayerToLookAt)
    {
        if (!GetComponent <InstantiateBoard>().allTilesInstantiated)
        {
            yield return(new WaitUntil(() => GetComponent <InstantiateBoard>().allTilesInstantiated));
        }
        allTiles = GetComponent <InstantiateBoard>().allTiles;
        directions.Clear();
        xValues.Clear();
        yValues.Clear();

        int tileX = tile.GetComponent <NewTileScript>().x;
        int tileY = tile.GetComponent <NewTileScript>().y;

        #region Calculate Neighbours:
        if (tile.GetComponent <NewTileScript>().everyOtherRow)
        {
            //Upp höger:
            xValues.Add(tileX);
            yValues.Add(tileY + 1);
            directions.Add("UpRight");

            //Upp vänster:
            xValues.Add(tileX - 1);
            yValues.Add(tileY + 1);
            directions.Add("UpLeft");

            //Ner höger:
            xValues.Add(tileX);
            yValues.Add(tileY - 1);
            directions.Add("DownRight");

            //Ner vänster:
            xValues.Add(tileX - 1);
            yValues.Add(tileY - 1);
            directions.Add("DownLeft");
        }
        else
        {
            //Upp höger:
            xValues.Add(tileX + 1);
            yValues.Add(tileY + 1);
            directions.Add("UpRight");

            //Upp vänster:
            xValues.Add(tileX);
            yValues.Add(tileY + 1);
            directions.Add("UpLeft");

            //Ner höger:
            xValues.Add(tileX + 1);
            yValues.Add(tileY - 1);
            directions.Add("DownRight");

            //Ner vänster:
            xValues.Add(tileX);
            yValues.Add(tileY - 1);
            directions.Add("DownLeft");
        }

        //Vänster:
        xValues.Add(tileX - 1);
        yValues.Add(tileY);
        directions.Add("Left");

        //Höger:
        xValues.Add(tileX + 1);
        yValues.Add(tileY);
        directions.Add("Right");
        #endregion

        for (int i = 0; i < allTiles.Count; i++)
        {
            for (int q = 0; q < xValues.Count; q++)
            {
                if (xValues[q] == allTiles[i].GetComponent <NewTileScript>().x&&
                    yValues[q] == allTiles[i].GetComponent <NewTileScript>().y)
                {
                    if (instantiateBoard)
                    {
                        tile.GetComponent <NewTileScript>().SetMyNeighbours(allTiles[i], directions[q]);
                    }
                    // Hoppa:
                    else
                    {
                        if (!allTiles[i].GetComponent <NewTileScript>().taken)
                        {
                            if (directions[q] == direction)
                            {
                                allTiles[i].GetComponent <NewTileScript>().jumpPosition = true;

                                if (playerState == PlayerState.Player)
                                {
                                    allTiles[i].GetComponent <NewTileScript>().moveHere = true;
                                    possibleMoves.Add(allTiles[i]);
                                    Behaviour halo = (Behaviour)allTiles[i].GetComponent("Halo");
                                    halo.enabled = true;
                                }
                                else if (playerState == PlayerState.Npc || playerState == PlayerState.NpcMinimax)
                                {
                                    // Här sätts hopp-positioner för npc:
                                    legalMoves.Add(allTiles[i].GetComponent <NewTileScript>());
                                    firstLegalMoves.Add(new PossibleMove(marble.GetComponent <MarbleScript>(), allTiles[i].GetComponent <NewTileScript>()));
                                    possibleMoves.Add(allTiles[i]);
                                }
                            }
                        }
                    }
                }
            }
        }
        if (node != null)
        {
            if (jumpOnly)
            {
                node.Jump(legalMoves);
            }
            else
            {
                node.AllMoves(legalMoves);
            }
        }
        if (firstLegalMoves != null && firstLegalMoves.Count > 0)
        {
            npcPlayerToLookAt.FirstLegalMoves = firstLegalMoves;
        }
    }