Пример #1
0
    void GestionClickGauche()
    {
        if (Input.GetMouseButton(0) && hitTerrain)                                                              // click gauche souris
        {
            if (GOAmenagement != null && amenagement != null && !EventSystem.current.IsPointerOverGameObject()) // place le batiment attaché au curseur du joueur
            {
                if (amenagement is Route)
                {
                    if (firstBoule == null)
                    {
                        firstBoule = Instantiate(boule, new Vector3(x, 5f, z), Quaternion.identity);
                        GOAmenagement.SetActive(false);
                        secondBoule = Instantiate(boule, new Vector3(x, 5f, z), Quaternion.identity);
                    }
                    secondBoule.transform.position = new Vector3(x, 5f, z);
                }
                else
                {
                    if (dispo && ville.PlacerUnAmenagement(amenagement)) // placement du batiment sur la carte
                    {
                        ChangeGameObjectTextureToOriginal(GOAmenagement, modelActuel);
                        amenagementSpawner.GetComponent <AmenagementSpawner>().Spawn(GOAmenagement.transform);

                        // on place le model attaché à la souris en remplacant le model attaché a la souris par un nouveau, l'autre est donc laissé sur place
                        GOAmenagement        = Instantiate(modelActuel, new Vector3(posX, 0, posZ), Quaternion.identity);
                        amenagement          = GOAmenagement.GetComponent <AmenagementPrefab>().Amenagement;
                        amenagement.Rotation = rotation;
                    }
                }
            }
        }

        // Placement des route en gardant le clique enfoncé
        if (Input.GetMouseButtonUp(0) && hitTerrain)
        {
            bool ligneDispo = true;
            GOAmenagement.SetActive(true);

            if (firstBoule != null && firstBoule.transform.position.x == x)
            {
                for (int i = Mathf.Min(Mathf.FloorToInt(firstBoule.transform.position.z) / 10, gridZ); i <= Mathf.Max(gridZ, Mathf.FloorToInt(firstBoule.transform.position.z) / 10); i++)
                {
                    if (!ville.Map.VerifierPlace(new Route("", 0, 1, Mathf.FloorToInt(x) / 10, i, false)) && !(ville.Map.Carte[i, Mathf.FloorToInt(x) / 10] is Route))
                    {
                        ligneDispo = false;
                    }
                }
                if (ligneDispo)
                {
                    Destroy(GOAmenagement);
                    for (int i = Mathf.Min(Mathf.FloorToInt(firstBoule.transform.position.z) / 10, gridZ); i <= Mathf.Max(gridZ, Mathf.FloorToInt(firstBoule.transform.position.z) / 10); i++)
                    {
                        if (ville.PlacerUnAmenagement(new Route("", 0, 1, Mathf.FloorToInt(x) / 10, i, false)))
                        {
                            GOAmenagement = Instantiate(modelActuel, new Vector3(ConvertToMapScale(gridX), 0.1f, ConvertToMapScale(i)), Quaternion.identity);

                            amenagement      = GOAmenagement.GetComponent <AmenagementPrefab>().Amenagement;
                            amenagement.PosX = gridX;
                            amenagement.PosY = i;
                        }
                    }
                }
                GOAmenagement = Instantiate(modelActuel, new Vector3(0, 0.1f, 0), Quaternion.identity);
                amenagement   = GOAmenagement.GetComponent <AmenagementPrefab>().Amenagement;
            }
            else if (firstBoule != null && firstBoule.transform.position.z == z)
            {
                for (int i = Mathf.Min(Mathf.FloorToInt(firstBoule.transform.position.x) / 10, gridX); i <= Mathf.Max(gridX, Mathf.FloorToInt(firstBoule.transform.position.x) / 10); i++)
                {
                    if (!ville.Map.VerifierPlace(new Route("", 0, 1, i, Mathf.FloorToInt(z) / 10, false)) && !(ville.Map.Carte[Mathf.FloorToInt(z) / 10, i] is Route))
                    {
                        ligneDispo = false;
                    }
                }
                if (ligneDispo)
                {
                    Destroy(GOAmenagement);
                    for (int i = Mathf.Min(Mathf.FloorToInt(firstBoule.transform.position.x) / 10, gridX); i <= Mathf.Max(gridX, Mathf.FloorToInt(firstBoule.transform.position.x) / 10); i++)
                    {
                        if (ville.PlacerUnAmenagement(new Route("", 0, 1, i, Mathf.FloorToInt(z) / 10, false)))
                        {
                            GOAmenagement    = Instantiate(modelActuel, new Vector3(ConvertToMapScale(i), 0.1f, ConvertToMapScale(gridZ)), Quaternion.identity);
                            amenagement      = GOAmenagement.GetComponent <AmenagementPrefab>().Amenagement;
                            amenagement.PosX = i;
                            amenagement.PosY = gridZ;
                        }
                    }
                    GOAmenagement = Instantiate(modelActuel, new Vector3(0, 0.1f, 0), Quaternion.identity);
                    amenagement   = GOAmenagement.GetComponent <AmenagementPrefab>().Amenagement;
                }
            }
            Destroy(firstBoule);
            Destroy(secondBoule);
            GOAmenagement.SetActive(true);
            secondBoule = null;
            firstBoule  = null;
        }

        //Selection ou desruction d'un batiment
        if (Input.GetMouseButtonDown(0) && GOAmenagement == null)
        {
            int  mask  = LayerMask.GetMask("Batiment");
            Ray  ray   = Camera.main.ScreenPointToRay(Input.mousePosition);
            bool touch = Physics.Raycast(ray, out hit, 5000f, mask);

            if (touch && !EventSystem.current.IsPointerOverGameObject())
            {
                targetBuilding = hit.transform;
                if (!targetBuilding.GetComponent <AmenagementPrefab>())
                {
                    targetBuilding = targetBuilding.parent;
                }


                if (isInDestroyMode)
                {
                    if (targetBuldingDestroy != null)
                    {
                        Amenagement temp = targetBuilding.GetComponent <AmenagementPrefab>().Amenagement;
                        ville.SupprimerAmenagement(temp);
                        Destroy(targetBuilding.gameObject);
                        targetBuilding = null;
                        Destroy(buildingTextureHolder.gameObject);
                    }
                }
                else // Le joueur a cliqué sur un batilent
                {
                    // On affiche les informations du batiments
                    menu.AfficheInfoAmenagement(targetBuilding);
                    //Affiche l'outline du batiment
                    DisplayOutlineSelectedBuilding();
                }
            }
            else if (!EventSystem.current.IsPointerOverGameObject()) // CLique sur un point de la map qui ne contient pas de batiment
            {
                // on cache les infos du batiment
                // on cache l'outline du batiment
                targetBuilding = null;
                RemoveOutlineSelectedTransform();
                menu.CacherInfoAmenagement();
            }
        }
    }