public static void CheckBulletWithMap(LVector2Int iPos, GameEntity entity, IGameAudioService audioService, IMap2DService map2DService)
        {
            var unit   = entity.unit;
            var bullet = entity.bullet;
            var id     = map2DService.Pos2TileId(iPos, false);

            if (id != 0 && unit.health > 0)
            {
                //collide bullet with world
                if (id == TilemapUtil.TileID_Brick)
                {
                    if (unit.camp == ECampType.Player)
                    {
                        audioService.PlayClipHitBrick();
                    }

                    map2DService.ReplaceTile(iPos, id, 0);
                    unit.health--;
                }
                else if (id == TilemapUtil.TileID_Iron)
                {
                    if (!bullet.canDestoryIron)
                    {
                        if (unit.camp == ECampType.Player)
                        {
                            audioService.PlayClipHitIron();
                        }

                        unit.health = 0;
                    }
                    else
                    {
                        if (unit.camp == ECampType.Player)
                        {
                            audioService.PlayClipDestroyIron();
                        }

                        unit.health = LMath.Max(unit.health - 2, 0);
                        map2DService.ReplaceTile(iPos, id, 0);
                    }
                }
                else if (id == TilemapUtil.TileID_Grass)
                {
                    if (bullet.canDestoryGrass)
                    {
                        if (unit.camp == ECampType.Player)
                        {
                            audioService.PlayClipDestroyGrass();
                        }

                        unit.health -= 0;
                        map2DService.ReplaceTile(iPos, id, 0);
                    }
                }
                else if (id == TilemapUtil.TileID_Wall)
                {
                    unit.health = 0;
                }
            }
        }
Пример #2
0
 public void DoAfterInit()
 {
     for (int i = 0; i < this.gridInfo.tileMaps.Length; i++)
     {
         TileInfos tileInfos = this.gridInfo.tileMaps[i];
         bool      isTagMap  = tileInfos.isTagMap;
         if (!isTagMap)
         {
             LVector2Int min  = tileInfos.min;
             LVector2Int size = tileInfos.size;
             this.mapDataMin.x = LMath.Min(this.mapDataMin.x, min.x);
             this.mapDataMin.y = LMath.Min(this.mapDataMin.y, min.y);
             this.mapDataMax.x = LMath.Max(this.mapDataMax.y, min.x + size.x);
             this.mapDataMax.y = LMath.Max(this.mapDataMax.y, min.y + size.y);
         }
     }
     this.mapDataSize = this.mapDataMax - this.mapDataMin + LVector2Int.one;
     this.mapDataIds  = new ushort[this.mapDataSize.x, this.mapDataSize.y];
     for (int j = 0; j < this.mapDataSize.x; j++)
     {
         for (int k = 0; k < this.mapDataSize.y; k++)
         {
             LVector2Int pos = new LVector2Int(mapDataMin.x + j, mapDataMin.y + k);
             this.mapDataIds[j, k] = this.RawPos2TileId(pos, false);
         }
     }
 }
Пример #3
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="o">射线起点</param>
        /// <param name="d">射线终点</param>
        /// <param name="min">矩形的左下角坐标</param>
        /// <param name="max">矩形的右上角坐标</param>
        /// <param name="tmin">返回距离</param>
        /// <returns></returns>
        public static bool TestRayAABB(LVector2 o, LVector2 d, LVector2 min, LVector2 max, out LFloat tmin)
        {
            tmin = LFloat.zero;
            LFloat tmax = LFloat.FLT_MAX;

            for (int i = 0; i < 2; i++)
            {
                if (LMath.Abs(d[i]) < LFloat.EPSILON)
                {
                    if (o[i] < min[i] || o[i] > max[i])
                    {
                        return(false);
                    }
                }
                else
                {
                    LFloat ood = LFloat.one / d[i];
                    LFloat t1  = (min[i] - o[i]) * ood;
                    LFloat t2  = (max[i] - o[i]) * ood;
                    if (t1 > t2)
                    {
                        var temp = t1;
                        t1 = t2;
                        t2 = temp;
                    }
                    tmin = LMath.Max(tmin, t1);
                    tmax = LMath.Min(tmax, t2);
                    if (tmin > tmax)
                    {
                        return(false);
                    }
                }
            }
            return(true);
        }
