Ejemplo n.º 1
0
    public void Start()
    {
        grid   = GameObject.Find("Grid").GetComponent <GridController>();
        ewc    = GameObject.Find("SpinWheel").GetComponent <EditorWheelController>();
        sr     = GameObject.Find("InvScroll").GetComponent <ScrollRect>();
        iwc    = GameObject.Find("InvContent").GetComponent <InventoryWindowController>();
        canvas = GameObject.Find("Canvas").GetComponent <Canvas>();
        //grid.editor = this;

        EventManager em = EventManager.Instance();

        em.RegisterForEventType("piece_tapped", this);
        em.RegisterForEventType("template_tapped", this);
        em.RegisterForEventType("piece_dropped_on_inventory", this);

        GameObject go = Instantiate(Resources.Load("Prefabs/ExistingPiece")) as GameObject;

        go.transform.SetParent(canvas.transform, false);
        go.transform.position = new Vector3(go.transform.position.x - 2, go.transform.position.y, go.transform.position.z);
        floatingPiece         = go.GetComponent <PieceController>();
        floatingPiece.ConfigureFromJSON("penetration_normal");
        floatingPiece.SetRotation(180);

        ewc.transform.rotation = floatingPiece.transform.rotation;
    }
Ejemplo n.º 2
0
    public void HandleEvent(GameEvent ge)
    {
        if (ge.type.Equals("piece_tapped"))
        {
            PieceController tappedPiece = (PieceController)ge.args[0];
            if (tappedPiece.GetGridLock())
            {
                if (floatingPiece != null)
                {
                    bool success = grid.TryAddPiece(floatingPiece);
                    if (success)
                    {
                        floatingPiece.SetGridLock(true);
                        floatingPiece = null;
                    }
                }
                Activate(tappedPiece);
            }
            else
            {
                bool success = grid.TryAddPiece(tappedPiece);
                if (success)
                {
                    tappedPiece.SetGridLock(true);
                    floatingPiece = null;
                }
            }
        }
        else if (ge.type.Equals("piece_dropped_on_inventory"))
        {
            PieceController piece = (PieceController)ge.args[0];
            InventoryAdd(piece);
        }
        else if (ge.type.Equals("template_tapped"))
        {
            PieceTemplateController tappedPiece = (PieceTemplateController)ge.args[0];
            Vector3 loc = (Vector3)ge.args[1];
            if (floatingPiece != null)
            {
                bool success = grid.TryAddPiece(floatingPiece);
                if (success)
                {
                    floatingPiece.SetGridLock(true);
                    floatingPiece = null;
                }
            }

            GameObject go = Instantiate(Resources.Load("Prefabs/ExistingPiece")) as GameObject;
            go.transform.SetParent(canvas.transform, false);
            go.transform.position = new Vector3(loc.x, loc.y, go.transform.position.z);
            PieceController newPiece = go.GetComponent <PieceController>();
            newPiece.ConfigureFromJSON(tappedPiece.GetFilename());
            newPiece.SetRotation(0);
            newPiece.SetMoving(true);
            Activate(newPiece);
            iwc.RemovePiece(newPiece);
            //tappedPiece.SetCount(tappedPiece.GetCount()-1);
        }
    }
Ejemplo n.º 3
0
    public void Start()
    {
        grid = GameObject.Find("Grid").GetComponent<GridController>();
        ewc = GameObject.Find("SpinWheel").GetComponent<EditorWheelController>();
        sr = GameObject.Find("InvScroll").GetComponent<ScrollRect>();
        iwc = GameObject.Find("InvContent").GetComponent<InventoryWindowController>();
        canvas = GameObject.Find ("Canvas").GetComponent<Canvas>();
        //grid.editor = this;

        EventManager em = EventManager.Instance();
        em.RegisterForEventType("piece_tapped",this);
        em.RegisterForEventType("template_tapped",this);
        em.RegisterForEventType("piece_dropped_on_inventory",this);

        GameObject go = Instantiate (Resources.Load ("Prefabs/ExistingPiece")) as GameObject;
        go.transform.SetParent(canvas.transform,false);
        go.transform.position = new Vector3(go.transform.position.x-2,go.transform.position.y,go.transform.position.z);
        floatingPiece = go.GetComponent<PieceController>();
        floatingPiece.ConfigureFromJSON("penetration_normal");
        floatingPiece.SetRotation(180);

        ewc.transform.rotation = floatingPiece.transform.rotation;
    }
