Пример #1
0
        private void HandleCollisions(Tilemap tilemap, Entity collidable)
        {
            var position = collidable.Get<Position>();
            var prevpos = collidable.Get<PreviousPosition>();
            var direction = collidable.Get<Direction>();
            var hotspots = collidable.Get<Hotspots>();

            if (!prevpos)
            {
                for (int i = 0; i < collidable.Count(); ++i)
                {
                    hotspots.BottomHit[i] = hotspots.TopHit[i] = hotspots.RightHit[i] = hotspots.LeftHit[i] = false;
                    if (direction.Y[i] >= 0)
                    {
                        hotspots.BottomHit[i] = HandlePointList(tilemap, position.X[i], ref position.Y[i], ref direction.Y[i], hotspots.Bottom[i]);
                    }
                    else
                    {
                        hotspots.TopHit[i] = HandlePointList(tilemap, position.X[i], ref position.Y[i], ref direction.Y[i], hotspots.Top[i]);
                    }
                    if (direction.X[i] >= 0)
                    {
                        hotspots.RightHit[i] = HandlePointList(tilemap, ref position.X[i], position.Y[i], ref direction.X[i], hotspots.Right[i]);
                    }
                    else if (direction.X[i] <= 0)
                    {
                        hotspots.LeftHit[i] = HandlePointList(tilemap, ref position.X[i], position.Y[i], ref direction.X[i], hotspots.Right[i]);
                    }
                }
            }

            if (prevpos)
            {
                for (int i = 0; i < collidable.Count(); ++i)
                {
                    hotspots.BottomHit[i] = hotspots.TopHit[i] = hotspots.RightHit[i] = hotspots.LeftHit[i] = false;
                    if (direction.Y[i] >= 0)
                    {
                        hotspots.BottomHit[i] = HandlePointList(tilemap, position.X[i], ref position.Y[i], ref direction.Y[i], prevpos.Y[i], hotspots.Bottom[i]);
                    }
                    else
                    {
                        hotspots.TopHit[i] = HandlePointList(tilemap, position.X[i], ref position.Y[i], ref direction.Y[i], prevpos.Y[i], hotspots.Top[i]);
                    }
                    if (direction.X[i] >= 0)
                    {
                        hotspots.RightHit[i] = HandlePointList(tilemap, ref position.X[i], position.Y[i], ref direction.X[i], prevpos.X[i], hotspots.Right[i]);
                    }
                    else if (direction.X[i] <= 0)
                    {
                        hotspots.LeftHit[i] = HandlePointList(tilemap, ref position.X[i], position.Y[i], ref direction.X[i], prevpos.X[i], hotspots.Right[i]);
                    }
                }
            }
        }
Пример #2
0
        private bool HandlePointList(Tilemap tilemap, ref float X, float Y, ref float V, float pX, List<Point> points)
        {
            foreach (var point in points)
            {
                int column = (point.X + (int)X) / tilemap.TileWidth;
                int row = (point.Y + (int)Y) / tilemap.TileHeight;
                int oldColumn = (point.X + (int)pX - 1) / tilemap.TileWidth;

                if (tilemap.ColMaskMat[row][column] != 0)
                {
                    return false;
                }
                if (tilemap.ColMaskMat[row][column] != 0)
                {
                    X = column * tilemap.TileWidth - point.X;
                    V = 0;
                    return true;
                }
            }
            return false;
        }
Пример #3
0
        private void RenderTilemap(Graphics g, Tilemap tilemap, Tilesheet tilesheet, Viewport viewport, Position viewpos)
        {
            int startColumn = (int)(viewpos.X[0] / tilemap.TileWidth);
            int startRow = (int)(viewpos.Y[0] / tilemap.TileHeight);
            int endColumn = (int)((viewpos.X[0] + viewport.Width) / tilemap.TileWidth) + 1;
            int endRow = (int)((viewpos.Y[0] + viewport.Height) / tilemap.TileHeight) + 1;

            startColumn = Math.Max(0, startColumn);
            startRow = Math.Max(0, startRow);
            endRow = Math.Min(tilemap.TileIdMat.Length, endRow);
            endColumn = Math.Min(tilemap.TileIdMat[0].Length, endColumn);

            for (int i = startRow; i < endRow; ++i)
            {
                for (int t = startColumn; t < endColumn; ++t)
                {
                    int id;
                    if ((id = tilemap.TileIdMat[i][t]) > 0)
                    {
                        --id;
                        var dest = new Rectangle(t * tilesheet.TileWidth, i * tilesheet.TileHeight, tilesheet.TileWidth, tilesheet.TileHeight);
                        int row = id / tilesheet.Columns;
                        int column = id % tilesheet.Rows;
                        var src = new Rectangle(column * tilesheet.TileWidth + column * tilesheet.PaddingRight,
                            row * tilesheet.TileHeight + row * tilesheet.PaddingBottom,
                            tilesheet.TileWidth, tilesheet.TileHeight);
                        g.DrawImage(tilesheet.Sheet, dest, src, GraphicsUnit.Pixel);
                        //g.DrawRectangle(Pens.Black, new Rectangle(t * tilemap.TileWidth, i * tilemap.TileHeight, tilemap.TileWidth, tilemap.TileHeight));
                    }
                }
            }
        }