Пример #4
0
        private void UpdatePingVal(float deltaTime)
        {
            _pingTimer += deltaTime;
            if (_pingTimer > 0.5f)
            {
                _pingTimer = 0;
                DelayVal   = (int)(_delays.Sum() / LMath.Max(_delays.Count, 1));
                _delays.Clear();
                PingVal = (int)(_pings.Sum() / LMath.Max(_pings.Count, 1));
                _pings.Clear();

                if (_minPing < _historyMinPing && _simulatorService._gameStartTimestampMs != -1)
                {
                    _historyMinPing = _minPing;
#if UNITY_EDITOR
                    Debug.LogWarning(
                        $"Recalc _gameStartTimestampMs {_simulatorService._gameStartTimestampMs} _guessServerStartTimestamp:{_guessServerStartTimestamp}");
#endif
                    _simulatorService._gameStartTimestampMs = LMath.Min(_guessServerStartTimestamp,
                                                                        _simulatorService._gameStartTimestampMs);
                }

                _minPing = Int64.MaxValue;
                _maxPing = Int64.MinValue;
            }
        }
        public void DoAfterInit()
        {
            for (int i = 0; i < gridInfo.tileMaps.Length; i++)
            {
                var tilemap = gridInfo.tileMaps[i];
                if (tilemap.isTagMap)
                {
                    continue;
                }
                var tilemapMin  = tilemap.min;
                var tilemapSize = tilemap.size;
                mapDataMin.x = LMath.Min(mapDataMin.x, tilemapMin.x);
                mapDataMin.y = LMath.Min(mapDataMin.y, tilemapMin.y);
                mapDataMax.x = LMath.Max(mapDataMax.y, tilemapMin.x + tilemapSize.x);
                mapDataMax.y = LMath.Max(mapDataMax.y, tilemapMin.y + tilemapSize.y);
            }

            mapDataSize = (mapDataMax - mapDataMin) + LVector2Int.one;
            mapDataIds  = new ushort[mapDataSize.x, mapDataSize.y];
            for (int x = 0; x < mapDataSize.x; x++)
            {
                for (int y = 0; y < mapDataSize.y; y++)
                {
                    var pos = new Vector2Int(mapDataMin.x + x, mapDataMin.y + y);
                    mapDataIds[x, y] = RawPos2TileId(pos, true);
                }
            }
        }
Пример #6
0
        public void Execute()
        {
            foreach (var entity in _moveRequest.GetEntities())
            {
                var deltaTime = _gameStateService.DeltaTime;
                var mover     = entity.move;
                var dir       = entity.dir.value;
                var pos       = entity.pos.value;
                var moveSpd   = mover.moveSpd;

                //can move 判定
                var dirVec       = DirUtil.GetDirLVec(dir);
                var moveDist     = (moveSpd * deltaTime);
                var fTargetHead  = pos + (TankUtil.TANK_HALF_LEN + moveDist) * dirVec;
                var fPreviewHead = pos + (TankUtil.TANK_HALF_LEN + TankUtil.FORWARD_HEAD_DIST) * dirVec;

                LFloat maxMoveDist = moveSpd * deltaTime;
                var    headPos     = pos + (TankUtil.TANK_HALF_LEN) * dirVec;
                var    dist        = _gameCollisionService.GetMaxMoveDist(dir, headPos, fTargetHead);
                var    dist2       = _gameCollisionService.GetMaxMoveDist(dir, headPos, fPreviewHead);
                maxMoveDist = LMath.Max(LFloat.zero, LMath.Min(maxMoveDist, dist, dist2));

                var diffPos = maxMoveDist * dirVec;
                pos = pos + diffPos;
                entity.pos.value = pos;
            }
        }
Пример #7
0
        public void DoUpdate(LFloat deltaTime)
        {
            if (!isEnable)
            {
                return;
            }
            if (!TestOnFloor(transform.Pos3))
            {
                isSleep = false;
            }

            lastPos = transform.Pos3;
            lastDeg = transform.deg;
            if (!isSleep)
            {
                if (!isOnFloor)
                {
                    Speed.y -= G * deltaTime;
                    Speed.y  = LMath.Max(MinYSpd, Speed.y);
                }

                var pos = transform.Pos3;
                pos += Speed * deltaTime;
                LFloat y = pos.y;
                //Test floor
                isOnFloor = TestOnFloor(transform.Pos3, ref y);
                if (isOnFloor && Speed.y <= 0)
                {
                    Speed.y = LFloat.zero;
                }

                if (Speed.y <= 0)
                {
                    pos.y = y;
                }

                //Test walls
                if (TestOnWall(ref pos))
                {
                    Speed.x = LFloat.zero;
                    Speed.z = LFloat.zero;
                }

                if (isOnFloor)
                {
                    var speedVal = Speed.magnitude - FloorFriction * deltaTime;
                    speedVal = LMath.Max(speedVal, LFloat.zero);
                    Speed    = Speed.normalized * speedVal;
                    if (speedVal < MinSleepSpeed)
                    {
                        isSleep = true;
                    }
                }

                transform.Pos3 = pos;
            }
        }
