Exemple #1
0
        public bool Tick(Location location, Entity entity)
        {
            ImageIndex = direction + ((int)walkIndex % walkCycle);

            if (!IsDead() && balance < 100 && (!animations.Any() || !animations.Peek().Peek().Name.Contains("back")))
            {
                balance++;
            }

            if (stamina < 30)
            {
                stamina += 1.0f / Program.TPS / 2.5f;
            }

            SkillBook.Tick();

            if (combo.IsStarted() && ((animations.Any() && !(animations.Peek().Peek() is AttackAnimation)) || !animations.Any()) && combo.Tick())
            {
                combo.Reset();
            }

            if (stun > 0)
            {
                stun--;
            }

            if (PreppedSkill != null)
            {
                skillActivation.Tick(this);
            }

            if (animations.Any())
            {
                if (stun > 0 && animations.Peek().Peek().IsInterruptable())
                {
                    return(true);
                }

                AnimationChain chain = animations.Peek();
                if (animations.Peek().Tick(this))
                {
                    if (chain == animations.Peek())
                    {
                        animations.Peek().Pop();
                    }

                    if (!animations.Peek().Any())
                    {
                        animations.Pop();
                    }
                }

                if (animations.Any() && animations.Peek().Peek().IsPausing())
                {
                    return(true);
                }
            }

            if (health <= 0)
            {
                return(true);
            }

            return(false);
        }
