示例#1
0
        /// <summary>
        /// Adds the given entity (non-terrain object) to its recorded location, removing it from the map it is currently a part of.  Returns true if the
        /// entity was added, and false otherwise (eg., collision detection would not allow it, etc.)
        /// </summary>
        /// <param name="entity">Entity to add.</param>
        /// <returns>True if the entity was successfully added to the map, false otherwise.</returns>
        public bool AddEntity(IGameObject entity)
        {
            if (entity.CurrentMap == this)
            {
                return(false);
            }

            if (entity.Layer < 1)
            {
                return(false);
            }

            if (!entity.IsWalkable && (!LayerMasker.HasLayer(LayersBlockingWalkability, entity.Layer) || !WalkabilityView[entity.Position]))
            {
                return(false);
            }

            if (!entity.IsTransparent && !LayerMasker.HasLayer(LayersBlockingTransparency, entity.Layer))
            {
                return(false);
            }

            if (!_entities.Add(entity, entity.Position))
            {
                return(false);
            }

            entity.CurrentMap?.RemoveEntity(entity);
            entity.OnMapChanged(this);
            return(true);
        }
示例#2
0
        /// <summary>
        /// Gets all objects encountered at the given position, in order from the highest existing layer in the layer mask downward.  Layer mask defaults
        /// to all layers.
        /// </summary>
        /// <param name="x">X-value of the position to get objects for.</param>
        /// <param name="y">Y-value of the position to get objects for.</param>
        /// <param name="layerMask">Layer mask for which layers can return an object.  Defaults to all layers.</param>
        /// <returns>All objects encountered at the given position, in order from the highest existing layer in the mask downward.</returns>
        public IEnumerable <IGameObject> GetObjects(int x, int y, uint layerMask = uint.MaxValue)
        {
            foreach (var entity in _entities.GetItems(x, y, layerMask))
            {
                yield return(entity);
            }

            if (LayerMasker.HasLayer(layerMask, 0) && _terrain[x, y] != null)
            {
                yield return(_terrain[x, y]);
            }
        }
示例#3
0
        /// <summary>
        /// Gets all objects encountered at the given position that are castable to type ObjectType, in order from the highest existing layer in the layer
        /// mask downward. Layer mask defaults to all layers.
        /// </summary>
        /// <typeparam name="ObjectType">Type of objects to return.</typeparam>
        /// <param name="x">X-value of the position to get objects for.</param>
        /// <param name="y">Y-value of the position to get objects for.</param>
        /// <param name="layerMask">Layer mask for which layers can return an object.  Defaults to all layers.</param>
        /// <returns>All objects encountered at the given position that are castable to the given type, in order from the highest existing layer
        /// in the mask downward.</returns>
        public IEnumerable <ObjectType> GetObjects <ObjectType>(int x, int y, uint layerMask = uint.MaxValue) where ObjectType : class, IGameObject
        {
            foreach (var entity in _entities.GetItems(x, y, layerMask))
            {
                if (entity is ObjectType e)
                {
                    yield return(e);
                }
            }

            if (LayerMasker.HasLayer(layerMask, 0) && _terrain[x, y] is ObjectType t)
            {
                yield return(t);
            }
        }