Exemple #1
0
    //button is clicked
    void ClickItem()
    {
        if (Input.GetMouseButtonDown (0)) {
            Vector3 v3 = Camera.main.ScreenToWorldPoint (Input.mousePosition);
            Vector2 v2 = new Vector2 (v3.x, v3.y);
            RaycastHit2D hit = Physics2D.Raycast (v2, Vector2.zero);
            if (hit.collider != null) {
                //Debug.Log(hit.collider.tag);
                if (hit.collider.gameObject == this.gameObject) {
                    //Debug.Log(m_ID);
                    if(afford){
                        RecordController.Operation op=new RecordController.Operation();
                        op.posX=createPos.x;
                        op.posY=createPos.y;
                        op.time=RecordController.Instance.timer;
                        //create tower
                        CreateTower();
                        if(Settings.Instance.sound){
                            AudioSource.PlayClipAtPoint(placing,Camera.main.transform.position,1.0f);
                        }

                        switch(tower.GetComponent<DefenderController>().m_towerType){
                        case DefenderController.TowerType.LightTower:
                            op.type=RecordController.OperationType.LightTower;
                            break;
                        case DefenderController.TowerType.HeavyTower:
                            op.type=RecordController.OperationType.HeavyTower;
                            break;
                        case DefenderController.TowerType.PoisonTower:
                            op.type=RecordController.OperationType.PoisonTower;
                            break;
                        case DefenderController.TowerType.IceTower:
                            op.type=RecordController.OperationType.IceTower;
                            break;
                        case DefenderController.TowerType.ElecTower:
                            op.type=RecordController.OperationType.ElecTower;
                            break;
                        }
                        RecordController.Instance.ops.Add(op);
                    }
                    Destroy(this.transform.parent.gameObject);
                }else{
                    if(hit.collider.tag.CompareTo("twButton")==0){
                        //do nothing
                        //Debug.Log("Do nothing");
                    }else{
                        Destroy(this.transform.parent.gameObject);
                    }
                }

            } else {
                    Debug.LogError ("Click nothing");
            }
        }
    }