Exemple #2
0
        public static LayeredTileMap FromTiledMapSave(string tiledMapSaveFile, string contentManager, TiledMapSave tms)
        {
            // Ultimately properties are tied to tiles by the tile name.
            // If a tile has no name but it has properties, those properties
            // will be lost in the conversion. Therefore, we have to add name properties.
            tms.MoveTypeToProperties();

#if DEBUG
            CheckForDuplicateTilesets(tms);
#endif

            tms.NameUnnamedTilesetTiles();
            tms.NameUnnamedObjects();


            string directory = FlatRedBall.IO.FileManager.GetDirectory(tiledMapSaveFile);

            var rtmi = ReducedTileMapInfo.FromTiledMapSave(
                tms, 1, 0, directory, FileReferenceType.Absolute);

            var toReturn = FromReducedTileMapInfo(rtmi, contentManager, tiledMapSaveFile);

            AddShapeCollections(toReturn, tms);

            foreach (var layer in tms.MapLayers)
            {
                var matchingLayer = toReturn.MapLayers.FirstOrDefault(item => item.Name == layer.Name);


                if (matchingLayer != null)
                {
                    if (layer is MapLayer)
                    {
                        var mapLayer = layer as MapLayer;
                        foreach (var propertyValues in mapLayer.properties)
                        {
                            matchingLayer.Properties.Add(new NamedValue
                            {
                                Name  = propertyValues.StrippedName,
                                Value = propertyValues.value,
                                Type  = propertyValues.Type
                            });
                        }

                        matchingLayer.Visible = mapLayer.visible == 1;
                        matchingLayer.Alpha   = mapLayer.Opacity;
                    }
                }
            }


            foreach (var tileset in tms.Tilesets)
            {
                foreach (var tile in tileset.TileDictionary.Values)
                {
                    int propertyCountFromTileset = 0;

                    if (tile.properties.Count != 0)
                    {
                        // this needs a name:
                        string name = tile.properties.FirstOrDefault(item => item.StrippedName.ToLowerInvariant() == "name")?.value;
                        // todo - eventually need to copy default values from the Tileset to the tile here
                        AddPropertiesToMap(tms, toReturn.TileProperties, tile.properties, null, name);
                    }
                }
            }

            foreach (var objectLayer in tms.objectgroup)
            {
                if (objectLayer.@object != null)
                {
                    foreach (var objectInstance in objectLayer.@object)
                    {
                        TMXGlueLib.Tileset tileset   = null;
                        int propertyCountFromTileset = 0;

                        var             objectProperties  = objectInstance.properties;
                        List <property> tilesetProperties = null;
                        if (objectInstance.gid != null)
                        {
                            var gidNoFlip = objectInstance.GidNoFlip;

                            tileset = tms.GetTilesetForGid(gidNoFlip.Value);
                            if (tileset.TileDictionary.ContainsKey(gidNoFlip.Value - tileset.Firstgid))
                            {
                                tilesetProperties        = tileset.TileDictionary[gidNoFlip.Value - tileset.Firstgid].properties;
                                propertyCountFromTileset = tilesetProperties.Count;
                            }
                        }


                        if (objectProperties.Count + propertyCountFromTileset != 0)
                        {
                            string name = objectInstance.Name;
                            // if name is null, check the properties:
                            if (string.IsNullOrEmpty(name))
                            {
                                name = objectProperties.FirstOrDefault(item => item.StrippedNameLower == "name")?.value;
                            }

                            var objectInstanceIsTile = objectInstance.gid != null;

                            if (objectInstanceIsTile)
                            {
                                AddPropertiesToMap(tms, toReturn.TileProperties, objectProperties, tilesetProperties, name);
                            }
                            else
                            {
                                AddPropertiesToMap(tms, toReturn.ShapeProperties, objectProperties, tilesetProperties, name);
                            }
                        }
                    }
                }
            }

            var tmxDirectory = FileManager.GetDirectory(tiledMapSaveFile);

            // add image layers
            foreach (var imageLayer in tms.ImageLayers)
            {
                var imageLayerFile = tmxDirectory + imageLayer.ImageObject.Source;
                var texture        = FlatRedBallServices.Load <Microsoft.Xna.Framework.Graphics.Texture2D>(imageLayerFile);

                var newSprite = new Sprite
                {
                    Texture = texture,
                    Width   = imageLayer.ImageObject.Width,
                    Height  = imageLayer.ImageObject.Height,
                    X       = imageLayer.ImageObject.Width / 2 + imageLayer.OffsetX,
                    Y       = -imageLayer.ImageObject.Height / 2 + imageLayer.OffsetY
                };

                var mdb = new MapDrawableBatch(1, texture);
                mdb.Alpha = imageLayer.Opacity;
                mdb.AttachTo(toReturn, false);
                mdb.Paste(newSprite);
                mdb.Visible = imageLayer.Visible;

                toReturn.mMapLists.Add(mdb);
            }

            var animationDictionary = new Dictionary <string, AnimationChain>();

            // add animations
            foreach (var tileset in tms.Tilesets)
            {
                string tilesetImageFile = tmxDirectory + tileset.Images[0].Source;

                if (tileset.SourceDirectory != ".")
                {
                    tilesetImageFile = tmxDirectory + tileset.SourceDirectory + tileset.Images[0].Source;
                }

                var texture = FlatRedBallServices.Load <Microsoft.Xna.Framework.Graphics.Texture2D>(tilesetImageFile, contentManager);

                foreach (var tile in tileset.Tiles.Where(item => item.Animation != null && item.Animation.Frames.Count != 0))
                {
                    var animation = tile.Animation;

                    var animationChain = new AnimationChain();
                    foreach (var frame in animation.Frames)
                    {
                        var animationFrame = new AnimationFrame();
                        animationFrame.FrameLength = frame.Duration / 1000.0f;
                        animationFrame.Texture     = texture;

                        int tileIdRelative = frame.TileId;
                        int globalTileId   = (int)(tileIdRelative + tileset.Firstgid);

                        int leftPixel;
                        int rightPixel;
                        int topPixel;
                        int bottomPixel;
                        TiledMapSave.GetPixelCoordinatesFromGid((uint)globalTileId, tileset, out leftPixel, out topPixel, out rightPixel, out bottomPixel);

                        animationFrame.LeftCoordinate  = MapDrawableBatch.CoordinateAdjustment + leftPixel / (float)texture.Width;
                        animationFrame.RightCoordinate = -MapDrawableBatch.CoordinateAdjustment + rightPixel / (float)texture.Width;

                        animationFrame.TopCoordinate    = MapDrawableBatch.CoordinateAdjustment + topPixel / (float)texture.Height;
                        animationFrame.BottomCoordinate = -MapDrawableBatch.CoordinateAdjustment + bottomPixel / (float)texture.Height;


                        animationChain.Add(animationFrame);
                    }

                    var property = tile.properties.FirstOrDefault(item => item.StrippedNameLower == "name");

                    if (property == null)
                    {
                        throw new InvalidOperationException(
                                  $"The tile with ID {tile.id} has an animation, but it doesn't have a Name property, which is required for animation.");
                    }
                    else
                    {
                        animationDictionary.Add(property.value, animationChain);
                    }
                }
            }

            toReturn.Animation = new LayeredTileMapAnimation(animationDictionary);

            AddTileShapeCollections(toReturn, tms, separateOnTileType: true);


            toReturn.MapProperties = tms.properties
                                     .Select(propertySave => new NamedValue
            {
                Name = propertySave.name, Value = propertySave.value, Type = propertySave.Type
            })
                                     .ToList();


            return(toReturn);
        }
