/// <summary> /// Check if enemy bullet collides with entities /// </summary> /// <param name="scene">scene</param> void CheckEnemyBullet(MainScene scene) { // Check if bullet is enemy bullet if (Collider.Tags[0] == (int)Tags.Enemy) { // Check if bullet collides with player tags if (Collider.CollideEntity(X, Y, Tags.Player) != null) { // Check if bullet collides with player if (!(Collider.CollideEntities(X, Y, Tags.Player)[0].GetType() == typeof(Bullet))) { if (scene.player.Alive) { RemoveSelf(); scene.player.playerLives -= 1; scene.player.Die(); scene.livesLeftTxt.String = scene.player.playerLives.ToString(); scene.livesLeftTxt.Refresh(); } // Check if enemy bullet collides with player bullet else { RemoveSelf(); } } } } }
/// <summary> /// Load enemies from file. /// Loads from "/levels/", you only need to put "level1.xml" /// </summary> /// <param name="file">file name to load</param> public static void LoadEnemies(string file) { MainScene scene = Program.game.GetScene <MainScene>(); // Open document XmlDocument doc = new XmlDocument(); doc.Load("levels/" + file); // Current enemy position to load Vector2 CurPos = new Vector2(scene.PlayPosition.X, scene.PlayPosition.Y); foreach (XmlElement node in doc.DocumentElement.ChildNodes) { // Create new enemy and add to scene Enemy enemy = AllEnemies[node.GetAttribute("type")](); enemy.Position = CurPos; scene.Add(enemy); // Set enemy position CurPos.X += EnemySize; if (CurPos.X > 420) { CurPos.X = scene.PlayPosition.X; CurPos.Y += EnemySize; } } }
/// <summary> /// Handles player movement /// </summary> void UpdateMovement() { MainScene scene = Program.game.GetScene <MainScene>(); if (Alive) { // Check if player is moving left if (Input.KeyDown(Key.A) || Input.KeyDown(Key.Left)) { X -= MoveSpeed; } // Check if player is moving right if (Input.KeyDown(Key.D) || Input.KeyDown(Key.Right)) { X += MoveSpeed; } // Check if player is shooting if (Input.KeyDown(Key.Space) && !bullet.Visible) { Shoot(); } } // Check if player is in play area if (X < scene.PlayPosition.X) { X = scene.PlayPosition.X; } else if (X > scene.PlayPosition.X + scene.PlayWidth.X) { X = scene.PlayPosition.X + scene.PlayWidth.X; } }
/// <summary> /// Called every frame /// </summary> public override void Update() { base.Update(); Ressurrection.Update(); if (!Alive && Ressurrection.AtMax) { MainScene scene = Program.game.GetScene <MainScene>(); Alive = true; RemoveGraphic(playerDieImage); AddGraphic(playerImage); scene.player.SetPosition(new Vector2(scene.PlayPosition.X + scene.PlayWidth.X, scene.PlayPosition.Y + scene.PlayWidth.Y)); } UpdateMovement(); //If playerLives are 0, then switch to highscore screen if (playerLives == 0) { Game.SwitchScene(new HighScoresScene()); } }
/// <summary> /// class used for the player /// </summary> public Player() { MainScene scene = Program.game.GetScene <MainScene>(); // Set position SetPosition(new Vector2(scene.PlayPosition.X + scene.PlayWidth.X, scene.PlayPosition.Y + scene.PlayWidth.Y)); // Set image AddGraphic(playerImage); // Add collider BoxCollider Collider = new BoxCollider(30, 30, Tags.Player); AddCollider(Collider); // Initialize bullet Image playerBullet = new Image("Assets/playerBullet.png"); BoxCollider bulletCollider = new BoxCollider(playerBullet.Width, playerBullet.Height, Tags.Player); bullet = new Bullet(-6.0f, new Vector2(0, 0), bulletCollider); bullet.Visible = false; bullet.Collidable = false; bullet.AddGraphic(playerBullet); scene.Add(bullet); }
/// <summary> /// Check if bullet collides with entities. /// </summary> void CheckBulletCollision() { MainScene scene = Program.game.GetScene <MainScene>(); CheckPlayerBullet(scene); CheckEnemyBullet(scene); CheckBarricade(scene); CheckUfo(scene); }
/// <summary> /// Check if bullet collides with ufo. /// </summary> /// <param name="scene">scene</param> void CheckUfo(MainScene scene) { UFO ufo = (UFO)Collider.CollideEntity(X, Y, Tags.Ufo); // If collidable is ufo then removes it if (ufo != null) { ufo.Die(); scene.player.ScoreAmount += ufo.Score; scene.player.bullet.Visible = false; scene.player.bullet.Collidable = false; } }
/// <summary> /// Reads XML and creates HighScore and HighScoreLeaderboard with it /// </summary> /// <returns>HighScore values</returns> public static string MainScreenXML() { MainScene scene = (MainScene)Program.game.FirstScene; //Loads in XML XmlDocument xmlDoc = new XmlDocument(); if (!System.IO.File.Exists("savefile.xml")) { XmlElement root = xmlDoc.CreateElement("leaderboard"); xmlDoc.AppendChild(root); } else { xmlDoc.Load("savefile.xml"); } var xmlnodes = xmlDoc.DocumentElement.ChildNodes; //Gets the score value from the "score" attribute int curScore = 0; if (xmlnodes.Count >= 1) { if (xmlnodes[0].Attributes["score"].Value != "") { curScore = Int32.Parse(xmlnodes[0].Attributes["score"].Value); } } // TODO: if score is 0 then it throws error //Checks if the attributes are empty, if not, then curscore gets highest score value. for (int i = 0; i < xmlnodes.Count; i++) { if (xmlnodes[i].Attributes["score"].Value != "") { if (Int32.Parse(xmlnodes[i].Attributes["score"].Value) > curScore) { if (xmlnodes[i].Attributes["name"].Value != "") { curScore = Int32.Parse(xmlnodes[i].Attributes["score"].Value); } } } } //Returns the score as a string return(curScore.ToString()); }
//Sets a random timer when UFO appears public UFO() { Random rnd = new Random(); MainScene scene = Program.game.GetScene <MainScene>(); AppearTimer = new AutoTimer(rnd.Next(1000, 2000)); if (Enemy.hasMoved) { AppearTimer.Start(); } BoxCollider collider = new BoxCollider(24, 24, Tags.Ufo); Visible = false; Collidable = false; Score = rnd.Next(0, 3) * 50; AddCollider(collider); AddGraphic(EnemyImage); }
/// <summary> /// Handles enemy movement. /// </summary> void UpdateMovement() { MainScene scene = Program.game.GetScene <MainScene>(); List <Enemy> enemies = Scene.GetEntities <Enemy>(); SetPosition(Position + MoveDir); // Check if enemy has moved 24p in Y axis, if true moves them in the X axis if (HeightToMove <= 0) { MoveDir = NextMoveDir; HeightToMove = 24; hasMoved = true; } // Check if enemy is on the right side of the screen, if true moves them lower if (Position.X > scene.GetPlayArea().X) { SetPosition(Position - MoveDir); MoveDir = new Vector2(0.0f, MoveSpeed); NextMoveDir = new Vector2(-1.0f * MoveSpeed, 0.0f); } // Check if enemy is on the left side of the screen, if true moves them lower if (Position.X < scene.PlayPosition.X) { SetPosition(Position - MoveDir); MoveDir = new Vector2(0.0f, MoveSpeed); NextMoveDir = new Vector2(1.0f * MoveSpeed, 0.0f); } // Check if enemy is in the end of the level, if true ends game if (Position.Y >= scene.GetPlayArea().Y - 100) { Game.SwitchScene(new HighScoresScene()); } // Moves enemy HeightToMove -= MoveDir.Y / enemies.Count; }
/// <summary> /// Check if bullet collides with barricade. /// </summary> /// <param name="scene">scene</param> void CheckBarricade(MainScene scene) { Barricade barricade = (Barricade)Collider.CollideEntity(X, Y, Tags.Barricade); if (barricade != null) { // Check if player bullet hits barricade if (Collider.Tags[0] == (int)Tags.Player) { barricade.TakeDamage(); Visible = false; Collidable = false; } // Check if enemy bullet hits barricade else { barricade.TakeDamage(); RemoveSelf(); } } }
static void Main(string[] args) { // Creates game window game = new Game("Space Invader", 800, 600, 60, false); game.Color = Color.Black; FreeConsole(); // Creates game scene MainScene scene = new MainScene(); game.AddScene(scene); var a = game.Scenes; scene.Initialize(); //Change window variables game.WindowResize = false; game.SetIcon("Assets/windowPicture.png"); // Starts the game game.Start(); }
/// <summary> /// Check if player bullet collides with entities. /// </summary> /// <param name="scene">scene</param> void CheckPlayerBullet(MainScene scene) { // Check if bullet is player bullet if (Collider.Tags[0] == (int)Tags.Player) { // Check if bullet collides with player tags if (Collider.CollideEntity(X, Y, Tags.Enemy) != null) { // Check if bullet hits bullet if ((Collider.CollideEntities(X, Y, Tags.Enemy)[0].GetType() == typeof(Bullet))) { Collider.CollideEntities(X, Y, Tags.Enemy)[0].RemoveSelf(); Visible = false; Collidable = false; } // Check if bullet hits enemy else { Enemy enemy = (Enemy)Collider.CollideEntities(X, Y, Tags.Enemy)[0]; enemy.RemoveSelf(); Visible = false; Collidable = false; if (scene.GetEntities <Enemy>().Count <= 1) { scene.NextLevel(); } scene.player.ScoreAmount += enemy.Score; scene.curScoreTxt.String = scene.player.ScoreAmount.ToString(); Program.curScoreTxt = scene.player.ScoreAmount.ToString(); scene.curScoreTxt.Refresh(); } } } }
/// <summary> /// Called to initialize the game /// Note: must be added to Game.FirstScene before calling /// </summary> public void Initialize() { OnBegin = delegate { Enemy.Initialize(); Barricade.Initialize(); MainScene scene = Program.game.GetScene <MainScene>(); Ufo = new UFO(); Program.game.GetScene <MainScene>().Add(Ufo); // Create player and add to scene player = new Player(); Add(player); // Gametext for the entire game pretty much #region gameText var background = new Image("Assets/background.png"); background.Alpha = 0.4f; scene.AddGraphic(background); //Setting a default config file for the RichText to use var txtConfig = new RichTextConfig() { TextAlign = TextAlign.Center, CharColor = Color.Green, FontSize = 18, SineAmpX = 1, SineAmpY = 2, SineRateX = 1, Font = new Font("Assets/VCR_OSD_MONO.ttf"), }; // Writing the text graphics and setting position var livesLeftTxtLabel = new RichText("Lives", txtConfig); livesLeftTxtLabel.SetPosition(50, 16); scene.livesLeftTxt = new RichText(scene.player.playerLives.ToString(), txtConfig); scene.livesLeftTxt.Name = "livesLeftTxt"; scene.livesLeftTxt.SetPosition(70, 32); var highScoreTxtLabel = new RichText("Highscore", txtConfig); highScoreTxtLabel.SetPosition(350, 15); highScoreTxt = new RichText(ReadXML.MainScreenXML(), txtConfig); highScoreTxt.Name = "highScoreTxt"; highScoreTxt.SetPosition(380, 30); var curScoreTxtLabel = new RichText("Score", txtConfig); curScoreTxtLabel.SetPosition(650, 15); scene.curScoreTxt = new RichText(scene.player.ScoreAmount.ToString(), txtConfig); scene.curScoreTxt.Name = "curScoreTxt"; scene.curScoreTxt.SetPosition(670, 32); // Adds Graphic to Scene scene.AddGraphic(livesLeftTxtLabel); scene.AddGraphic(highScoreTxtLabel); scene.AddGraphic(curScoreTxtLabel); scene.AddGraphic(scene.livesLeftTxt); scene.AddGraphic(scene.highScoreTxt); scene.AddGraphic(scene.curScoreTxt); #endregion gameText }; }