public static GameObject instance_create(Vector2 position, Type obj, string layer) { // First we need to convert layer name to actual layer RoomLayer realLayer = roomLayers.FirstOrDefault(x => x.Name == layer); GameObject o = (GameObject)Activator.CreateInstance(obj); Spritesheet s = new Spritesheet(); if (o.Sprite != null) { s = Sprites.FirstOrDefault(x => x.Name == o.Sprite.TextureSource); } o.OriginalType = obj; o.TypeString = obj.ToString(); if (s == null) { Texture2D tx = ConvertToTexture(SimplexIde.Properties.Resources.Question_16x, GraphicsDevice); o.Sprite = new Sprite(); o.Sprite.Texture = tx; o.Sprite.ImageRectangle = new Microsoft.Xna.Framework.Rectangle(0, 0, 16, 16); o.Sprite.TextureRows = 1; o.Sprite.TextureCellsPerRow = 1; o.Sprite.ImageSize = new Vector2(16, 16); o.Sprite.FramesCount = 1; o.FramesCount = 1; o.Sprite.cellW = 16; o.Sprite.cellH = 16; o.Position = new Vector2(position.X - 8, position.Y - 8); o.Sprite.ImageRectangle = new Microsoft.Xna.Framework.Rectangle(0, 0, 16, 16); } else { o.Sprite.Texture = s.Texture; o.Sprite.ImageRectangle = new Microsoft.Xna.Framework.Rectangle(0, 0, s.CellWidth, s.CellHeight); o.Sprite.TextureRows = s.Rows; o.Sprite.TextureCellsPerRow = s.Texture.Width / s.CellWidth; o.Sprite.ImageSize = new Vector2(s.CellWidth, s.CellHeight); o.Sprite.FramesCount = Math.Max((s.Texture.Width / s.CellWidth) * (s.Texture.Height / s.CellHeight) - 1, 1); o.FramesCount = Math.Max(o.Sprite.FramesCount - 1, 1); o.Sprite.cellW = s.CellHeight; o.Sprite.cellH = s.CellWidth; o.Position = new Vector2(position.X - s.CellWidth / 2f, position.Y - s.CellHeight / 2f); o.Sprite.ImageRectangle = new Microsoft.Xna.Framework.Rectangle(0, 0, s.CellWidth, s.CellHeight); } o.LayerName = realLayer.Name; o.Layer = (ObjectLayer)realLayer; currentObject = o; o.EvtCreate(); o.Layer.Objects.Add(o); SceneObjects.Add(o); sh.RegisterObject(o); return(o); }
public static void game_load(string path) { bool flag = false; // First we f**k current scene ClearAll(); RoomEditor?.ClearNodes(); // Prepare XML serializer // Then we load raw data XmlSerializer ser = new XmlSerializer(typeof(Root), Form1.reflectedTypes.ToArray()); StreamReader w = new StreamReader(path); bool flagGood = true; Root rawData = null; try { rawData = (Root)ser.Deserialize(w); } catch (Exception e) { flagGood = false; DarkMessageBox.Show("Loading of room failed\n\n" + e.InnerException.Message, "Simplex Engine 3"); } if (flagGood) { // Filter out duplicated layers List <string> names = new List <string>(); List <RoomLayer> goodLayers = new List <RoomLayer>(); for (var i = 0; i < rawData.Room.Layers.Count; i++) { if (!names.Contains(rawData.Room.Layers[i].Name)) { names.Add(rawData.Room.Layers[i].Name); goodLayers.Add(rawData.Room.Layers[i]); } } rawData.Room.Layers = goodLayers; // First load back room itself Form1.width = (int)rawData.Room.Size.X; Form1.height = (int)rawData.Room.Size.Y; currentRoom = rawData.Room; if (RoomEditor != null) { RoomEditor.currentRoom = currentRoom; RoomEditor?.PropagateNodes(); RoomEditor.roomsControl.execute = false; RoomEditor.roomsControl.dtv.SelectNode(RoomEditor.roomsControl.dtv.Nodes[0].Nodes .FirstOrDefault(x => x.Text == currentRoom.GetType().ToString().Split('.').Last())); RoomEditor.roomsControl.execute = true; } // we need to initialize layers by type if (tilesets != null) { foreach (RoomLayer rl in currentRoom.Layers) { if (rl.LayerType == RoomLayer.LayerTypes.typeTile) { // Start with empty cell data and load stuff later on ((TileLayer)rl).Data = new int[(int)currentRoom.Size.X / 32, (int)currentRoom.Size.Y / 32]; // Now select correct tileset and assign it to this.. well tileset Tileset tl = tilesets.FirstOrDefault(x => x.Name == ((TileLayer)rl).TilelistName); // this can fail so check for that if (tl != null) { // also we need to load textures for the tileset // all good ((TileLayer)rl).Tileset = tl; } } } } // Time to load babies foreach (GameObject g in rawData.Objects) { Spritesheet s = Sprites.FirstOrDefault(x => x.Name == g.Sprite.TextureSource); g.Sprite.Texture = s.Texture; g.Sprite.ImageRectangle = new Microsoft.Xna.Framework.Rectangle(0, 0, s.CellWidth, s.CellHeight); g.Sprite.TextureRows = s.Rows; g.Sprite.TextureCellsPerRow = s.Texture.Width / s.CellWidth; g.Sprite.ImageSize = new Vector2(s.CellWidth, s.CellHeight); g.Sprite.FramesCount = (s.Texture.Width / s.CellWidth) * (s.Texture.Height / s.CellHeight) - 1; g.FramesCount = g.Sprite.FramesCount - 1; g.Sprite.cellW = s.CellHeight; g.Sprite.cellH = s.CellWidth; RoomLayer rt = currentRoom.Layers.FirstOrDefault(x => x.Name == g.LayerName); g.Layer = (ObjectLayer)rt; g.Sprite.UpdateImageRectangle(); g.UpdateState(); g.UpdateColliders(); // Need to give object OriginalType !!! g.OriginalType = Type.GetType(g.TypeString); currentObject = g; g.EvtCreate(); g.EvtLoad(); SceneObjects.Add(g); g.Layer.Objects.Add(g); sh.RegisterObject(g); if (g.Colliders.Count > 0) { SceneColliders.Add(g); } } foreach (GameObject g in SceneObjects) { g.EvtCreateEnd(); } // Load tiles foreach (Tile t in rawData.Tiles) { RoomLayer correctLayer = currentRoom.Layers.FirstOrDefault(x => x.Name == t.TileLayerName); if (correctLayer != null) { TileLayer crt = (TileLayer)correctLayer; t.TileLayer = crt; crt.Tiles.Add(t); } else { Debug.WriteLine("Tile could not be loaded - TileLayerName is wrong"); } } // Now update all tiles Texture2D ttt = tilesets.FirstOrDefault(x => x.Name == "tileset0").Texture; foreach (RoomLayer rl in currentRoom.Layers) { if (rl is TileLayer) { TileLayer crt = (TileLayer)rl; foreach (Tile t in crt.Tiles) { t.SourceTexture = ttt; Autotile.UpdateTile(t, crt); } } } w.Close(); } }
public static Root game_load(string path, bool execCreateEvt = true, bool execLoadEvt = true) { bool flag = false; // First we f**k current scene ClearAll(); RoomEditor?.ClearNodes(); // Prepare XML serializer // Then we load raw data XmlSerializer ser = new XmlSerializer(typeof(Root), Form1.reflectedTypes.ToArray()); StreamReader w = new StreamReader(path); bool flagGood = true; Root rawData = null; try { rawData = (Root)ser.Deserialize(w); } catch (Exception e) { flagGood = false; DarkMessageBox.Show("Loading of room failed\n\n" + e.InnerException.Message, "Simplex Engine 3"); } if (flagGood) { // Filter out duplicated layers List <string> names = new List <string>(); List <RoomLayer> goodLayers = new List <RoomLayer>(); for (var i = 0; i < rawData.Room.Layers.Count; i++) { if (!names.Contains(rawData.Room.Layers[i].Name)) { names.Add(rawData.Room.Layers[i].Name); goodLayers.Add(rawData.Room.Layers[i]); } } rawData.Room.Layers = goodLayers; // First load back room itself Form1.width = (int)rawData.Room.Size.X; Form1.height = (int)rawData.Room.Size.Y; currentRoom = rawData.Room; if (RoomEditor != null) { RoomEditor.currentRoom = currentRoom; RoomEditor?.PropagateNodes(); RoomEditor.roomsControl.execute = false; RoomEditor.roomsControl.dtv.SelectNode(RoomEditor.roomsControl.dtv.Nodes[0].Nodes.FirstOrDefault(x => x.Text == currentRoom.GetType().ToString().Split('.').Last())); RoomEditor.roomsControl.execute = true; } // we need to initialize layers by type if (tilesets != null) { foreach (RoomLayer rl in currentRoom.Layers) { if (rl.LayerType == RoomLayer.LayerTypes.typeTile) { // Start with empty cell data and load stuff later on ((TileLayer)rl).Data = new int[(int)currentRoom.Size.X / 32, (int)currentRoom.Size.Y / 32]; // Now select correct tileset and assign it to this.. well tileset Tileset tl = tilesets.FirstOrDefault(x => x.Name == ((TileLayer)rl).TilelistName); // this can fail so check for that if (tl != null) { // also we need to load textures for the tileset // all good ((TileLayer)rl).Tileset = tl; } } } } // Time to load babies __loadGameObjects(rawData.Objects, execCreateEvt, execLoadEvt); // Load tiles foreach (Tile t in rawData.Tiles) { RoomLayer correctLayer = currentRoom.Layers.FirstOrDefault(x => x.Name == t.TileLayerName); if (correctLayer != null) { TileLayer crt = (TileLayer)correctLayer; t.TileLayer = crt; crt.Tiles.Add(t); } else { Debug.WriteLine("Tile could not be loaded - TileLayerName is wrong"); } } // Now update all tiles Texture2D ttt = tilesets.FirstOrDefault(x => x.Name == "tileset0").Texture; foreach (RoomLayer rl in currentRoom.Layers) { if (rl is TileLayer) { TileLayer crt = (TileLayer)rl; foreach (Tile t in crt.Tiles) { t.SourceTexture = ttt; Autotile.UpdateTile(t, crt); } } } w.Close(); } return(rawData); }
public static void __loadGameObjects(List <GameObject> objects, bool execCreateEvt = true, bool execLoadEvt = true, bool execSetupEvt = true, bool reset = true) { SceneObjects.Clear(); foreach (RoomLayer rl in currentRoom.Layers) { if (rl is ObjectLayer) { ObjectLayer ol = (ObjectLayer)rl; ol.Objects.Clear(); } } foreach (GameObject g in objects) { if (reset) { g.Reset(); } Spritesheet s = Sprites.FirstOrDefault(x => x.Name == g.Sprite.TextureSource); g.Position = g.__DefaultPosition; g.ImageAngle = g.__DefaultImageAngle; g.Direction = g.__DefaultDirection; g.ImageScale = g.__DefaultImageScale; g.ImageIndex = g.__DefaultImageIndex; g.Sprite.Texture = s.Texture; g.Sprite.ImageRectangle = new Microsoft.Xna.Framework.Rectangle(0, 0, s.CellWidth, s.CellHeight); g.Sprite.TextureRows = s.Rows; g.Sprite.TextureCellsPerRow = s.Texture.Width / s.CellWidth; g.Sprite.ImageSize = new Vector2(s.CellWidth, s.CellHeight); g.Sprite.FramesCount = (s.Texture.Width / s.CellWidth) * (s.Texture.Height / s.CellHeight) - 1; g.FramesCount = g.Sprite.FramesCount - 1; g.Sprite.cellW = s.CellHeight; g.Sprite.cellH = s.CellWidth; RoomLayer rt = currentRoom.Layers.FirstOrDefault(x => x.Name == g.LayerName); g.Layer = (ObjectLayer)rt; g.Sprite.UpdateImageRectangle(); g.UpdateState(); g.UpdateColliders(); // Need to give object OriginalType !!! g.OriginalType = Type.GetType(g.TypeString); currentObject = g; if (execCreateEvt) { g.EvtCreate(); } if (execLoadEvt) { g.EvtLoad(); } if (execSetupEvt) { g.EvtSetup(); } SceneObjects.Add(g); g.Layer.Objects.Add(g); sh.RegisterObject(g); if (g.Colliders.Count > 0) { SceneColliders.Add(g); } } foreach (GameObject g in SceneObjects) { g.EvtCreateEnd(); } }
public static void game_load(string path) { bool flag = false; // First we f**k current scene ClearAll(); RoomEditor?.ClearNodes(); // Prepare XML serializer // Then we load raw data Root root = new Root(); XmlSerializer ser = new XmlSerializer(typeof(Root), Form1.reflectedTypes.ToArray()); StreamReader w = new StreamReader(path); Root rawData = (Root)ser.Deserialize(w); // First load back room itself Form1.width = (int)rawData.Room.Size.X; Form1.height = (int)rawData.Room.Size.Y; Form1.ActiveForm.Text = "Simplex RPG Engine / " + rawData.Room.Name; currentRoom = rawData.Room; if (RoomEditor != null) { RoomEditor.currentRoom = currentRoom; RoomEditor?.PropagateNodes(); RoomEditor.roomsControl.execute = false; RoomEditor.roomsControl.dtv.SelectNode(RoomEditor.roomsControl.dtv.Nodes[0].Nodes.FirstOrDefault(x => x.Text == currentRoom.GetType().ToString().Split('.').Last())); RoomEditor.roomsControl.execute = true; } // if (currentRoom != null) // { // we need to initialize layers by type if (tilesets != null) { foreach (RoomLayer rl in roomLayers) { if (rl.LayerType == RoomLayer.LayerTypes.typeTile) { // Start with empty cell data and load stuff later on ((TileLayer)rl).Data = new int[(int)currentRoom.Size.X / 32, (int)currentRoom.Size.Y / 32]; // Now select correct tileset and assign it to this.. well tileset Tileset tl = tilesets.FirstOrDefault(x => x.Name == ((TileLayer)rl).TilelistName); // this can fail so check for that if (tl != null) { // also we need to load textures for the tileset // tl.AutotileLib = // all good ((TileLayer)rl).Tileset = tl; } } } } // Time to load babies foreach (GameObject g in rawData.Objects) { Spritesheet s = Sprites.FirstOrDefault(x => x.Name == g.Sprite.TextureSource); g.Sprite.Texture = s.Texture; g.Sprite.ImageRectangle = new Microsoft.Xna.Framework.Rectangle(0, 0, s.CellWidth, s.CellHeight); g.Sprite.TextureRows = s.Rows; g.Sprite.TextureCellsPerRow = s.Texture.Width / s.CellWidth; g.Sprite.ImageSize = new Vector2(s.CellWidth, s.CellHeight); g.Sprite.FramesCount = (s.Texture.Width / s.CellWidth) * (s.Texture.Height / s.CellHeight) - 1; g.FramesCount = g.Sprite.FramesCount - 1; g.Sprite.cellW = s.CellHeight; g.Sprite.cellH = s.CellWidth; RoomLayer rt = roomLayers.FirstOrDefault(x => x.Name == g.LayerName); g.Layer = (ObjectLayer)rt; g.Sprite.UpdateImageRectangle(); g.UpdateState(); g.UpdateColliders(); // Need to give object OriginalType !!! g.OriginalType = Type.GetType(g.TypeString); g.EvtCreate(); g.EvtLoad(); g.Layer.Objects.Add(g); sh.RegisterObject(g); } // Load tiles foreach (Tile t in rawData.Tiles) { RoomLayer correctLayer = roomLayers.FirstOrDefault(x => x.Name == t.TileLayerName); if (correctLayer != null) { TileLayer crt = (TileLayer)correctLayer; t.TileLayer = crt; crt.Tiles.Add(t); } else { Debug.WriteLine("Tile could not be loaded - TileLayerName is wrong"); } } // Now update all tiles Texture2D ttt = RoomEditorEditor.Content.Load <Texture2D>(Path.GetFullPath("../../../SimplexRpgEngine3/Content/bin/Windows/Sprites/Tilesets/" + "tileset0")); foreach (RoomLayer rl in roomLayers) { if (rl is TileLayer) { TileLayer crt = (TileLayer)rl; foreach (Tile t in crt.Tiles) { t.SourceTexture = ttt; Autotile.UpdateTile(t, crt); } } } w.Close(); }