コード例 #1
0
        public T SpawnEntity <T>(CEntitySpawnParameters parameters = null) where T : CEntity, new()
        {
            if (LoadedLevel != null)
            {
                if (parameters == null)
                {
                    parameters = new CEntitySpawnParameters();
                }

                T newEntity = new T();
                newEntity.Init(this, parameters.userData);

                newEntity.Id   = ++m_entityIdCounter;
                newEntity.Name = typeof(T).Name.Substring(1) + m_entityIdCounter.ToString();

                m_entityIdMap.Add(m_entityIdCounter, newEntity);

                LoadedLevel.AddEntity(newEntity);

                newEntity.SetWorldPosition(parameters.position);
                newEntity.SetWorldRotation(parameters.rotation);
                newEntity.SetWorldScale(parameters.scale);

                OnEntitySpawned?.Invoke(newEntity);

                return(newEntity);
            }

            return(default);
コード例 #2
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 };
        }