Ejemplo n.º 4
0
    public void LoadTower(string filename)
    {
        towerFileName = filename;
        //Debug.Log(towerFileName);

        FileLoader fl = new FileLoader(Application.persistentDataPath, "Towers", filename);
        //FileLoader fl = new FileLoader ("JSONData" + Path.DirectorySeparatorChar + "Towers",filename);
        string json = fl.Read();
        Dictionary <string, System.Object> data = (Dictionary <string, System.Object>)Json.Deserialize(json);

        towerName     = (string)data["towerName"];
        decalFilename = (string)data["decalFilename"];
        towerType     = (string)data["towerType"];

        List <System.Object> pieces = data["pieces"] as List <System.Object>;

        foreach (System.Object pObj in pieces)
        {
            Dictionary <string, System.Object> pdata = pObj as Dictionary <string, System.Object>;
            int  x       = (int)(long)pdata["anchorX"];
            int  y       = (int)(long)pdata["anchorY"];
            int  rot     = (int)(long)pdata["rotation"];
            bool flipped = false;
            int  fliprot = 0;
            if (pdata.ContainsKey("flipped"))
            {
                flipped = (bool)pdata["flipped"];
                fliprot = (int)(long)pdata["flipRotation"];
            }
            string piecefile = (string)pdata["pieceFilename"];

            GameObject go = Instantiate(Resources.Load("Prefabs/ExistingPiece")) as GameObject;
            go.transform.SetParent(EditorController.piecesLayer, false);
            PieceController p = go.GetComponent <PieceController>();
            p.SetGridLock(true);
            p.ConfigureFromJSON(piecefile);
            p.SetRotation(rot);
            p.SetFlippedLoadOnly(flipped);
            p.SetFlipRotationLoadOnly(fliprot);
            if (flipped)
            {
                p.CalibrateFlip();
            }
            allPieces.Add(new PieceRecord(p, x, y));

            //SpriteRenderer sr = go.GetComponent<SpriteRenderer>();
            //PutThatThingBackWhereItCameFromOrSoHelpMe(go.GetComponent<PieceController>());
            RectTransform    prt       = (RectTransform)go.transform;
            RectTransform    rt        = (RectTransform)transform;
            UIRectTranslator translate = transform.gameObject.GetComponent <UIRectTranslator>();
            Vector3[]        corners   = new Vector3[4];
            rt.GetWorldCorners(corners);
            Vector3 gridTopCorner = corners[1];            //new Vector3(gridCorner.x,gridCorner.y+gridLength,gridCorner.z);
            Vector3 extents       = translate.WorldSize(prt) / 2;
            go.transform.position = gridTopCorner + new Vector3(squareWidth * x + extents.x,
                                                                -squareWidth * (y) - extents.y,
                                                                go.transform.position.z);


            int[,] values = p.GetArray();             //we assume all pieces fit- no error checking
            for (int i = 0; i < values.GetLength(0); i++)
            {
                for (int j = 0; j < values.GetLength(1); j++)
                {
                    int       value = values[i, j];
                    Occupancy o     = grid[y + i, x + j];
                    if (value == 1)
                    {
                        o.north = p;
                        o.east  = p;
                        o.south = p;
                        o.west  = p;
                    }
                    else if (value == PieceController.NORTHWEST_CODE)
                    {
                        o.north = p;
                        o.west  = p;
                    }
                    else if (value == PieceController.NORTHEAST_CODE)
                    {
                        o.north = p;
                        o.east  = p;
                    }
                    else if (value == PieceController.SOUTHEAST_CODE)
                    {
                        o.east  = p;
                        o.south = p;
                    }
                    else if (value == PieceController.SOUTHWEST_CODE)
                    {
                        o.south = p;
                        o.west  = p;
                    }
                }
            }
        }
        UpdateReadout();
    }