Пример #8
0
 public void Init(ColliderPrefab prefab, CTransform2D trans)
 {
     this.Prefab = prefab;
     _bound      = prefab.GetBounds();
     MaxSideSize = LMath.Max(_bound.halfSize.x, _bound.halfSize.y);
     Transform2D = trans;
     _prePos     = Transform2D.pos;
     _preDeg     = Transform2D.deg;
     unchecked {
         Id = autoIncId++;
     }
 }
Пример #9
0
    public static bool CheckCollision(LVector2 posA, LFloat rA, LVector2 sizeA, LVector2 posB, LFloat rB,
                                      LVector2 sizeB)
    {
        var diff      = posA - posB;
        var allRadius = rA + rB;

        //circle 判定
        if (diff.sqrMagnitude > allRadius * allRadius)
        {
            return(false);
        }

        var isBoxA = sizeA != LVector2.zero;
        var isBoxB = sizeB != LVector2.zero;

        if (!isBoxA && !isBoxB)
        {
            return(true);
        }
        var absX = LMath.Abs(diff.x);
        var absY = LMath.Abs(diff.y);

        if (isBoxA && isBoxB)
        {
            //AABB and AABB
            var allSize = sizeA + sizeB;
            if (absX > allSize.x)
            {
                return(false);
            }
            if (absY > allSize.y)
            {
                return(false);
            }
            return(true);
        }
        else
        {
            //AABB & circle
            var size   = sizeB;
            var radius = rA;
            if (isBoxA)
            {
                size   = sizeA;
                radius = rB;
            }

            var x = LMath.Max(absX - size.x, LFloat.zero);
            var y = LMath.Max(absY - size.y, LFloat.zero);
            return(x * x + y * y < radius * radius);
        }
    }
Пример #10
0
        public PtrList(int initSize = AlignSize)
        {
            initSize = LMath.Max(AlignSize, initSize);
            if (initSize % AlignSize != 0)
            {
                initSize = (initSize % AlignSize + 1) * AlignSize;
            }

            capacity   = initSize;
            count      = 0;
            _ptrs      = (void **)NativeHelper.AllocAndZero(sizeof(void *) * initSize);
            _dirtyBits = (byte *)NativeHelper.AllocAndZero(initSize / 8);
        }
Пример #11
0
 private void Update()
 {
     if (delays == null)
     {
         return;
     }
     _guiTimer += Time.deltaTime;
     if (_guiTimer > 0.5f)
     {
         _guiTimer           = 0;
         GameManager.pingVal = (int)(delays.Sum() * 1000 / LMath.Max(delays.Count, 1));
         delays.Clear();
     }
 }
Пример #12
0
 private void Update()
 {
     if (delays == null)
     {
         return;
     }
     _guiTimer += Time.deltaTime;
     if (_guiTimer > 0.5f)
     {
         _guiTimer = 0;
         SimulatorService.PingVal = (int)(delays.Sum() * 1000 / LMath.Max(delays.Count, 1));
         delays.Clear();
     }
 }
Пример #13
0
        public static bool TestCircleOBB(LVector2 posA, LFloat rA, LVector2 posB, LFloat rB, LVector2 sizeB,
                                         LVector2 up)
        {
            var diff      = posA - posB;
            var allRadius = rA + rB;

//circle 判定CollisionHelper
            if (diff.sqrMagnitude > allRadius * allRadius)
            {
                return(false);
            }

//空间转换
            var absX   = LMath.Abs(LVector2.Dot(diff, new LVector2(up.y, -up.x)));
            var absY   = LMath.Abs(LVector2.Dot(diff, up));
            var size   = sizeB;
            var radius = rA;
            var x      = LMath.Max(absX - size.x, LFloat.zero);
            var y      = LMath.Max(absY - size.y, LFloat.zero);

            return(x * x + y * y < radius * radius);
        }
