Beispiel #1
0
        public void StartGame(byte n, byte m, byte count)
        {
            Assert.IsTrue(count < n * m, "Element types count higher than n * m!");

            var nValue          = n != 0 ? n : _n;
            var mValue          = m != 0 ? m : _m;
            var typesCountValue = count != 0 ? count : _elementTypesCount;

            _playground     = new Playground(nValue, mValue, typesCountValue);
            _playgroundView = new GameObject(nameof(PlaygroundView)).AddComponent <PlaygroundView>();
            _playgroundView.Init(_playground);

            _mainCamera.orthographicSize = Math.Max(n, m);
            _ingameMenu.gameObject.SetActive(true);
    #if UNITY_EDITOR
            print(PrettyLog.GetMessage(nameof(App), nameof(StartGame), $"n = {n}, m = {m}, count = {count}"));
    #endif
        }
Beispiel #2
0
        public void Init(Playground playground)
        {
    #if UNITY_EDITOR
            print(PrettyLog.GetMessage(typeof(PlaygroundView).GetMethod(nameof(Init)), playground.ToString())); // =)
    #endif

            _playground = playground;
            _playground.OnPlaygroundUpdated += UpdatePlaygroundView;

            // Initialization background
            _backgroundPlane = GameObject.CreatePrimitive(PrimitiveType.Plane);
            _backgroundPlane.transform.SetParent(transform);
            _backgroundPlane.transform.localScale = new Vector3(playground.Width, 1, playground.Height);
            _backgroundPlane.transform.rotation   = new Quaternion(0, -90, 90, 0);
            _backgroundPlane.GetComponent <MeshRenderer>().material =
                new Material(Shader.Find(Defaults.BackgroundPlaneShader))
            {
                color = Color.black
            };

            // Initialization game elements pallete
            _colors = new List <Color>();
            _colors.Add(Color.black);

    #if UNITY_EDITOR
            print(PrettyLog.GetMessage(Color.white, "is color of empty element 0"));
    #endif

            for (var i = 1; i <= playground.ElementTypesCount; i++)
            {
                var color = Random.ColorHSV(0f, 1f, 1f, 1f, 0.5f, 1f);
                _colors.Add(color);

    #if UNITY_EDITOR
                print(PrettyLog.GetMessage(color, $"is color of element {i}"));
    #endif
            }

            // Initialization game elements
            _elements = new GameObject(Defaults.ElementsGameObjectName);
            _elements.transform.SetParent(transform);

            int index;
            playground.ToList().ForEach(element =>
            {
                var @object          = GameObject.CreatePrimitive(PrimitiveType.Cube);
                var elementComponent = @object.AddComponent <Element>();

                elementComponent.OnTrySwap += TrySwapElements;
                @object.transform.SetParent(_elements.transform);
                index = @object.transform.GetSiblingIndex();
                @object.GetComponent <MeshRenderer>().material =
                    new Material(Shader.Find(Defaults.BackgroundPlaneShader))
                {
                    color = _colors[playground[index]]
                };

                var x = (index * 2) % (2 * playground.Width) - playground.Width;
                var y = playground.Height - (index / playground.Width * 2) % (playground.Height * 2);

                @object.name = $"{index}";
                @object.transform.localPosition = new Vector3(x, y, -1);
            });
        }