Example #1
0
        public bool ReplaceWith <T>(GameActorPosition original, T replacement)
        {
            int x = (int)Math.Floor(original.Position.X);
            int y = (int)Math.Floor(original.Position.Y);

            /*Tile item = GetActorAt(x, y);
             *
             * if (item != original.Actor)
             *  return false;
             */

            Tiles[x][y] = null;
            Tile tileReplacement = replacement as Tile;

            if (replacement == null)
            {
                m_tileCount--;
                return(true);
            }

            if (Tiles[x][y] == null)
            {
                m_tileCount++;
            }

            Tiles[x][y] = tileReplacement;
            return(true);
        }
Example #2
0
 public ISwitchableGameActor SwitchOff(GameActorPosition gameActorPosition, IAtlas atlas)
 {
     TilesetId = AlternativeTextures.Id("Open");
     atlas.MoveToOtherLayer(new GameActorPosition(this, new Vector2(Position), LayerType.OnGroundInteractable), LayerType.ObstacleInteractable);
     m_closed = false;
     return(this);
 }
Example #3
0
        public IEnumerable <GameActorPosition> ActorsAt(Vector2 position, LayerType type = LayerType.All, float width = 0.5f)
        {
            IEnumerable <ILayer <GameActor> > selectedLayers = Layers.Where(t => type.HasFlag(t.LayerType));
            // for all layers except object layer
            IEnumerable <ILayer <GameActor> > selectedTileLayers = selectedLayers.Where(t => LayerType.TileLayers.HasFlag(t.LayerType));

            foreach (ILayer <GameActor> layer in selectedTileLayers)
            {
                GameActorPosition actorPosition = TileAt(layer, position);
                if (actorPosition != null)
                {
                    yield return(actorPosition);
                }
            }

            var circle = new CircleShape(position, width);
            IEnumerable <ILayer <GameActor> > selectedObjectLayers = selectedLayers.Where(t => LayerType.ObjectLayers.HasFlag(t.LayerType));

            foreach (ILayer <GameActor> layer in selectedObjectLayers)
            {
                foreach (IGameObject gameObject in ((IObjectLayer)layer).GetGameObjects())
                {
                    GameActorPosition actorPosition = GameObjectAt(gameObject, circle, position, layer);
                    if (actorPosition != null)
                    {
                        yield return(actorPosition);
                    }
                }
            }
        }
Example #4
0
 public bool MoveToOtherLayer(GameActorPosition actor, LayerType layerType)
 {
     Remove(actor);
     actor.Layer = layerType;
     Add(actor);
     return(true);
 }
Example #5
0
        public void Update(IAtlas atlas)
        {
            List <Vector2I> free = atlas.FreePositionsAround(Position, LayerType.Obstacle | LayerType.Object).ToList();

            if (free.Count == 0)
            {
                return;
            }

            // filter out position with PathTiles
            free = free.Where(x => atlas.ActorsAt((Vector2)x, LayerType.Background).First().Actor.GetType() != typeof(PathTile)).ToList();
            if (free.Count == 0)
            {
                return;
            }

            Vector2I targetPosition = free[m_rng.Next(free.Count)];

            object[]          args          = { targetPosition };
            Fruit             fruit         = (Fruit)Activator.CreateInstance(typeof(T), args);
            GameActorPosition fruitPosition = new GameActorPosition(fruit, new Vector2(targetPosition), LayerType.ObstacleInteractable);

            atlas.Add(fruitPosition);

            NextUpdateAfter = m_updatePeriod;
        }
Example #6
0
 public void Switch(GameActorPosition gameActorPosition, IAtlas atlas, ITilesetTable table)
 {
     var leverOn = new LeverSwitchOn(table, Position);
     atlas.ReplaceWith(new GameActorPosition(this, (Vector2)Position, LayerType.ObstacleInteractable), leverOn);
     if (Switchable == null) return;
     leverOn.Switchable = Switchable.Switch(null, atlas, table);
 }
Example #7
0
 public ISwitchableGameActor Switch(GameActorPosition gameActorPosition, IAtlas atlas, ITilesetTable table)
 {
     RcDoorClosed closedDoor = new RcDoorClosed(table, Position);
     bool added = atlas.Add(new GameActorPosition(closedDoor, (Vector2)Position, LayerType.ObstacleInteractable), true);
     if (!added) return this;
     atlas.Remove(new GameActorPosition(this, (Vector2)Position, LayerType.OnGroundInteractable));
     return closedDoor;
 }