Пример #14
0
        public static bool TestCircleAABB(LVector2 posA, LFloat rA, LVector2 posB, LFloat rB, LVector2 sizeB)
        {
            var diff      = posA - posB;
            var allRadius = rA + rB;

//circle 判定
            if (diff.sqrMagnitude > allRadius * allRadius)
            {
                return(false);
            }

            var absX = LMath.Abs(diff.x);
            var absY = LMath.Abs(diff.y);

//AABB & circle
            var size   = sizeB;
            var radius = rA;
            var x      = LMath.Max(absX - size.x, LFloat.zero);
            var y      = LMath.Max(absY - size.y, LFloat.zero);

            return(x * x + y * y < radius * radius);
        }
Пример #15
0
        public void AddBuff(int id, int ownerId)
        {
            if (id2BuffConfig.TryGetValue(id, out var val))
            {
                var type = val.BigType;
                if (!val.CanMutil)
                {
                    foreach (var va in buffs)
                    {
                        if (va.config.BigType == type)
                        {
                            va.remainTime = LMath.Max(va.config.remainTime, va.remainTime);
                        }
                    }
                }

                var buff = new Buff(ownerId, val);
                AddBuff(buff);
                return;
            }

            Debug.LogError("Miss id " + id);
        }
Пример #16
0
        private void UpdatePingVal(float deltaTime)
        {
            this._pingTimer += deltaTime;
            bool flag = this._pingTimer > 0.5f;

            if (flag)
            {
                this._pingTimer = 0f;
                this.DelayVal   = (int)(this._delays.Sum() / (long)LMath.Max(this._delays.Count, 1));
                this._delays.Clear();
                this.PingVal = (int)(this._pings.Sum() / (long)LMath.Max(this._pings.Count, 1));
                this._pings.Clear();
                bool flag2 = this._minPing < this._historyMinPing && this._simulatorService._gameStartTimestampMs != -1L;
                if (flag2)
                {
                    this._historyMinPing = this._minPing;
                    Debug.LogWarning(string.Format("Recalc _gameStartTimestampMs {0} _guessServerStartTimestamp:{1}", this._simulatorService._gameStartTimestampMs, this._guessServerStartTimestamp), Array.Empty <object>());
                    this._simulatorService._gameStartTimestampMs = LMath.Min(this._guessServerStartTimestamp, this._simulatorService._gameStartTimestampMs);
                }
                this._minPing = long.MaxValue;
                this._maxPing = long.MinValue;
            }
        }
Пример #17
0
        /// <summary>
        ///  判定点是否在多边形内
        /// https://stackoverflow.com/questions/217578/how-can-i-determine-whether-a-2d-point-is-within-a-polygon
        /// </summary>
        public static bool IsPointInPolygon(LVector2 p, LVector2[] polygon)
        {
            var minX = polygon[0]._x;
            var maxX = polygon[0]._x;
            var minY = polygon[0]._y;
            var maxY = polygon[0]._y;

            for (int i = 1; i < polygon.Length; i++)
            {
                LVector2 q = polygon[i];
                minX = LMath.Min(q._x, minX);
                maxX = LMath.Max(q._x, maxX);
                minY = LMath.Min(q._y, minY);
                maxY = LMath.Max(q._y, maxY);
            }

            if (p._x < minX || p._x > maxX || p._y < minY || p._y > maxY)
            {
                return(false);
            }

// http://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html
            bool inside = false;

            for (int i = 0, j = polygon.Length - 1; i < polygon.Length; j = i++)
            {
                if ((polygon[i]._y > p._y) != (polygon[j]._y > p._y) &&
                    p._x < (polygon[j]._x - polygon[i]._x) * (p._y - polygon[i]._y) /
                    (polygon[j]._y - polygon[i]._y) +
                    polygon[i]._x)
                {
                    inside = !inside;
                }
            }

            return(inside);
        }
 public void Execute(ref Transform3D transform3D, ref PlayerData playerData)
 {
     _gameStateService.CurScale = _gameConfigService.InitScale * (playerData.Score.ToLFloat() / 300 + 1);
     transform3D.Scale          = LMath.Max(LFloat.one, _gameStateService.CurScale);
 }