Esempio n. 1
0
        private IEnumerator WaitingMovementRoutine()
        {
            //Random small movement

            while (_state == DroneState.SEARCHING_ENEMY)
            {
                var point = _startPosition + MRandom.OnUnitCircle() * width * 0.5f;

                var   distance    = point - _pos;
                float distanceMag = distance.Magnitude;

                while (distanceMag > 20)
                {
                    var distanceNorm = distance.Normalized;

                    _currentSpeed = (_state == DroneState.SEARCHING_ENEMY) ? _waitingSpeed : _maxSpeed;

                    var nextPos = distanceNorm * _currentSpeed;

                    //Console.WriteLine($"{this}: {x:0.00} | {y:0.00} | dist: {distanceMag:0.00} | startpos: {_startPosition} | point: {point} | speed: {speed} | nextPos: {nextPos}");

                    Translate(nextPos.x * Time.delta, nextPos.y * Time.delta);

                    yield return(null);

                    distance    = point - _pos;
                    distanceMag = distance.Magnitude;
                }
            }
        }
Esempio n. 2
0
        public void PlayHudFx(int index = -1)
        {
            if (index == -1)
            {
                index = MRandom.Range(0, _hudFxs.Length);
            }
            else
            {
                index = index % _hudFxs.Length;
            }

            _fxHudChannels[index] = _hudFxs[index].Play();
        }
Esempio n. 3
0
        IEnumerator SmallSnowFlakesParticlesRoutine2(GameObject target, Vector2 offset, float range, int duration)
        {
            while (target?.parent != null)
            {
                for (int i = 0; i < _smallSnowFlakes2.Length; i++)
                {
                    var pos  = MRandom.InsideUnitCircle() * range;
                    var snow = _smallSnowFlakes2[i];

                    target.parent.AddChild(snow);
                    snow.Show(target, pos, offset);

                    yield return(new WaitForMilliSeconds(duration / _smallSnowFlakes2.Length));
                }
            }
        }
Esempio n. 4
0
        public void PlayExplosionFx(int index = -1)
        {
            if (index == -1)
            {
                index = MRandom.Range(0, _explosionFxs.Length);
            }
            else
            {
                index = index % _explosionFxs.Length;
            }

            if (_fxExplosionChannels[index] != null && _fxExplosionChannels[index].IsPlaying)
            {
                _fxExplosionChannels[index].Stop();
            }

            _fxExplosionChannels[index] = _explosionFxs[index].Play();
        }
