Exemple #1
0
        public void UpdateCollisionInArea(Pos pos)
        {
            //TODO use rects

            //Debug.Log("GenerateCollisionInArea: " + pos.x + " , " + pos.y + ", areaWidth: " + _parameters.areaWidth + ", areaHeight:" + _parameters.areaHeight);
            var rect = new Rect(pos.x - _parameters.areaWidth / 2f, pos.y - _parameters.areaHeight / 2f, _parameters.areaWidth, _parameters.areaHeight);

            //_lastRect

            for (int x = (int)rect.xMin; x < (int)rect.xMax; x++)
            {
                for (int y = (int)rect.yMin; y < (int)rect.yMax; y++)
                {
                    var  tilePos     = new Pos(x, y);
                    var  blockType   = _mapInfo.GetBlockAt(tilePos);
                    bool hasCollider = _colliders.ContainsKey(tilePos);
                    if (blockType == BlockType.EMPTY)
                    {
                        if (hasCollider)
                        {
                            var coll = _colliders[tilePos];
                            _colliderPool.Return(coll);
                            _colliders.Remove(tilePos);
                        }
                        continue;
                    }
                    if (hasCollider)
                    {
                        continue;
                    }

                    var collInst = _colliderPool.Get();
                    collInst.offset = new Vector2(x, y) + Vector2.one * 0.5f;
                    collInst.size   = Vector2.one;

                    _colliders.Add(new Pos(x, y), collInst);
                }
            }

            _lastRect = rect;
        }
        private List <Pos> GetTilesOnBresenhamLine(int xStart, int yStart, int xEnd, int yEnd)
        {
            var tilesOnLine = new List <Pos>();

            int x, y, sx = 0, sy = 0;

            int dx = Mathf.Abs(xEnd - xStart);
            int dy = Mathf.Abs(yEnd - yStart);

            if (xStart < xEnd)
            {
                sx = 1;
            }
            else if (xStart > xEnd)
            {
                sx = -1;
            }
            if (yStart < yEnd)
            {
                sy = 1;
            }
            else if (yStart > yEnd)
            {
                sy = -1;
            }
            int err  = dx - dy;
            int err2 = 0;

            x = xStart;
            y = yStart;

            while (true)
            {
                tilesOnLine.Add(new Pos(x, y));

                //var blockType = _mapInfo.GetBlockAt(x, y);
                if (x == xEnd && y == yEnd || (int)_mapInfo.GetBlockAt(x, y) >= (int)BlockType.IMPASSABLE)
                {
                    break;                                                                                                        //has reached goal?
                }
                err2 = err * 2;
                if (err2 > -dy)
                {
                    err = err - dy;
                    x   = x + sx;

                    tilesOnLine.Add(new Pos(x, y + sy));
                    if (_mapInfo.GetBlockAt(x, y + sy) >= BlockType.IMPASSABLE)
                    {
                        break;
                    }
                }
                if (x == xEnd && y == yEnd || (int)_mapInfo.GetBlockAt(x, y) >= (int)BlockType.IMPASSABLE)                  //has reached goal now???
                {
                    tilesOnLine.Add(new Pos(x, y));
                    break;
                }
                if (err2 < dx)
                {
                    err = err + dx;
                    y   = y + sy;

                    tilesOnLine.Add(new Pos(x + sx, y));
                    if ((int)_mapInfo.GetBlockAt(x + sx, y) >= (int)BlockType.IMPASSABLE)
                    {
                        break;
                    }
                }
            }
            return(tilesOnLine);
        }