コード例 #1
0
    public override void draw()
    {
        int w = (int)(HardwareInterface.timeSinceLevelLoad) % 4;

        if (w == 3)
        {
            w = 1;
        }
        int x1 = Core.safeDiv(Core.p.getCameraX() - Graphics.WIDTH / 2 - Graphics.xOffset - Core.TILE_SIZE, CHUNK_SIZE * Core.TILE_SIZE) - 1;
        int x2 = Core.safeDiv(Core.p.getCameraX() + Graphics.WIDTH / 2 + Graphics.xOffset + Core.TILE_SIZE, CHUNK_SIZE * Core.TILE_SIZE) + 1;
        int y1 = Core.safeDiv(Core.p.getCameraY() - Graphics.HEIGHT / 2 - Graphics.yOffset - Core.TILE_SIZE, CHUNK_SIZE * Core.TILE_SIZE) - 1;
        int y2 = Core.safeDiv(Core.p.getCameraY() + Graphics.HEIGHT / 2 + Graphics.yOffset + Core.TILE_SIZE, CHUNK_SIZE * Core.TILE_SIZE) + 1;

        for (int x = x1; x <= x2; x++)
        {
            for (int y = y1; y <= y2; y++)
            {
                Chunk c;
                if (!chunks.TryGetValue(new Coord(x, y), out c))
                {
                    continue;
                }
                foreach (var pair in c.tiles)
                {
                    var list = pair.Value;

                    Graphics.lockMaterial(Graphics.tilesets[pair.Key * 3 + Core.getGraphicsLevel()]);
                    for (int i = 0; i < list.Count; i++)
                    {
                        Chunk.Tile tile = list[i];
                        int        id   = tile.id;
                        if (Core.exceptions.Count > 0)
                        {
                            Coord coord = new Coord(tile.x + x * CHUNK_SIZE, tile.y + y * CHUNK_SIZE);
                            if (Core.exceptions.ContainsKey(coord))
                            {
                                id = Core.exceptions[coord].newTile;
                                if (id < 0)
                                {
                                    id = tile.id + id;
                                }
                            }
                        }
                        if (id >= 37 && id <= 40)
                        {
                            if (Mathf.FloorToInt(HardwareInterface.timeSinceLevelLoad * 5 + 3.14F * tile.x + tile.y) % (id >= 38 && id <= 39?4:2) == 0)
                            {
                                id += 16;
                            }
                        }
                        Graphics.draw(Graphics.tileset[pair.Key * 256 + id], Core.getOnscreenX(Core.TILE_SIZE * (tile.x + x * CHUNK_SIZE)), Core.getOnscreenY(Core.TILE_SIZE * (tile.y + y * CHUNK_SIZE)), null);
                    }
                    Graphics.unlockMaterial();
                }
            }
        }
    }
