private void CloneDrawableObjects(GameTypesXmlReader a_GameTypesXml) { Vector2 offset = new Vector2(); List<Object> clonedObjects = new List<object>(); foreach (Object item in m_SceneXmlReader.Objects) { Type type = item.GetType(); GameType gameType = a_GameTypesXml.GameTypes.Find(t => t.Name == type.Name); bool hasBehavior = false; bool hasValue = gameType.Behaviors.TryGetValue("Drawable", out hasBehavior); if (gameType != null && hasBehavior) { // get position PropertyInfo propertyInfo = type.GetProperty("Position"); Vector2 position = (Vector2)propertyInfo.GetValue(item, null); propertyInfo = type.GetProperty("SpriteRectangle"); Rectangle rectangle = (Rectangle)propertyInfo.GetValue(item, null); propertyInfo = type.GetProperty("SpriteRepeat"); Vector2 spriteRepeat = (Vector2)propertyInfo.GetValue(item, null); propertyInfo = type.GetProperty("RepeatRandomRangeX"); Vector2 repeatRandomRangeX = (Vector2)propertyInfo.GetValue(item, null); float repeatRandomWidthX = repeatRandomRangeX.Y - repeatRandomRangeX.X; propertyInfo = type.GetProperty("RepeatRandomRangeY"); Vector2 repeatRandomRangeY = (Vector2)propertyInfo.GetValue(item, null); float repeatRandomWidthY = repeatRandomRangeY.Y - repeatRandomRangeY.X; for (int y = 0; y < spriteRepeat.Y; y++) { for (int x = 0; x < spriteRepeat.X; x++) { if (x == 0 && y == 0) continue; //Do not clone the first one because it already exists Vector2 randomOffset = GetRandomOffset(repeatRandomRangeX, repeatRandomRangeY, repeatRandomWidthX, repeatRandomWidthY); offset.X = rectangle.Width * x + randomOffset.X; offset.Y = rectangle.Height * y + randomOffset.Y; ConstructorInfo ci = type.GetConstructor(Type.EmptyTypes); Object newObject = ci.Invoke(null); foreach (PropertyField property in gameType.Properties.Values) { propertyInfo = type.GetProperty(property.PropertyName); propertyInfo.SetValue(newObject, propertyInfo.GetValue(item, null), null); } propertyInfo = type.GetProperty("Position"); propertyInfo.SetValue(newObject, position + offset, null); clonedObjects.Add(newObject); } } } } m_SceneXmlReader.Objects.AddRange(clonedObjects); }
private void InitDynamicObjects(GameTypesXmlReader a_GameTypesXml) { foreach (Object item in m_SceneXmlReader.Objects) { Type type = item.GetType(); GameType gameType = a_GameTypesXml.GameTypes.Find(t => t.Name == type.Name); bool hasBehavior = false; bool hasValue = gameType.Behaviors.TryGetValue("Movable", out hasBehavior); if (gameType != null && hasBehavior) { PropertyInfo propertyInfo = type.GetProperty("Position"); Vector2 position = (Vector2)propertyInfo.GetValue(item, null); propertyInfo = type.GetProperty("InitialPosition"); propertyInfo.SetValue(item, position, null); PropertyInfo propertyInfoVel = type.GetProperty("Velocity"); Vector2 velocity = (Vector2)propertyInfoVel.GetValue(item, null); propertyInfo = type.GetProperty("RandomVelocityX"); Vector2 randomVelocityX = (Vector2)propertyInfo.GetValue(item, null); propertyInfo = type.GetProperty("RandomVelocityY"); Vector2 randomVelocityY = (Vector2)propertyInfo.GetValue(item, null); if (randomVelocityX != Vector2.Zero) { velocity.X = (float)m_Random.Next((int)randomVelocityX.X, (int)randomVelocityX.Y); } if (randomVelocityY != Vector2.Zero) { velocity.Y = (float)m_Random.Next((int)randomVelocityY.X, (int)randomVelocityY.Y); } propertyInfoVel.SetValue(item, velocity, null); } } }
protected override void Initialize() { // setup player m_Player.Sprite = Content.Load<Texture2D>(@"Sprites\TileSet.png"); m_Player.SpriteRectangle = new Rectangle(467, 302, 38, 192); m_Player.AABB = new Rectangle(0, 0, 38, 192); m_Player.InitBullets(); m_PlayerCollisionRectangle = new Rectangle(0,0,0,0); // setup dynamic game and create genGame.dll BehaviorTypesXmlReader behaviorTypesXml = new BehaviorTypesXmlReader(); behaviorTypesXml.Parse(Directory.GetCurrentDirectory() + @"\Content\BehaviorTypes.xml"); GameTypesXmlReader gameTypesXml = new GameTypesXmlReader(); gameTypesXml.Parse(Directory.GetCurrentDirectory() + @"\Content\GameTypes.xml", behaviorTypesXml.GameBehaviorProperties); gameTypesXml.ParseProperties(behaviorTypesXml.GameBehaviorProperties); GameTypesAssemblyBuilder ass = new GameTypesAssemblyBuilder(); ass.GenerateAssembly(gameTypesXml.GameTypes); m_SceneXmlReader = new SceneXmlReader(Content); m_SceneXmlReader.Parse(Directory.GetCurrentDirectory() + @"\Content\Scene.xml", gameTypesXml.GameTypes); CloneDrawableObjects(gameTypesXml); InitDynamicObjects(gameTypesXml); m_SceneManager = new SceneManager(); m_SceneManager.ParseObjects(m_SceneXmlReader.Objects, gameTypesXml.GameTypes, behaviorTypesXml.GameBehaviorProperties); List<Object> staticCollidableList = m_SceneManager.GetStaticObjectList("Collidable"); List<Object> staticDrawableList = m_SceneManager.GetStaticObjectList("Drawable"); m_DynamicObjects = m_SceneManager.GetObjectList("Movable"); List<Tuple<Rectangle, int>> staticRectangles = new List<Tuple<Rectangle, int>>(); foreach (Object obj in staticCollidableList) staticRectangles.Add(new Tuple<Rectangle, int>(GetRectangleFromObject(obj), GetHashFromObject(obj))); m_QuadTreeRect = new Rectangle(-3000, -3000, 6000, 6000); quadTree = new AssemblyQuadTree(m_QuadTreeRect, staticRectangles); // this class will generate the game assembly GameAssemblyBuilder gameAssemblyBuilder = new GameAssemblyBuilder(); //gameAssemblyBuilder.GenerateGameObjects(m_SceneXmlReader.Objects, gameTypesXml.GameTypes); gameAssemblyBuilder.GenerateDrawFunction(staticDrawableList); gameAssemblyBuilder.GenerateStaticCollisionFunction(quadTree); gameAssemblyBuilder.Save(); m_Collidables = staticCollidableList; // loads game dll (genGame.dll) LoadGameDLL(); // clear all game objects, wont delete objects that are still referenced! m_SceneXmlReader.Clear(); m_SceneManager.Clear(); CreateDebugQuad(); base.Initialize(); }