公共物品和团结的整体

时间:2018-04-03 00:03:15

标签: c# object unity3d

好的,先关闭,我对团结和C#知之甚少。我最初是用javascript制作的,但后来由于各种原因将其转换为c#和unity。我制作了一个垄断模拟器,我有一个可以独立播放整个游戏的脚本,但它不能移动棋盘上的任何棋子,只是不断更新玩家的价值&# 39;的位置和金钱以及所有财产的状况。我想要做的是,对于每个玩家,从玩家对象获取位置值,使用以下代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Player
{

internal int money;
internal int position;
internal int jail;

public Player()
{
    money = 0;
    position = 0;
    jail = 0;
}

public virtual void addMon(int newMoney)
{
    money = money + newMoney;
}
public virtual int Pos
{
    set
    {
        position = position + value;
    }
    get
    {
        return position;
    }
}
public virtual int Jail
{
    set
    {
        jail = value;
    }
    get
    {
        return jail;
    }
}

public virtual int Mon
{
    get
    {
        return money;
    }
}
}

并将其位置与(x,y)坐标数组匹配,以将玩家精灵移动到每个位置。

问题在于我不知道如何在不同的脚本中获取位置。我尝试将Player对象公开,但这使得其余代码无法更改值在主要代码中。如何在不同的课程中获得整数?

P.S。 这是玩游戏的代码。它没有100%完成,但它显示了这个想法。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Random = UnityEngine.Random;