コード例 #2
0
    private bool loadFromJson(string path)
    {
        try
        {
            jobject        json      = Json.jsonObject(File.ReadAllText(path));
            List <Tileset> tilesets  = new List <Tileset>();
            Tileset        collision = null;
            foreach (object _ in (jarray)json["tilesets"])
            {
                jobject tileset = _ as jobject;
                Tileset t       = new Tileset();
                t.firstgid = (int)tileset["firstgid"];
                int    index = 0;
                string path2 = (string)tileset["source"];
                if (path2.Contains("tileset"))
                {
                    path2   = path2.Substring(path2.IndexOf("tileset") + 7);
                    path2   = path2.Substring(0, path2.Length - 4);
                    index   = int.Parse(path2) - 1;
                    t.index = index;

                    //Console.WriteLine("Found tileset " + index + " starting at gid " + t.firstgid);
                }
                else
                {
                    t.index   = 0;
                    collision = t;
                }
                tilesets.Add(t);
            }
            //find width and height
            foreach (jobject layer in (jarray)json["layers"])
            {
                if ((string)layer["type"] != "tilelayer")
                {
                    continue;
                }
                if (xOffset > (int)layer["startx"])
                {
                    xOffset = (int)layer["startx"];
                }
                if (yOffset > (int)layer["starty"])
                {
                    yOffset = (int)layer["starty"];
                }
                if (width < (int)layer["width"])
                {
                    width = (int)layer["width"];
                }
                if (height < (int)layer["height"])
                {
                    height = (int)layer["height"];
                }
            }

            foreach (jobject layer in (jarray)json["layers"])
            {
                if ((string)layer["type"] != "tilelayer")
                {
                    if ((string)layer["type"] == "objectgroup")
                    {
                        foreach (jobject o in (jarray)layer["objects"])
                        {
                            objects.Add((string)o["name"], o);
                        }
                    }
                    continue;
                }

                int lower = (int)findProperty(layer, "lower", 0);
                int upper = (int)findProperty(layer, "upper", 0);

                foreach (jobject chunk in (jarray)layer["chunks"])
                {
                    int    startx      = (int)chunk["x"];
                    int    starty      = (int)chunk["y"];
                    jarray data        = (jarray)chunk["data"];
                    bool   isCollision = (string)layer["name"] == "Collision";
                    Chunk  c;
                    Coord  coord = new Coord(Core.safeDiv(startx, CHUNK_SIZE), Core.safeDiv(starty, CHUNK_SIZE));
                    if (!chunks.TryGetValue(coord, out c))
                    {
                        c = new Chunk();
                        chunks.Add(coord, c);
                    }
                    for (int y = 0; y < CHUNK_SIZE; y++)
                    {
                        for (int x = 0; x < CHUNK_SIZE; x++)
                        {
                            ushort value = (ushort)(int)data[x + CHUNK_SIZE * y];
                            if (!isCollision)
                            {
                                foreach (Tileset t in tilesets)
                                {
                                    if (value >= t.firstgid && value < t.firstgid + 256)
                                    {
                                        value = (ushort)(value - t.firstgid + t.index * 256);
                                        Chunk.Tile tile = new Chunk.Tile();
                                        tile.id    = (byte)(value & 0xFF);
                                        tile.x     = (byte)x;
                                        tile.y     = (byte)y;
                                        tile.lower = (short)lower;
                                        tile.upper = (short)upper;
                                        List <Chunk.Tile> list;
                                        if (!c.tiles.TryGetValue(t.index, out list))
                                        {
                                            list = new List <Chunk.Tile>();
                                            c.tiles.Add(t.index, list);
                                        }
                                        list.Add(tile);
                                        break;
                                    }
                                }
                            }
                            else
                            {
                                if (value != 0)
                                {
                                    c.collision[x + CHUNK_SIZE * y] = (byte)(value - collision.firstgid);
                                }
                            }
                        }
                    }
                }
            }
            return(true);
        }
        catch (Exception e)
        {
            Debug.WriteLine("Trouble loading Tiled map " + name);
            Debug.WriteLine(e.Message + "\n" + e.StackTrace);
            return(false);
        }
    }
