Esempio n. 1
0
        private void SetTile(Tilemap tilemap, DungeonTile tile, int tid, Direction direction = Direction.Down)
        {
            if (tile.Anim != null)
            {
                var sprite = new Animator(tile.Anim);
                switch (tile.Type)
                {
                case DungeonTileType.Ground:
                    sprite.Layer = Global.LayerMapGround;
                    break;

                case DungeonTileType.Wall:
                    sprite.Layer = Global.LayerMapWall;
                    break;

                case DungeonTileType.Roof:
                    sprite.Layer = Global.LayerMapRoof;
                    break;
                }
                sprite.Play(direction.ToString());
                Add(sprite, tile.X * Global.TileSize, tile.Y * Global.TileSize);
            }
            else if (tile.DisplayTid != -1)
            {
                tilemap.SetTileAt(tile.X, tile.Y, tile.DisplayTid);
            }
            else
            {
                tilemap.SetTileAt(tile.X, tile.Y, tid);
            }
        }
Esempio n. 2
0
        private void InitEntity(DungeonTile tile, TiledMapProperties properties)
        {
            if (!properties.ContainsKey("entity"))
            {
                Log.Error("Invalid entity at " + tile + ": 'entity' not found.");
                return;
            }

            if (!Enum.TryParse(properties.GetString("direction", "Down"), out Direction direction))
            {
                Log.Error("Unknown entity direction '" + properties.GetString("direction") + "' at " + tile);
            }

            var entityName = properties.GetString("entity");

            if (entityName == "player")
            {
                PlayerStartTile      = tile;
                PlayerStartDirection = direction;
            }
            else
            {
                var entity = EntityData.Get(entityName);
                if (entity == null)
                {
                    Log.Error("Unknown entity '" + entityName + "' at " + tile);
                }
                else
                {
                    entities.Add(new EntityArguments(entity, tile.X, tile.Y, direction));
                }
            }
        }
Esempio n. 3
0
        private bool TrySetTile(Tilemap tilemap, DungeonTile tile, int tid, TilePositions neighbors)
        {
            if (GetNeighbors(tile) != neighbors)
            {
                return(false);
            }

            SetTile(tilemap, tile, tid);
            return(true);
        }
Esempio n. 4
0
        public void InitPlayer(PlayerData data, DungeonTile tile, Direction direction)
        {
            if (player != null)
            {
                Remove(player);
            }

            player = new Player(data, tile, direction);
            Add(player);
            cameraPosition = player.Position - new Vector2(Engine.HalfWidth, Engine.HalfHeight);
        }
Esempio n. 5
0
        private DungeonData(TiledMap map, string name)
        {
            this.map    = map;
            Name        = name;
            WidthTiles  = map.Width;
            HeightTiles = map.Height;
            Width       = WidthTiles / Global.RoomWidth;
            Height      = HeightTiles / Global.RoomHeight;
            Tileset     = new Tileset("gfx/game/" + map.Properties.GetString("tileset"), Global.TileSize, Global.TileSize);

            tiles = new DungeonTile[WidthTiles, HeightTiles];

            var layer = map.GetLayer <TiledMapTileLayer>("main");

            for (var ix = 0; ix < WidthTiles; ix++)
            {
                for (var iy = 0; iy < HeightTiles; iy++)
                {
                    var tid = -1;
                    if (layer.TryGetTile(ix, iy, out TiledMapTile? tiledTile))
                    {
                        tid = tiledTile.Value.GlobalIdentifier;
                    }

                    var properties = map.GetTilePropertiesAt(layer, ix, iy);
                    var tile       = new DungeonTile(ix, iy, tid, properties);
                    if (properties != null)
                    {
                        InitTile(tile, properties);
                    }
                    tiles[ix, iy] = tile;
                }
            }

            if (PlayerStartTile == null)
            {
                throw new Exception("Dungeon " + this + " has no starting player tile.");
            }

            Log.Debug("Created " + this + " of size " + Width + "x" + Height + " with player start tile " + PlayerStartTile + ".");
        }
Esempio n. 6
0
        private TilePositions GetNeighbors(DungeonTile tile)
        {
            TilePositions result = TilePositions.None;

            if (GetTileTypeAt(tile.X - 1, tile.Y) == tile.Type)
            {
                result |= TilePositions.Left;
            }
            if (GetTileTypeAt(tile.X + 1, tile.Y) == tile.Type)
            {
                result |= TilePositions.Right;
            }
            if (GetTileTypeAt(tile.X, tile.Y - 1) == tile.Type)
            {
                result |= TilePositions.Up;
            }
            if (GetTileTypeAt(tile.X, tile.Y + 1) == tile.Type)
            {
                result |= TilePositions.Down;
            }
            return(result);
        }
