/// <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); }