//Main update method public override void Update(Level currentLevel, GameTime gameTime) { if (!alive) return; Move(MovementDirection, 0.3f); Vector2 lastPosition = Position; Position += new Vector2(velocity.X, 0); velocity.X *= 0.75f; var gobj = IntersectsWithAny(currentLevel.GameObjects); if (gobj != null && gobj.solid) { Position = lastPosition; HitWall(); } base.Update(currentLevel, gameTime); }
//add gravity to the velocity vector then check for //collision and move the piece in the vertical axis public void ApplyGravity(Level level) { if (alive) { Vector2 originalPosition = Position; velocity.Y += 0.3f; Vector2 verticalVelocity = new Vector2(0, velocity.Y); Position += verticalVelocity; GameObject iobj = IntersectsWithAny(level.GameObjects); if (iobj != null) { Position = originalPosition; //object is below this if (iobj.Position.Y > Position.Y) ReachedBottom(); //stop falling } } }
//Main update method public override void Update(Level currentLevel, GameTime gameTime) { ApplyGravity(currentLevel); if (Position.Y > Game1.ScreenHeight) { //below visible screen Die(true); } base.Update(currentLevel, gameTime); }
//Main update method public override void Update(Level currentLevel, GameTime gameTime) { if (!alive) return; Vector2 lastPosition = Position; //moving X position Position = new Vector2(Position.X + velocity.X, Position.Y); var gobj = IntersectsWithAny(currentLevel.GameObjects); if (gobj != null && gobj.solid) Position = lastPosition; velocity.X *= 0.75f; lastPosition = Position; //moving Y position Position = new Vector2(Position.X, Position.Y + velocity.Y); gobj = IntersectsWithAny(currentLevel.GameObjects); if (gobj != null) { if (gobj.Type == ObjectType.ItemBox && velocity.Y < 0) { ItemBox ibox = (ItemBox)gobj; if (Position.Y >= (ibox.Position.Y + ibox.Height - 5)) if ((Position.X + Width/2 > ibox.Position.X) && (Position.X + Width/2 < ibox.Position.X + ibox.Width)) if (!ibox.hit) ibox.Hit(); } else if (gobj.Type == ObjectType.Item) { Item I = (Item)gobj; switch (I.ItemIndex) { case 0: status = Status.Big; break; case 1: previousStatus = status; status = Status.Invincible; break; } I.alive = false; } if (gobj.solid) { Position = lastPosition; } if (!canJump) { velocity.Y = 0; if (Position.Y < gobj.Position.Y) canJump = true; } } else { canJump = false; } //increment the hurt timer //So the player doesn't die right away even when big if (hurt) hurtTimer++; if (hurtTimer > hurtTime) { hurt = false; hurtTimer = 0; } //increment the invincible timer if (status == Status.Invincible) invincibleTimer++; if (invincibleTimer > invincibleTime) { status = previousStatus; invincibleTimer = 0; } base.Update(currentLevel, gameTime); }
//checks if the player is at the designated finish position public bool LevelComplete(Level currentLevel) { if (SpriteBounds.Intersects(currentLevel.FinishZone)) return true; return false; }
public override void Update(Level currentLevel) { if (!alive) return; Vector2 lastPosition = Position; Position = new Vector2(Position.X + velocity.X, Position.Y); if (IntersectsWithAny(currentLevel.GameObjects) != null) Position = lastPosition; velocity.X *= 0.75f; lastPosition = Position; Position = new Vector2(Position.X, Position.Y + velocity.Y); var gobj = IntersectsWithAny(currentLevel.GameObjects); if (gobj != null) { Position = lastPosition; if (!canJump) { velocity.Y = 0; if (Position.Y < gobj.Position.Y) canJump = true; } } ApplyGravity(currentLevel); if (Position.Y > Game1.ScreenHeight) { //below visible screen Die(); } }
public virtual void Update(Level currentLevel) { }
private void LoadLevel(string levelname) { currentLevel = LoadLevelFromFile(levelname); currentLevel.Name = levelname; player = new Player(currentLevel.StartPosition, new Vector2(0, 0), playerTexture); currentLevel.GameObjects.Add(player); camera.LockToObject(player); }
internal Level LoadLevelFromFile(string filename) { var level = new Level(); using (var sr = new StreamReader(filename)) { while (!sr.EndOfStream) { string s = sr.ReadLine(); if (s != null && (s.StartsWith("//") || s == "")) continue; if (s != null) { string[] split = s.Split(new[] { "," }, StringSplitOptions.RemoveEmptyEntries); if (split[2] == "startzone") { Vector2 sp = new Vector2(float.Parse(split[0]), float.Parse(split[1])); level.StartPosition = sp; } else if (split[2] == "finishzone") { Vector2 fp = new Vector2(float.Parse(split[0]), float.Parse(split[1])); level.FinishZone = new Rectangle((int)fp.X, (int)fp.Y, 32, 32); } else if (split[2] == "enemy") { Texture2D sprite = enemyTexture; Enemy enemy = new Enemy(new Vector2(float.Parse(split[0]), float.Parse(split[1])), Vector2.Zero, sprite); level.GameObjects.Add(enemy); } else { var sprite = Content.Load<Texture2D>("objects/" + split[2]); sprite.Name = split[2]; var gameObj = new GameObject( new Vector2(float.Parse(split[0]), float.Parse(split[1])), new Vector2(0, 0), sprite, GameObject.ObjectType.Block); level.GameObjects.Add(gameObj); } } } } return level; //first line = X //second line = Y //third line = spritename }
//parse the .txt file and create a new Level object that contains all of the objects and positions //of the new level. returns the new Level object. internal Level LoadLevelFromFile(string filename) { var level = new Level(); using (var sr = new StreamReader(filename)) { while (!sr.EndOfStream) { string s = sr.ReadLine(); if (s != null && (s.StartsWith("//") || s == "")) continue; if (s != null) { string[] split = s.Split(new[] { "," }, StringSplitOptions.RemoveEmptyEntries); if (split[2] == "startzone") { Vector2 sp = new Vector2(float.Parse(split[0]), float.Parse(split[1])); level.StartPosition = sp; } else if (split[2] == "finishzone") { Vector2 fp = new Vector2(float.Parse(split[0]), float.Parse(split[1])); level.FinishZone = new Rectangle((int)fp.X, (int)fp.Y, 32, 32); } else if (split[2] == "enemy") { Texture2D sprite = enemyTexture; Enemy enemy = new Enemy(new Vector2(float.Parse(split[0]), float.Parse(split[1])), Vector2.Zero, sprite); level.GameObjects.Add(enemy); } else if (split[2] == "itembox") { Texture2D sprite1 = Content.Load<Texture2D>("objects/itembox"); Texture2D sprite2 = Content.Load<Texture2D>("objects/itembox_hit"); Item newItem = new Item(new Vector2(float.Parse(split[4]), float.Parse(split[5])), ItemTextures[int.Parse(split[3])], int.Parse(split[3])); newItem.alive = false; ItemBox itemBoxObj = new ItemBox(new Vector2(float.Parse(split[0]), float.Parse(split[1])), Vector2.Zero, sprite1, sprite2, GameObject.ObjectType.ItemBox, newItem); level.GameObjects.Add(itemBoxObj); level.GameObjects.Add(newItem); } else { Texture2D sprite = Content.Load<Texture2D>("objects/" + split[2]); GameObject gameObj = new GameObject( new Vector2(float.Parse(split[0]), float.Parse(split[1])), Vector2.Zero, sprite, GameObject.ObjectType.Block); level.GameObjects.Add(gameObj); } } } } return level; }
//To be overriden by child classes public virtual void Update(Level currentLevel, GameTime gameTime) { }