Esempio n. 7
0
        private void InitTile(DungeonTile tile, TiledMapProperties properties)
        {
            if (Enum.TryParse(properties.GetString("solidType", "None"), out TileSolidType solidType))
            {
                tile.SolidType = solidType;
            }
            else
            {
                Log.Error("Unknown tile solid type '" + properties.GetString("solidType") + "' at " + tile);
            }

            if (properties.ContainsKey("tile"))
            {
                if (Enum.TryParse(properties.GetString("tile"), out DungeonTileType type))
                {
                    tile.Type = type;
                }
                else
                {
                    Log.Error("Unknown tile type '" + properties.GetString("type") + "' at " + tile);
                }
            }

            tile.Trigger = properties.GetString("trigger");

            tile.Anim = properties.GetString("anim");

            if (properties.ContainsKey("displayTid"))
            {
                tile.DisplayTid = properties.GetInt("displayTid");
            }

            if (properties.ContainsKey("entity"))
            {
                InitEntity(tile, properties);
            }
        }
Esempio n. 8
0
        public Player(PlayerData data, DungeonTile tile, Direction direction)
        {
            Data = data;

            X     = tile.ScreenX + Global.TileSize / 2;
            Y     = tile.ScreenY + Global.TileSize / 2;
            Angle = DirectionUtils.GetAngle(direction);

            Type     = Global.TypePlayer;
            Layer    = Global.LayerMain;
            Collider = data.PixelMask;
            OriginX  = Width / 2;
            OriginY  = Height / 2;

            sortNode = new Renderable();
            Add(sortNode);

            sprite = new Animator(data.Anim);
            sprite.CenterOrigin();
            sprite.Play("idle");
            sortNode.Add(sprite);

            bloodFrontSprite = new Animator(data.Anim);
            bloodFrontSprite.CenterOrigin();
            sortNode.Add(bloodFrontSprite);

            bloodBackSprite = new Animator(data.Anim);
            bloodBackSprite.CenterOrigin();
            sortNode.Add(bloodBackSprite);

            hurtSprite = new Animator(data.Anim);
            hurtSprite.CenterOrigin();
            sortNode.Add(hurtSprite);

            blinker         = new Blinker(PlayerData.InvincibleBlinkInterval, sortNode);
            blinker.Enabled = false;
            Add(blinker);

            if (engineSound == null)
            {
                engineSound = Asset.LoadSoundEffect("sfx/car_engine").CreateInstance();
            }
            engineSound.Volume   = 0.0f;
            engineSound.IsLooped = true;
            engineSound.Play();

            if (driftSounds == null)
            {
                driftSounds = new SoundEffectInstance[2];
                for (var i = 0; i < driftSounds.Length; i++)
                {
                    driftSounds[i]        = Asset.LoadSoundEffect("sfx/car_drift_" + (i + 1)).CreateInstance();
                    driftSounds[i].Volume = 0.7f;
                }
            }

            if (breakSound == null)
            {
                breakSound = Asset.LoadSoundEffect("sfx/car_break").CreateInstance();
            }
            breakSound.Volume = 0.7f;

            Engine.Track(this, "Position");
            Engine.Track(this, "Speed");
            Engine.Track(this, "Angle");
            Engine.Track(this, "engineState");
            Engine.Track(this, "enginePct");
            Engine.Track(this, "driftPct");
            Engine.Track(this, "tireBloodPct");
            Engine.Track(this, "velocity");
            Engine.Track(this, "bounceVelocity");
            Engine.Track(this, "driftAngle");
            Engine.Track(this, "forward");
        }
Esempio n. 9
0
 public Item(EntityData data, Dungeon dungeon, DungeonTile tile) : base(data, dungeon, tile)
 {
 }