public class boardmove:MonoBehaviour
{
// find all the card and property objects?
void Start()
{

    //create/initialize variables
    Random Random = new Random();
    int rand;
    int counter;
    int currentPlayer = 1;
    int move;
    int roll1;
    int roll2;
    int speeding;

    Player Player1 = new Player();
    Player Player2 = new Player();
    Player Player3 = new Player();
    Player Player4 = new Player();

    int startposition;
    int endposition;

    //create all positions as property objects
    Property[] buyList;
    buyList = new Property[40];
    buyList[0] = new Property(0, 0);
    buyList[1] = new Property(60, 2);
    buyList[2] = new Property(0, 0);
    buyList[3] = new Property(60, 4);
    buyList[4] = new Property(0, 0);
    buyList[5] = new Property(200, -1);
    buyList[6] = new Property(100, 6);
    buyList[7] = new Property(0, 0);
    buyList[8] = new Property(100, 6);
    buyList[9] = new Property(120, 8);
    buyList[10] = new Property(0, 0);
    buyList[11] = new Property(140, 10);
    buyList[12] = new Property(150, -2);
    buyList[13] = new Property(140, 10);
    buyList[14] = new Property(160, 12);
    buyList[15] = new Property(200, -1);
    buyList[16] = new Property(180, 14);
    buyList[17] = new Property(0, 0);
    buyList[18] = new Property(180, 14);
    buyList[19] = new Property(200, 16);
    buyList[20] = new Property(0, 0);
    buyList[21] = new Property(220, 18);
    buyList[22] = new Property(0, 0);
    buyList[23] = new Property(220, 18);
    buyList[24] = new Property(240, 20);
    buyList[25] = new Property(200, -1);
    buyList[26] = new Property(260, 22);
    buyList[27] = new Property(260, 22);
    buyList[28] = new Property(150, -2);
    buyList[29] = new Property(280, 24);
    buyList[30] = new Property(0, 0);
    buyList[31] = new Property(300, 26);
    buyList[32] = new Property(300, 26);
    buyList[33] = new Property(0, 0);
    buyList[34] = new Property(350, 35);
    buyList[35] = new Property(200, -1);
    buyList[36] = new Property(0, 0);
    buyList[37] = new Property(350, 35);
    buyList[38] = new Property(0, 0);
    buyList[39] = new Property(400, 50);

    string[] position;
    position = new string[41];
    //initialize all possible board positions
    position[0] = "Go";
    position[1] = "Mediterranean Ave.";
    position[2] = "Community Chest";
    position[3] = "Baltic Ave.";
    position[4] = "Income Tax";
    position[5] = "Reading Railroad";
    position[6] = "Oriental Ave.";
    position[7] = "Chance";
    position[8] = "Vermont Ave.";
    position[9] = "Connecticut Ave.";
    position[10] = "Visiting Jail";
    position[11] = "St. Charles Place";
    position[12] = "Electric Company";
    position[13] = "States Ave.";
    position[14] = "Virginia Ave.";
    position[15] = "Pennsylvania Railroad";
    position[16] = "St. James Place";
    position[17] = "Community Chest";
    position[18] = "Tennessee Ave.";
    position[19] = "New York Ave.";
    position[20] = "Free Parking";
    position[21] = "Kentucky Ave.";
    position[22] = "Chance";
    position[23] = "Indiana Ave.";
    position[24] = "Illinois Ave.";
    position[25] = "B.&.O. Railroad";
    position[26] = "Atlantic Ave.";
    position[27] = "Ventnor Ave.";
    position[28] = "Water Works";
    position[29] = "Marvin Gardens";
    position[30] = "Go To Jail";
    position[31] = "Pacific Ave.";
    position[32] = "North Carolina";
    position[33] = "Community Chest";
    position[34] = "Pennsylvania Ave.";
    position[35] = "Short Line Railroad";
    position[36] = "Chance";
    position[37] = "Park Place";
    position[38] = "Luxury Tax";
    position[39] = "Boardwalk";
    position[40] = "In Jail";

    Card[] chance;
    //initialize all chance cards
    chance = new Card[17];
    chance[0] = new Card(0, "Advance to Go (Collect $200)");
    chance[1] = new Card(24, "Advance to Illinois Ave. - If you pass Go, collect $200");
    chance[2] = new Card(11, "Advance to St. Charles Place - if you pass Go, collect $200");
    chance[3] = new Card(-1, "Advance to the nearest Utility. if unowned, you may buy it from the bank. if owned, throw dice and pay owner ten times the ammount thrown.");
    chance[4] = new Card(-2, "Advance to the nearest Railroad and pay owner twice the rental to which he/she is otherwise entitled. \n If Railroad is unowned, you may buy it from the bank.");
    chance[5] = new Card(-2, "Advance to the nearest Railroad and pay owner twice the rental to which he/she is otherwise entitled. \n If Railroad is unowned, you may buy it from the bank.");
    chance[6] = new Card(50, "Bank pays you dividend of $50 (Collect $50)");
    chance[7] = new Card(10, "Get out of Jail Free - This card may be kept until needed.");
    chance[8] = new Card(3, "Go back 3 Spaces");
    chance[9] = new Card(40, "Go to Jail. - Do not pass Go, do not collect $200");
    chance[10] = new Card(0, "Make general repairs on all your Property - For each house, pay $25 - for each hotel, pay $100.");
    chance[11] = new Card(15, "Pay poor tax of $15");
    chance[12] = new Card(5, "Take a trip to reading Railroad - If you pass Go, collect $200 (Advance to reading Railroad)");
    chance[13] = new Card(39, "Advance to Boardwalk - Take a walk to the Boardwalk");
    chance[14] = new Card(50, "You have been elected Chairman of the Board - Give each Player $50");
    chance[16] = new Card(100, "You have won a crosword competition - Collect $100");



    //initialize community chest cards
    Card[] comChest;
    comChest = new Card[16];
    comChest[0] = new Card(0, "Advance to GO (collect $200)");
    comChest[1] = new Card(200, "Collect $200 (Bank error in your favor)");
    comChest[2] = new Card(50, "Pay $50 (Doctor's fee)");
    comChest[3] = new Card(50, "Collect $50 (From sale of stock)");
    comChest[4] = new Card(0, "Get out of jail free.");
    comChest[5] = new Card(41, "Go to jail (Advance to jail)");
    comChest[6] = new Card(150, "Collect $50 from every Player - Grand Opera Night");
    comChest[7] = new Card(100, "Collect $100 - Holliday fund");
    comChest[8] = new Card(20, "Collect 20$ (Income Tax Refund)");
    comChest[9] = new Card(10, "Collect $10 (It's your birthday)");
    comChest[10] = new Card(100, "Collect $100 (your bonds mature)");
    comChest[11] = new Card(100, "Pay $100 (Hospital Fees)");
    comChest[12] = new Card(150, "Pay $150 in School tax");
    comChest[13] = new Card(25, "Collect $25 consultancy fees.");
    comChest[14] = new Card(0, "You are assessed for street repair - pay $40 per house - pay $115 per hotel");
    comChest[15] = new Card(10, "Collect $10. You won second place in a beuty pagent.");




    /* determine which Player's turn it is 
     * escaping jail
     * move their piece on the board according to their roll 
     * allow time for current Player to trade Property
     * allow Player to buy un-owned Property
     * make Player pay rent on Property if already owned (monopolies, houses, hotels)
     * special spaces and cards(luxury tax, community chest, chance, rail roads, appliances)
     * second turn on doubles
     * speeding rule
     */

    if (currentPlayer == 1)
    {
        //reset speeding rule and die rolls
        speeding = 0;
        roll1 = 1;
        roll2 = 1;
        // before turn begins, ask Player if they want to trade property or buy/sell houses and hotels.

        //if Player is in jail
        if (Player1.Pos == 40)
        {
            roll1 = 0;
            roll2 = 0;
            // ask if they want to pay, use a card, or roll doubles

            //if the Player uses a card or pays, set their position to visiting
            if ()
            {
                Player1.Pos = 10;
            }
            //if the Player chooses to roll, let them roll and have their turn continue if they succeed
            else
            {
                //get and read roll inputs
                if (roll1 == roll2)
                {
                    // Player takes their turn
                    Player1.Pos = 10;
                }
            }
        }

        if (roll1 != 0 && roll2 != 0)
        {
            do
            {
                // get read roll inputs
                move = roll1 + roll2;
                startposition = Player1.Pos;
                endposition = startposition + move;
                speeding++;

                //if the Player reaches the end of the board
                if (endposition >= 40)
                {
                    endposition = endposition - 40;
                }
                //passing go
                if (endposition <= startposition)
                {
                    Player1.addMon(200);
                }
                //set new position
                Player1.Pos = endposition;

                //buying, paying rent, and taxes
                if (buyList[endposition].getown().Equals("none"))
                {
                    if (buyList[endposition].getprice() > 0 && Player1.Mon >= buyList[endposition].getprice())
                    {
                        //ask Player if they want to buy property. 
                        //if yes
                        if ()
                        {
                            Player1.addMon(-buyList[endposition].getprice());
                            buyList[endposition].Own = "Player1";
                        }
                    }
                    if (buyList[endposition].getprice() == 0)
                    {
                        if (position[endposition].Equals("Communtity Chest"))
                        {
                            //check and reset deck
                            counter = 0;
                            for (int i = 0; i < 16; i++)
                            {
                                if (comChest[i].Use == 0)
                                {
                                    counter++;
                                }
                            }
                            if (counter >= 16)
                            {
                                for (int i = 0; i < 16; i++)
                                {
                                    comChest[i].reset();
                                }
                            }

                            //pull community chest card
                            do
                            {
                                rand = Random.Range(0,15);
                            } while (comChest[rand].Use != 1);

                            //card effects
                            if (rand == 0)
                            {
                                Player1.Pos = 0;
                            }
                            if (comChest[rand].Text.Contains("Advance to"))
                            {
                                //pass go rules
                                startposition = Player1.Pos;
                                //set new position and pass go rule
                                Player1.Pos = comChest[rand].Num;

                                if (Player1.Pos <= startposition)
                                {
                                    Player1.addMon(200);
                                }

                            }
                            else if (comChest[rand].Text.Contains("Pay"))
                            {
                                Player1.addMon(-(comChest[rand].Num));
                            }
                            else if (comChest[rand].Text.Contains("Collect"))
                            {
                                Player1.addMon(comChest[rand].Num);
                            }
                            else if (comChest[rand].Text.Contains("Get out of jail"))
                            {
                                // give Player get out of jail card
                            }
                            else if (comChest[rand].Text.Contains("street repairs"))
                            {
                                for (int i = 0; i < 40; i++)
                                {
                                    counter = 0;
                                    if (buyList[i].getown().Equals("Player1"))
                                    {
                                        Player1.addMon((buyList[i].Hotel) * -100);
                                        Player1.addMon((buyList[i].House) * -25);
                                    }
                                }
                            }
                        }
                        if (position[endposition].Equals("Chance"))
                        {
                            //check and reset deck
                            counter = 0;
                            for (int i = 0; i < 17; i++)
                            {
                                if (chance[i].Use == 1)
                                {
                                    counter++;
                                }
                            }
                            if (counter == 17)
                            {
                                for (int i = 0; i < 17; i++)
                                {
                                    chance[i].reset();
                                }
                            }
                            //pull chance card
                            do
                            {
                                rand = Random.Range(0,16);
                            } while (chance[rand].Use != 1);

                            if (chance[rand].Text.Contains("Advance to"))
                            {
                                Player1.Pos = chance[rand].Num;
                            }
                            else if (chance[rand].Text.Contains("Collect"))
                            {
                                Player1.addMon(chance[rand].Num);
                            }
                            else if (chance[rand].Text.Contains("Pay"))
                            {
                                Player1.addMon(-chance[rand].Num);
                            }
                            else if (rand == 8)
                            {
                                endposition = endposition - 3;
                            }
                            else if (rand == 9)
                            {
                                speeding = 3;
                            }
                            else if (rand == 10)
                            {
                                for (int i = 0; i < 40; i++)
                                {
                                    counter = 0;
                                    if (buyList[i].getown().Equals("Player1"))
                                    {
                                        Player1.addMon((buyList[i].Hotel) * -115);
                                        Player1.addMon((buyList[i].House) * -40);
                                    }
                                }
                            }
                            else if (rand == 14)
                            {
                                Player1.addMon(-3 * chance[rand].Num);
                                Player2.addMon(chance[rand].Num);
                                Player3.addMon(chance[rand].Num);
                                Player4.addMon(chance[rand].Num);
                            }
                            //include pass go rules
                        }
                        //paying income tax
                        if (position[endposition].Equals("Income Tax"))
                        {
                            if ((Player1.Mon / 10) > 200)
                            {
                                Player1.addMon(-(Player1.Mon / 10));
                            }
                            else
                            {
                                Player1.addMon(-200);
                            }
                        }
                        //paying luxury tax
                        if (position[endposition].Equals("Luxury Tax"))
                        {
                            Player1.addMon(-75);
                        }
                        //going to jail
                        if (position[endposition].Equals("Go To Jail"))
                        {
                            //speeding rule will send Player to jail
                            speeding = 3;
                        }

                    }
                    else if (!"Player1".Equals(buyList[endposition].getown()))
                    {
                        //add code for paying rent
                    }

                }
            } while (roll1 == roll2 && speeding < 3);
            //Repeat turn if Player rolled doubles and Player not speeding
            if (speeding == 3)
            {
                //send Player to jail
                Player1.Pos = 40;
            }
        }
        currentPlayer = 2;
    }
    else if (currentPlayer == 2)
    {
        //movement code
    }
    else if (currentPlayer == 3)
    {
        //movement code
    }
    else if (currentPlayer == 4)
    {
        //movement code
    }

}
void Update()
{
    //set all Player locations on board to locations in code
    //transfer property cards from card bank to Players
}
}