Esempio n. 5
0
        void Update()
        {
            if (!Enabled)
            {
                return;
            }

            Vector2 pos = new Vector2(this.x, this.y);

            if (_followEnabled && _target != null)
            {
                Vector2 targetP = new Vector2(_target.x, _target.y);

                if (_hasSpeedListener)
                {
                    //Target point is in front of target, set offset
                    float dist = Mathf.Map(_iHasSpeed.Speed, 0, _iHasSpeed.MaxSpeed, 0, TargetFrontDistance);

                    //TODO: make IHasSpeed has a Vector2 _forward
                    float xDir = Mathf.Cos(_target.rotation.DegToRad());
                    float yDir = Mathf.Sin(_target.rotation.DegToRad());

                    targetP = targetP + (new Vector2(xDir, yDir)) * dist;
                }

                Vector2 targetDirection = (targetP - pos);
                float   directionMag    = targetDirection.Magnitude;

                if (directionMag > 0)
                {
                    interpVelocity = directionMag * speed;

                    float lastX = _targetPos.x;
                    float lastY = _targetPos.y;

                    _targetPos = pos + (targetDirection.Normalized * interpVelocity * Time.delta);

                    if (_targetPos.x - MyGame.HALF_SCREEN_WIDTH < 0 || _targetPos.x + MyGame.HALF_SCREEN_WIDTH > _map.MapWidthInPixels)
                    {
                        _targetPos.x = lastX;
                    }

                    if (_targetPos.y - MyGame.HALF_SCREEN_HEIGHT < 0 || _targetPos.y + MyGame.HALF_SCREEN_HEIGHT > _map.MapHeightInPixels)
                    {
                        _targetPos.y = lastY;
                    }

                    //var nextPos = Vector2.Lerp(pos, targetPos + offset, 0.25f);
                    // float nextX = Easing.Ease(Easing.Equation.CubicEaseOut, 1, pos.x, _targetPos.x, 4);
                    // float nextY = Easing.Ease(Easing.Equation.CubicEaseOut, 1, pos.y, _targetPos.y, 4);

                    float nextX = pos.x + Easing.Ease(Easing.Equation.CubicEaseOut, 1, 0, _targetPos.x - pos.x, 4);
                    float nextY = pos.y + Easing.Ease(Easing.Equation.CubicEaseOut, 1, 0, _targetPos.y - pos.y, 4);

                    this.SetXY(nextX, nextY);
                }
            }

            pos.x = x;
            pos.y = y;

            if (shakeDuration > 0)
            {
                var shakePos = pos + MRandom.InsideUnitCircle() * shakeAmount;
                shakeDuration -= Time.deltaTime * decreaseFactor;

                SetXY(shakePos.x, shakePos.y);
            }
            else
            {
                shakeDuration = 0f;
                SetXY(pos.x, pos.y);
            }

            //Debug
            float lScale = scale;

            if (Input.GetKey(Key.W))
            {
                lScale -= 5 * Time.delta;
            }
            else if (Input.GetKey(Key.S))
            {
                lScale += 5 * Time.delta;
            }
            else if (Input.GetKey(Key.X))
            {
                lScale = 1;
            }

            scale = Mathf.Clamp(lScale, 0.01f, 100);
        }
        public void DrawBorders(GameObject parent, float alpha = 1f)
        {
            var borderCloudesTileSet = _mapData.TileSets.FirstOrDefault(ts => ts.Name == "Clouds Border");

            var topCloudsRects = new Rectangle[]
            {
                new Rectangle(0, 128 * 0, 512, 128),
                new Rectangle(0, 128 * 1, 512, 128),
                new Rectangle(0, 128 * 2, 512, 128),
            };

            var bottomCloudsRects = new Rectangle[]
            {
                new Rectangle(0, 128 * 3, 512, 128),
                new Rectangle(0, 128 * 4, 512, 128),
                new Rectangle(0, 128 * 5, 512, 128),
            };

            var rightCloudsRects = new Rectangle[]
            {
                new Rectangle(512 + 128 * 0, 0, 128, 512),
                new Rectangle(512 + 128 * 1, 0, 128, 512),
                new Rectangle(512 + 128 * 2, 0, 128, 512),
            };

            var leftCloudsRects = new Rectangle[]
            {
                new Rectangle(512 + 128 * 3, 0, 128, 512),
                new Rectangle(512 + 128 * 0, 512, 128, 512),
                new Rectangle(512 + 128 * 1, 512, 128, 512),
            };

            int tilsetIndex  = _tilesetIndexMap["Clouds Borders"];
            var tilesetImage = _tileSetImages[tilsetIndex];

            int widthSteps = Mathf.Ceiling((float)_mapData.Width * _mapData.TileWidth / topCloudsRects[0].Width);

            //Draw top border
            int canvasW = topCloudsRects[0].Width;
            int canvasH = topCloudsRects[0].Height;

            for (int i = 0; i < widthSteps; i++)
            {
                int randRect = MRandom.Range(0, topCloudsRects.Length);

                var canvas = new Canvas(canvasW, canvasH, false);

                canvas.graphics.DrawImage(tilesetImage,
                                          new Rectangle(0, 0, canvasW, canvasH),
                                          topCloudsRects[randRect], GraphicsUnit.Pixel
                                          );

                parent.AddChild(canvas);

                canvas.x = i * canvasW;
                canvas.y = 0;

                canvas.alpha = alpha;
            }

            //Draw bottom border
            canvasW = bottomCloudsRects[0].Width;
            canvasH = bottomCloudsRects[0].Height;

            int yHeight = (_mapData.Height - 1) * _mapData.TileHeight;

            for (int i = 0; i < widthSteps; i++)
            {
                int randRect = MRandom.Range(0, bottomCloudsRects.Length);

                var canvas = new Canvas(canvasW, canvasH, false);

                canvas.graphics.DrawImage(tilesetImage,
                                          new Rectangle(0, 0, canvasW, canvasH),
                                          bottomCloudsRects[randRect], GraphicsUnit.Pixel
                                          );

                parent.AddChild(canvas);

                canvas.x = i * canvasW;
                canvas.y = yHeight;

                canvas.alpha = alpha;
            }

            int heightSteps = Mathf.Ceiling((float)_mapData.Height * _mapData.TileHeight / leftCloudsRects[0].Width);

            //Draw left border
            canvasW = leftCloudsRects[0].Width;
            canvasH = leftCloudsRects[0].Height;

            int xWidth = 0;

            for (int i = 0; i < heightSteps; i++)
            {
                int randRect = MRandom.Range(0, leftCloudsRects.Length);

                var canvas = new Canvas(canvasW, canvasH, false);

                canvas.graphics.DrawImage(tilesetImage,
                                          new Rectangle(0, 0, canvasW, canvasH),
                                          leftCloudsRects[randRect], GraphicsUnit.Pixel
                                          );

                parent.AddChild(canvas);

                canvas.x = xWidth;
                canvas.y = i * canvasH;

                canvas.alpha = alpha;
            }

            //Draw right border
            canvasW = rightCloudsRects[0].Width;
            canvasH = rightCloudsRects[0].Height;

            xWidth = (_mapData.Width - 1) * _mapData.TileWidth;

            for (int i = 0; i < heightSteps; i++)
            {
                int randRect = MRandom.Range(0, rightCloudsRects.Length);

                var canvas = new Canvas(canvasW, canvasH, false);

                canvas.graphics.DrawImage(tilesetImage,
                                          new Rectangle(0, 0, canvasW, canvasH),
                                          rightCloudsRects[randRect], GraphicsUnit.Pixel
                                          );

                parent.AddChild(canvas);

                canvas.x = xWidth;
                canvas.y = i * canvasH;

                canvas.alpha = alpha;
            }
        }