Exemple #3
0
 public void SetOverlayAnimationChain(AnimationChain achToSet)
 {
     throw new NotImplementedException();
     //mSprite.SetAnimationChain(achToSet, TimeManager.CurrentTime);
 }
Exemple #4
0
        /// <summary>
        /// Moves the player one unit in a specific direction.
        /// </summary>
        /// <param name="engine">The current engine state.</param>
        /// <param name="direction">A unit direction to move.</param>
        public void Walk(Engine engine, Vector3 direction)
        {
            bool _downLadder = false;

            if (AutoControl)
            {
                if (LadderVector != Vector3.Zero)
                {
                    if (Block.VectorToSide[LadderVector] < 4)
                    {
                        if (direction == LadderVector)
                        {
                            if (!Cube.IsMoveLadderGrab(engine, GridPosition + direction))
                            {
                                if (Move(engine, GridPosition - Vector3.UnitZ))
                                {
                                    animation.SetAnimation(Block.VectorToSide[direction] + 4);
                                    _downLadder = true;
                                }
                            }
                        }
                    }
                }
            }

            if (!_downLadder)
            {
                // Tries moving the player.
                if (!Move(engine, GridPosition + direction))
                {
                    // If failed, then looks for a ladder.
                    int _side = Block.VectorToSide[direction];

                    // Gets the block the player would run into.
                    Block _b = engine.room.GetGridBlockSafe(GridPosition + direction);

                    // If the block exists.
                    if (_b != null)
                    {
                        // If the side is a ladder side.
                        if (_b.SideProperty[_side] == 1)
                        {
                            // Walk up ladder.
                            if (Move(engine, GridPosition + Vector3.UnitZ))
                            {
                                animation.SetAnimation(Block.VectorToSide[-direction] + 4);
                                MovementChain.Enqueue(direction);
                                AnimationChain.Enqueue(Block.VectorToSide[-direction]);
                            }
                        }
                    }
                }
                // If moving properly, then set the animation.
                else
                {
                    if (LadderVector == Vector3.Zero)
                    {
                        animation.SetAnimation(Block.VectorToSide[-direction]);
                    }
                    else
                    {
                        int _dir = Block.VectorToSide[LadderVector];
                        if (_dir < 4)
                        {
                            animation.SetAnimation(_dir + 4);
                        }
                        else
                        {
                            animation.SetAnimation(Block.VectorToSide[-direction] + 13);
                        }
                    }
                }
            }
        }
        public static LayeredTileMap FromTiledMapSave(string fileName, string contentManager)
        {
            TiledMapSave tms = TiledMapSave.FromFile(fileName);

            // Ultimately properties are tied to tiles by the tile name.
            // If a tile has no name but it has properties, those properties
            // will be lost in the conversion. Therefore, we have to add name properties.
            tms.NameUnnamedTilesetTiles();
            tms.NameUnnamedObjects();


            string directory = FlatRedBall.IO.FileManager.GetDirectory(fileName);

            var rtmi = ReducedTileMapInfo.FromTiledMapSave(
                tms, 1, 0, directory, FileReferenceType.Absolute);

            var toReturn = FromReducedTileMapInfo(rtmi, contentManager, fileName);


            foreach (var mapObjectgroup in tms.objectgroup)
            {
                int indexInAllLayers = tms.MapLayers.IndexOf(mapObjectgroup);

                var shapeCollection = tms.ToShapeCollection(mapObjectgroup.Name);
                if (shapeCollection != null && shapeCollection.IsEmpty == false)
                {
                    // This makes all shapes have the same Z as the index layer, which is useful if instantiating objects, so they're layered properly
                    shapeCollection.Shift(new Microsoft.Xna.Framework.Vector3(0, 0, indexInAllLayers));

                    shapeCollection.Name = mapObjectgroup.Name;
                    toReturn.ShapeCollections.Add(shapeCollection);
                }
            }

            foreach (var layer in tms.MapLayers)
            {
                var matchingLayer = toReturn.MapLayers.FirstOrDefault(item => item.Name == layer.Name);


                if (matchingLayer != null)
                {
                    if (layer is MapLayer)
                    {
                        var mapLayer = layer as MapLayer;
                        foreach (var propertyValues in mapLayer.properties)
                        {
                            matchingLayer.Properties.Add(new NamedValue
                            {
                                Name  = propertyValues.StrippedName,
                                Value = propertyValues.value,
                                Type  = propertyValues.Type
                            });
                        }

                        matchingLayer.Visible = mapLayer.visible == 1;
                    }
                }
            }


            foreach (var tileset in tms.Tilesets)
            {
                foreach (var tile in tileset.TileDictionary.Values)
                {
                    if (tile.properties.Count != 0)
                    {
                        // this needs a name:
                        string name = tile.properties.FirstOrDefault(item => item.StrippedName.ToLowerInvariant() == "name")?.value;
                        AddPropertiesToMap(tms, toReturn.TileProperties, tile.properties, name);
                    }
                }
            }

            foreach (var objectLayer in tms.objectgroup)
            {
                if (objectLayer.@object != null)
                {
                    foreach (var objectInstance in objectLayer.@object)
                    {
                        if (objectInstance.properties.Count != 0)
                        {
                            string name = objectInstance.Name;
                            // if name is null, check the properties:
                            if (string.IsNullOrEmpty(name))
                            {
                                name = objectInstance.properties.FirstOrDefault(item => item.StrippedNameLower == "name")?.value;
                            }
                            var properties = objectInstance.properties;

                            var objectInstanceIsTile = objectInstance.gid != null;

                            if (objectInstanceIsTile)
                            {
                                AddPropertiesToMap(tms, toReturn.TileProperties, properties, name);
                            }
                            else
                            {
                                AddPropertiesToMap(tms, toReturn.ShapeProperties, properties, name);
                            }
                        }
                    }
                }
            }

            var tmxDirectory = FileManager.GetDirectory(fileName);

            var animationDictionary = new Dictionary <string, AnimationChain>();

            // add animations
            foreach (var tileset in tms.Tilesets)
            {
                string tilesetImageFile = tmxDirectory + tileset.Images[0].Source;

                if (tileset.SourceDirectory != ".")
                {
                    tilesetImageFile = tmxDirectory + tileset.SourceDirectory + tileset.Images[0].Source;
                }

                var texture = FlatRedBallServices.Load <Microsoft.Xna.Framework.Graphics.Texture2D>(tilesetImageFile);

                foreach (var tile in tileset.Tiles.Where(item => item.Animation != null && item.Animation.Frames.Count != 0))
                {
                    var animation = tile.Animation;

                    var animationChain = new AnimationChain();
                    foreach (var frame in animation.Frames)
                    {
                        var animationFrame = new AnimationFrame();
                        animationFrame.FrameLength = frame.Duration / 1000.0f;
                        animationFrame.Texture     = texture;

                        int tileIdRelative = frame.TileId;
                        int globalTileId   = (int)(tileIdRelative + tileset.Firstgid);

                        int leftPixel;
                        int rightPixel;
                        int topPixel;
                        int bottomPixel;
                        TiledMapSave.GetPixelCoordinatesFromGid((uint)globalTileId, tileset, out leftPixel, out topPixel, out rightPixel, out bottomPixel);

                        animationFrame.LeftCoordinate  = MapDrawableBatch.CoordinateAdjustment + leftPixel / (float)texture.Width;
                        animationFrame.RightCoordinate = -MapDrawableBatch.CoordinateAdjustment + rightPixel / (float)texture.Width;

                        animationFrame.TopCoordinate    = MapDrawableBatch.CoordinateAdjustment + topPixel / (float)texture.Height;
                        animationFrame.BottomCoordinate = -MapDrawableBatch.CoordinateAdjustment + bottomPixel / (float)texture.Height;


                        animationChain.Add(animationFrame);
                    }

                    var property = tile.properties.FirstOrDefault(item => item.StrippedNameLower == "name");

                    if (property == null)
                    {
                        throw new InvalidOperationException(
                                  $"The tile with ID {tile.id} has an animation, but it doesn't have a Name property, which is required for animation.");
                    }
                    else
                    {
                        animationDictionary.Add(property.value, animationChain);
                    }
                }
            }

            toReturn.Animation = new LayeredTileMapAnimation(animationDictionary);

            toReturn.MapProperties = tms.properties
                                     .Select(propertySave => new NamedValue
            {
                Name = propertySave.name, Value = propertySave.value, Type = propertySave.Type
            })
                                     .ToList();


            return(toReturn);
        }
 public AnimationChainContainer(AnimationChain animationChain)
 {
     this.AnimationChain = animationChain;
     AnimationSpeed      = 1;
     CurrentFrameIndex   = 1;
 }