public void Update(GameTime gameTime, bool collision, Map map) { int i = (int)((PlayerPostion().X + 50) / 100); int j = (int)((PlayerPostion().Y + 0) / 100); Collision = collision; previousPosition = position; //previousInput = currentInput; currentInput = Keyboard.GetState(); if (!Collision) acceleration.Y = -gravity; velocity -= movement; movement = Vector2.Zero; if (currentInput.IsKeyDown(Keys.Left)) { movement.X -= speed; if ((j > 0) && map.isEmptyTile(i, j - 1)) gravity = 200f; } if (currentInput.IsKeyDown(Keys.Right)) { movement.X += speed; if ((j > 0) && map.isEmptyTile(i, j - 1)) gravity = 200f; } if (currentInput.IsKeyDown(Keys.Up)) { movement.Y += speed * 1.5f; gravity = 200f; } //if (currentInput.IsKeyDown(Keys.Down)) //movement.Y -= speed; float deltatime = (float)gameTime.ElapsedGameTime.TotalSeconds; velocity = movement + acceleration; position += velocity * deltatime; if (position.X <= 0) position.X = 0; if (position.Y <= 0) position.Y = 0; if (position.X > (114 * 100)) position.X = (114 * 100); if (position.Y > (30 * 100)) position.Y = (30 * 100); CollisionBox = new BoundingBox(new Vector3(position.X - (int)(10 * .68), position.Y, -(int)(60 * .68)), new Vector3(position.X + (int)(30 * .68), position.Y + (int)(140 * .68), (int)(60 * .68))); }
protected override void Initialize() { this.IsMouseVisible = true; Window.AllowUserResizing = true; camera = new Camera(GraphicsDevice.Viewport); windowBlock = new GameObject(); windowBlock.initializeMovement(new Vector3(0,0,0), new Vector3(0, 0, 0)); topBlock = new GameObject(); topBlock.initializeMovement(new Vector3(0, 0, 0), new Vector3(0, 0, 0)); rightBlock = new GameObject(); rightBlock.initializeMovement(new Vector3(0, 0, 0), new Vector3(0, 0, 0)); leftBlock = new GameObject(); leftBlock.initializeMovement(new Vector3(0, 0, 0), new Vector3(0, 0, 0)); rightCornerBlock = new GameObject(); rightCornerBlock.initializeMovement(new Vector3(0, 0, 0), new Vector3(0, 0, 0)); leftCornerBlock = new GameObject(); leftCornerBlock.initializeMovement(new Vector3(0, 0, 0), new Vector3(0, 0, 0)); porchBlock = new GameObject(); porchBlock.initializeMovement(new Vector3(0, 0, 0), new Vector3(0, 0, 0)); backgroundBlocks = new GameObject(); backgroundBlocks.initializeMovement(new Vector3(0, 100, -4000), new Vector3(0, 0, 0)); player = new GameObject(); player.initializeMovement(new Vector3(0, 0, 0), new Vector3(0, 0, 0)); map1 = new Map(Content, Path.Combine(Content.RootDirectory, "level1.txt"), new Point(100, 100), '*', 'P'); map1.AddRegion('P', player); map1.AddRegion('X', windowBlock); map1.AddRegion('T', topBlock); map1.AddRegion('>', rightBlock); map1.AddRegion('<', leftBlock); map1.AddRegion('E', windowBlock); // enemy map1.AddRegion('B', windowBlock); // boss map1.AddRegion('R', porchBlock); map1.AddRegion('A', leftCornerBlock); map1.AddRegion('C', rightCornerBlock); map1.AddBackground(backgroundBlocks); level1 = new Level(map1); currentGameState = GameState.TitleScreen; base.Initialize(); }
public void HandleTileCollision(Map map, bool collision, GameTime gameTime) { depthX = depthY = float.MaxValue; int i = (int)((PlayerPostion().X + 50) / 100); int j = (int)((PlayerPostion().Y + 0) / 100); Vector3 pos = new Vector3(map.TileDimensions.X * i, map.TileDimensions.Y * j, 0); BoundingBox bb = new BoundingBox(new Vector3(pos.X - (int)(80 * .68), pos.Y, -(int)(80 * .68)), new Vector3(pos.X + (int)(80 * .68), pos.Y + (int)(map.IsPorch(i, j) ? 10 : 140 * .68), (int)(60 * .68))); float deltaX, deltaY; if (velocity.Y > 0) { deltaY = -CollisionBox.GetCorners()[0].Y + bb.GetCorners()[3].Y; } else if (velocity.Y < 0) { deltaY = CollisionBox.GetCorners()[0].Y - bb.GetCorners()[0].Y; deltaY -= 50; gravity = 0f; } else deltaY = 0; if (velocity.X > 0) { deltaX = CollisionBox.GetCorners()[1].X - bb.GetCorners()[0].X; deltaY = 0; } else if (velocity.X < 0) { deltaX = CollisionBox.GetCorners()[0].X - bb.GetCorners()[1].X; deltaY = 0; } else deltaX = 0; position.X -= deltaX; position.Y += deltaY; }
public Level(Map map) { Player = new Player(map.SpawnPoint.X, map.SpawnPoint.Y, map.TileDimensions.X, map.TileDimensions.Y); activateMap = map; }