这是我试图获取player1位置值的代码。它是一项正在进行中的实际工作,但它是我得到的。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Player1Move : MonoBehaviour {

GameObject playerIcon;
// Use this for initialization
void Start () {
    int PlayerPos = (FindObjectOfType<boardmove(Player1Move.getPos())>);
    Vector2[] positions = Vector2[40];
    positions[0] =new Vector2(6,-6);
    positions[1] = new Vector2();
    positions[2] = new Vector2();
    positions[3] = new Vector2();
    positions[4] = new Vector2();
    positions[5] = new Vector2();
    positions[6] = new Vector2();
    positions[7] = new Vector2();
    positions[8] = new Vector2();
    positions[9] = new Vector2();
    positions[10] = new Vector2(-6,-6);
    positions[11] = new Vector2();
    positions[12] = new Vector2();
    positions[13] = new Vector2();
    positions[14] = new Vector2();
    positions[15] = new Vector2();
    positions[16] = new Vector2();
    positions[17] = new Vector2();
    positions[18] = new Vector2();
    positions[19] = new Vector2();
    positions[20] = new Vector2(6,6);
    positions[21] = new Vector2();
    positions[22] = new Vector2();
    positions[23] = new Vector2();
    positions[24] = new Vector2();
    positions[25] = new Vector2();
    positions[26] = new Vector2();
    positions[27] = new Vector2();
    positions[28] = new Vector2();
    positions[29] = new Vector2();
    positions[30] = new Vector2(-6,6);
    positions[31] = new Vector2();
    positions[32] = new Vector2();
    positions[33] = new Vector2();
    positions[34] = new Vector2();
    positions[35] = new Vector2();
    positions[36] = new Vector2();
    positions[37] = new Vector2();
    positions[38] = new Vector2();
    positions[39] = new Vector2();
    positions[40] = new Vector2();
}

// Update is called once per frame
void Update () {

}
}

1 个答案:

答案 0 :(得分:1)

所以经过一些研究后我回答了自己的问题。呃...我现在意识到我问的问题相当糟糕,但我发现我可以通过以下代码从PLayer1Move中的boardmove获取Player1.position值:

 void Update () {
    GameObject board = GameObject.Find("Board");
    boardmove movescript = board.GetComponent<boardmove>();
    int playerpos = movescript.Player1.position;