public override void Update(GameTime gameTime) { float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds; KeyboardState k = Keyboard.GetState(); if (died == 0) { foreach (var c in Game.Components) { if (mage != null) { Attack a = c as Attack; //put this in the enemy class if (a != null) { if (boundingBox.Intersects(a.boundingBox) && boundingBox.Left < a.boundingBox.Left) { life -= calculateDamage(mage.giveInventory().First().type, this.Type); Console.WriteLine("monster life: " + life); at = (Attack)c; if (life <= 0) { at = (Attack)c; died = 1; if (at.Type == 1) { deathrec = new Rectangle(0, 0, 33, 33); //red } else if (at.Type == 2) { deathrec = new Rectangle(33, 0, 33, 33); //blue } else { deathrec = new Rectangle(66, 0, 33, 33); //yellow } } } } } Ground b = c as Ground; //put this in the enemy class if (boundingBox.Left < 25) { Position = new Vector2(25, Position.Y); } if (boundingBox.Right >= 775) { Position = new Vector2(725, Position.Y); } if (b != null) { int diff = sprhei - 25; //25 = Ground height if (boundingBox.Intersects(b.bb) && ((b.type == 1) || (b.type == 2) || (b.type == 3))) { if ((Position.Y > (b.bb.Top - diff)) && (Position.Y < (b.bb.Bottom - diff))) // if the main player's position is between the top and the bottom of the bounding box { //it's a side collision. check if position is to the left or right, and handle appropriately if (Position.X < b.position.X) //it's on the left { Position = new Vector2(b.bb.Left - boundingBox.Width, Position.Y); } else { Position = new Vector2(b.bb.Right, Position.Y); } } else if (Position.Y < b.bb.Top) // if the position of the player is above the top of the box, it's an above collision { Position = new Vector2(Position.X, b.bb.Top - boundingBox.Height + 1); } else { Position = new Vector2(Position.X, b.bb.Bottom); } //else it hit the bottom, but this should never happen. } //ladders if (boundingBox.Contains(b.bb) || (boundingBox.Intersects(b.bb)) && (b.type == 4)) { if (Position.Y > mage.Position.Y) { if ((step >= 0 && step <= 4) || (step < 0)) //if he had a walking sprite { step = 6; } Position -= new Vector2(0, elapsed) * 40; climbed = 1; } if (Position.Y < mage.Position.Y) { if ((step >= 0 && step <= 4) || (step < 0)) //if he had a walking sprite { step = 6; } Position += new Vector2(0, elapsed) * 40; climbed = 1; } } if (boundingBox.Intersects(b.bb) && (b.type == 0) && (b.position.Y > (Position.Y - 50))) { Position += new Vector2(0, elapsed) * 40; } } } if (mage != null) { if (Position.X > mage.Position.X) { Position -= new Vector2(elapsed, 0) * 40; if (walked == 1) { step = -1; } walked = -1; } if (Position.X < mage.Position.X) { Position += new Vector2(elapsed, 0) * 40; if (walked == -1) { step = 1; } walked = 1; } } } else { if (dying < 60) { if (dying % 10 == 0) { //monster dying animation //((Attack)at).Type = 1; deathAnimation *= -1; } dying++; } else { Game.Components.Remove(this); } } if (at != null) { Game.Components.Remove((IGameComponent)at); } base.Update(gameTime); }
/// <summary> /// Note, the different symbols in the .txt files correspond to various levels. /// We have: 0-Nothing/Sky,1-Ground,2-Hero,3-Enemy,4-StoneRed,5-StoneBlue, 6-StoneYellow, 7-Treasure, 8-Ladder /// </summary> protected override void Initialize() { // TODO: Add your initialization logic here foodCount = 0; TheMatrix = MatrixInit(); level = LevelRead("level" + levelCounter + ".txt"); player = new hero(this, new Vector2(525, 525), new Vector2(525, 525)); for (int i = 0; i < TheMatrix.Length; i++) { switch (level[i]) { case 0: // Sky Components.Add( myWorld[i] = new Ground(this, TheMatrix[i], level[i]) ); break; case 1: // Ground Components.Add( myWorld[i] = new Ground(this, TheMatrix[i], level[i]) ); break; case 2: // Player Components.Add(myWorld[i] = player); break; case 3: // Enemy Components.Add(myWorld[i] = new Enemy(this, TheMatrix[i], player, random.Next(0, 3), 100)); break; case 4: // Ladder Components.Add(myWorld[i] = new Ground(this, TheMatrix[i], level[i])); break; case 5: //Red Stone Components.Add(myWorld[i] = new stone(1, 1, this, TheMatrix[i])); break; case 6: // Blue Stone Components.Add(myWorld[i] = new stone(2, 1, this, TheMatrix[i])); break; case 7: // Yellow Stone Components.Add(myWorld[i] = new stone(3, 1, this, TheMatrix[i])); break; case 8: // Treasure/Food Components.Add(myWorld[i] = new treasure(1, random.Next(0, 27), this, TheMatrix[i])); foodCount++; break; case 9: // Add portal Components.Add(myWorld[i] = new Portal(this, player, TheMatrix[i])); break; } } base.Initialize(); }
public override void Update(GameTime gameTime) { if (lives == 0) { Game1.Screen = Game1.GameState.GameOver; } float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds; KeyboardState k = Keyboard.GetState(); if (k.IsKeyDown(Keys.Space)) { if (inventory.First != null) { if (attacked == 0) { Attack a = new Attack(g, this.Position, inventory.First().type, lastWalk); Game.Components.Add(a); attacked = 1; } } } if (k.IsKeyUp(Keys.Space)) { attacked = 0; } if (k.IsKeyDown(Keys.Left)) { Position -= new Vector2(elapsed, 0) * 100; walked = -1; lastWalk = walked; if (step > 0) //if he was walking right or was climbing { step = -1; } } if (k.IsKeyDown(Keys.Right)) { Position += new Vector2(elapsed, 0) * 100; walked = 1; lastWalk = walked; if ((step < 0) || (step > 6)) //if he was walking left or was climbing { step = 1; } } if (jumping == 1) { jump(gameTime); } IGameComponent removeMe = null; IGameComponent removeMe2 = null; //Collision Detection (for stones) foreach (var c in Game.Components) { stone s = c as stone; if (s != null) { if (boundingBox.Intersects(s.boundingBox)) { pickUpStone(s); } } Ground b = c as Ground; if (b != null) { int diff = sprhei - 25; //25 = Ground height if (boundingBox.Intersects(b.bb) && ((b.type == 1) || (b.type == 2) || (b.type == 3))) { if ((Position.Y > (b.bb.Top - diff)) && (Position.Y < (b.bb.Bottom - diff))) // if the main player's position is between the top and the bottom of the bounding box { //it's a side collision. check if position is to the left or right, and handle appropriately if (Position.X < b.position.X) //it's on the left { Position = new Vector2(b.bb.Left - boundingBox.Width, Position.Y); } else { Position = new Vector2(b.bb.Right, Position.Y); } } else if (Position.Y < b.bb.Top) // if the position of the player is above the top of the box, it's an above collision { Position = new Vector2(Position.X, b.bb.Top - boundingBox.Height + 1); } else { Position = new Vector2(Position.X, b.bb.Bottom); } //else it hit the bottom, but this should never happen. } if (boundingBox.Top < 50) { Position = new Vector2(Position.X, 50); } if (boundingBox.Left < 25) { Position = new Vector2(25, Position.Y); } if (boundingBox.Right >= 775) { Position = new Vector2(725, Position.Y); } //if (boundingBox.Top < 40) Position = new Vector2(Position.X, 40 + boundingBox.Height / 2); if ((boundingBox.Contains(b.bb) || (boundingBox.Intersects(b.bb))) && (b.type == 4)) // if (boundingBox.Contains(b.bb) && (b.type == 4)) { Position = new Vector2(Position.X, Position.Y - 2); if (k.IsKeyDown(Keys.Up)) { if ((step >= 0 && step <= 4) || (step < 0)) //if he had a walking sprite { step = 6; } Position -= new Vector2(0, elapsed) * 100; climbed = 1; } if (k.IsKeyDown(Keys.Down)) { if ((step >= 0 && step <= 4) || (step < 0)) //if he had a walking sprite { step = 6; } Position += new Vector2(0, elapsed) * 80; climbed = 1; } } if (boundingBox.Intersects(b.bb) && (b.type == 0) && (b.position.Y > (Position.Y - 64))) { Position += new Vector2(0, elapsed) * 80; } } treasure t = c as treasure; if (t != null) { if (boundingBox.Intersects(t.boundingBox)) { score += t.points; removeMe = t; Game1.foodCount--; } } Enemy e = c as Enemy; if (e != null) { if (boundingBox.Intersects(e.boundingBox)) { removeMe2 = getHit(); removeMe = e; } } Portal p = c as Portal; if (p != null) { if (Game1.foodCount <= 0) { p.SetActive(true); } if (p.IsUsed()) { Game1.Screen = Game1.GameState.LevelComplete; } } } if (removeMe != null) { Game.Components.Remove(removeMe); if (removeMe2 != null) { Game.Components.Remove(removeMe2); } } base.Update(gameTime); }
public override void Update(GameTime gameTime) { float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds; KeyboardState k = Keyboard.GetState(); if (k.IsKeyDown(Keys.Up)) { if (ladder > 0) // if he's going up a ladder { if ((step >= 0 && step <= 4) || (step < 0)) //if he had a walking sprite { step = 6; } Position -= new Vector2(0, elapsed) * 80; climbed = 1; } else { //if he's jumping jumping = 1; } } if (k.IsKeyDown(Keys.Down)) { if (ladder > 0) // if he's going down a ladder { if ((step >= 0 && step <= 4) || (step < 0)) //if he had a walking sprite { step = 6; } Position += new Vector2(0, elapsed) * 80; climbed = 1; } } if (k.IsKeyDown(Keys.Left)) { Position -= new Vector2(elapsed, 0) * 100; walked = -1; if (step > 0) //if he was walking right or was climbing { step = -1; } } if (k.IsKeyDown(Keys.Right)) { Position += new Vector2(elapsed, 0) * 100; walked = 1; if ((step < 0) || (step > 6)) //if he was walking left or was climbing { step = 1; } } if (jumping == 1) { jump(gameTime); } //Collision Detection (for the ground) foreach (var c in Game.Components) { Ground s = c as Ground; if (s != null) { /*if (boundingBox.Intersects(s.bb) && (boundingBox.Left < s.bb.Right)) * { * Position = new Vector2(s.bb.Right, Position.Y); * }*/ /*if (boundingBox.Intersects(s.bb) && (boundingBox.Right > s.bb.Left)) //you're on the left * { * Position = new Vector2(s.bb.Left - boundingBox.Width, Position.Y); * }*/ if (boundingBox.Intersects(s.bb) && (boundingBox.Bottom > s.bb.Top)) //top { Position = new Vector2(Position.X, s.bb.Top - boundingBox.Height); } /*if (boundingBox.Intersects(s.bb) && (boundingBox.Top < s.bb.Bottom)) //bottom * { * Position = new Vector2(Position.X, s.bb.Bottom - s.bb.Height/2); * * }*/ } } }