Example #8
0
        private void EatFruit(GameActorPosition applePosition)
        {
            var fruit = applePosition.Actor as Fruit;

            if (fruit is IEatable)
            {
                Energy += ENERGY_FOR_EATING_FRUIT;
            }
        }
Example #9
0
        public override void Resolve(GameActorPosition target, IAtlas atlas, ITilesetTable table)
        {
            ICanPickGameObject picker = Sender as ICanPickGameObject;
            IPickableGameActor pickItem = target.Actor as IPickableGameActor;

            if (picker == null || pickItem == null) return;

            if (picker.AddToInventory(pickItem))
                atlas.Remove(target);
        }
Example #10
0
        public void ReplaceWith(GameActorPosition original, GameActor replacement)
        {
            if ((original.Actor is Tile && replacement is GameObject) ||
                (original.Actor is GameObject && replacement is Tile))
            {
                throw new ArgumentException("atlas.ReplaceWith tries replace Tile with GameObject or GameObject with Tile");
            }

            GetLayer(original.Layer).ReplaceWith(original, replacement);
        }
Example #11
0
 public void Use(GameActorPosition senderPosition, IAtlas atlas, ITilesetTable tilesetTable)
 {
     IEnumerable<GameActorPosition> actorsInFrontOf =
         atlas.ActorsInFrontOf(senderPosition.Actor as ICharacter).ToList();
     if (actorsInFrontOf.Select(x => x.Actor).Contains(Switchable as GameActor))
     {
         GameActorPosition switchable = actorsInFrontOf.First(x => x.Actor == Switchable);
         Switch(switchable, atlas, tilesetTable);
     }
 }
Example #12
0
 public void Switch(GameActorPosition gameActorPosition, IAtlas atlas)
 {
     if (m_On)
     {
         SwitchOff(gameActorPosition, atlas);
     }
     else
     {
         SwitchOn(gameActorPosition, atlas);
     }
 }
Example #13
0
        public void Switch(GameActorPosition gameActorPosition, IAtlas atlas, ITilesetTable table)
        {
            var leverOff = new LeverSwitchOff(table, Position);

            atlas.ReplaceWith(new GameActorPosition(this, (Vector2)Position, LayerType.ObstacleInteractable), leverOff);
            if (Switchable == null)
            {
                return;
            }
            leverOff.Switchable = Switchable.Switch(null, atlas, table) as ISwitchableGameActor;
        }
Example #14
0
        public void Use(GameActorPosition senderPosition, IAtlas atlas)
        {
            IEnumerable <GameActorPosition> actorsInFrontOf =
                atlas.ActorsInFrontOf(senderPosition.Actor as ICharacter).ToList();

            if (actorsInFrontOf.Select(x => x.Actor).Contains(Switchable as GameActor))
            {
                GameActorPosition switchable = actorsInFrontOf.First(x => x.Actor == Switchable);
                Switch(switchable, atlas);
            }
        }
Example #15
0
 public ISwitchableGameActor Switch(GameActorPosition gameActorPosition, IAtlas atlas)
 {
     if (m_closed)
     {
         SwitchOff(gameActorPosition, atlas);
     }
     else
     {
         SwitchOn(gameActorPosition, atlas);
     }
     return(this);
 }
Example #16
0
        private static GameActorPosition TileAt(ILayer <GameActor> layer, Vector2 position)
        {
            GameActor actor = layer.GetActorAt((int)Math.Floor(position.X), (int)Math.Floor(position.Y));

            if (actor == null)
            {
                return(null);
            }
            GameActorPosition actorPosition = new GameActorPosition(actor, position, layer.LayerType);

            return(actorPosition);
        }
Example #17
0
        public ISwitchableGameActor Switch(GameActorPosition gameActorPosition, IAtlas atlas, ITilesetTable table)
        {
            RcDoorClosed closedDoor = new RcDoorClosed(table, Position);
            bool         added      = atlas.Add(new GameActorPosition(closedDoor, (Vector2)Position, LayerType.ObstacleInteractable), true);

            if (!added)
            {
                return(this);
            }
            atlas.Remove(new GameActorPosition(this, (Vector2)Position, LayerType.OnGroundInteractable));
            return(closedDoor);
        }