Esempio n. 7
0
        private IEnumerator CollisionWithTornadoRoutine(TornadoGameObject tornado)
        {
            MyGame.ThisInstance.Camera.shakeDuration = 3000;

            _stork.InputEnabled        = false;
            _stork.IsMoveEnabled       = false;
            _stork.IsCollisionDisabled = true;

            _stork.SetSpeed(0);

            //Turn stork
            var turnStorkRoutine = CoroutineManager.StartCoroutine(TurnStorkInTornadoRoutine(), this);

            //Snap to tornado
            int       snapTimer    = 0;
            const int snapDuration = 200;

            var snapStartPos = _stork.Pos;

            SoundManager.Instance.PlayFx(9);

            while (snapTimer < snapDuration)
            {
                float xPos = (tornado.Pos.x - snapStartPos.x) *
                             Easing.Ease(Easing.Equation.QuadEaseOut, snapTimer, 0, 1, snapDuration);
                float yPos = (tornado.Pos.y - snapStartPos.y) *
                             Easing.Ease(Easing.Equation.QuadEaseOut, snapTimer, 0, 1, snapDuration);

                _stork.SetXY(snapStartPos.x + xPos, snapStartPos.y + yPos);

                snapTimer += Time.deltaTime;

                yield return(null);
            }

            //Keep stork in tornado
            int       stuckTimer    = 0;
            const int stuckDuration = 2000;

            while (stuckTimer < stuckDuration)
            {
                _stork.SetXY(tornado.x, tornado.y);

                stuckTimer += Time.deltaTime;
                yield return(null);
            }

            //Thrown Stork

            SoundManager.Instance.PlayFx(6);

            float throwAngle = 0;

            if (tornado.ThrowAngleMin == tornado.ThrowAngleMax)
            {
                throwAngle = tornado.ThrowAngleMin;
            }
            else
            {
                throwAngle = MRandom.Range((float)tornado.ThrowAngleMin, (float)tornado.ThrowAngleMax);
            }

            throwAngle = throwAngle.DegToRad();

            var throwDirection = new Vector2()
            {
                x = Mathf.Cos(throwAngle),
                y = Mathf.Sin(throwAngle)
            };

            var throwStartPos = tornado.Pos;
            var throwPos      = throwStartPos + throwDirection * tornado.ThrowDistance;

            int thrownTimer    = 0;
            int thrownDuration = 1000;

            float startScale = _stork.scale;

            Console.WriteLine($"{this}: throwAngle: {throwAngle.RadToDegree()}");

            while (thrownTimer < thrownDuration)
            {
                float xPos = (throwPos.x - throwStartPos.x) *
                             Easing.Ease(Easing.Equation.QuadEaseOut, thrownTimer, 0, 1, thrownDuration);
                float yPos = (throwPos.y - throwStartPos.y) *
                             Easing.Ease(Easing.Equation.QuadEaseOut, thrownTimer, 0, 1, thrownDuration);

                _stork.SetXY(throwStartPos.x + xPos, throwStartPos.y + yPos);

                float scale;
                if (thrownTimer < thrownDuration * 0.5f)
                {
                    scale = startScale + Easing.Ease(Easing.Equation.QuadEaseOut, thrownTimer, 0, 1, thrownDuration * 0.5f);
                }
                else
                {
                    scale = startScale + 1 - Easing.Ease(Easing.Equation.QuadEaseIn, thrownTimer - thrownDuration * 0.5f, 0, 1, thrownDuration * 0.5f);
                }

                _stork.SetScaleXY(scale, scale);

                thrownTimer += Time.deltaTime;
                yield return(null);
            }

            _stork.SetScaleXY(startScale, startScale);

            _inCollisionWithTornado = false;
            _lastTornadoCollided    = null;

            _stork.InputEnabled        = true;
            _stork.IsMoveEnabled       = true;
            _stork.IsCollisionDisabled = false;
        }
