public override void LoadContent() { // Load Textures continueTex = Content.Load <Texture2D>("Textures/Menu/Continue"); optionsTex = Content.Load <Texture2D>("Textures/Menu/Options"); howToPlayTex = Content.Load <Texture2D>("Textures/Menu/HowToPlay"); exitTex = Content.Load <Texture2D>("Textures/Menu/Exit"); titleTex = Content.Load <Texture2D>("Textures/Menu/Title"); triangleTex = Content.Load <Texture2D>("Textures/Menu/TriangleMan"); newgameTex = Content.Load <Texture2D>("Textures/Menu/NewGame"); hexagonTex = Content.Load <Texture2D>("Textures/Menu/HexagonGuy"); // Create sprites for title and display characters sTitle = new Sprite3(true, titleTex, 0, 0); sTriangle = new Sprite3(true, triangleTex, 0, 0); // Set sprite offsets sTitle.setHSoffset(new Vector2(titleTex.Width / 2, titleTex.Height / 2)); sTriangle.setHSoffset(new Vector2(triangleTex.Width / 2, triangleTex.Height / 2)); // Set Position of Sprites sTitle.setPos(new Vector2(xAligned, 0 + sTitle.getHeight())); sTriangle.setPos(675, 275); buttonNewGame = new Button(newgameTex, new Vector2(xAligned, (95 + sTitle.getPosY()) + (newgameTex.Height)), Resources.ColorHighLight, Button.ButtonType.ResetGame, 0); buttonContinue = new Button(continueTex, new Vector2(xAligned, buttonNewGame.getPosY() + (minorOffset + continueTex.Height)), Resources.ColorHighLight, Button.ButtonType.ChangeLevelSet, Resources.currPlayLevel); buttonOptions = new Button(optionsTex, new Vector2(xAligned, buttonContinue.getPosY() + (minorOffset + optionsTex.Height)), Resources.ColorHighLight, Button.ButtonType.ChangeLevelSet, 5); buttonHowToPlay = new Button(howToPlayTex, new Vector2(xAligned, buttonOptions.getPosY() + (minorOffset + howToPlayTex.Height)), Resources.ColorHighLight, Button.ButtonType.ChangeLevelPush, 8); buttonExit = new Button(exitTex, new Vector2(xAligned, buttonHowToPlay.getPosY() + (minorOffset + exitTex.Height)), Resources.ColorHighLight, Button.ButtonType.Exit, 0); buttonHighScore = new Button(hexagonTex, new Vector2(135, 339), Resources.ColorLose, Button.ButtonType.ChangeLevelSet, 13); }
public override void LoadContent() { // Create a new SpriteBatch, which can be used to draw textures. spriteBatch = new SpriteBatch(graphicsDevice); // put a font to tell play to press space to start //Dir.texBackground = Util.texFromFile(graphicsDevice, Dir.dir + @"Art\Press_Space.png"); float xLocation = Dir.rightBoundary / 2; texpressStart = Util.texFromFile(graphicsDevice, Dir.dir + @"Art\Press_Space.png"); texStartSub = Util.texFromFile(graphicsDevice, Dir.dir + @"Art\navio-hi.png"); titleFont = Content.Load <SpriteFont>("display"); texTitle = Util.texFromFile(graphicsDevice, Dir.dir + @"Art\title.png"); introSound = Content.Load <SoundEffect>("intro_theme"); intro = introSound.CreateInstance(); pressStart = new Sprite3(true, texpressStart, xLocation - 302, Dir.topBoundary + 400); startSub = new Sprite3(true, texStartSub, movingXLocation, Dir.bottomBoundary / 2); title = new Sprite3(true, texTitle, xLocation - 300, 50); pressStart.setWidthHeight(pressStart.getWidth() * 2, pressStart.getHeight() * 2); title.setWidthHeight((title.getWidth() * 3) / 2, (title.getHeight() * 3) / 2); startSub.setWidthHeight(startSub.getWidth() / 2, startSub.getWidth() / 2); pressStart.setColor(Color.Aqua); startSub.setColor(Color.GreenYellow); }
/// <summary> /// Create a basic button /// </summary> /// <param name="tex">Button texture</param> /// <param name="pos">Position</param> /// <param name="highLight">Highlighted colour</param> /// <param name="buttonType">Button type</param> /// <param name="nextLevel">nextlevel/value if applicable</param> public Button(Texture2D tex, Vector2 pos, Color highLight, ButtonType buttonType, int nextLevel) { // Initialise our Button this.highLight = highLight; this.nextLevel = nextLevel; type = buttonType; button = new Sprite3(true, tex, 0, 0); button.setHSoffset(new Vector2(button.getWidth() / 2, button.getHeight() / 2)); button.setPos(pos); }
public override void LoadContent() { pauseHelpText = Content.Load <SpriteFont>("helpText"); texPauseScreen = Util.texFromFile(graphicsDevice, Dir.dir + @"Art\Pause.jpg"); // texBack = Util.texFromFile(graphicsDevice, Dir.dir + @"Art\Pause.jpg"); // texPause = Util.texFromFile(graphicsDevice, Dir.dir + @"Art\Pause2.jpg"); // pauseBackground = new ImageBackground(texBack, Color.White, graphicsDevice); pause = new Sprite3(true, texPause, 444, 30); pause.setWidthHeight(pause.getWidth() / 2, pause.getHeight() / 2); pause.setColor(Color.Aquamarine); }
public void Follow(Sprite3 target) { var position = Matrix.CreateTranslation( -target.getPosX() - (target.getWidth() / 2), -target.getPosY() - (target.getHeight() / 2), 0); var offset = Matrix.CreateTranslation( Game1.screenWidth / 2, Game1.screenHeight / 2, 0); Transform = position * offset; }
/// <summary> /// Allows the game to run logic such as updating the world, /// checking for collisions, gathering input, and playing audio. /// </summary> /// <param name="gameTime">Provides a snapshot of timing values.</param> /// This is a mess, please forgive me protected override void Update(GameTime gameTime) { //Console.WriteLine(enemies.count()); curPos = horse.getPos(); bool keyDown = false; switch (level) { case 1: horse.setWidthHeight(1568 / 8 * 0.3f, texHorseRun.Height * 0.3f); k = Keyboard.GetState(); if (!started) { started = true; StartMovement(8); } if (k.IsKeyDown(Keys.Down)) { keyDown = true; horse.setPosY(horse.getPosY() + movementSpeed - 2); } if (k.IsKeyDown(Keys.Up)) { keyDown = true; horse.setPosY(horse.getPosY() - movementSpeed + 2); } if (k.IsKeyDown(Keys.Left)) { keyDown = true; horse.setFlip(SpriteEffects.FlipHorizontally); horse.setPosX(horse.getPosX() - movementSpeed + 2); } if (k.IsKeyDown(Keys.Right)) { keyDown = true; horse.setFlip(SpriteEffects.None); horse.setPosX(horse.getPosX() + movementSpeed - 2); } if (!keyDown) { horse.setAnimationSequence(anim, 0, 7, 0); } else { horse.setAnimationSequence(anim, 0, 7, 8); } //387 330 if (curPos.X >= -155 && curPos.X <= -75 && curPos.Y >= 330 && curPos.Y <= 387) { horse.setPos(xx, yy); started = false; horse.setFlip(SpriteEffects.None); level = 0; } horseRun.animationTick(gameTime); mainCamera.Follow(horse); break; default: if (!started) { if (k.IsKeyDown(Keys.D1) || k.IsKeyDown(Keys.NumPad1)) { difficulty = 1; } else if (k.IsKeyDown(Keys.D2) || k.IsKeyDown(Keys.NumPad2)) { difficulty = 2; } else if (k.IsKeyDown(Keys.D3) || k.IsKeyDown(Keys.NumPad3)) { difficulty = 3; } } switch (difficulty) { case 1: difficultyOffset = 1; break; case 2: difficultyOffset = 0.5f; break; case 3: difficultyOffset = 0.1f; break; default: break; } horse.setWidthHeight(1568 / 8 * 0.5f, texHorseRun.Height * 0.5f); //This timer makes basic instructions disappear after 3 seconds if (textFadeTimer < 3 && started) { textFadeTimer += (float)gameTime.ElapsedGameTime.TotalSeconds; } if (started) { enemySpawnTimer += (float)gameTime.ElapsedGameTime.TotalSeconds; } if (enemySpawnTimer > difficultyOffset) { enemySpawnTimer = 0; LoadEnemies(); } //Escape key exits game if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape)) { Exit(); } //Keyboard current and previous state prevK = k; k = Keyboard.GetState(); //Begin all game functionality essentially if (k.IsKeyDown(Keys.Enter) && !started) { started = true; StartMovement(8); } if (started) { //Bounding box activation key if (k.IsKeyDown(Keys.B) && prevK.IsKeyUp(Keys.B)) { showbb = !showbb; } //Game over is a bool that is used to ensure the player can't move horse after it is dead, causes errors otherwise if (!gameOver) { //Player movement down if (k.IsKeyDown(Keys.Down)) { if (horseRun.getSprite(0).getPosY() < bot - horse.getHeight()) { horseRun.getSprite(0).setPosY(horseRun.getSprite(0).getPosY() + movementSpeed); } } //Player movement up if (k.IsKeyDown(Keys.Up)) { if (horseRun.getSprite(0).getPosY() > top) { horseRun.getSprite(0).setPosY(horseRun.getSprite(0).getPosY() - movementSpeed); } } } for (int e = 0; e < enemies.count(); e++) { if (enemies[e].getVisible()) { enemies[e].setPosX(enemies[e].getPosX() - enemyMovementSpeed); } if (enemies[e].getPosX() < 0 - texEnemy.Width) { enemies[e].setVisible(false); } } //Collision detection, arrow to enemy for (int ea = 0; ea < quiver.count(); ea++) { int ac = enemies.collisionWithRect(quiver[ea].getBoundingBoxAA()); if (ac != -1) { score++; Blood(enemies.getSprite(ac).getPosX(), enemies.getSprite(ac).getPosY(), false); enemies.getSprite(ac).setVisible(false); quiver[ea].setVisible(false); quiver[ea].setPos(new Vector2(0, 0)); //LoadEnemies(); } } //Allow player to increase or decrease speed if (Keyboard.GetState().IsKeyDown(Keys.Right)) { enemyMovementSpeed = fastEnemy; horse.setAnimationSequence(anim, 0, 7, fastPlayer); } else if (Keyboard.GetState().IsKeyDown(Keys.Left)) { enemyMovementSpeed = slowEnemy; horse.setAnimationSequence(anim, 0, 7, slowPlayer); } else if (started) { enemyMovementSpeed = normalEnemy; horse.setAnimationSequence(anim, 0, 7, normalPlayer); } //Scrolling background functionality if (scrolling1.rectangle.X + scrolling1.texture.Width <= 0) { scrolling1.rectangle.X = scrolling2.rectangle.X + scrolling2.texture.Width; } if (scrolling2.rectangle.X + scrolling2.texture.Width <= 0) { scrolling2.rectangle.X = scrolling1.rectangle.X + scrolling1.texture.Width; } scrolling1.Update(); scrolling2.Update(); //Game over is a bool that is used to ensure the player can't move horse after it is dead, causes errors otherwise if (!gameOver) { //Collision detection, enemy to player int rc = enemies.collisionWithRect(horse.getBoundingBoxAA()); if (rc != -1) { Blood(horse.getPosX(), horse.getPosY(), true); movementSpeed = 0; enemyMovementSpeed = 0; scrolling1.speed = 0; scrolling2.speed = 0; for (int i = 0; i < horseRun.count(); i++) { horseRun.deleteSprite(i); } gameOver = true; } } //Shooting arrow functionality if (Keyboard.GetState().IsKeyDown(Keys.Space) && !arrowShot) { Arrow(); arrowShot = true; } for (int a = 0; a < quiver.count(); a++) { if (quiver[a].getVisible()) { quiver[a].savePosition(); quiver[a].moveByDeltaXY(); } if (quiver[a].getPosX() > 800) { quiver[a].setVisible(false); } } if (arrowShot) { arrowTimer += (float)gameTime.ElapsedGameTime.TotalSeconds; } if (arrowTimer > 0.5f) { arrowTimer = 0; arrowShot = false; } } //Animation ticks for anything that is being animated horseRun.animationTick(gameTime); enemies.animationTick(gameTime); bloodSplat.animationTick(gameTime); if (gameOver) { deathTimer += (float)gameTime.ElapsedGameTime.TotalSeconds; } break; } base.Update(gameTime); }
/// <summary> /// LoadContent will be called once per game and is the place to load /// all of your content. /// </summary> protected override void LoadContent() { //Create a new SpriteBatch, which can be used to draw textures. spriteBatch = new SpriteBatch(GraphicsDevice); mainCamera = new Camera(); //Load all the textures and fonts gameOverText = Content.Load <SpriteFont>("MedievalFont"); directions = Content.Load <SpriteFont>("Gold"); font = Content.Load <SpriteFont>("MedievalFont"); difficultySelectText = Content.Load <SpriteFont>("MedievalFont"); startText = Content.Load <SpriteFont>("Gold"); texStartBanner = Util.texFromFile(GraphicsDevice, dir + "startBanner.png"); texGoldBanner = Util.texFromFile(GraphicsDevice, dir + "goldBanner.png"); texBook = Util.texFromFile(GraphicsDevice, dir + "openBookWithText.png"); texHorseRun = Util.texFromFile(GraphicsDevice, dir + "horseRun.png"); texEnemy = Util.texFromFile(GraphicsDevice, dir + "Enemy.png"); texBlood = Util.texFromFile(GraphicsDevice, dir + "bloodSide.png"); texArrow = Util.texFromFile(GraphicsDevice, dir + "Arrow.png"); texWorldMap = Util.texFromFile(GraphicsDevice, dir + "FantasyWorldMap_2.png"); //Define playarea playArea = new Rectangle(lhs, top, rhs - lhs, bot - top); // width and height //Load sprites and change size if necessary worldMap = new Sprite3(true, texWorldMap, -2000, -1000); worldMap.setWidthHeight(6400, 4800); book = new Sprite3(true, texBook, 50, 50); book.setWidthHeight(700, 500); startBanner = new Sprite3(true, texStartBanner, 0, 0); startBanner.setWidthHeight(800, 600); goldBanner = new Sprite3(true, texGoldBanner, 15, 15); goldBanner.setWidthHeight(130, 40); horse = new Sprite3(true, texHorseRun, xx, yy); //Load some empty spritelists enemies = new SpriteList(); bloodSplat = new SpriteList(); horseRun = new SpriteList(); quiver = new SpriteList(); for (int a = 0; a < 5; a++) { arrow = new Sprite3(false, texArrow, 0, 0); arrow.setWidthHeight(arrow.getWidth() * 0.09f, arrow.getHeight() * 0.09f); quiver.addSpriteReuse(arrow); } //Used to change size of sprites float scale = 0.5f; //Player animation setup horse.setXframes(8); horse.setWidthHeight(1568 / 8 * scale, texHorseRun.Height * scale); horse.setBB(30, 10, (horse.getWidth() / scale) - 60, horse.getHeight() / scale - 20); for (int h = 0; h < anim.Length; h++) { anim[h].X = h; } horseRun.addSpriteReuse(horse); //enemy.setBBToTexture(); //Enemy animation setup for (int k = 0; k < 100; k++) { enemy = new Sprite3(false, texEnemy, 850, 400); enemy.setXframes(10); enemy.setYframes(5); enemy.setWidthHeight(320 / 10, 160 / 5); enemy.setBB(0, 0, enemy.getWidth(), enemy.getHeight()); int count = 0; for (int i = 0; i < 5; i++) { for (int j = 0; j < 10; j++) { animEnemy[count].X = j; animEnemy[count].Y = i; count++; } } enemy.setAnimationSequence(animEnemy, 20, 29, 15); enemy.setAnimFinished(0); enemy.animationStart(); enemy.setVisible(false); enemies.addSpriteReuse(enemy); } //Load scrolling background images scrolling1 = new Scrolling(Util.texFromFile(GraphicsDevice, dir + "GPT Background 800x600.png"), new Rectangle(0, 0, 800, 600), scrollingSpeed); scrolling2 = new Scrolling(Util.texFromFile(GraphicsDevice, dir + "GPT Background2 800x600.png"), new Rectangle(800, 0, 800, 600), scrollingSpeed); }
/// <summary> /// Allows the game to run logic such as updating the world, /// checking for collisions, gathering input, and playing audio. /// </summary> /// <param name="gameTime">Provides a snapshot of timing values.</param> protected override void Update(GameTime gameTime) { // TODO: Add your update logic here InputManager.Instance.Update(); //deltaTime is in seconds, so calculations = pixels per second. deltaTime = (float)gameTime.ElapsedGameTime.TotalSeconds; //Press enter to start game/level if (!startGame) { if (InputManager.Instance.KeyPressed(Keys.Enter)) { startGame = true; } return; } timer += deltaTime; //update player spritePlayer.Update(gameTime); //update backgrounds scrollBack.Update(gameTime); scrollFore.Update(gameTime); scrollBack.setScrollSpeed(playerSpeeeeeed - 2); scrollFore.setScrollSpeed(playerSpeeeeeed); //limit scroll speed if (playerSpeeeeeed > -1) { playerSpeeeeeed = -1; } else if (playerSpeeeeeed < -10) { playerSpeeeeeed = -10; } //Every 2 seconds, spawn an enemy randomly. if (timer > spawnInterval) { AddEnemy(enemyList); timer = 0; } //inputs if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape)) { Exit(); } if (InputManager.Instance.KeyPressed(Keys.B)) { showbb = !showbb; } //PLAYER MOVEMENT if (InputManager.Instance.KeyDown(Keys.Left)) { if (spritePlayer.getPosX() > playArea.Left) { spritePlayer.setPosX(spritePlayer.getPosX() - playerSpeed); } playerSpeeeeeed += 0.1f; } if (InputManager.Instance.KeyDown(Keys.Right)) { if (spritePlayer.getPosX() < playArea.Right - spritePlayer.getWidth()) { spritePlayer.setPosX(spritePlayer.getPosX() + playerSpeed); } playerSpeeeeeed += -0.1f; } if (InputManager.Instance.KeyDown(Keys.Up)) { if (spritePlayer.getPosY() > playArea.Bottom) { spritePlayer.setPosY(spritePlayer.getPosY() - playerSpeed); } } if (InputManager.Instance.KeyDown(Keys.Down)) { if (spritePlayer.getPosY() < playArea.Top - spritePlayer.getHeight()) { spritePlayer.setPosY(spritePlayer.getPosY() + playerSpeed); } } //PLAYER SHOOT if (InputManager.Instance.KeyPressed(Keys.Space)) { AddBullet((int)spritePlayer.getPosX(), (int)spritePlayer.getPosY()); } //Update all enemies and bullets to move. also inactive if outofbounds //In the past I called this instead of having an extended class with custom update // enemyList.moveDeltaX(-3); //Update/remove enemies enemyList.Update(gameTime); enemyList.removeIfOutside(new Rectangle(-100, -100, SCREEN_WIDTH + 200, SCREEN_HEIGHT + 200)); //update/remove bullets playerBulletList.moveDeltaX(5); playerBulletList.removeIfOutside(new Rectangle(-100, -100, SCREEN_WIDTH + 200, SCREEN_HEIGHT + 200)); //If player collides with enemy int collisionIndex = enemyList.collisionAA(spritePlayer); if (collisionIndex != -1) { Sprite3 temp = enemyList.getSprite(collisionIndex); //player collides with boss if (temp.getName() == "boss") { } else { //enemy dies? temp.active = false; temp.visible = false; AddExplosion((int)temp.getPosX(), (int)temp.getPosY()); score -= 5; textScore.text = "Score : " + score.ToString(); } } //if a bullet collides with enemy for (int i = 0; i < playerBulletList.count(); i++) { Sprite3 currentBullet = playerBulletList.getSprite(i); if (currentBullet == null) { continue; } if (currentBullet.active != true) { continue; } if (currentBullet.visible != true) { continue; } //if collision success int collision = enemyList.collisionAA(currentBullet); if (collision != -1) { Sprite3 currentEnemy = enemyList.getSprite(collision); //kill both enemy and bullet currentEnemy.active = false; currentEnemy.visible = false; currentBullet.active = false; currentBullet.visible = false; //add particle AddExplosion((int)currentEnemy.getPosX(), (int)currentEnemy.getPosY()); //add score score += 10; textScore.text = "Score : " + score.ToString(); } } //update particles particleList.animationTick(gameTime); base.Update(gameTime); }
public override void Update(GameTime gameTime) { base.Update(gameTime); float deltaTime = (float)gameTime.ElapsedGameTime.TotalSeconds; //MOVEMENT //ALL CHECKS AND CHANGES BEFORE THE POSITION SET. //PLAYER SLICE COOLDOWN if (canSlice == false) { sliceTimer += deltaTime; } if (sliceTimer > sliceCooldown) { sliceTimer = 0; canSlice = true; iconSlice.isReady = true; } //DASH MECHANIC...no gravity for a duration //if (justDashed) //{ // acceleration.Y = 0; // noGravityTimer += deltaTime; // if(noGravityTimer > noGravityLimit) // { // noGravityTimer = 0; // justDashed = false; // } //} velocity += acceleration * deltaTime; if (velocity.X > velocityHorizontalMovespeedCap) { velocity.X -= accelerationDecay.X * deltaTime; if (velocity.X < velocityHorizontalMovespeedCap) { velocity.X = velocityHorizontalMovespeedCap; } acceleration.X = 0; } else if (velocity.X < -velocityHorizontalMovespeedCap) { velocity.X += accelerationDecay.X * deltaTime; if (velocity.X > -velocityHorizontalMovespeedCap) { velocity.X = -velocityHorizontalMovespeedCap; } acceleration.X = 0; } //FINALLY SET POSITION //set x pos. setPosX(getPosX() + velocity.X * deltaTime); //check horizontal collisions //COLLISION TEST WITH PLATFORMS for (int i = 0; i < platformList.count(); i++) { Sprite3 currentPlatform = platformList.getSprite(i); if (this.collision(currentPlatform)) { if (velocity.X > 0) { velocity.X = 0; this.setPosX(currentPlatform.getPosX() - this.getWidth()); } else if (velocity.X < 0) { velocity.X = 0; this.setPosX(currentPlatform.getPosX() + currentPlatform.getWidth()); } } } setPosY(getPosY() + velocity.Y * deltaTime); //COLLISION TEST WITH PLATFORMS for (int i = 0; i < platformList.count(); i++) { Sprite3 currentPlatform = platformList.getSprite(i); if (this.collision(currentPlatform)) { if (velocity.Y >= 0 && this.getPosY() <= currentPlatform.getPosY()) { this.setPos(getPosX(), currentPlatform.getPosY() - this.getHeight()); velocity.Y = 0; IsOnGround = true; } else if (velocity.Y < 0 && this.getPosY() >= currentPlatform.getPosY()) { this.setPos(getPosX(), currentPlatform.getPosY() + currentPlatform.getHeight()); velocity.Y = 0; } } } //If no input movement, slow down player if (canPlayerMove) { if (InputManager.Instance.KeyUp(Keys.Left) && InputManager.Instance.KeyUp(Keys.Right) && GamePad.GetState(PlayerIndex.One).ThumbSticks.Left.X == 0) { acceleration.X = 0; if (velocity.X != 0) { if (velocity.X > 0) { velocity.X -= deltaTime * velocityDecay.X; if (velocity.X < 0) { velocity.X = 0; } } else if (velocity.X < 0) { velocity.X += deltaTime * velocityDecay.X; if (velocity.X > 0) { velocity.X = 0; } } } } else { //If travelling right and player moves left, increase the acceleration to opposite if (InputManager.Instance.KeyDown(Keys.Left)) { if (acceleration.X > -HorizontalMoveSpeed) { acceleration.X = -HorizontalMoveSpeed; } //If player velocity is more than the cap, slowly decay to the cap. if (velocity.X > 0) { velocity.X -= deltaTime * velocityDecay.X; if (velocity.X < 0) { velocity.X = 0; } } } if (GamePad.GetState(PlayerIndex.One).ThumbSticks.Left.X != 0) { acceleration.X = HorizontalMoveSpeed * GamePad.GetState(PlayerIndex.One).ThumbSticks.Left.X; if (GamePad.GetState(PlayerIndex.One).ThumbSticks.Left.X > 0) { if (velocity.X < 0) { velocity.X += deltaTime * velocityDecay.X; if (velocity.X > 0) { velocity.X = 0; } } } else if (GamePad.GetState(PlayerIndex.One).ThumbSticks.Left.X < 0) { if (velocity.X > 0) { velocity.X -= deltaTime * velocityDecay.X; if (velocity.X < 0) { velocity.X = 0; } } } } if (InputManager.Instance.KeyDown(Keys.Right)) { if (acceleration.X < HorizontalMoveSpeed) { acceleration.X = HorizontalMoveSpeed; } if (velocity.X < 0) { velocity.X += deltaTime * velocityDecay.X; if (velocity.X > 0) { velocity.X = 0; } } } //If both left and right pressed down, 0 acceleration if (InputManager.Instance.KeyDown(Keys.Right) && InputManager.Instance.KeyDown(Keys.Left)) { acceleration.X = 0; } } } //Is the player currently on a platform? bool isOnPlatform = false; for (int i = 0; i < platformList.count(); i++) { Sprite3 currentPlatform = platformList.getSprite(i); if (currentPlatform.getActive()) { if (currentPlatform.getBoundingBoxAA().Intersects(rectPlatformCheck)) { isOnPlatform = true; } } } if (isOnPlatform == false) { IsOnGround = false; } //PLAYER JUMP. if (canPlayerMove) { if (InputManager.Instance.KeyPressed(Keys.Z) || InputManager.Instance.ButtonPressed(Buttons.A)) { if (IsOnGround) { velocity.Y = -jumpVelocity; IsOnGround = false; soundJump.Play(); } else { if (canDoubleJump == true) { velocity.Y = -doubleJumpVelocity; canDoubleJump = false; iconDoubleJump.isReady = false; soundDoubleJump.Play(); } } } } //If player holds the jump button, gets maximum jump, otherwise fall faster. if (canPlayerMove) { if (InputManager.Instance.KeyDown(Keys.Z) || InputManager.Instance.ButtonDown(Buttons.A) && IsOnGround == false) { acceleration.Y = accelerationPlayerFall.Y; } else { acceleration.Y = accelerationDefault.Y; } } //PLAYER DASH if (canPlayerMove) { if (InputManager.Instance.KeyPressed(Keys.C) || InputManager.Instance.ButtonPressed(Buttons.B)) { if (canDash) { if (InputManager.Instance.KeyDown(Keys.Right) || GamePad.GetState(PlayerIndex.One).ThumbSticks.Left.X > 0) { velocity.X = horizontalDashVelocity; velocity.Y = -20; canDash = false; iconDash.isReady = false; noGravityTimer = 0; justDashed = true; soundDash.Play(); } else if (InputManager.Instance.KeyDown(Keys.Left) || GamePad.GetState(PlayerIndex.One).ThumbSticks.Left.X < 0) { velocity.X = -horizontalDashVelocity; velocity.Y = -20; canDash = false; iconDash.isReady = false; noGravityTimer = 0; justDashed = true; soundDash.Play(); } } } } if (IsOnGround == false) { if (velocity.Y > 0) { //check for platforms isCheckGroundPlatforms = true; } } else { isCheckGroundPlatforms = false; } rectPlatformCheck = new Rectangle((int)this.getPosX(), (int)this.getPosY() + (int)this.getHeight(), (int)this.getWidth(), 5); }
public override void Update(GameTime gameTime) { base.Update(gameTime); float deltaTime = (float)gameTime.ElapsedGameTime.TotalSeconds; Game1.TimeScore += deltaTime; cam.Pos = new Vector2(cam.Pos.X, Game1.SCREEN_HEIGHT / 2); if (InputManager.Instance.KeyDown(Keys.W)) { cam.Pos = new Vector2(cam.Pos.X, cam.Pos.Y - 5); } if (InputManager.Instance.KeyDown(Keys.S)) { cam.Pos = new Vector2(cam.Pos.X, cam.Pos.Y + 5); } if (InputManager.Instance.KeyDown(Keys.A)) { cam.Pos = new Vector2(cam.Pos.X - 5, cam.Pos.Y); } if (InputManager.Instance.KeyDown(Keys.D)) { cam.Pos = new Vector2(cam.Pos.X + 5, cam.Pos.Y); } if (InputManager.Instance.KeyPressed(Keys.Back)) { gameStateManager.setLevel(gameStateManager.getCurrentLevelNum() - 1); } //CAMERA STICK ON PLAYER. if (cam.Pos.X < player.getPosX() - 100) { cam.Pos = new Vector2(player.getPosX() - 100, cam.Pos.Y); } else if (cam.Pos.X > player.getPosX() + player.getWidth() + 100) { cam.Pos = new Vector2(player.getPosX() + player.getWidth() + 100, cam.Pos.Y); } ////Move camera to infront of player. //if (player.velocity.X > 0) //{ // CamPlayerPositiveVelocity = true; //} //else if (player.velocity.X < 0) //{ // CamPlayerPositiveVelocity = false; //} //if (CamPlayerPositiveVelocity) //{ // cam.Pos = new Vector2(cam.Pos.X + 10, cam.Pos.Y); //} //else //{ // cam.Pos = new Vector2(cam.Pos.X - 10, cam.Pos.Y); //} if (cam.Pos.X < CamOriginalPosition.X) { cam.Pos = new Vector2(CamOriginalPosition.X, cam.Pos.Y); } else if (cam.Pos.X + Game1.SCREEN_WIDTH / 2 > OutOfBounds.X + OutOfBounds.Width) { cam.Pos = new Vector2(OutOfBounds.X + OutOfBounds.Width - CamOriginalPosition.X, cam.Pos.Y); } //cam.Pos = new Vector2(Game1.SCREEN_WIDTH/2, Game1.SCREEN_HEIGHT/2); if (InputManager.Instance.KeyPressed(Keys.B)) { showbb = !showbb; } if (InputManager.Instance.MousePressed(MouseButton.Left)) { Slice newSlice = new Slice((float)InputManager.Instance.GetMousePositionX() - Slice.sliceWidth / 2 + cam.Pos.X - Game1.SCREEN_WIDTH / 2, (float)InputManager.Instance.GetMousePositionY()); sliceList.addSpriteReuse(newSlice); for (int i = 0; i < enemyList.count(); i++) { Sprite3 currentEnemy = enemyList.getSprite(i); } } //PLAYER SLICE if (InputManager.Instance.KeyPressed(Keys.X) || InputManager.Instance.ButtonPressed(Buttons.RightShoulder)) { if (player.canSlice == true) { Slice newSlice = new Slice(player.getBoundingBoxMiddle().X - Slice.sliceWidth / 2, player.getBoundingBoxMiddle().Y); sliceList.addSpriteReuse(newSlice); player.Slice(); soundSlice.Play(); } } if (InputManager.Instance.MousePressed(MouseButton.Right)) { TestEnemy newEnemy = new TestEnemy(InputManager.Instance.GetMousePosition() + cam.Pos - new Vector2(Game1.SCREEN_WIDTH / 2 + 70 / 2, Game1.SCREEN_HEIGHT / 2 + 70 / 2)); enemyList.addSpriteReuse(newEnemy); Console.WriteLine("targetSpawnPositions.Add(new Vector2(" + newEnemy.getPos().X + "," + newEnemy.getPos().Y + "));"); } player.Update(gameTime); sliceList.Update(gameTime); particleList.Update(gameTime); enemyList.Update(gameTime); platformList.Update(gameTime); goal.Update(gameTime); //GOAL CONDITION if (player.getBoundingBoxAA().Intersects(goal.rectGoalZone)) { if (player.IsOnGround) { //WIN player.SetCanPlayerMove(false); player.velocity = Vector2.Zero; player.acceleration = new Vector2(0, 1000f); NextLevel(); } } //update abilities abilityIconDoubleJump.Update(gameTime); abilityIconDash.Update(gameTime); abilityIconSlice.Update(gameTime); //IF PLAYER IS OUT OF BOUNDS if (player.getPosX() < OutOfBounds.X || player.getPosX() > OutOfBounds.X + OutOfBounds.Width || player.getPosY() + player.getHeight() * 2 < OutOfBounds.Y || player.getPosY() > OutOfBounds.Y + OutOfBounds.Height) { soundDeath.Play(); player.SetCanPlayerMove(false); player.AbilityReset(); ResetPlayer(); } //DETECT COLLISION BETWEEN SLICES AND TARGETS for (int i = 0; i < enemyList.count(); i++) { //Get current enemy to test. //If not active, skip. Sprite3 currentSprite = enemyList.getSprite(i); if (currentSprite == null) { continue; } if (currentSprite.active != true) { continue; } if (currentSprite.visible != true) { continue; } if (currentSprite is Enemy == false) { continue; } TestEnemy currentEnemy = currentSprite as TestEnemy; //Get current slice to test. //If not active, skip. for (int j = 0; j < sliceList.count(); j++) { currentSprite = sliceList.getSprite(j); if (currentSprite == null) { continue; } if (currentSprite.active != true) { continue; } if (currentSprite.visible != true) { continue; } if (currentSprite is Slice) { Slice currentSlice = currentSprite as Slice; //loop thru all the current enemy's hitboxes until a slice collides with it. for (int k = 0; k < currentEnemy.listHitbox.count(); k++) { Sprite3 currentHitBox = currentEnemy.listHitbox.getSprite(k); if (currentHitBox == null) { continue; } if (currentHitBox.active != true) { continue; } if (currentEnemy.active == false) { continue; } //Check if the slice has hit anything else yet. if (currentSlice.canHit == true) { //SLICE HIT THE ENEMYHITBOX. if (currentSlice.collision(currentHitBox)) { float sliceWidth = currentSlice.getBB().Width; float sliceLeft = currentSlice.getBoundingBoxMiddle().X - sliceWidth / 2; float sliceRight = currentSlice.getBoundingBoxMiddle().X + sliceWidth / 2; float hitboxWidth = currentHitBox.getBB().Width; float hitboxLeft = currentHitBox.getBoundingBoxMiddle().X - hitboxWidth / 2; float hitboxRight = currentHitBox.getBoundingBoxMiddle().X + hitboxWidth / 2; if (sliceLeft <= hitboxLeft && sliceRight >= hitboxRight) { //Console.WriteLine("FULL CUT"); soundSliceHit.Play(); SliceParticle topSlice; float scaleWidth = currentHitBox.getWidth() / currentEnemy.getTextureBase().Width; float scaleHeight = currentHitBox.getHeight() / currentEnemy.getTextureBase().Height; Vector2 ScaleVec = new Vector2(scaleWidth, scaleHeight); Rectangle topDest = new Rectangle(currentHitBox.getBoundingBoxAA().X, currentHitBox.getBoundingBoxAA().Y, currentHitBox.getBoundingBoxAA().Width, (int)currentSlice.getBoundingBoxMiddle().Y - currentHitBox.getBoundingBoxAA().Y); float topScaleX; float topScaleY; Rectangle topSourceRect = new Rectangle(0, 0, (int)(currentHitBox.getBoundingBoxAA().Width / scaleWidth), (int)((currentSlice.getBoundingBoxMiddle().Y - currentHitBox.getBoundingBoxAA().Y) / scaleHeight)); //Rectangle topSourceRect = currentHitBox.texBase.Bounds; //Console.WriteLine("TOPSLICE: " + topDest.ToString()); topSlice = new SliceParticle(topDest, currentEnemy.getTextureBase(), topSourceRect, ScaleVec, 0.8f, new Vector2(player.velocity.X / 2, -50 - Math.Abs(player.velocity.X))); particleList.addReuse(topSlice); SliceParticle bottomSlice; Rectangle bottomDest = new Rectangle(currentHitBox.getBoundingBoxAA().X, (int)currentSlice.getBoundingBoxAA().Y, currentHitBox.getBoundingBoxAA().Width, currentHitBox.getBoundingBoxAA().Y + currentHitBox.getBoundingBoxAA().Height - currentSlice.getBoundingBoxAA().Y); Rectangle bottomSourceRect = new Rectangle(0, (int)((currentSlice.getBoundingBoxMiddle().Y - currentHitBox.getBoundingBoxAA().Y) / scaleHeight), (int)(currentHitBox.getBoundingBoxAA().Width / scaleWidth), (int)(currentEnemy.texBase.Height - ((currentSlice.getBoundingBoxMiddle().Y - currentHitBox.getBoundingBoxAA().Y) / scaleHeight))); bottomSlice = new SliceParticle(bottomDest, currentEnemy.getTextureBase(), bottomSourceRect, ScaleVec, -0.8f, new Vector2(player.velocity.X / 2, 0 - Math.Abs(player.velocity.X) / 2)); particleList.addReuse(bottomSlice); //Console.WriteLine("BOTTOMSLICE: " + bottomDest.ToString()); if (!currentEnemy.canRespawn) { currentEnemy.setActive(false); } else { currentEnemy.isDead = true; } currentEnemy.setVisible(false); player.AbilityReset(); break; } } } } } else { continue; } } } }
public override void Update(GameTime gameTime) { base.Update(gameTime); selector.Update(gameTime); //MOVE OPTION if (InputManager.Instance.KeyPressed(Keys.Up) || InputManager.Instance.ButtonPressed(Buttons.DPadUp)) { menuSelector -= 1; if (menuSelector < 0) { menuSelector = menuStrings.Count - 1; } soundMenu.Play(); } if (InputManager.Instance.KeyPressed(Keys.Down) || InputManager.Instance.ButtonPressed(Buttons.DPadDown)) { menuSelector += 1; if (menuSelector > menuStrings.Count - 1) { menuSelector = 0; } soundMenu.Play(); } //SELECT OPTION if (notSelected) { if (InputManager.Instance.KeyPressed(Keys.Z) || InputManager.Instance.ButtonPressed(Buttons.A)) { notSelected = false; switch (menuStrings[menuSelector]) { case "START GAME": BaseLevel.isChallengeMode = false; Task.Delay(1000).ContinueWith(t => gameStateManager.setLevel(1)); break; case "TUTORIAL": BaseLevel.isChallengeMode = false; Task.Delay(1000).ContinueWith(t => gameStateManager.setLevel(0)); break; case "EXIT GAME": Task.Delay(1000).ContinueWith(t => Game1.game1.Exit()); break; case "CHALLENGE MODE": BaseLevel.isChallengeMode = true; Task.Delay(1000).ContinueWith(t => gameStateManager.setLevel(1)); break; default: Game1.game1.Exit(); break; } sliceList.addSpriteReuse(new Slice(selector.getPosX(), selector.getPosY() + selector.getHeight() / 2)); } } // particleList.Update(gameTime); sliceList.Update(gameTime); //DETECT COLLISION BETWEEN SLICES AND TARGETS for (int i = 0; i < enemyList.count(); i++) { //Get current enemy to test. //If not active, skip. Sprite3 currentSprite = enemyList.getSprite(i); if (currentSprite == null) { continue; } if (currentSprite.active != true) { continue; } if (currentSprite.visible != true) { continue; } if (currentSprite is Enemy == false) { continue; } Enemy currentEnemy = currentSprite as Enemy; //Get current slice to test. //If not active, skip. for (int j = 0; j < sliceList.count(); j++) { currentSprite = sliceList.getSprite(j); if (currentSprite == null) { continue; } if (currentSprite.active != true) { continue; } if (currentSprite.visible != true) { continue; } if (currentSprite is Slice) { Slice currentSlice = currentSprite as Slice; //loop thru all the current enemy's hitboxes until a slice collides with it. for (int k = 0; k < currentEnemy.listHitbox.count(); k++) { Sprite3 currentHitBox = currentEnemy.listHitbox.getSprite(k); if (currentHitBox == null) { continue; } if (currentHitBox.active != true) { continue; } if (currentEnemy.active == false) { continue; } //Check if the slice has hit anything else yet. if (currentSlice.canHit == true) { //SLICE HIT THE ENEMYHITBOX. if (currentSlice.collision(currentHitBox)) { float sliceWidth = currentSlice.getBB().Width; float sliceLeft = currentSlice.getBoundingBoxMiddle().X - sliceWidth / 2; float sliceRight = currentSlice.getBoundingBoxMiddle().X + sliceWidth / 2; float hitboxWidth = currentHitBox.getBB().Width; float hitboxLeft = currentHitBox.getBoundingBoxMiddle().X - hitboxWidth / 2; float hitboxRight = currentHitBox.getBoundingBoxMiddle().X + hitboxWidth / 2; if (sliceLeft <= hitboxLeft && sliceRight >= hitboxRight) { //Console.WriteLine("FULL CUT"); BaseLevel.soundSliceHit.Play(); SliceParticle topSlice; float scaleWidth = currentHitBox.getWidth() / currentEnemy.getTextureBase().Width; float scaleHeight = currentHitBox.getHeight() / currentEnemy.getTextureBase().Height; Vector2 ScaleVec = new Vector2(scaleWidth, scaleHeight); Rectangle topDest = new Rectangle(currentHitBox.getBoundingBoxAA().X, currentHitBox.getBoundingBoxAA().Y, currentHitBox.getBoundingBoxAA().Width, (int)currentSlice.getBoundingBoxMiddle().Y - currentHitBox.getBoundingBoxAA().Y); float topScaleX; float topScaleY; Rectangle topSourceRect = new Rectangle(0, 0, (int)(currentHitBox.getBoundingBoxAA().Width / scaleWidth), (int)((currentSlice.getBoundingBoxMiddle().Y - currentHitBox.getBoundingBoxAA().Y) / scaleHeight)); //Rectangle topSourceRect = currentHitBox.texBase.Bounds; //Console.WriteLine("TOPSLICE: " + topDest.ToString()); topSlice = new SliceParticle(topDest, currentEnemy.getTextureBase(), topSourceRect, ScaleVec, 0.8f, new Vector2(200, -50 - Math.Abs(100))); particleList.addReuse(topSlice); SliceParticle bottomSlice; Rectangle bottomDest = new Rectangle(currentHitBox.getBoundingBoxAA().X, (int)currentSlice.getBoundingBoxAA().Y, currentHitBox.getBoundingBoxAA().Width, currentHitBox.getBoundingBoxAA().Y + currentHitBox.getBoundingBoxAA().Height - currentSlice.getBoundingBoxAA().Y); Rectangle bottomSourceRect = new Rectangle(0, (int)((currentSlice.getBoundingBoxMiddle().Y - currentHitBox.getBoundingBoxAA().Y) / scaleHeight), (int)(currentHitBox.getBoundingBoxAA().Width / scaleWidth), (int)(currentEnemy.texBase.Height - ((currentSlice.getBoundingBoxMiddle().Y - currentHitBox.getBoundingBoxAA().Y) / scaleHeight))); bottomSlice = new SliceParticle(bottomDest, currentEnemy.getTextureBase(), bottomSourceRect, ScaleVec, -0.8f, new Vector2(-100, 0 - Math.Abs(-5))); particleList.addReuse(bottomSlice); //Console.WriteLine("BOTTOMSLICE: " + bottomDest.ToString()); currentEnemy.setActive(false); currentEnemy.setVisible(false); break; } } } } } else { continue; } } } }
public override void Draw(GameTime gameTime) { if (score >= 1000) { status = "THE END!"; } else if (player.hitPoints == 0) { status = "YOU DEAD!"; } else { status = "Missile Left: " + missileCount; } spriteBatch.Begin(); backgroundLevel2.Draw(spriteBatch); spriteBatch.DrawString(scoreFont, "Score: " + score + "/1000", new Vector2(20, 20), Color.Black); // Draw Score on top left spriteBatch.DrawString(scoreFont, status, new Vector2(1100, 20), Color.Black); // Draw missile count spriteBatch.Draw(texMouse, new Rectangle((int)mouse_x, (int)mouse_y, 16, 16), Color.White); // draw the mouse here enemy1.Draw(spriteBatch); enemy2.Draw(spriteBatch); player.Draw(spriteBatch); missileSmoke.Draw(spriteBatch); if (timer > 300) { life.setActiveAndVisible(true); life.draw(spriteBatch); } // If certain conditions are met, then draw these weapons if (drawMissile == true && missileCount != 0) { missile.draw(spriteBatch); missile.setPosY(missile.getPosY() - 3); missileSmoke.sysPos = new Vector2(missile.getPosX() + missile.getWidth() / 2, missile.getPosY() + missile.getHeight()); } if (anchorDraw == true) { anchor.setActiveAndVisible(true); anchor.draw(spriteBatch); anchor.setPosY(anchor.getPosY() + 4); } if (drawMines && mineCount == 1) { mine.setPos(minePos.X, minePos.Y); mine.draw(spriteBatch); } // If there is a collision, draw explosion animation if (collision1 == true) { drawMissile = false; drawExplosion(currXMissile, currYMissile); enemy1.setPos(Dir.rnd.Next(-400, -250), Dir.rnd.Next(200, 700)); if (explostionTimer == 100) { collision1 = false; score += 50; explostionTimer = 0; } } if (collision2 == true) { drawMissile = false; drawExplosion(currXMissile, currYMissile); enemy2.setPos(Dir.rnd.Next(-400, -250), Dir.rnd.Next(200, 700)); if (explostionTimer == 80) { score += 100; collision2 = false; explostionTimer = 0; missileSmoke.deActivate(); } } if (mineCollisionToggle == true) { mineCollission = false; mineExplostionAnimation.setPos(mineCurrPos.X - mine.getWidth() / 2, mineCurrPos.Y - mine.getHeight() / 2); mineExplostionAnimation.draw(spriteBatch); if (explostionTimer == 100) { mineExplostionAnimation.makeInactive = true; enemy1.setPos(Dir.rnd.Next(-400, -250), Dir.rnd.Next(200, 700)); mineCollisionToggle = false; explostionTimer = 0; } } if (mineCollisionToggle2 == true) { mineCollission2 = false; mineExplostionAnimation.setPos(mineCurrPos.X - mine.getWidth() / 2, mineCurrPos.Y - mine.getHeight() / 2); mineExplostionAnimation.draw(spriteBatch); if (explostionTimer == 100) { mineExplostionAnimation.makeInactive = true; enemy2.setPos(Dir.rnd.Next(-400, -250), Dir.rnd.Next(200, 700)); mineCollisionToggle2 = false; explostionTimer = 0; } } // Draw BB if (showBB) { player.drawInfo(spriteBatch, Color.White, Color.Yellow); enemy1.drawInfo(spriteBatch, Color.White, Color.Yellow); enemy2.drawInfo(spriteBatch, Color.White, Color.Yellow);; missile.drawInfo(spriteBatch, Color.White, Color.Yellow); life.drawInfo(spriteBatch, Color.White, Color.Yellow); mine.drawInfo(spriteBatch, Color.White, Color.Yellow); anchor.drawInfo(spriteBatch, Color.White, Color.Yellow); //weaponList.drawInfo(spriteBatch, Color.White, Color.Yellow); } spriteBatch.End(); }
public override void Update(GameTime gameTime) { prevKeyState = keyState; keyState = Keyboard.GetState(); timer += 1; explostionTimer += 1; explosionAnimation.animationTick(gameTime); mineExplostionAnimation.animationTick(gameTime); missileSmoke.Update(gameTime); exLimSound.Update(gameTime); previousMouseState = currentMouseState; currentMouseState = Mouse.GetState(); mouse_x = currentMouseState.X; mouse_y = currentMouseState.Y; // Sets up mine if (currentMouseState.LeftButton == ButtonState.Pressed) { minePos.X = mouse_x; minePos.Y = mouse_y; drawMines = true; mine.active = true; mineCount = 1; } // Controls player movements if (keyState.IsKeyDown(Keys.Left)) { if (player.getPosX() > Dir.leftBoundary) { player.setPosX(player.getPosX() - 4); // Moves player 3 units left player.setFlip(SpriteEffects.FlipHorizontally); } } if (keyState.IsKeyDown(Keys.Right)) { if (player.getPosX() < (Dir.rightBoundary - (player.getWidth() - 5))) { player.setPosX(player.getPosX() + 4); // Moves player 3 units right player.setFlip(SpriteEffects.None); } } // Moves enemy sprite enemy1.setPosX(enemy1.getPosX() + Dir.rnd.Next(1, 3)); enemy2.setPosX(enemy2.getPosX() - Dir.rnd.Next(1, 3)); // If enemy runs off screen, reset position to random x postion if (enemy1.getPosX() > Dir.rightBoundary + 60) { enemy1.setPos(Dir.rnd.Next(-400, -250), Dir.rnd.Next(200, 700)); } if (enemy2.getPosX() < Dir.leftBoundary - (enemy2.getWidth() + 50)) { enemy2.setPos(Dir.rnd.Next(Dir.rightBoundary, Dir.rightBoundary + 200), Dir.rnd.Next(200, 700)); } // Shoot missile if (keyState.IsKeyDown(Keys.Space) && prevKeyState.IsKeyUp(Keys.Space)) { missile.setPos(player.getPosX() + player.getWidth() / 2, player.getPosY() - missile.getHeight()); missile.active = true; drawSmoke(); drawMissile = true; if (missileCount > 0) { missileCount--; } } if (missileCount == 0) { missileCount = 5; } if (missile.getPosY() < 185 + missile.getHeight()) { missile.setPos(player.getPos()); drawMissile = false; missileSmoke.deActivate(); } if (timer % 361 == 360) { anchor.setPos(enemy1.getPosX() + enemy1.getWidth() / 2, enemy1.getPosY() + enemy1.getHeight() / 2); anchorDraw = true; } if (anchor.getPosY() > Dir.bottomBoundary + anchor.getHeight()) { anchor.active = false; anchorDraw = false; } // Collision Control enemy1Collision = missile.collision(enemy1); enemy2Collision = missile.collision(enemy2); if (enemy1Collision) { enemy1.hitPoints = enemy1.hitPoints - 10; currXMissile = enemy1.getPosX() + enemy1.getWidth() / 2; currYMissile = enemy1.getPosY() - enemy1.getHeight() / 2; if (enemy1.hitPoints <= 0) { collision1 = true; enemy1.hitPoints = 10; Dir.enemy1Count++; missileSmoke.deActivate(); } getSetMissile(); } if (enemy2Collision) { missileSmoke.deActivate(); enemy2.hitPoints = enemy2.hitPoints - 10; drawMissile = false; if (enemy2.hitPoints <= 0) { currXMissile = enemy2.getPosX() + enemy2.getWidth() / 2; currYMissile = enemy2.getPosY() - enemy2.getHeight() / 2; collision2 = true; Dir.enemy2Count++; enemy2.hitPoints = 20; } getSetMissile(); } // Gain 10 hp if missile hits the anchor preserve bool lifeGain = missile.collision(life); if (lifeGain == true) { if (player.hitPoints < 50) { player.hitPoints = player.hitPoints + 10; } lifeGain = false; drawMissile = false; life.active = false; life.setPos(new Vector2(Dir.rnd.Next(250, 800), Dir.rnd.Next(400, 600))); missile.active = false; missile.setPos(player.getPosX(), player.getPosY()); missileSmoke.deActivate(); } bool anchorCollision = anchor.collision(player); if (anchorCollision) { anchorDraw = false; anchor.setActiveAndVisible(false); anchor.setPos(new Vector2(-100, -100)); player.hitPoints = player.hitPoints - 20; } mineCollission = mine.collision(enemy1); if (mineCollission) { mine.active = false; drawMines = false; mineCurrPos = mine.getPos(); mine.setPos(new Vector2(-100, 0)); enemy1.setPos(Dir.rnd.Next(-400, -250), Dir.rnd.Next(200, 700)); mineCollisionToggle = true; score += 150; mineCount = 0; Dir.enemy1Count += 1; exLimSound.playSoundIfOk(); } mineCollission2 = mine.collision(enemy2); if (mineCollission2) { mine.active = false; drawMines = false; mineCurrPos = mine.getPos(); mine.setPos(new Vector2(-100, 0)); enemy2.setPos(Dir.rnd.Next(-400, -250), Dir.rnd.Next(200, 700)); mineCollisionToggle2 = true; score += 200; mineCount = 0; Dir.enemy2Count += 1; exLimSound.playSoundIfOk(); } // Toggle BB if (keyState.IsKeyDown(Keys.B) && prevKeyState.IsKeyUp(Keys.B)) { showBB = !showBB; } // Pause if (keyState.IsKeyDown(Keys.P) && !prevKeyState.IsKeyDown(Keys.P)) { gameStateManager.pushLevel(3); } // Help State if (keyState.IsKeyDown(Keys.F1) && !prevKeyState.IsKeyDown(Keys.F1)) { gameStateManager.pushLevel(5); } if (player.hitPoints < 0) { player.hitPoints = 0; } // End State if (score >= 1000 || player.hitPoints <= 0) { if (timer % 181 == 180) { gameStateManager.setLevel(4); } } }
public override void LoadContent() { // Create a new SpriteBatch, which can be used to draw textures. spriteBatch = new SpriteBatch(graphicsDevice); weaponList = new SpriteList(20); playerXLocation = Dir.rnd.Next(0, 1000); playerYLocation = 880; enemyXLocation = -20; enemyYLocation = Dir.rnd.Next(213, 600); enemy2XLocation = Dir.rnd.Next(Dir.rightBoundary - 300, Dir.rightBoundary); anchorXLocation = Dir.rnd.Next(Dir.leftBoundary + 100, Dir.rightBoundary - 100); anchorYLocation = 175; texBackgroundLevel2 = Util.texFromFile(graphicsDevice, Dir.dir + @"Art\Sea04.png"); texPlayer = Util.texFromFile(graphicsDevice, Dir.dir + @"Art\ship-md.png"); texEnemy1 = Util.texFromFile(graphicsDevice, Dir.dir + @"Art\Merchant1.png"); texEnemy2 = Util.texFromFile(graphicsDevice, Dir.dir + @"Art\Merchant2.png"); texMissile = Util.texFromFile(graphicsDevice, Dir.dir + @"Art\Torpedo5up.png"); texMissileSmoke = Util.texFromFile(graphicsDevice, Dir.dir + @"Art\Particle1.png"); texExplostionAnimation = Util.texFromFile(graphicsDevice, Dir.dir + @"Art\explodeWater1024x64.png"); texMineExplostion = Util.texFromFile(graphicsDevice, Dir.dir + @"Art\explode1.png"); texMouse = Util.texFromFile(graphicsDevice, Dir.dir + @"Art\Mouse.png"); texAnchor = Util.texFromFile(graphicsDevice, Dir.dir + @"Art\anchor-th.png"); texLife = Util.texFromFile(graphicsDevice, Dir.dir + @"Art\life-preserver-th.png"); texMine = Util.texFromFile(graphicsDevice, Dir.dir + @"Art\water_mine.png"); scoreFont = Content.Load <SpriteFont>("display"); helpFont = Content.Load <SpriteFont>("helpText"); explostionSound = Content.Load <SoundEffect>("bazooka_fire"); exLimSound = new LimitSound(explostionSound, 3); backgroundLevel2 = new ImageBackground(texBackgroundLevel2, Color.White, graphicsDevice); player = new Sprite3(true, texPlayer, playerXLocation, playerYLocation); enemy1 = new Sprite3(true, texEnemy1, enemyXLocation, enemyYLocation); enemy2 = new Sprite3(true, texEnemy2, enemy2XLocation, enemyYLocation); explosionAnimation = new Sprite3(true, texExplostionAnimation, 0, playerYLocation); mineExplostionAnimation = new Sprite3(true, texMineExplostion, 0, playerYLocation); mine = new Sprite3(true, texMine, 0, 0); mine.setHSoffset(new Vector2(296, 313)); weaponList.addSpriteReuse(mine); life = new Sprite3(true, texLife, Dir.rnd.Next(Dir.leftBoundary + 200, Dir.rightBoundary - 200), Dir.rnd.Next(300, 600)); life.setActiveAndVisible(false); for (int i = 1; i < 3; i++) { anchor = new Sprite3(true, texAnchor, anchorXLocation, anchorYLocation); anchor.setWidthHeight(50, 50); weaponList.addSpriteReuse(anchor); } for (int m = 1; m < 6; m++) { missile = new Sprite3(true, texMissile, playerXLocation, playerYLocation); missile.setWidthHeight(missile.getWidth() / 2, missile.getHeight() / 2); weaponList.addSpriteReuse(missile); } player.setColor(Color.Red); // Adjusting sprite image sizes to suit my taste player.setWidthHeight(210, 64); enemy1.setWidthHeight(256, 64); enemy2.setWidthHeight(enemy1.getWidth() / 2, 64); life.setWidthHeight(48, 48); mine.setWidthHeight(40, 45); // animation for torpedo explosion explosionAnimation.setWidthHeightOfTex(1024, 64); explosionAnimation.setXframes(16); explosionAnimation.setYframes(1); explosionAnimation.setWidthHeight(80, 80); for (int moveX = 0; moveX <= 15; moveX++) { anim[moveX].X = moveX; anim[moveX].Y = 0; } explosionAnimation.setAnimationSequence(anim, 0, 15, 3); explosionAnimation.setAnimFinished(0); explosionAnimation.animationStart(); // animation for mine explostion mineExplostionAnimation.setWidthHeightOfTex(1024, 64); mineExplostionAnimation.setXframes(16); mineExplostionAnimation.setYframes(1); mineExplostionAnimation.setWidthHeight(80, 80); for (int moveX = 0; moveX <= 15; moveX++) { mineAnim[moveX].X = moveX; mineAnim[moveX].Y = 0; } mineExplostionAnimation.setAnimationSequence(mineAnim, 0, 15, 4); mineExplostionAnimation.setAnimFinished(0); mineExplostionAnimation.animationStart(); HealthBarAttached h1 = new HealthBarAttached(Color.Aquamarine, Color.Green, Color.Red, 9, true); h1.offset = new Vector2(0, -1); // one pixel above the bounding box h1.gapOfbar = 2; enemy1.hitPoints = 10; enemy1.maxHitPoints = 10; enemy1.attachedRenderable = h1; HealthBarAttached h2 = new HealthBarAttached(Color.Aquamarine, Color.Green, Color.Red, 9, true); h2.offset = new Vector2(0, -1); // one pixel above the bounding box h2.gapOfbar = 2; enemy2.hitPoints = 20; enemy2.maxHitPoints = 20; enemy2.attachedRenderable = h2; HealthBarAttached playerHP = new HealthBarAttached(Color.Aquamarine, Color.Green, Color.Red, 9, true); playerHP.offset = new Vector2(0, -1); // one pixel above the bounding box playerHP.gapOfbar = 2; player.hitPoints = 50; player.maxHitPoints = 50; player.attachedRenderable = playerHP; drawSmoke(); }
public override void Update(GameTime gameTime) { prevKeyState = keyState; keyState = Keyboard.GetState(); timer += 1; explosionAnimation.animationTick(gameTime); missileSmoke.Update(gameTime); if (keyState.IsKeyDown(Keys.Left)) { if (player.getPosX() > Dir.leftBoundary) { player.setPosX(player.getPosX() - 4); // Moves player 4 units left player.setFlip(SpriteEffects.FlipHorizontally); } } if (keyState.IsKeyDown(Keys.Right)) { if (player.getPosX() < (Dir.rightBoundary - (player.getWidth() - 5))) { player.setPosX(player.getPosX() + 4); // Moves player 4 units right player.setFlip(SpriteEffects.None); } } enemy1.setPosX(enemy1.getPosX() + Dir.rnd.Next(1, 3)); enemy2.setPosX(enemy2.getPosX() - Dir.rnd.Next(1, 3)); enemy3.setPosX(enemy3.getPosX() + Dir.rnd.Next(2, 4)); // If enemy runs off screen, reset position to random x postion if (enemy1.getPosX() > Dir.rightBoundary + 60) { enemy1.setPos(Dir.rnd.Next(-400, -250), Dir.rnd.Next(200, 700)); } if (enemy2.getPosX() < Dir.leftBoundary - (enemy2.getWidth() + 50)) { enemy2.setPos(Dir.rnd.Next(Dir.rightBoundary, Dir.rightBoundary + 200), Dir.rnd.Next(200, 700)); } if (enemy3.getPosX() > Dir.rightBoundary + 60) { enemy3.setPos(Dir.rnd.Next(-400, -250), Dir.rnd.Next(200, 700)); } if (keyState.IsKeyDown(Keys.Space) && prevKeyState.IsKeyUp(Keys.Space)) { missile.setPos(player.getPosX() + player.getWidth() / 2, player.getPosY() - missile.getHeight()); missile.active = true; drawMissile = true; drawSmoke(); if (missileCount > 0) { missileCount--; } } if (missileCount == 0) { missileCount = 5; } if (missile.getPosY() < 185 + missile.getHeight()) { missile.setPos(player.getPos()); drawMissile = false; missileSmoke.deActivate(); } enemy1Collision = missile.collision(enemy1); enemy2Collision = missile.collision(enemy2); enemy3Collision = missile.collision(enemy3); explostionTimer += 1; if (enemy1Collision) { enemy1.hitPoints = enemy1.hitPoints - 10; currXMissile = enemy1.getPosX() + enemy1.getWidth() / 2; currYMissile = enemy1.getPosY() - enemy1.getHeight() / 2; if (enemy1.hitPoints <= 0) { collision1 = true; Dir.enemy1Count++; enemy1.hitPoints = 10; } getSetMissile(); } if (enemy2Collision) { missileSmoke.deActivate(); enemy2.hitPoints = enemy2.hitPoints - 10; drawMissile = false; if (enemy2.hitPoints <= 0) { currXMissile = enemy2.getPosX() + enemy2.getWidth() / 2; currYMissile = enemy2.getPosY() - enemy2.getHeight() / 2; collision2 = true; Dir.enemy2Count++; enemy2.hitPoints = 20; } getSetMissile(); } if (enemy3Collision) { enemy3.hitPoints = enemy3.hitPoints - 10; currXMissile = enemy3.getPosX() + enemy3.getWidth() / 2; currYMissile = enemy3.getPosY() - enemy3.getHeight() / 2; if (enemy3.hitPoints <= 0) { collision3 = true; Dir.enemy3Count++; enemy3.hitPoints = 10; } getSetMissile(); } // Toggle showing Boundary Box if (keyState.IsKeyDown(Keys.B) && prevKeyState.IsKeyUp(Keys.B)) { showBoundary = !showBoundary; } if (keyState.IsKeyDown(Keys.P) && !prevKeyState.IsKeyDown(Keys.P)) { gameStateManager.pushLevel(3); } if (keyState.IsKeyDown(Keys.F1) && !prevKeyState.IsKeyDown(Keys.F1)) { gameStateManager.pushLevel(5); } if (score >= 300) { if (timer % 181 == 180) { gameStateManager.setLevel(2); } } exLimSound.Update(gameTime); }
public override void LoadContent() { enemyList = new SpriteList(20); playerXLocation = Dir.rnd.Next(0, 1000); playerYLocation = 880; enemyXLocation = -20; enemyYLocation = Dir.rnd.Next(213, 600); enemy2XLocation = Dir.rnd.Next(Dir.rightBoundary - 300, Dir.rightBoundary); enemy3YLocation = enemyYLocation + Dir.rnd.Next(100, 200); // Image Locations Dir.texBackground = Util.texFromFile(graphicsDevice, Dir.dir + @"Art\Sea04.png"); texPlayer = Util.texFromFile(graphicsDevice, Dir.dir + @"Art\ship-md.png"); texEnemy1 = Util.texFromFile(graphicsDevice, Dir.dir + @"Art\Merchant1.png"); texEnemy2 = Util.texFromFile(graphicsDevice, Dir.dir + @"Art\Merchant2.png"); texMissile = Util.texFromFile(graphicsDevice, Dir.dir + @"Art\Torpedo5up.png"); texMissileSmoke = Util.texFromFile(graphicsDevice, Dir.dir + @"Art\Particle1.png"); texExplostionAnimation = Util.texFromFile(graphicsDevice, Dir.dir + @"Art\explode1.png"); scoreFont = Content.Load <SpriteFont>("display"); helpFont = Content.Load <SpriteFont>("helpText"); explostionSound = Content.Load <SoundEffect>("bazooka_fire"); exLimSound = new LimitSound(explostionSound, 3); // Set Sprites Dir.background = new ImageBackground(Dir.texBackground, Color.White, graphicsDevice); player = new Sprite3(true, texPlayer, playerXLocation, playerYLocation); enemy1 = new Sprite3(true, texEnemy1, enemyXLocation, enemyYLocation); enemy2 = new Sprite3(true, texEnemy2, enemy2XLocation, enemyYLocation); enemy3 = new Sprite3(true, texEnemy1, enemyXLocation, enemy3YLocation); missile = new Sprite3(true, texMissile, playerXLocation, playerYLocation); explosionAnimation = new Sprite3(true, texExplostionAnimation, 0, playerYLocation); // Adjusting sprite image sizes to suit my taste player.setWidthHeight(210, 64); enemy1.setWidthHeight(256, 64); enemy2.setWidthHeight(enemy1.getWidth() / 2, 64); enemy3.setWidthHeight(256, 64); missile.setWidthHeight(missile.getWidth() / 2, missile.getHeight() / 2); HealthBarAttached h1 = new HealthBarAttached(Color.Aquamarine, Color.Green, Color.Red, 9, true); h1.offset = new Vector2(0, -1); // one pixel above the bounding box h1.gapOfbar = 2; enemy1.hitPoints = 10; enemy1.maxHitPoints = 10; enemy1.attachedRenderable = h1; HealthBarAttached h2 = new HealthBarAttached(Color.Aquamarine, Color.Green, Color.Red, 9, true); h2.offset = new Vector2(0, -1); // one pixel above the bounding box h2.gapOfbar = 2; enemy2.hitPoints = 20; enemy2.maxHitPoints = 20; enemy2.attachedRenderable = h2; HealthBarAttached h3 = new HealthBarAttached(Color.Aquamarine, Color.Green, Color.Red, 9, true); h3.offset = new Vector2(0, -1); // one pixel above the bounding box h3.gapOfbar = 2; enemy3.hitPoints = 10; enemy3.maxHitPoints = 10; enemy3.attachedRenderable = h3; // Add to sprite list enemyList.addSpriteReuse(enemy1); enemyList.addSpriteReuse(enemy2); enemyList.addSpriteReuse(enemy3); explosionAnimation.setWidthHeightOfTex(1024, 64); explosionAnimation.setXframes(16); explosionAnimation.setYframes(1); explosionAnimation.setWidthHeight(90, 90); for (int moveX = 0; moveX <= 15; moveX++) { anim[moveX].X = moveX; anim[moveX].Y = 0; } explosionAnimation.setAnimationSequence(anim, 0, 15, 4); explosionAnimation.setAnimFinished(0); explosionAnimation.animationStart(); drawSmoke(); }