Example #18
0
        public bool Add(GameActorPosition gameActorPosition)
        {
            IGameObject gameObject = gameActorPosition.Actor as IGameObject;

            Debug.Assert(gameObject != null, "gameObject != null");

            Vector2 position = gameActorPosition.Position;

            gameObject.Position = position;

            GameObjects.Add(gameObject);
            return(true);
        }
Example #19
0
        private static GameActorPosition GameObjectAt(IGameObject gameObject, IShape shape, Vector2 position, ILayer <GameActor> layer)
        {
            IShape gameObjectShape = gameObject.PhysicalEntity.Shape;

            if (!gameObjectShape.CollidesWith(shape))
            {
                return(null);
            }
            GameActor         actor         = (GameActor)gameObject;
            GameActorPosition actorPosition = new GameActorPosition(actor, position, layer.LayerType);

            return(actorPosition);
        }
Example #20
0
        private void RemoveSpeed(GameActorPosition target)
        {
            var actor      = target.Actor as IForwardMovable;
            var gameObject = target.Actor as IGameObject;

            if (actor == null)
            {
                return;
            }
            Debug.Assert(gameObject != null, "gameObject != null");
            Energy            -= actor.ForwardSpeed * gameObject.Weight * ENERGY_COEF_FOR_CATCHING_MOVING_OBJECT;
            actor.ForwardSpeed = 0;
        }
Example #21
0
        public override void Resolve(GameActorPosition target, IAtlas atlas, ITilesetTable table)
        {
            if (target.Actor is Apple || target.Actor is Pear)
            {
                atlas.Remove(target);
            }

            var switcher = target.Actor as ISwitcherGameActor;

            if (switcher != null)
            {
                switcher.Switch(target, atlas, table);
            }
        }
Example #22
0
        public override void Resolve(GameActorPosition target, IAtlas atlas)
        {
            ICanPickGameObject picker   = Sender as ICanPickGameObject;
            IPickableGameActor pickItem = target.Actor as IPickableGameActor;

            if (picker == null || pickItem == null)
            {
                return;
            }

            if (picker.AddToInventory(pickItem))
            {
                atlas.Remove(target);
            }
        }
        public void GoTo(string type, float distance = (float)0.1)
        {
            // ac:GoTo("Pinecone", 1.2)
            GameActorPosition gameActorPosition = GetNearest(type);

            if (gameActorPosition == null)
            {
                throw new Exception("No object of type " + type + " found.");
            }
            Vector2 position = gameActorPosition.Position;

            if (gameActorPosition.Actor is Tile)
            {
                position = Tile.Center((Vector2I)position);
            }
            GoTo(position, distance);
        }
Example #24
0
        public bool ReplaceWith <T>(GameActorPosition original, T replacement)
        {
            GameObject item = (GameObject)original.Actor;

            if (item != original.Actor)
            {
                return(false);
            }

            GameObjects.Remove(item);

            if (!(replacement is GameObject))
            {
                return(true);
            }
            GameObjects.Add(replacement as GameObject);
            return(true);
        }
Example #25
0
        private static void SetTileRelations(IAtlas atlas, Map map)
        {
            ObjectGroup foregroundObjects = map.ObjectGroups.FirstOrDefault(x => x.Name == "ForegroundObject");

            Debug.Assert(foregroundObjects != null, "foregroundObjects != null");
            List <TmxObject>        tmxMapObjects = foregroundObjects.TmxMapObjects;
            IEnumerable <TmxObject> switcherToSwitchablePolylines = tmxMapObjects.Where(x => x.Type == "SwitcherToSwitchable");

            foreach (TmxObject switcherToSwitchablePolyline in switcherToSwitchablePolylines)
            {
                Polyline polyline = switcherToSwitchablePolyline.Polyline;
                if (polyline == null)
                {
                    throw new ArgumentException("Foreground object SwitcherToSwitchable is wrong type. Should be Polyline.");
                }
                List <Vector2> polylinePoints = PolylineTransform(map, switcherToSwitchablePolyline).ToList();
                Vector2        source         = polylinePoints.First();
                Vector2        target         = polylinePoints.Last();
                IEnumerable <GameActorPosition> sourceGameActors = atlas.ActorsAt(source);
                GameActorPosition switcherPosition = sourceGameActors.FirstOrDefault(x => x.Actor is ISwitcherGameActor);
                if (switcherPosition == null)
                {
                    Log.Instance.Error("SwitcherToSwitchable polyline expects Switcher type at [" + source.X + ";" + source.Y + "].");
                    return;
                }
                ISwitcherGameActor switcherGameActor = switcherPosition.Actor as ISwitcherGameActor;

                IEnumerable <GameActorPosition> targetGameActors = atlas.ActorsAt(target);
                GameActorPosition switchablePosition             = targetGameActors.FirstOrDefault(x => x.Actor is ISwitchableGameActor);
                if (switchablePosition == null)
                {
                    Log.Instance.Error("SwitcherToSwitchable polyline expects Switchable type at [" + target.X + ";" + target.Y + "].");
                    return;
                }
                ISwitchableGameActor switchable = switchablePosition.Actor as ISwitchableGameActor;

                if (switcherGameActor != null)
                {
                    switcherGameActor.Switchable = switchable;
                }
            }
        }