Exemple #2
0
    void detectCreateTower()
    {
        //If game have already over
        if (m_life <= 0) {
            return;
        }
        if (Input.GetMouseButtonDown (1)) {//cancel m_ui
            if(m_ui){
                Destroy(m_ui);
            }
            if(m_button){
                if(m_button.m_state==TowerButtonNew.StateID.ACTIVE){
                    m_button.m_state=TowerButtonNew.StateID.NORMAL;
                    m_button.spriteRenderer.sprite=m_button.m_ButtonSkin[0];
                }
            }
        }
        else if (Input.GetMouseButtonDown (0)) {
            //get the mouse click position
            Vector3 v3 = Camera.main.ScreenToWorldPoint (Input.mousePosition);
            Vector2 v2 = new Vector2 (v3.x, v3.y);

            RaycastHit2D hit = Physics2D.Raycast (v2, Vector2.zero);
            if (hit.collider != null) {

                //Debug.Log(hit.collider.tag);
                //check whether hit on the background
                if (hit.collider.tag.CompareTo("background")==0) {
                    if(clickUpgradeButtons){
                        clickUpgradeButtons=false;
                        Debug.Log("click on upgrade buttons!");
                        return;
                    }

                    if(m_ui){
                        Destroy(m_ui);
                    }
                    float rate=bgController.Instance.rate;
                    //convert click position to array index
                    float newX=(v2.x+(rate-1.0f)*this.transform.position.x)/rate;
                    int inewX=(int)(newX*10.0f+0.5f)/10;

                    //if the point beyond the scene
                    ix=inewX;
                    iy=(int)v2.y;
                    if(ix>=GridMap.Instance.MapSizeX || iy>GridMap.Instance.MapSizeY || ix<0 || iy<0)
                    {
                        return;
                    }

                    //calculate the place position
                    float posX=ix*rate-(rate-1.0f)*6.66f;
                    Vector3 pos=new Vector3(posX+0.5f*rate,iy+0.5f,-2.0f);

                    //check if this position can be put a defender
                    if(GridMap.Instance.m_map[ix,iy].fieldtype==MapData.FieldTypeID.GuardPosition){
                        if(Settings.Instance.interID==1){
                            m_ui=(GameObject)Instantiate(createUI,pos,Quaternion.identity);
                            m_ui.transform.parent=this.transform.parent;
                            m_ui.transform.localScale=new Vector3(1.0f,1,1);
                        }
                        else if(Settings.Instance.interID==0){
                            //Build a tower
                            if(m_button){
                                RecordController.Operation op=new RecordController.Operation();
                                op.posX=pos.x;
                                op.posY=pos.y;
                                op.time=RecordController.Instance.timer;
                                //create tower
                                CreateTower(pos);
                                if(Settings.Instance.sound){
                                    AudioSource.PlayClipAtPoint(placing,Camera.main.transform.position,1.0f);
                                }

                                switch(m_button.tower.GetComponent<DefenderController>().m_towerType){
                                case DefenderController.TowerType.LightTower:
                                    op.type=RecordController.OperationType.LightTower;
                                    break;
                                case DefenderController.TowerType.HeavyTower:
                                    op.type=RecordController.OperationType.HeavyTower;
                                    break;
                                case DefenderController.TowerType.PoisonTower:
                                    op.type=RecordController.OperationType.PoisonTower;
                                    break;
                                case DefenderController.TowerType.IceTower:
                                    op.type=RecordController.OperationType.IceTower;
                                    break;
                                case DefenderController.TowerType.ElecTower:
                                    op.type=RecordController.OperationType.ElecTower;
                                    break;
                                }
                                RecordController.Instance.ops.Add(op);
                                m_button = null;
                            }
                        }
                    }else{
                        m_button=null;
                        //Debug.LogWarning("Cannot put here!!!");
                        m_forbid=(GameObject)Instantiate(forbidUI,pos,Quaternion.identity);
                        m_forbid.transform.parent=this.transform.parent;
                        m_forbid.transform.localScale=new Vector3(1.0f,1,1);
                        Destroy(m_forbid,0.3f);
                    }
                }else{
                    //Debug.Log("Do nothing");
                }
            }else{
                Debug.LogError("Click Nothing!");
            }
        }
    }
    void ClickItem()
    {
        if(Input.GetMouseButtonDown(0)){
            Vector3 v3=Camera.main.ScreenToWorldPoint(Input.mousePosition);
            Vector2 v2=new Vector2(v3.x,v3.y);
            RaycastHit2D hit=Physics2D.Raycast(v2,Vector2.zero);
            if(hit.collider!=null)
            {
                if(hit.collider.gameObject==this.gameObject){
                    RecordController.Operation op=new RecordController.Operation();
                    op.time=RecordController.Instance.timer;
                    op.type=RecordController.OperationType.Enemy;
                    op.posX=v2.x;
                    op.posY=v2.y;

                    if(!Settings.Instance.replay){
                        EnemyOnClick();
                    }

                    RecordController.Instance.ops.Add(op);
                }
            }else{
                Debug.Log("not hitted");
            }
        }
    }
    void ClickItem()
    {
        if (Input.GetMouseButtonDown (1)) {
            this.selected=false;
            towerbutton.gameObject.SetActive(false);
            attackArea.gameObject.SetActive(false);
        }else if(Input.GetMouseButtonDown(0)){
            Vector3 v3=Camera.main.ScreenToWorldPoint(Input.mousePosition);
            Vector2 v2=new Vector2(v3.x,v3.y);
            RaycastHit2D hit=Physics2D.Raycast(v2,Vector2.zero);
            if(hit.collider!=null)
            {
                //Debug.Log(hit.collider.tag);
                if(hit.collider.gameObject==this.gameObject){
                    if(GameManager.Instance.clickUpgradeButtons){
                        GameManager.Instance.clickUpgradeButtons=false;
                        return;
                    }

                    this.selected=true;
                    //show the menu
                    towerbutton.gameObject.SetActive(true);
                    attackArea.gameObject.SetActive(true);

                }else{
                    if(this.selected){
                        if(hit.collider.tag.CompareTo("upgrade")==0){
                            GameManager.Instance.clickUpgradeButtons=true;
                            GameManager.Instance.ChangeClickUpgradeButton();
                            if(m_level!=TowerLevel.Super){
                                if(GameManager.Instance.m_point>=money){
                                    RecordController.Operation op=new RecordController.Operation();
                                    op.time=RecordController.Instance.timer;
                                    op.type=RecordController.OperationType.Upgrade;
                                    op.posX=this.transform.position.x;
                                    op.posY=this.transform.position.y;
                                    //Debug.Log ("tower up grade");
                                    UpdateTower();

                                    RecordController.Instance.ops.Add(op);
                                }
                            }
                        }else if(hit.collider.tag.CompareTo("remove")==0){
                            GameManager.Instance.clickUpgradeButtons=true;
                            GameManager.Instance.ChangeClickUpgradeButton();
                            RecordController.Operation op=new RecordController.Operation();
                            op.time=RecordController.Instance.timer;
                            op.type=RecordController.OperationType.Sell;
                            op.posX=this.transform.position.x;
                            op.posY=this.transform.position.y;
                            //Debug.Log ("tower removed");
                            RemoveTower();

                            RecordController.Instance.ops.Add(op);
                        }
                    }
                    this.selected=false;
                    towerbutton.gameObject.SetActive(false);
                    attackArea.gameObject.SetActive(false);
                }
            }else{
                Debug.Log("not hitted anything!");
            }
        }
    }