Ejemplo n.º 5
0
    public void HandleEvent(GameEvent ge)
    {
        if (ge.type.Equals("piece_tapped"))
        {
            PieceController tappedPiece = (PieceController)ge.args[0];
            if (tappedPiece.GetGridLock())
            {
                if (floatingPiece != null)
                {
                    bool success = grid.TryAddPiece(floatingPiece);
                    if (success)
                    {
                        floatingPiece.SetGridLock(true);
                        floatingPiece = null;
                    }
                }
                Activate(tappedPiece);
            }
            else
            {
                bool success = grid.TryAddPiece(tappedPiece);
                if (success)
                {
                    tappedPiece.SetGridLock(true);
                    floatingPiece = null;
                }
            }
        }
        else if (ge.type.Equals("piece_dropped_on_inventory"))
        {
            PieceController piece = (PieceController)ge.args[0];
            InventoryAdd(piece);
        }
        else if (ge.type.Equals("template_tapped"))
        {
            PieceTemplateController tappedPiece = (PieceTemplateController)ge.args[0];
            Vector3 loc = (Vector3)ge.args[1];
            if (floatingPiece != null)
            {
                bool success = grid.TryAddPiece(floatingPiece);
                if (success)
                {
                    floatingPiece.SetGridLock(true);
                    floatingPiece = null;
                }
            }

            GameObject go = Instantiate(Resources.Load("Prefabs/ExistingPiece")) as GameObject;
            go.transform.SetParent(canvas.transform, false);
            go.transform.position = new Vector3(loc.x, loc.y, go.transform.position.z);
            PieceController newPiece = go.GetComponent <PieceController>();
            newPiece.ConfigureFromJSON(tappedPiece.GetFilename());
            newPiece.SetRotation(0);
            newPiece.SetMoving(true);
            Activate(newPiece);
            iwc.RemovePiece(newPiece);
            //tappedPiece.SetCount(tappedPiece.GetCount()-1);
        }
        else if (ge.type.Equals("alt_click"))
        {
            if (floatingPiece == null)
            {
                return;
            }
            if (!finger1down)
            {
                return;
            }

            Vector3 altClickPos = (Vector3)ge.args[0];
            //check if the second click is on one of the rotate buttons
            float ymax = rotCounterclockwise.rect.y + (rotCounterclockwise.rect.height / 2f);
            float xmax = rotCounterclockwise.rect.x + (rotCounterclockwise.rect.width / 2f);
            if (altClickPos.x < xmax && altClickPos.y < ymax)             //then you're on the buttons
            {
                return;
            }

            Vector3 firstClickPos = InputWatcher.GetInputPosition();
            Vector3 direction     = altClickPos - firstClickPos;
            twoFingerAngle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
            pieceAngle     = floatingPiece.transform.eulerAngles.z;

            floatingPiece.SetTwoFingerMovement(true);
            finger2down = true;
        }
        else if (ge.type.Equals("alt_release"))
        {
            Debug.Log("alt release happened");
            if (floatingPiece == null)
            {
                return;
            }
            if (!finger2down)
            {
                return;
            }

            if (waitingForOtherFingerToReset)
            {
                floatingPiece.SetTwoFingerMovement(false);
                waitingForOtherFingerToReset = false;
            }
            else
            {
                waitingForOtherFingerToReset = true;
                floatingPiece.LockRotation();
            }
            finger2down = false;
        }
        else if (ge.type.Equals("mouse_click"))
        {
            Vector3 pos = (Vector3)ge.args[0];
            if (grid.TouchIsOnMe(pos))
            {
                finger1down = true;
            }
        }
        else if (ge.type.Equals("mouse_release"))
        {
            if (floatingPiece == null)
            {
                return;
            }
            if (waitingForOtherFingerToReset)
            {
                floatingPiece.SetTwoFingerMovement(false);
                waitingForOtherFingerToReset = false;
            }
            else
            {
                waitingForOtherFingerToReset = true;
                floatingPiece.LockRotation();
            }
            finger1down = false;
        }
    }