Example #26
0
        public override void Resolve(GameActorPosition target, IAtlas atlas)
        {
            ICanPickGameObject picker    = Sender as ICanPickGameObject;
            ICharacter         character = Sender as ICharacter;

            if (picker == null || character == null)
            {
                return;
            }
            Vector2 positionInFrontOf = target.Position;

            // solving case when positions where character should place tile collides with character's position
            if (target.Actor is Tile)
            {
                Tile            tile               = (target.Actor as Tile);
                IPhysicalEntity physicalEntity     = tile.GetPhysicalEntity(new Vector2I(positionInFrontOf));
                bool            collidesWithSource = physicalEntity.CollidesWith(character.PhysicalEntity);
                if (collidesWithSource)
                {
                    /*  <- change to //* to switch
                     * // to the center of current tile
                     * character.Position = Vector2.Floor(character.Position) + Vector2.One/2;
                     *
                     * /*/
                    // back WRT his direction
                    do
                    {
                        character.Position = Physics.Utils.Move(character.Position, character.Direction, -0.01f);
                    } while (physicalEntity.CollidesWith(character.PhysicalEntity));
                    // */
                }
            }

            GameActorPosition toLayDown = new GameActorPosition(target.Actor, positionInFrontOf, target.Layer);

            bool added = atlas.Add(toLayDown, true);

            if (added)
            {
                picker.RemoveFromInventory();
            }
        }
Example #27
0
        public bool Add(GameActorPosition gameActorPosition)
        {
            int x = (int)gameActorPosition.Position.X;
            int y = (int)gameActorPosition.Position.Y;

            if (Tiles[x][y] != null)
            {
                return(false);
            }

            Tile actor = gameActorPosition.Actor as Tile;

            Tiles[x][y] = actor;

            if (actor != null)
            {
                m_tileCount++;
            }

            return(true);
        }
Example #28
0
        public bool Add(GameActorPosition gameActorPosition, bool collidesWithObstacles = false)
        {
            if (collidesWithObstacles)
            {
                IObjectLayer gameObjectLayer = GetLayer(LayerType.Object) as IObjectLayer;
                Debug.Assert(gameObjectLayer != null, "gameObjectLayer != null");
                Vector2         position = gameActorPosition.Position;
                GameActor       actor    = gameActorPosition.Actor;
                IPhysicalEntity physicalEntity;
                if (actor is IGameObject)
                {
                    physicalEntity          = (actor as IGameObject).PhysicalEntity;
                    physicalEntity.Position = position;
                }
                else if (actor is Tile)
                {
                    physicalEntity = (actor as Tile).GetPhysicalEntity(new Vector2I(gameActorPosition.Position));
                }
                else
                {
                    throw new ArgumentException("actor is unknown Type");
                }
                bool anyCollision = gameObjectLayer.GetPhysicalEntities().Any(x => x.CollidesWith(physicalEntity));
                if (anyCollision)
                {
                    return(false);
                }
                bool anyObstacleOnPosition =
                    GetObstaceLayers().Any(x => x.GetActorAt(new Vector2I(gameActorPosition.Position)) != null);
                if (anyObstacleOnPosition)
                {
                    return(false);
                }
            }

            ILayer <GameActor> layer = GetLayer(gameActorPosition.Layer);

            return(layer.Add(gameActorPosition));
        }
