コード例 #1
0
ファイル: LevelScreen.cs プロジェクト: zdepav/into-the-blaze
        public GameObjectInstance GetNearestHydrant(Vector2 pos, float distanceLimit = 10_000)
        {
            distanceLimit *= distanceLimit;
            GameObjectInstance nearest = null;
            var dist = float.PositiveInfinity;

            foreach (var h in gameObjectInstances.Where(i => i.Type.Type == ObjectType.Hydrant))
            {
                var d = (h.Position.ToVector2() - pos).LengthSquared();
                if (d < dist && d < distanceLimit)
                {
                    dist    = d;
                    nearest = h;
                }
            }
            return(nearest);
        }
コード例 #2
0
ファイル: LevelScreen.cs プロジェクト: zdepav/into-the-blaze
        /// <returns>this</returns>
        public LevelScreen LoadLevel(int id)
        {
            try {
                levelId             = id;
                backgroundGenerated = false;
                player.Reset();
                using (var br = new BinaryReader(File.OpenRead($"Levels/{id}.level"))) {
                    // magic number
                    var bytes = br.ReadBytes(9);
                    for (var i = 0; i < 9; ++i)
                    {
                        if (bytes[i] != levelFileMagicNumber[i])
                        {
                            throw new FormatException();
                        }
                    }
                    // walls
                    for (var y = 0; y < 17; ++y)
                    {
                        var u = br.ReadUInt32();
                        for (var x = 0; x < 32; ++x)
                        {
                            walls[x, y] = (byte)(BinaryHelper.GetBit(u, x) ? 0xF0 : 0);
                        }
                    }
                    for (var x = 0; x < 32; ++x)
                    {
                        for (var y = 0; y < 17; ++y)
                        {
                            if (walls[x, y] > 0)
                            {
                                BinaryHelper.SetBit(ref walls[x, y], 0, WallAt(x - 1, y));
                                BinaryHelper.SetBit(ref walls[x, y], 1, WallAt(x, y + 1));
                                BinaryHelper.SetBit(ref walls[x, y], 2, WallAt(x + 1, y));
                                BinaryHelper.SetBit(ref walls[x, y], 3, WallAt(x, y - 1));
                            }
                        }
                    }
                    wallColliders.Clear();
                    var processed = new bool[32, 17];
                    for (var x = 0; x < 32; ++x)
                    {
                        for (var y = 0; y < 17; ++y)
                        {
                            if (processed[x, y])
                            {
                                continue;
                            }
                            if (walls[x, y] > 0)
                            {
                                if (WallAt(x + 1, y))
                                {
                                    var w = 2;
                                    processed[x + 1, y] = true;
                                    for (var _x = x + 2; WallAt(_x, y); processed[_x, y] = true, ++_x, ++w)
                                    {
                                    }
                                    wallColliders.Add(new Rect(x * 32 + w * 16, y * 32 + 16, w * 32, 32));
                                }
                                else if (WallAt(x, y + 1))
                                {
                                    var h = 2;
                                    processed[x, y + 1] = true;
                                    for (var _y = y + 2; WallAt(x, _y); processed[x, _y] = true, ++_y, ++h)
                                    {
                                    }
                                    wallColliders.Add(new Rect(x * 32 + 16, y * 32 + h * 16, 32, h * 32));
                                }
                                else
                                {
                                    wallColliders.Add(new Rect(x * 32 + 16, y * 32 + 16, 32, 32));
                                }
                            }
                            processed[x, y] = true;
                        }
                    }

                    // floor
                    for (var y = 0; y < 17; ++y)
                    {
                        var u = br.ReadUInt64();
                        for (var x = 0; x < 32; ++x)
                        {
                            floor[x, y] = (byte)(
                                (BinaryHelper.GetBit(u, x * 2) ? 1 : 0) +
                                (BinaryHelper.GetBit(u, x * 2 + 1) ? 2 : 0)
                                );
                        }
                    }

                    // objects
                    gameObjectInstances.Clear();
                    var count = br.ReadInt16();
                    for (var i = 0; i < count; ++i)
                    {
                        var name     = br.ReadString();
                        var x        = br.ReadUInt16();
                        var y        = br.ReadUInt16();
                        var rotation = br.ReadUInt16();
                        if (name == "player")
                        {
                            br.ReadBoolean(); // discard 1 bool
                            player.Position = new Vector2(x, y);
                        }
                        else
                        {
                            var inst = new GameObjectInstance(
                                gameObjects[name],
                                new Point(x, y),
                                rotation
                                );
                            if (br.ReadBoolean())
                            {
                                inst.SetAlight();
                            }
                            gameObjectInstances.Add(inst);
                        }
                    }
                    startingCost = gameObjectInstances.Sum(inst => inst.Type.Cost);
                }
                return(this);
            } catch (Exception e) {
                return(null);
            }
        }