/// <summary> /// Constructor for the bullet class /// </summary> /// <param name="a_world">World to create the bullet in</param> /// <param name="a_texture">Texture to draw for the bullet</param> /// <param name="a_position">where to create the bullet initially</param> /// <param name="a_velocity">initial velocity to assign the bullet</param> public Bullet(World a_world, Texture2D a_texture, Vector2 a_position, Vector2 a_velocity, PlayingGameState a_playingGameState) { m_sprite = new Sprite(a_world, a_texture, 5, 1, a_position); m_sprite.m_body.LinearVelocity = a_velocity; m_playingGameState = a_playingGameState; m_sprite.m_body.UserData = "bullet"; }
/// <summary> /// Allows the game to perform any initialization it needs to before starting to run. /// This is where it can query for any required services and load any non-graphic /// related content. Calling base.Initialize will enumerate through any components /// and initialize them as well. /// </summary> protected override void Initialize() { this.IsMouseVisible = true; ContentLibrary.LoadContentLibrary(Content); m_scoreList = new List<Score>(); m_scoreList = LevelIO.LoadScore("scores.bin"); //GameState Manager Stuff m_gameStateManager = new GameStateManager(this); PlayingGameState m_playingGameState = new PlayingGameState(m_gameStateManager, windowRes); LevelEditor m_editor = new LevelEditor(m_gameStateManager, windowRes); SplashScreen m_splash = new SplashScreen(m_gameStateManager, windowRes); MenuGameState m_menu = new MenuGameState(m_gameStateManager, windowRes); PauseGameState m_pause = new PauseGameState(m_gameStateManager, windowRes); TutorialGameState m_tutorial = new TutorialGameState(m_gameStateManager, windowRes); Level1GameState m_level1 = new Level1GameState(m_gameStateManager, windowRes); Level2GameState m_level2 = new Level2GameState(m_gameStateManager, windowRes); Level3GameState m_level3 = new Level3GameState(m_gameStateManager, windowRes); m_gameStateManager.SetState("playingGS", m_playingGameState); m_gameStateManager.SetState("editor", m_editor); m_gameStateManager.SetState("splashScreen", m_splash); m_gameStateManager.SetState("menu", m_menu); m_gameStateManager.SetState("pause", m_pause); m_gameStateManager.SetState("tutorial", m_tutorial); m_gameStateManager.SetState("level1", m_level1); m_gameStateManager.SetState("level2", m_level2); m_gameStateManager.SetState("level3", m_level3); m_gameStateManager.PushState("splashScreen"); base.Initialize(); for (int i = 0; i < m_scoreList.Count; i++) { Console.WriteLine(m_scoreList[i].m_level); Console.WriteLine(m_scoreList[i].m_time); Console.WriteLine(m_scoreList[i].m_deaths); } }
/// <summary> /// Constructor for the Turret class /// </summary> /// <param name="a_world">World for the turret to exist in</param> /// <param name="a_topTexture">Texture for the top of the turret</param> /// <param name="a_baseTexture">Texture for the base of the turret</param> /// <param name="a_bulletTexture">Texture for the bullet</param> /// <param name="a_position">Position to create the turret at !(warning, turret draws from centre of base)!</param> /// <param name="a_player">Target for the turret to follow</param> /// <param name="a_rotation">Initial rotation for the turret</param> public Turret( PlayingGameState a_state, Texture2D a_topTexture, Texture2D a_baseTexture, Texture2D a_bulletTexture, Vector2 a_position, Player a_player, float a_rotation) { Vector2 tempRot = new Vector2(-(float)Math.Sin(a_rotation), (float)Math.Cos(a_rotation)); m_bulletTexture = a_bulletTexture; m_state = a_state; m_base = new Sprite( m_state.m_world, a_baseTexture, new Vector2(80, 40), 50, a_position); m_base.m_body.BodyType = BodyType.Static; m_base.m_body.Rotation = (float)(Math.Atan2(tempRot.Y, tempRot.X) - (Math.PI / 2)); m_top = new Sprite( m_state.m_world, a_topTexture, 15, 50, m_base.Position + (tempRot * 15)); m_top.m_body.BodyType = BodyType.Static; m_angle = new Vector2(-1, 0); m_player = a_player; m_cooldown = 0; }
/// <summary> /// Constructor for the turret class, passing in a platform for level editing purposes /// </summary> /// <param name="a_state">World for the turret to exist in</param> /// <param name="a_topTexture">Texture for the top of the turret</param> /// <param name="a_baseTexture">Texture for the base of the turret</param> /// <param name="a_bulletTexture">Texture for the bullet</param> /// <param name="a_platform">Platform provides position and rotation to create the turret</param> /// <param name="a_player">Target for the turret to follow</param> public Turret( PlayingGameState a_state, Texture2D a_topTexture, Texture2D a_baseTexture, Texture2D a_bulletTexture, Platform a_platform, Player a_player) { m_bulletTexture = a_bulletTexture; m_state = a_state; Vector2 tempRot = new Vector2(-(float)Math.Sin(a_platform.m_sprite.m_body.Rotation - (Math.PI)), (float)Math.Cos(a_platform.m_sprite.m_body.Rotation - (Math.PI))); m_base = new Sprite( m_state.m_world, a_baseTexture, new Vector2(80, 40), 50, a_platform.m_sprite.Position + (tempRot * 26)); m_base.m_body.BodyType = BodyType.Static; m_base.m_body.Rotation = a_platform.m_sprite.m_body.Rotation - (float)(Math.PI); m_top = new Sprite( m_state.m_world, a_topTexture, 15, 50, m_base.Position + (tempRot * 15)); m_top.m_body.BodyType = BodyType.Static; m_angle = new Vector2(-1, 0); m_player = a_player; m_cooldown = 0; }
/// <summary> /// Saves level data from the playingGameState into the xml document specified by a_levelName /// </summary> /// <param name="a_levelName">The string that contains the name of the file the level data will be saved to.</param> /// <param name="a_state">The playingGameState from which the level data is retrieved and saved</param> public static void SaveLevel(String a_levelName, PlayingGameState a_state) { XmlDocument doc = new XmlDocument(); XmlElement rootElement = doc.CreateElement("LevelData"); XmlElement platforms = doc.CreateElement("Platforms"); XmlElement turrets = doc.CreateElement("Turrets"); for (int i = 0; i < a_state.m_platformList.Count; i++) { XmlElement platform = doc.CreateElement("Platform"); XmlAttribute x1 = doc.CreateAttribute("x1"); XmlAttribute y1 = doc.CreateAttribute("y1"); XmlAttribute x2 = doc.CreateAttribute("x2"); XmlAttribute y2 = doc.CreateAttribute("y2"); x1.Value = a_state.m_platformList[i].m_leftPoint.X.ToString(); y1.Value = a_state.m_platformList[i].m_leftPoint.Y.ToString(); x2.Value = a_state.m_platformList[i].m_rightPoint.X.ToString(); y2.Value = a_state.m_platformList[i].m_rightPoint.Y.ToString(); platform.Attributes.Append(x1); platform.Attributes.Append(y1); platform.Attributes.Append(x2); platform.Attributes.Append(y2); platforms.AppendChild(platform); } for (int i = 0; i < a_state.m_turretList.Count; i++) { XmlElement turret = doc.CreateElement("Turret"); XmlAttribute xPos = doc.CreateAttribute("xPos"); XmlAttribute yPos = doc.CreateAttribute("yPos"); XmlAttribute rotation = doc.CreateAttribute("rotation"); xPos.Value = a_state.m_turretList[i].m_base.Position.X.ToString(); yPos.Value = a_state.m_turretList[i].m_base.Position.Y.ToString(); rotation.Value = a_state.m_turretList[i].m_base.m_body.Rotation.ToString(); turret.Attributes.Append(xPos); turret.Attributes.Append(yPos); turret.Attributes.Append(rotation); turrets.AppendChild(turret); } XmlElement playerSpawn = doc.CreateElement("PlayerSpawn"); XmlAttribute playerLocationX = doc.CreateAttribute("px"); XmlAttribute playerLocationY = doc.CreateAttribute("py"); playerLocationX.Value = a_state.m_playerSpawnLocation.X.ToString(); playerLocationY.Value = a_state.m_playerSpawnLocation.Y.ToString(); playerSpawn.Attributes.Append(playerLocationX); playerSpawn.Attributes.Append(playerLocationY); XmlElement targetGoal = doc.CreateElement("Goal"); XmlAttribute goalX = doc.CreateAttribute("gx"); XmlAttribute goalY = doc.CreateAttribute("gy"); goalX.Value = a_state.m_goalLocation.X.ToString(); goalY.Value = a_state.m_goalLocation.Y.ToString(); targetGoal.Attributes.Append(goalX); targetGoal.Attributes.Append(goalY); //goalX.Value = a_state.m_goal.Position.X.ToString(); //goalY.Value = a_state.m_goal.Position.Y.ToString(); rootElement.AppendChild(playerSpawn); rootElement.AppendChild(targetGoal); rootElement.AppendChild(platforms); rootElement.AppendChild(turrets); doc.AppendChild(rootElement); doc.Save(a_levelName); }
/// <summary> /// Loads level data from the specified file and generates platform, turret, player location, and endpoint data in the playingGameState /// </summary> /// <param name="a_levelName">The file that the data is to be loaded from</param> /// <param name="a_state">The playingGameState that the data is to be loaded to</param> public static void LoadLevel(String a_levelName, PlayingGameState a_state) { for (int i = 0; i < a_state.m_platformList.Count; i++) { a_state.m_world.RemoveBody(a_state.m_platformList[i].m_sprite.m_body); a_state.m_world.RemoveBody(a_state.m_platformList[i].m_connection.m_body); a_state.m_world.RemoveBody(a_state.m_platformList[i].m_connection2.m_body); a_state.m_platformList.RemoveAt(i--); } for (int i = 0; i < a_state.m_turretList.Count; i++) { a_state.m_world.RemoveBody(a_state.m_turretList[i].m_base.m_body); a_state.m_world.RemoveBody(a_state.m_turretList[i].m_top.m_body); a_state.m_turretList.RemoveAt(i--); } a_state.m_goalLocation = new Vector2(0, 0); a_state.m_playerSpawnLocation = new Vector2(0, 0); XmlDocument xml = new XmlDocument(); #if PSM xml.Load(@"Application\Content\" + a_levelName); #else xml.Load(a_levelName); #endif XmlElement levelData = xml.DocumentElement; XmlElement playerSpawn = levelData.FirstChild as XmlElement; XmlElement goalLocation = playerSpawn.NextSibling as XmlElement; XmlElement platformList = goalLocation.NextSibling as XmlElement; XmlElement turretList = platformList.NextSibling as XmlElement; XmlElement platformElement = platformList.FirstChild as XmlElement; while (platformElement != null) { Vector2 leftPoint = new Vector2(0, 0); Vector2 rightPoint = new Vector2(0, 0); XmlAttribute x1 = platformElement.Attributes["x1"]; XmlAttribute y1 = platformElement.Attributes["y1"]; XmlAttribute x2 = platformElement.Attributes["x2"]; XmlAttribute y2 = platformElement.Attributes["y2"]; try { float.TryParse(x1.Value, out leftPoint.X); float.TryParse(y1.Value, out leftPoint.Y); float.TryParse(x2.Value, out rightPoint.X); float.TryParse(y2.Value, out rightPoint.Y); a_state.m_platformList.Add(new Platform(a_state.m_world, leftPoint, rightPoint, ContentLibrary.platformTexture)); } catch (Exception) { // failed to load XML Console.WriteLine("Failed to load platforms"); } platformElement = platformElement.NextSibling as XmlElement; } XmlElement turretElement = turretList.FirstChild as XmlElement; while (turretElement != null) { Vector2 position = new Vector2(0, 0); float rotation = 0.0f; XmlAttribute xPos = turretElement.Attributes["xPos"]; XmlAttribute yPos = turretElement.Attributes["yPos"]; XmlAttribute rot = turretElement.Attributes["rotation"]; try { float.TryParse(xPos.Value, out position.X); float.TryParse(yPos.Value, out position.Y); float.TryParse(rot.Value, out rotation); a_state.m_turretList.Add( new Turret( a_state, ContentLibrary.turretTopTexture, ContentLibrary.turretBaseTexture, ContentLibrary.circleTexture, position, a_state.m_player, rotation ) ); } catch (Exception) { // failed to load XML Console.WriteLine("Failed to load turrets"); } turretElement = turretElement.NextSibling as XmlElement; } XmlAttribute playerX = playerSpawn.Attributes["px"]; XmlAttribute playerY = playerSpawn.Attributes["py"]; try { float.TryParse(playerX.Value, out a_state.m_playerSpawnLocation.X); float.TryParse(playerY.Value, out a_state.m_playerSpawnLocation.Y); } catch (Exception) { // failed to load XML Console.WriteLine("Failed to load Player spawn location"); } XmlAttribute goalX = goalLocation.Attributes["gx"]; XmlAttribute goalY = goalLocation.Attributes["gy"]; try { float.TryParse(goalX.Value, out a_state.m_goalLocation.X); float.TryParse(goalY.Value, out a_state.m_goalLocation.Y); //Vector2 goalPos = new Vector2(0, 0); //float.TryParse(goalX.Value, out goalPos.X); //float.TryParse(goalY.Value, out goalPos.Y); //a_state.m_goal.Position = goalPos; } catch (Exception) { // failes to load XML Console.WriteLine("Failed to load goal location"); } }