Esempio n. 8
0
        public void SpawnClouds()
        {
            var cloudsTileArray     = _map.GetCloudsTileArray();
            var cloudsLayerFirstGid = _map.MapData.TileSets.FirstOrDefault(ts => ts.Name == "Clouds Tileset").FirstGId;

            for (int col = 0; col < cloudsTileArray.GetLength(0); col++)
            {
                for (int row = 0; row < cloudsTileArray.GetLength(1); row++)
                {
                    CloudGameObject cloud;
                    var             tile = cloudsTileArray[col, row] - cloudsLayerFirstGid;

                    if (tile == 20 || tile == 50)
                    {
                        //Instantiate a horizontal cloud
                        cloud = new CloudGameObject(MRandom.Range(-1, 1));
                        _level.AddChild(cloud);

                        float px = (col) * _map.MapData.TileWidth;
                        float py = (row) * _map.MapData.TileHeight;

                        cloud.SetXY(px, py);

                        if (tile == 50)
                        {
                            cloud.SetXY(px + _map.MapData.TileWidth, py);
                            cloud.Turn(90);
                        }

                        _clouds.Add(cloud);
                    }

                    //Check if a cloud is incorrectly drew in Tiled and than "ERASE" the tile,
                    //a cloud need the "pivot" tile (20 for horizontal clouds, 50 for vertical clouds)
                    //Ugly solution, hard coded

                    int tileCheck;
                    if (tile >= 21 && tile <= 24)
                    {
                        int gap = tile - 20;

                        if (gap > -1)
                        {
                            tileCheck = cloudsTileArray[col - gap, row] - cloudsLayerFirstGid;
                            if (tileCheck != 20)
                            {
                                _map.SetCloudsTileArray(col, row, 0);
                                Console.WriteLine($"{this}: {col}|{row} cloud tile erased for 21");
                            }
                        }
                    }
                    else if (tile >= 36 && tile <= 40)
                    {
                        int gapX = tile - 36;
                        int gapY = 1;

                        if (gapX > -1 && gapY > -1)
                        {
                            tileCheck = cloudsTileArray[col - gapX, row - gapY] - cloudsLayerFirstGid;
                            if (tileCheck != 20)
                            {
                                _map.SetCloudsTileArray(col, row, 0);
                                Console.WriteLine($"{this}: {col}|{row} cloud tile erased for 37");
                            }
                        }
                    }
                    else if (tile == 49)
                    {
                        int gapX = 50 - tile;
                        if (gapX < _map.MapData.Height)
                        {
                            tileCheck = cloudsTileArray[col + gapX, row] - cloudsLayerFirstGid;
                            if (tileCheck != 50)
                            {
                                _map.SetCloudsTileArray(col, row, 0);
                                Console.WriteLine($"{this}: {col}|{row} cloud tile erased for 50");
                            }
                        }
                    }
                    else if (tile >= 65 && tile <= 66)
                    {
                        int gapX = 66 - tile;
                        int gapY = 1;

                        if (gapX < _map.MapData.Height && gapY > -1)
                        {
                            tileCheck = cloudsTileArray[col + gapX, row - gapY] - cloudsLayerFirstGid;
                            if (tileCheck != 50)
                            {
                                _map.SetCloudsTileArray(col, row, 0);
                                Console.WriteLine($"{this}: {col}|{row} cloud tile erased for 50, 65");
                            }
                        }
                    }
                    else if (tile >= 81 && tile <= 82)
                    {
                        int gapX = 82 - tile;
                        int gapY = 2;

                        if (gapX < _map.MapData.Height && gapY > -1)
                        {
                            tileCheck = cloudsTileArray[col + gapX, row - gapY] - cloudsLayerFirstGid;
                            if (tileCheck != 50)
                            {
                                _map.SetCloudsTileArray(col, row, 0);
                                Console.WriteLine($"{this}: {col}|{row} cloud tile erased for 50, 81");
                            }
                        }
                    }
                    else if (tile >= 97 && tile <= 98)
                    {
                        int gapX = 98 - tile;
                        int gapY = 3;

                        if (gapX < _map.MapData.Height && gapY > -1)
                        {
                            tileCheck = cloudsTileArray[col + gapX, row - gapY] - cloudsLayerFirstGid;
                            if (tileCheck != 50)
                            {
                                _map.SetCloudsTileArray(col, row, 0);
                                Console.WriteLine($"{this}: {col}|{row} cloud tile erased for 50, 97");
                            }
                        }
                    }
                    else if (tile >= 113 && tile <= 114)
                    {
                        int gapX = 114 - tile;
                        int gapY = 4;

                        if (gapX < _map.MapData.Height && gapY > -1)
                        {
                            tileCheck = cloudsTileArray[col + gapX, row - gapY] - cloudsLayerFirstGid;
                            if (tileCheck != 49)
                            {
                                _map.SetCloudsTileArray(col, row, 0);
                                Console.WriteLine($"{this}: {col}|{row} cloud tile erased for 50, 113");
                            }
                        }
                    }
                }
            }
        }