void RemoveBuffs(ClashSpecies cs, string tag)
    {
        var team = GameObject.FindGameObjectsWithTag (tag);

        foreach (var teammate in team) {
            //teammate != this.gameObject so it doesn't get a buff from itself
            if(teammate != this.gameObject) {
                var teammateAttribute = teammate.GetComponent<ClashBattleUnit>();

                switch (cs.name) {
                case "Big Tree":	//hp buff
                    teammateAttribute.currentHealth -= 100;
                    break;
                case "Baobab":	//damage buff
                    teammateAttribute.damage -= 8;
                    break;
                case "Trees and Shrubs":	//attack speed buff
                    if(teammateAttribute.agent != null)
                        teammateAttribute.agent.speed -= 5.0f;
                    break;
                default:
                    break;
                }
            }
        }
    }
    void RemoveBuffs(ClashSpecies cs, string tag)
    {
        var team = GameObject.FindGameObjectsWithTag(tag);

        foreach (var teammate in team)
        {
            //teammate != this.gameObject so it doesn't get a buff from itself
            if (teammate != this.gameObject)
            {
                var teammateAttribute = teammate.GetComponent <ClashBattleUnit>();

                switch (cs.name)
                {
                case "Big Tree":                        //hp buff
                    teammateAttribute.currentHealth -= 100;
                    break;

                case "Baobab":                  //damage buff
                    teammateAttribute.damage -= 8;
                    break;

                case "Trees and Shrubs":                        //attack speed buff
                    if (teammateAttribute.agent != null)
                    {
                        teammateAttribute.agent.speed -= 5.0f;
                    }
                    break;

                default:
                    break;
                }
            }
        }
    }
Esempio n. 3
0
    void Update()
    {
        if (selected == null)
        {
            return;
        }

        if (Input.GetButtonDown("Fire1") && !EventSystem.current.IsPointerOverGameObject())
        {
            RaycastHit hit;
            Ray        ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            if (Physics.Raycast(ray, out hit, 100000, LayerMask.GetMask("Terrain")))
            {
                NavMeshHit placement;
                if (NavMesh.SamplePosition(hit.point, out placement, 1000, 1))
                {
                    var allyResource = Resources.Load <GameObject>("Prefabs/ClashOfSpecies/Units/" + selected.name);
                    var allyObject   = Instantiate(allyResource, placement.position, Quaternion.identity) as GameObject;
                    allyObject.tag = "Ally";

                    Vector2 normPos = new Vector2(placement.position.x - terrain.transform.position.x,
                                                  placement.position.z - terrain.transform.position.z);
                    normPos.x = normPos.x / terrain.terrainData.size.x;
                    normPos.y = normPos.y / terrain.terrainData.size.z;

                    manager.pendingDefenseConfig.layout[selected].Add(normPos);
                    remaining[selected.id]--;

                    var toggle = toggleGroup.ActiveToggles().FirstOrDefault();
                    toggle.transform.parent.GetComponent <ClashUnitListItem>().amountLabel.text = remaining[selected.id].ToString();

                    if (remaining[selected.id] == 0)
                    {
                        toggle.enabled      = false;
                        toggle.interactable = false;
                        selected            = null;
                    }
                }
            }
        }
    }