コード例 #3
0
ファイル: Level.cs プロジェクト: BrianLive/MemoryLeak
        //TODO: Actually load from file
        public static State Load(string fileName, Fader fader)
        {
            fader.Timestep = 0.1f;
            fader.FadeIn();

            //disabled because otherwise it gets annoying to run the game while listening to music and stuff
            //Resource<Sound>.Get("BGM/HonorForAll", "mp3").IsLooped = true;
            //Resource<Sound>.Get("BGM/HonorForAll", "mp3").Play();

            string file = Path.Combine("Content/Maps/", fileName);
            var level = new LoadedLevel(new FileStream(file, FileMode.Open));

            var chunk = new Chunk(level.Width, level.Height);
            var camera = new Camera();

            var layers = level.Layers;
            var objectgroups = level.ObjectGroups;

            foreach (var layer in layers)
            {
                int[] tiles = layer.Value.Tiles;
                int z = int.Parse(layer.Key);

                for (int x = 0; x < chunk.Width; x++)
                    for (int y = 0; y < chunk.Height; y++)
                    {
                        Texture2D tex = Resource<Texture2D>.Get(level.Tileset.Image);

                        int id = tiles[x + y * chunk.Width];

                        //Texture coordinates

                        int tx = (int)(id * 32 / 1.5); //why does this work
                        int ty = 0;
                        //int tx = (id % (tex.Width / 32)) * 32;
                        //int ty = (id / (tex.Width / 32)) * 32;
                        int tw = 32;
                        int th = 32;

                        var tile = new Chunk.Tile(tex, tx, ty, tw, th);

                        Dictionary<string, string> properties;
                        if (level.Tileset.TileData.TryGetValue(id, out properties)) //If id isn't found, that means the tile id has no properties defined
                            foreach (var property in properties)
                                tile.AddProperty(property.Key, property.Value); //If id IS found, set all properties

                        tile.AddProperty("FrictionMultiplier", 1);

                        chunk.Set(x, y, z, tile);
                    }
            }

            Physical player = null; //This gets set down here

            foreach (var objectgroup in objectgroups)
            {
                var objects = objectgroup.Value.Entities;
                int z = int.Parse(objectgroup.Key); //TODO: Does this even work too?

                foreach (var obj in objects)
                {
                    string name = obj.Name;

                    bool isPassable = false;
                    bool isRamp = false;

                    if (obj.Properties != null)
                    {
                        isRamp = bool.Parse(obj.Properties["IsRamp"]);
                        isPassable = bool.Parse(obj.Properties["IsPassable"]);
                    }

                    int x = (int)(obj.Position.X / 32f) - 0; //This is...
                    int y = (int)(obj.Position.Y / 32f) - 1; //Pretty wonky.

                    if (isRamp) //If it's a ramp, we don't create an entity at all, but a tile!
                    {
                        Texture2D tex = Resource<Texture2D>.Get(level.Tileset.Image);

                        int id = obj.GID;

                        //Texture coordinates

                        int tx = (int)(id * 16); //why does this work
                        int ty = 0;
                        int tw = 32;
                        int th = 32;

                        var tile = new Chunk.Tile(tex, tx, ty, tw, th);

                        foreach (var property in obj.Properties)
                            tile.AddProperty(property.Key, property.Value);

                        chunk.Set(x, y, z, tile);
                    }
                    else
                    {
                        Physical entity;

                        if (name == "player.Start") //TODO: Maybe make these "hardcoded" entities a lookup table of prefabs?
                        {
                            entity = new Physical(Resource<Texture2D>.Get("debug-entity"), x, y, z);
                            player = entity;
                        }
                        else
                            entity = new Physical(Resource<Texture2D>.Get("debug-entity"), //TODO: Read texture from entity
                                                  x, y, z,
                                                  isPassable);

                        //TODO: For now, entities don't seem to have a way to set properties...

                        chunk.Add(entity);
                    }
                }
            }

            for (int i = 0; i < 0; i++) //LOOK AT ALL THOSE DUDES HAVING FUN
            {
                var otherDude = new Physical(Resource<Texture2D>.Get("debug-entity"), RandomHelper.Range(3, 16-3), RandomHelper.Range(3, 16-3), 0);

                float time = RandomHelper.Range();
                otherDude.Tick += f =>
                    {
                        time += f * 0.25f;
                        otherDude.Move((int)Math.Round(Math.Cos(time * Math.PI * 2)), (int)Math.Round(Math.Sin(time * Math.PI * 2)), 100 * f);
                    };

                chunk.Add(otherDude);
            }

            if (player == null)
                throw new Exception("Well, well, well. Looks like SOMEONE forgot to put a player.Start in the level. Have fun with all your NullReferenceExceptions down here.");

            player.Tick += dt =>
                {
                    var k = Keyboard.GetState();

                    var move = Vector2.Zero;

                    foreach (var i in k.GetPressedKeys())
                    {
                        switch (i)
                        {
                            case Keys.W:
                                move.Y = -1;
                                break;
                            case Keys.S:
                                move.Y = 1;
                                break;
                            case Keys.A:
                                move.X = -1;
                                break;
                            case Keys.D:
                                move.X = 1;
                                break;
                            case Keys.LeftShift:
                                Console.WriteLine(player.Depth);
                                break;
                        }
                    }

                    if (move != Vector2.Zero) player.Move((int)move.X, (int)move.Y, 200 * dt);

                    camera.Position = player.CenterPosition + Vector2.One / 2; //Add (0.5, 0.5) to player position so we don't get shakyness (it works trust me DON'T REMOVE IT)
                };

            chunk.Add(player);

            return new State(chunk, camera) { Player = player };
        }