Esempio n. 10
0
        private void RenderWallTile(GridCollider solidGrid, Tilemap tilemap, DungeonTile tile, int tid)
        {
            var neighbors = GetNeighbors(tile);

            // vertical
            if (GetTileTypeAt(tile.X - 1, tile.Y) == DungeonTileType.Roof && GetTileTypeAt(tile.X + 1, tile.Y) == DungeonTileType.Ground)
            {
                SetTile(tilemap, tile, tid + 16, Direction.Left);
            }
            else if (GetTileTypeAt(tile.X + 1, tile.Y) == DungeonTileType.Roof && GetTileTypeAt(tile.X - 1, tile.Y) == DungeonTileType.Ground)
            {
                SetTile(tilemap, tile, tid + 18, Direction.Right);
            }

            // horizontal
            else if (GetTileTypeAt(tile.X, tile.Y - 1) == DungeonTileType.Roof && GetTileTypeAt(tile.X, tile.Y + 1) == DungeonTileType.Ground)
            {
                SetTile(tilemap, tile, tid + 1, Direction.Up);
            }
            else if (GetTileTypeAt(tile.X, tile.Y + 1) == DungeonTileType.Roof && GetTileTypeAt(tile.X, tile.Y - 1) == DungeonTileType.Ground)
            {
                SetTile(tilemap, tile, tid + 33, Direction.Down);
            }

            // corner outer
            else if (GetTileTypeAt(tile.X + 1, tile.Y + 1) == DungeonTileType.Ground && GetTileTypeAt(tile.X + 1, tile.Y) == DungeonTileType.Wall && GetTileTypeAt(tile.X, tile.Y + 1) == DungeonTileType.Wall)
            {
                SetTile(tilemap, tile, tid + 0);
            }
            else if (GetTileTypeAt(tile.X - 1, tile.Y + 1) == DungeonTileType.Ground && GetTileTypeAt(tile.X - 1, tile.Y) == DungeonTileType.Wall && GetTileTypeAt(tile.X, tile.Y + 1) == DungeonTileType.Wall)
            {
                SetTile(tilemap, tile, tid + 2);
            }
            else if (GetTileTypeAt(tile.X + 1, tile.Y - 1) == DungeonTileType.Ground && GetTileTypeAt(tile.X + 1, tile.Y) == DungeonTileType.Wall && GetTileTypeAt(tile.X, tile.Y - 1) == DungeonTileType.Wall)
            {
                SetTile(tilemap, tile, tid + 32);
            }
            else if (GetTileTypeAt(tile.X - 1, tile.Y - 1) == DungeonTileType.Ground && GetTileTypeAt(tile.X - 1, tile.Y) == DungeonTileType.Wall && GetTileTypeAt(tile.X, tile.Y - 1) == DungeonTileType.Wall)
            {
                SetTile(tilemap, tile, tid + 34);
            }

            // corner inner
            else if (GetTileTypeAt(tile.X - 1, tile.Y - 1) == DungeonTileType.Ground && GetTileTypeAt(tile.X + 1, tile.Y) == DungeonTileType.Wall && GetTileTypeAt(tile.X, tile.Y + 1) == DungeonTileType.Wall)
            {
                SetTile(tilemap, tile, tid + 3);
                solidGrid.SetPixelMaskAt(tile.X, tile.Y, tid + 3);
            }
            else if (GetTileTypeAt(tile.X + 1, tile.Y - 1) == DungeonTileType.Ground && GetTileTypeAt(tile.X - 1, tile.Y) == DungeonTileType.Wall && GetTileTypeAt(tile.X, tile.Y + 1) == DungeonTileType.Wall)
            {
                SetTile(tilemap, tile, tid + 4);
                solidGrid.SetPixelMaskAt(tile.X, tile.Y, tid + 4);
            }
            else if (GetTileTypeAt(tile.X - 1, tile.Y + 1) == DungeonTileType.Ground && GetTileTypeAt(tile.X + 1, tile.Y) == DungeonTileType.Wall && GetTileTypeAt(tile.X, tile.Y - 1) == DungeonTileType.Wall)
            {
                SetTile(tilemap, tile, tid + 19);
                solidGrid.SetPixelMaskAt(tile.X, tile.Y, tid + 19);
            }
            else if (GetTileTypeAt(tile.X + 1, tile.Y + 1) == DungeonTileType.Ground && GetTileTypeAt(tile.X - 1, tile.Y) == DungeonTileType.Wall && GetTileTypeAt(tile.X, tile.Y - 1) == DungeonTileType.Wall)
            {
                SetTile(tilemap, tile, tid + 20);
                solidGrid.SetPixelMaskAt(tile.X, tile.Y, tid + 20);
            }

            // error
            else
            {
                Log.Error("Unkown wall tile at " + tile);
            }
        }
Esempio n. 11
0
        private void RenderFlatTile(Tilemap tilemap, DungeonTile tile, int tid)
        {
            var neighbors = GetNeighbors(tile);

            // straight edges
            if (TrySetTile(tilemap, tile, tid + 18, TilePositions.Up | TilePositions.Down | TilePositions.Left))
            {
                return;
            }
            if (TrySetTile(tilemap, tile, tid + 16, TilePositions.Up | TilePositions.Down | TilePositions.Right))
            {
                return;
            }
            if (TrySetTile(tilemap, tile, tid + 33, TilePositions.Left | TilePositions.Right | TilePositions.Up))
            {
                return;
            }
            if (TrySetTile(tilemap, tile, tid + 1, TilePositions.Left | TilePositions.Right | TilePositions.Down))
            {
                return;
            }

            // corners
            if (TrySetTile(tilemap, tile, tid + 0, TilePositions.Right | TilePositions.Down))
            {
                return;
            }
            if (TrySetTile(tilemap, tile, tid + 2, TilePositions.Left | TilePositions.Down))
            {
                return;
            }
            if (TrySetTile(tilemap, tile, tid + 32, TilePositions.Right | TilePositions.Up))
            {
                return;
            }
            if (TrySetTile(tilemap, tile, tid + 34, TilePositions.Left | TilePositions.Up))
            {
                return;
            }

            // vertical
            if (TrySetTile(tilemap, tile, tid + 20, TilePositions.Up | TilePositions.Down))
            {
                return;
            }

            // horizontal
            if (TrySetTile(tilemap, tile, tid + 36, TilePositions.Left | TilePositions.Right))
            {
                return;
            }

            var rnd = Rand.NextFloat();

            if (rnd < 0.9f)
            {
                tilemap.SetTileAt(tile.X, tile.Y, tid + 17);
            }
            else if (rnd < 0.94f)
            {
                tilemap.SetTileAt(tile.X, tile.Y, tid + 3);
            }
            else if (rnd < 0.975f)
            {
                tilemap.SetTileAt(tile.X, tile.Y, tid + 19);
            }
            else
            {
                tilemap.SetTileAt(tile.X, tile.Y, tid + 35);
            }
        }