Exemplo n.º 1
0
        public void UpdateLists()
        {
            // handle removals
            if (_entitiesToRemove.Count > 0)
            {
                Utils.Swap(ref _entitiesToRemove, ref _tempEntityList);
                foreach (var entity in _tempEntityList)
                {
                    // handle the tagList
                    RemoveFromTagList(entity);

                    // handle the regular entity list
                    _entities.Remove(entity);
                    entity.OnRemovedFromScene();
                    entity.Scene = null;

                    if (Core.entitySystemsEnabled)
                    {
                        Scene.EntityProcessors.OnEntityRemoved(entity);
                    }
                }

                _tempEntityList.Clear();
            }

            // handle additions
            if (_entitiesToAdd.Count > 0)
            {
                Utils.Swap(ref _entitiesToAdd, ref _tempEntityList);
                foreach (var entity in _tempEntityList)
                {
                    _entities.Add(entity);
                    entity.Scene = Scene;

                    // handle the tagList
                    AddToTagList(entity);

                    if (Core.entitySystemsEnabled)
                    {
                        Scene.EntityProcessors.OnEntityAdded(entity);
                    }
                }

                // now that all entities are added to the scene, we loop through again and call onAddedToScene
                foreach (var entity in _tempEntityList)
                {
                    entity.OnAddedToScene();
                }

                _tempEntityList.Clear();
                _isEntityListUnsorted = true;
            }

            if (_isEntityListUnsorted)
            {
                _entities.Sort();
                _isEntityListUnsorted = false;
            }

            // sort our tagList if needed
            if (_unsortedTags.Count > 0)
            {
                foreach (var tag in _unsortedTags)
                {
                    _entityDict[tag].Sort();
                }
                _unsortedTags.Clear();
            }
        }
Exemplo n.º 2
0
        void CalculateVertices()
        {
            if (!_areVertsDirty || _points.Length < 2)
            {
                return;
            }

            _areVertsDirty = false;
            _indices.Reset();
            _vertices.Reset();

            var maxX = float.MinValue;
            var minX = float.MaxValue;
            var maxY = float.MinValue;
            var minY = float.MaxValue;

            if (_useStartEndWidths)
            {
                _maxWidth = System.Math.Max(_startWidth, _endWidth);
            }

            // calculate line length first and simulataneously get our min/max points for the bounds
            var lineLength   = 0f;
            var halfMaxWidth = _maxWidth * 0.5f;

            _points.Buffer[0].LengthFromPreviousPoint = 0;
            for (var i = 0; i < _points.Length - 1; i++)
            {
                var distance = Vector2.Distance(_points.Buffer[i].Position, _points.Buffer[i + 1].Position);
                _points.Buffer[i + 1].LengthFromPreviousPoint = distance;
                lineLength += distance;

                maxX = Mathf.MaxOf(maxX, _points.Buffer[i].Position.X + halfMaxWidth,
                                   _points.Buffer[i + 1].Position.X + halfMaxWidth);
                minX = Mathf.MinOf(minX, _points.Buffer[i].Position.X - halfMaxWidth,
                                   _points.Buffer[i + 1].Position.X - halfMaxWidth);
                maxY = Mathf.MaxOf(maxY, _points.Buffer[i].Position.Y + halfMaxWidth,
                                   _points.Buffer[i + 1].Position.Y + halfMaxWidth);
                minY = Mathf.MinOf(minY, _points.Buffer[i].Position.Y - halfMaxWidth,
                                   _points.Buffer[i + 1].Position.Y - halfMaxWidth);
            }

            _bounds.X      = minX;
            _bounds.Y      = minY;
            _bounds.Width  = maxX - minX;
            _bounds.Height = maxY - minY;

            // special case: single segment
            if (_points.Length == 2)
            {
                if (_useStartEndWidths)
                {
                    _points.Buffer[0].Width = _startWidth;
                    _points.Buffer[1].Width = _endWidth;
                }

                if (_useStartEndColors)
                {
                    _points.Buffer[0].Color = _startColor;
                    _points.Buffer[1].Color = _endColor;
                }

                _firstSegment.SetPoints(ref _points.Buffer[0], ref _points.Buffer[1]);
                AddSingleSegmentLine(ref _firstSegment, _points.Buffer[1].Color);
                return;
            }

            var distanceSoFar = 0f;
            var fusedPoint    = Vector2.Zero;
            var vertIndex     = 0;
            var thirdPoint    = new SegmentPoint();

            for (var i = 0; i < _points.Length - 1; i++)
            {
                var firstPoint  = _points.Buffer[i];
                var secondPoint = _points.Buffer[i + 1];

                var hasThirdPoint = _points.Length > i + 2;
                if (hasThirdPoint)
                {
                    thirdPoint = _points.Buffer[i + 2];
                }

                // we need the distance along the line of both the first and second points. distanceSoFar will always be for the furthest point
                // which is the previous point before adding the current segment distance.
                var firstPointDistance = distanceSoFar;
                distanceSoFar += secondPoint.LengthFromPreviousPoint;

                var firstPointRatio  = firstPointDistance / lineLength;
                var secondPointRatio = distanceSoFar / lineLength;
                var thirdPointRatio  = 0f;
                if (hasThirdPoint)
                {
                    thirdPointRatio = (distanceSoFar + thirdPoint.LengthFromPreviousPoint) / lineLength;
                }

                if (_useStartEndColors)
                {
                    ColorExt.Lerp(ref _startColor, ref _endColor, out firstPoint.Color, firstPointRatio);
                    ColorExt.Lerp(ref _startColor, ref _endColor, out secondPoint.Color, secondPointRatio);

                    if (hasThirdPoint)
                    {
                        ColorExt.Lerp(ref _startColor, ref _endColor, out thirdPoint.Color, thirdPointRatio);
                    }
                }

                if (_useStartEndWidths)
                {
                    firstPoint.Width  = Mathf.Lerp(_startWidth, _endWidth, firstPointRatio);
                    secondPoint.Width = Mathf.Lerp(_startWidth, _endWidth, secondPointRatio);

                    if (hasThirdPoint)
                    {
                        thirdPoint.Width = Mathf.Lerp(_startWidth, _endWidth, thirdPointRatio);
                    }
                }


                if (i == 0)
                {
                    _firstSegment.SetPoints(ref firstPoint, ref secondPoint);
                    _secondSegment.SetPoints(ref secondPoint, ref thirdPoint);
                }
                else
                {
                    Utils.Swap(ref _firstSegment, ref _secondSegment);
                    if (hasThirdPoint)
                    {
                        _secondSegment.SetPoints(ref secondPoint, ref thirdPoint);
                    }
                }

                // dont recalculate the fusedPoint for the last segment since there will be no third point to work with
                if (hasThirdPoint)
                {
                    var shouldFuseBottom =
                        Vector2Ext.IsTriangleCCW(firstPoint.Position, secondPoint.Position, thirdPoint.Position);
                    _secondSegment.SetFusedData(shouldFuseBottom, ref _firstSegment);
                }

                // special care needs to be take with the first segment since it has a different vert count
                if (i == 0)
                {
                    AddFirstSegment(ref _firstSegment, ref _secondSegment, ref vertIndex);
                }
                else
                {
                    AddSegment(ref _firstSegment, ref vertIndex);
                }

                _lastSegment.CloneFrom(ref _firstSegment);
            }
        }