Esempio n. 4
0
    // Use this for initialization
    void Start()
    {
        var terrainObject = Resources.Load <GameObject>("Prefabs/ClashOfSpecies/Terrains/" + manager.pendingDefenseConfig.terrain);

        terrain = (Instantiate(terrainObject, Vector3.zero, Quaternion.identity) as GameObject).GetComponent <Terrain>();
        terrain.transform.position   = Vector3.zero;
        terrain.transform.localScale = Vector3.one;

        Camera.main.GetComponent <ClashBattleCamera>().target = terrain;

        foreach (var species in manager.pendingDefenseConfig.layout.Keys)
        {
            var currentSpecies = species;
            var item           = Instantiate(defenseItemPrefab) as GameObject;
            remaining.Add(currentSpecies.id, 5);

            var itemReference = item.GetComponent <ClashUnitListItem>();

            var texture = Resources.Load <Texture2D>("Images/" + currentSpecies.name);
            itemReference.toggle.GetComponent <Image>().sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f));
            itemReference.toggle.onValueChanged.AddListener((val) => {
                if (val)
                {
                    selected = currentSpecies;
                    itemReference.toggle.GetComponent <Image>().color = new Color(1.0f, 1.0f, 1.0f, 0.5f);
                }
                else
                {
                    selected = null;
                    itemReference.toggle.GetComponent <Image>().color = new Color(1.0f, 1.0f, 1.0f, 1.0f);
                }
            });

            itemReference.toggle.group = toggleGroup;
            item.transform.SetParent(unitList.transform);
            item.transform.position        = new Vector3(item.transform.position.x, item.transform.position.y, 0.0f);
            item.transform.localScale      = Vector3.one;
            itemReference.amountLabel.text = remaining[currentSpecies.id].ToString();
        }
    }
    /// <summary>
    /// Fill the Response object with data from the server
    /// </summary>
    /// <param name="dataStream">The input stream</param>
    public static NetworkResponse Parse(MemoryStream dataStream)
    {
        ResponseClashSpeciesList response = new ResponseClashSpeciesList();

        int count = DataReader.ReadInt(dataStream);
        //		Debug.Log("received " + count + " species");
        for(int i = 0; i < count; i++) {
            ClashSpecies s = new ClashSpecies();
            s.id = DataReader.ReadInt(dataStream);
            s.name = DataReader.ReadString(dataStream);
            s.cost = DataReader.ReadInt(dataStream);
            s.type = (SpeciesType)DataReader.ReadInt(dataStream);
            s.description = DataReader.ReadString(dataStream);
            s.attack = DataReader.ReadInt(dataStream);
            s.hp = DataReader.ReadInt(dataStream);
            s.moveSpeed = DataReader.ReadInt(dataStream);
            s.attackSpeed = DataReader.ReadInt(dataStream);

            response.speciesList.Add(s);
        }
        return response;
    }
    //status: true if plant spawn; false if it died
    public void UpdateBuffPanel(ClashSpecies unit, bool status)
    {
        int val = 0;

        switch (unit.name)
        {
        case "Big Tree":                //hp buff
            if (Int32.TryParse(hpBuffValue.text, out val))
            {
                val = (status) ? val + 100 : val - 100;
            }
            hpBuffValue.text = val.ToString();
            break;

        case "Baobab":          //damage buff
            if (Int32.TryParse(dmgBuffValue.text, out val))
            {
                val = (status) ? val + 8 : val - 8;
            }
            dmgBuffValue.text = val.ToString();
            break;

        case "Trees and Shrubs":                //movement speed buff
            if (Int32.TryParse(spdBuffValue.text, out val))
            {
                val = (status) ? val + 5 : val - 5;
            }
            spdBuffValue.text = val.ToString();
            break;

        default:
            spdBuffValue.text = "0";
            hpBuffValue.text  = "0";
            dmgBuffValue.text = "0";
            break;
        }
    }
    /// <summary>
    /// Fill the Response object with data from the server
    /// </summary>
    /// <param name="dataStream">The input stream</param>
    public static NetworkResponse Parse(MemoryStream dataStream)
    {
        ResponseClashSpeciesList response = new ResponseClashSpeciesList();

        int count = DataReader.ReadInt(dataStream);

//		Debug.Log("received " + count + " species");
        for (int i = 0; i < count; i++)
        {
            ClashSpecies s = new ClashSpecies();
            s.id          = DataReader.ReadInt(dataStream);
            s.name        = DataReader.ReadString(dataStream);
            s.cost        = DataReader.ReadInt(dataStream);
            s.type        = (SpeciesType)DataReader.ReadInt(dataStream);
            s.description = DataReader.ReadString(dataStream);
            s.attack      = DataReader.ReadInt(dataStream);
            s.hp          = DataReader.ReadInt(dataStream);
            s.moveSpeed   = DataReader.ReadInt(dataStream);
            s.attackSpeed = DataReader.ReadInt(dataStream);

            response.speciesList.Add(s);
        }
        return(response);
    }
    void Start()
    {
        var terrainResource = Resources.Load("Prefabs/ClashOfSpecies/Terrains/" + manager.currentTarget.terrain);
        var terrainObject   = Instantiate(terrainResource, Vector3.zero, Quaternion.identity) as GameObject;

        var terrain = terrainObject.GetComponentInChildren <Terrain>();

        Camera.main.GetComponent <ClashBattleCamera>().target = terrain;

        foreach (var pair in manager.currentTarget.layout)
        {
            var species = pair.Key;

            // Place navmesh agent.
            List <Vector2> positions = pair.Value;
            foreach (var pos in positions)
            {
                var        speciesPos = new Vector3(pos.x * terrain.terrainData.size.x, 0.0f, pos.y * terrain.terrainData.size.z);
                NavMeshHit placement;
                if (NavMesh.SamplePosition(speciesPos, out placement, 1000, 1))
                {
                    var speciesResource = Resources.Load <GameObject>("Prefabs/ClashOfSpecies/Units/" + species.name);
                    var speciesObject   = Instantiate(speciesResource, placement.position, Quaternion.identity) as GameObject;
                    speciesObject.tag = "Enemy";

                    var unit = speciesObject.AddComponent <ClashBattleUnit>();
                    enemiesList.Add(unit);
                    unit.species = species;

                    var trigger = speciesObject.AddComponent <SphereCollider>();
                    trigger.radius = 2.0f;

                    var bar = Instantiate(healthBar, unit.transform.position, Quaternion.identity) as GameObject;
                    bar.transform.SetParent(unit.transform);
                    bar.transform.localPosition = new Vector3(0.0f, 8.0f, 0.0f);
                    bar.SetActive(false);

                    GetBuffs(unit, speciesObject.tag);
                    if (species.type == UnitType.PLANT)
                    {
                        GiveBuffs(unit, speciesObject.tag);
                    }
                }
                else
                {
                    Debug.LogWarning("Failed to place unit: " + species.name);
                }
            }
        }

        // Populate user's selected unit panel.
        foreach (var species in manager.attackConfig.layout)
        {
            var currentSpecies = species;
            var item           = Instantiate(attackItemPrefab) as GameObject;
            remaining.Add(currentSpecies.id, 5);

            var itemReference = item.GetComponent <ClashUnitListItem>();

            var texture = Resources.Load <Texture2D>("Images/" + species.name);
            itemReference.toggle.GetComponent <Image>().sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f));
            itemReference.toggle.onValueChanged.AddListener((val) => {
                if (val)
                {
                    selected = currentSpecies;
                    itemReference.toggle.GetComponent <Image>().color = new Color(1.0f, 1.0f, 1.0f, 0.5f);
                }
                else
                {
                    selected = null;
                    itemReference.toggle.GetComponent <Image>().color = new Color(1.0f, 1.0f, 1.0f, 1.0f);
                }
            });

            itemReference.toggle.group = toggleGroup;
            item.transform.SetParent(unitList.transform);
            item.transform.position        = new Vector3(item.transform.position.x, item.transform.position.y, 0.0f);
            item.transform.localScale      = Vector3.one;
            itemReference.amountLabel.text = remaining[currentSpecies.id].ToString();
        }

        // Send initiate battle request to the server
        List <int> speciesIds = new List <int>();

        foreach (var species in manager.attackConfig.layout)
        {
            speciesIds.Add(species.id);
        }
        var request = ClashInitiateBattleProtocol.Prepare(manager.currentTarget.owner.GetID(), speciesIds);

        NetworkManager.Send(request, (res) => {
            var response = res as ResponseClashInitiateBattle;
            Debug.Log("Received ResponseClashInitiateBattle from server. valid = " + response.valid);
        });
    }
    void Update()
    {
        // Cheat!

        /*
         * if (Input.GetKeyDown(KeyCode.Equals)) {
         *      Debug.Log ("plus");
         *      ReportBattleOutcome(ClashEndBattleProtocol.BattleResult.WIN);
         * } else if (Input.GetKeyDown(KeyCode.Minus)) {
         *      Debug.Log ("minus");
         *      ReportBattleOutcome(ClashEndBattleProtocol.BattleResult.LOSS);
         * }
         */

        if (selected == null)
        {
            return;
        }


        if (Input.GetButtonDown("Fire1") && !EventSystem.current.IsPointerOverGameObject())
        {
            RaycastHit hit;
            Ray        ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            if (Physics.Raycast(ray, out hit, 100000, LayerMask.GetMask("Terrain")))
            {
                NavMeshHit placement;
                if (NavMesh.SamplePosition(hit.point, out placement, 1000, 1))
                {
                    //Added by Omar
                    var allyResource = Resources.Load <GameObject>("Prefabs/ClashOfSpecies/Units/" + selected.name);
                    var allyObject   = Instantiate(allyResource, placement.position, Quaternion.identity) as GameObject;
                    allyObject.tag = "Ally";

                    var unit = allyObject.AddComponent <ClashBattleUnit>();
                    alliesList.Add(unit);
                    unit.species = selected;

                    var trigger = allyObject.AddComponent <SphereCollider>();
                    trigger.radius = 2.0f;

                    var bar = Instantiate(healthBar, unit.transform.position, Quaternion.identity) as GameObject;
                    bar.transform.SetParent(unit.transform);
                    bar.transform.localPosition = new Vector3(0.0f, 8.0f, 0.0f);
                    bar.SetActive(false);

                    GetBuffs(unit, allyObject.tag);
                    if (unit.species.type == UnitType.PLANT)
                    {
                        GiveBuffs(unit, allyObject.tag);
                        UpdateBuffPanel(unit.species, true);
                    }

                    remaining[selected.id]--;
                    var toggle = toggleGroup.ActiveToggles().FirstOrDefault();
                    toggle.transform.parent.GetComponent <ClashUnitListItem>().amountLabel.text = remaining[selected.id].ToString();

                    if (remaining[selected.id] == 0)
                    {
                        toggle.enabled      = false;
                        toggle.interactable = false;
                        selected            = null;
                    }
                }
            }
        }
    }
    void Update()
    {
        // Cheat!
        /*
        if (Input.GetKeyDown(KeyCode.Equals)) {
            Debug.Log ("plus");
            ReportBattleOutcome(ClashEndBattleProtocol.BattleResult.WIN);
        } else if (Input.GetKeyDown(KeyCode.Minus)) {
            Debug.Log ("minus");
            ReportBattleOutcome(ClashEndBattleProtocol.BattleResult.LOSS);
        }
        */

        if (selected == null) return;

        if (Input.GetButtonDown("Fire1") && !EventSystem.current.IsPointerOverGameObject()) {
            RaycastHit hit;
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            if (Physics.Raycast(ray, out hit, 100000, LayerMask.GetMask("Terrain"))) {
                NavMeshHit placement;
                if (NavMesh.SamplePosition(hit.point, out placement, 1000, 1)) {
                    //Added by Omar
                    var allyResource = Resources.Load<GameObject>("Prefabs/ClashOfSpecies/Units/" + selected.name);
                    var allyObject = Instantiate(allyResource, placement.position, Quaternion.identity) as GameObject;
                    allyObject.tag = "Ally";

                    var unit = allyObject.AddComponent<ClashBattleUnit>();
                    alliesList.Add(unit);
                    unit.species = selected;

                    var trigger = allyObject.AddComponent<SphereCollider>();
                    trigger.radius = 2.0f;

                    var bar = Instantiate(healthBar, unit.transform.position, Quaternion.identity) as GameObject;
                    bar.transform.SetParent(unit.transform);
                    bar.transform.localPosition = new Vector3(0.0f, 8.0f, 0.0f);
                    bar.SetActive(false);

                    GetBuffs(unit, allyObject.tag);
                    if(unit.species.type == UnitType.PLANT) {
                        GiveBuffs(unit, allyObject.tag);
                        UpdateBuffPanel(unit.species, true);
                    }

                    remaining[selected.id]--;
                    var toggle = toggleGroup.ActiveToggles ().FirstOrDefault();
                    toggle.transform.parent.GetComponent<ClashUnitListItem>().amountLabel.text = remaining[selected.id].ToString();

                    if (remaining[selected.id] == 0) {
                        toggle.enabled = false;
                        toggle.interactable = false;
                        selected = null;
                    }
                }
            }
        }
    }
    void Start()
    {
        var terrainResource = Resources.Load("Prefabs/ClashOfSpecies/Terrains/" + manager.currentTarget.terrain);
        var terrainObject = Instantiate(terrainResource, Vector3.zero, Quaternion.identity) as GameObject;

        var terrain = terrainObject.GetComponentInChildren<Terrain>();
        Camera.main.GetComponent<ClashBattleCamera>().target = terrain;

        foreach (var pair in manager.currentTarget.layout) {
            var species = pair.Key;

            // Place navmesh agent.
            List<Vector2> positions = pair.Value;
            foreach (var pos in positions) {
                var speciesPos = new Vector3(pos.x * terrain.terrainData.size.x, 0.0f, pos.y * terrain.terrainData.size.z);
                NavMeshHit placement;
                if (NavMesh.SamplePosition(speciesPos, out placement, 1000, 1)) {
                    var speciesResource = Resources.Load<GameObject>("Prefabs/ClashOfSpecies/Units/" + species.name);
                    var speciesObject = Instantiate(speciesResource, placement.position, Quaternion.identity) as GameObject;
                    speciesObject.tag = "Enemy";

                    var unit = speciesObject.AddComponent<ClashBattleUnit>();
                    enemiesList.Add(unit);
                    unit.species = species;

                    var trigger = speciesObject.AddComponent<SphereCollider>();
                    trigger.radius = 2.0f;

                    var bar = Instantiate(healthBar, unit.transform.position, Quaternion.identity) as GameObject;
                    bar.transform.SetParent(unit.transform);
                    bar.transform.localPosition = new Vector3(0.0f, 8.0f, 0.0f);
                    bar.SetActive(false);

                    GetBuffs(unit, speciesObject.tag);
                    if(species.type == UnitType.PLANT) {
                        GiveBuffs(unit, speciesObject.tag);
                    }
                } else {
                    Debug.LogWarning("Failed to place unit: " + species.name);
                }
            }
        }

        // Populate user's selected unit panel.
        foreach (var species in manager.attackConfig.layout) {
            var currentSpecies = species;
            var item = Instantiate(attackItemPrefab) as GameObject;
            remaining.Add(currentSpecies.id, 5);

            var itemReference = item.GetComponent<ClashUnitListItem>();

            var texture = Resources.Load<Texture2D>("Images/" + species.name);
            itemReference.toggle.GetComponent<Image>().sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f));
            itemReference.toggle.onValueChanged.AddListener((val) => {
                if (val) {
                    selected = currentSpecies;
                    itemReference.toggle.GetComponent<Image>().color = new Color(1.0f, 1.0f, 1.0f, 0.5f);
                } else {
                    selected = null;
                    itemReference.toggle.GetComponent<Image>().color = new Color(1.0f, 1.0f, 1.0f, 1.0f);
                }
            });

            itemReference.toggle.group = toggleGroup;
            item.transform.SetParent(unitList.transform);
            item.transform.position = new Vector3(item.transform.position.x, item.transform.position.y, 0.0f);
            item.transform.localScale = Vector3.one;
            itemReference.amountLabel.text = remaining[currentSpecies.id].ToString();
        }

        // Send initiate battle request to the server
        List<int> speciesIds = new List<int>();
        foreach (var species in manager.attackConfig.layout) {
            speciesIds.Add(species.id);
        }
        var request = ClashInitiateBattleProtocol.Prepare(manager.currentTarget.owner.GetID(), speciesIds);
        NetworkManager.Send(request, (res) => {
            var response = res as ResponseClashInitiateBattle;
            Debug.Log("Received ResponseClashInitiateBattle from server. valid = " + response.valid);
        });
    }
 //status: true if plant spawn; false if it died
 public void UpdateBuffPanel(ClashSpecies unit, bool status)
 {
     int val = 0;
     switch (unit.name) {
     case "Big Tree":	//hp buff
         if(Int32.TryParse(hpBuffValue.text, out val)) {
             val = (status) ? val + 100 : val - 100;
         }
         hpBuffValue.text = val.ToString();
         break;
     case "Baobab":	//damage buff
         if(Int32.TryParse(dmgBuffValue.text, out val)) {
             val = (status) ? val + 8 : val - 8;
         }
         dmgBuffValue.text = val.ToString();
         break;
     case "Trees and Shrubs":	//movement speed buff
         if(Int32.TryParse(spdBuffValue.text, out val)) {
             val = (status) ? val + 5 : val - 5;
         }
         spdBuffValue.text = val.ToString();
         break;
     default:
         spdBuffValue.text = "0";
         hpBuffValue.text = "0";
         dmgBuffValue.text = "0";
         break;
     }
 }
    // Use this for initialization
    void Start()
    {
        var terrainObject = Resources.Load<GameObject>("Prefabs/ClashOfSpecies/Terrains/" + manager.pendingDefenseConfig.terrain);
        terrain = (Instantiate(terrainObject, Vector3.zero, Quaternion.identity) as GameObject).GetComponent<Terrain>();
        terrain.transform.position = Vector3.zero;
        terrain.transform.localScale = Vector3.one;

        Camera.main.GetComponent<ClashBattleCamera>().target = terrain;

        foreach (var species in manager.pendingDefenseConfig.layout.Keys) {
            var currentSpecies = species;
            var item = Instantiate(defenseItemPrefab) as GameObject;
            remaining.Add(currentSpecies.id, 5);

            var itemReference = item.GetComponent<ClashUnitListItem>();

            var texture = Resources.Load<Texture2D>("Images/" + currentSpecies.name);
            itemReference.toggle.GetComponent<Image>().sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f));
            itemReference.toggle.onValueChanged.AddListener((val) => {
                if (val) {
                    selected = currentSpecies;
                    itemReference.toggle.GetComponent<Image>().color = new Color(1.0f, 1.0f, 1.0f, 0.5f);
                } else {
                    selected = null;
                    itemReference.toggle.GetComponent<Image>().color = new Color(1.0f, 1.0f, 1.0f, 1.0f);
                }
            });

            itemReference.toggle.group = toggleGroup;
            item.transform.SetParent(unitList.transform);
            item.transform.position = new Vector3(item.transform.position.x, item.transform.position.y, 0.0f);
            item.transform.localScale = Vector3.one;
            itemReference.amountLabel.text = remaining[currentSpecies.id].ToString();
        }
    }
    void Update()
    {
        if (selected == null) return;

        if (Input.GetButtonDown("Fire1") && !EventSystem.current.IsPointerOverGameObject()) {
            RaycastHit hit;
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            if (Physics.Raycast(ray, out hit, 100000, LayerMask.GetMask("Terrain"))) {
                NavMeshHit placement;
                if (NavMesh.SamplePosition(hit.point, out placement, 1000, 1)) {
                    var allyResource = Resources.Load<GameObject>("Prefabs/ClashOfSpecies/Units/" + selected.name);
                    var allyObject = Instantiate(allyResource, placement.position, Quaternion.identity) as GameObject;
                    allyObject.tag = "Ally";

                    Vector2 normPos = new Vector2(placement.position.x - terrain.transform.position.x,
                                                  placement.position.z - terrain.transform.position.z);
                    normPos.x = normPos.x / terrain.terrainData.size.x;
                    normPos.y = normPos.y / terrain.terrainData.size.z;

                    manager.pendingDefenseConfig.layout[selected].Add(normPos);
                    remaining[selected.id]--;

                    var toggle = toggleGroup.ActiveToggles().FirstOrDefault();
                    toggle.transform.parent.GetComponent<ClashUnitListItem>().amountLabel.text = remaining[selected.id].ToString();

                    if (remaining[selected.id] == 0) {
                        toggle.enabled = false;
                        toggle.interactable = false;
                        selected = null;
                    }
                }
            }
        }
    }