示例#1
0
文件: Squad.cs 项目: laiqiqi/Unity-8
    public void AddUnits(UnitType type, Vector3 location, int count, Allegiance allegiance)
    {
        if (count == 0)
        {
            return;
        }

        if (mSquadMembers == null)
        {
            mSquadMembers = new List <Unit>();
        }

        mAllegiance = allegiance;

        this.transform.position = location;

        GameObject unitPrefab = TextureResource.GetUnitPrefab(type, (allegiance == Allegiance.Rodelle));

        List <Vector3> randomPositions = this.RandomSectionLocations(count, kSquadMemberWidth * 1.5f);

        for (int i = 0; i < count; ++i)
        {
            // instantiate the unit from the prefab
            GameObject o = (GameObject)Instantiate(unitPrefab);
            Unit       u = (Unit)o.GetComponent <Unit>();
            u.Squad = this;
            mSquadMembers.Add(u);
            u.Allegiance = mAllegiance;

            // offset from squad center
            Vector3 memberPosition = this.transform.position;
            memberPosition      += randomPositions [i];
            u.transform.position = memberPosition;
        }

        float squadSpeed = UnitStats.GetStat(SquadLeader.UnitType, UnitStat.MovementSpeed);

        // Sets the speed of the squad to the speed of the squad leader
        for (int i = 0; i < mSquadMembers.Count; ++i)
        {
            // TODO refactor this process. Remove the public access to MoveSpeed and implement a better method of
            // modifying a unit's base stats
            mSquadMembers[i].MoveSpeed = squadSpeed;
        }

        if (SquadLeader.Allegiance == Allegiance.AI) // Reduce the sight range of armed enemy peasants
        {
            SquadLeader.SightRange = UnitStats.GetStat(UnitType.Peasant, UnitStat.SightRange);
        }

        float sightRadius = SquadLeader.SightRange;

        this.GetComponent <CircleCollider2D> ().radius            = 3;
        this.GetComponent <SpriteRenderer>().transform.localScale = new Vector3(sightRadius / 3, sightRadius / 3, 0f);

        this.SetDestination(this.RallyPoint);
        this.ShowSelector(false);
    }
示例#2
0
文件: Squad.cs 项目: laiqiqi/Unity-8
    public void Spawn(UnitType type, Vector3 location, int count, Allegiance allegiance)
    {
        mAllegiance = allegiance;

        this.transform.position = location;

        mUnitPrefab = TextureResource.GetUnitPrefab(type, (allegiance == Allegiance.Rodelle));

        if (mSquadMembers != null)
        {
            foreach (Unit u in mSquadMembers)
            {
                Destroy(u.gameObject);
            }
        }

        mSquadMembers = new List <Unit> ();

        List <Vector3> randomPositions = this.RandomSectionLocations(count, kSquadMemberWidth * 1.5f);

        // Instantiates and initializes the position of each member in the squad
        // TODO fix the placement for large numbers of squad members
        for (int i = 0; i < count; ++i)
        {
            // instantiate the unit from the prefab
            GameObject o = (GameObject)Instantiate(mUnitPrefab);
            Unit       u = (Unit)o.GetComponent(typeof(Unit));
            u.Squad = this;
            mSquadMembers.Add(u);

            // offset from squad center
            Vector3 memberPosition = this.transform.position;
            memberPosition      += randomPositions [i];
            u.transform.position = memberPosition;
            u.Allegiance         = mAllegiance;
        }

        if (SquadLeader.Allegiance == Allegiance.AI) // Reduce the sight range of armed enemy peasants
        {
            SquadLeader.SightRange = UnitStats.GetStat(UnitType.Peasant, UnitStat.SightRange);
        }

        float sightRadius = SquadLeader.SightRange;

        InitializeSightCircleSprite();
        this.GetComponent <CircleCollider2D> ().radius            = 3;
        this.GetComponent <SpriteRenderer>().transform.localScale = new Vector3(sightRadius / 3, sightRadius / 3, 0f);

        this.SetDestination(this.RallyPoint);
        this.ShowSelector(false);
    }