Example #29
0
        public override void Resolve(GameActorPosition target, IAtlas atlas, ITilesetTable table)
        {
            ICanPickGameObject picker = Sender as ICanPickGameObject;
            ICharacter character = Sender as ICharacter;
            if (picker == null || character == null) return;
            Vector2 positionInFrontOf = target.Position;

            // solving case when positions where character should place tile collides with character's position
            if (target.Actor is Tile)
            {
                Tile tile = (target.Actor as Tile);
                IPhysicalEntity physicalEntity = tile.GetPhysicalEntity(new Vector2I(positionInFrontOf));
                bool collidesWithSource = physicalEntity.CollidesWith(character.PhysicalEntity);
                if (collidesWithSource)
                {
                    /*  <- change to //* to switch
                    // to the center of current tile
                    character.Position = Vector2.Floor(character.Position) + Vector2.One/2;
                    
                    /*/
                    // back WRT his direction
                    do
                    {
                        character.Position = Physics.Utils.Move(character.Position, character.Direction, -0.01f);
                    } while (physicalEntity.CollidesWith(character.PhysicalEntity));
                    // */
                }
            }

            GameActorPosition toLayDown = new GameActorPosition(target.Actor, positionInFrontOf, target.Layer);

            bool added = atlas.Add(toLayDown, true);
            if (added)
            {
                picker.RemoveFromInventory();
            }
        }
Example #30
0
 /// <summary>
 /// Resolve implements default action implementation (where applicable)
 /// </summary>
 /// <param name="target">Target of the action</param>
 /// <param name="atlas"></param>
 /// <param name="tilesetTable"></param>
 public abstract void Resolve(GameActorPosition target, IAtlas atlas, ITilesetTable tilesetTable);
Example #31
0
 public override void Resolve(GameActorPosition target, IAtlas atlas, ITilesetTable table)
 {
     throw new System.NotImplementedException();
 }
Example #32
0
 /// <summary>
 /// Resolve implements default action implementation (where applicable)
 /// </summary>
 /// <param name="target">Target of the action</param>
 /// <param name="atlas"></param>
 /// <param name="tilesetTable"></param>
 public abstract void Resolve(GameActorPosition target, IAtlas atlas, ITilesetTable tilesetTable);
Example #33
0
        private void EatFruit(GameActorPosition applePosition)
        {
            var fruit = applePosition.Actor as Fruit;

            if (fruit is IEatable)
            {
                Energy += ENERGY_FOR_EATING_FRUIT;
            }
        }
Example #34
0
 public void Switch(GameActorPosition gameActorPosition, IAtlas atlas)
 {
     Switchable = Switchable?.SwitchOn(null, atlas);
 }
Example #35
0
 private void RemoveSpeed(GameActorPosition target)
 {
     var actor = target.Actor as IForwardMovable;
     var gameObject = target.Actor as IGameObject;
     if (actor == null) return;
     Debug.Assert(gameObject != null, "gameObject != null");
     Energy -= actor.ForwardSpeed * gameObject.Weight * ENERGY_COEF_FOR_CATCHING_MOVING_OBJECT;
     actor.ForwardSpeed = 0;
 }
Example #36
0
 public void Burn(GameActorPosition gameActorPosition, IAtlas atlas, ITilesetTable table)
 {
     Ignite(atlas, table);
 }
Example #37
0
 public void Burn(GameActorPosition gameActorPosition, IAtlas atlas)
 {
     atlas.Remove(gameActorPosition);
 }
Example #38
0
 public void Remove(GameActorPosition target)
 {
     ReplaceWith(target, null);
 }
Example #39
0
 public void Switch(GameActorPosition gameActorPosition, IAtlas atlas, ITilesetTable table)
 {
     Switchable = Switchable.Switch(null, atlas, table);
 }
Example #40
0
 public void Burn(GameActorPosition gameActorPosition, IAtlas atlas, ITilesetTable table)
 {
     Ignite(atlas, table);
 }
Example #41
0
 public void Burn(GameActorPosition gameActorPosition, IAtlas atlas, ITilesetTable table)
 {
     atlas.Remove(gameActorPosition);
 }
Example #42
0
 /// <summary>
 /// Resolve implements default action implementation (where applicable)
 /// </summary>
 /// <param name="target">Target of the action</param>
 /// <param name="atlas"></param>
 public abstract void Resolve(GameActorPosition target, IAtlas atlas);