//Kollar ifall spelaren kolliderar med ett objekt /// <summary> /// Kollar ifall något kolliderar /// </summary>> public void Check(Player p, ref Room room, List <Rectangle> rlg, ref List <Box> bl, ref List <Chest> cl, ref List <Bullet> pl, ref List <Enemy> el, ref List <Loot> ll, int g, KeyboardState kstate, KeyboardState oldstate, int ww, int wh) { //kollar ifall något kollidrerar med marken eller en platform foreach (var r in rlg) { // spelare if (p.pBot.Intersects(r) || p.J == true) { p.F = false; } else { p.F = true; } if (p.pBot.Intersects(r)) { p.DJ = false; p.J = false; p.RectangleStuff(p.P.X, r.Y - p.P.Height, p.P.Width, p.P.Height); p.UpdatePosition(ww); p.JS = 0; } if (p.pTop.Intersects(r)) { if (p.GB == false) { p.RectangleStuff(p.P.X, r.Y + r.Height, p.P.Width, p.P.Height); } else { p.RectangleStuff(p.P.X, r.Y + r.Height + ww / 26, p.P.Width, p.P.Height); } p.UpdatePosition(ww); p.JS *= -1; } if (p.pRight.Intersects(r)) { p.RectangleStuff(r.X + r.Width, p.P.Y, p.P.Width, p.P.Height); p.UpdatePosition(ww); } if (p.pLeft.Intersects(r)) { p.RectangleStuff(r.X - p.P.Width, p.P.Y, p.P.Width, p.P.Height); p.UpdatePosition(ww); } // fiende foreach (var e in el) { if (e.eBot.Intersects(r)) { e.F = false; e.FS = 0; e.enemy.Y = r.Y - e.enemy.Height; e.UpdatePosition(); } else { e.F = true; } if (e.eTop.Intersects(r)) { e.enemy.Y = r.Y + r.Height; e.UpdatePosition(); } if (e.eRight.Intersects(r)) { e.enemy.X = r.X + r.Width; e.Speed *= -1; e.UpdatePosition(); } if (e.eLeft.Intersects(r)) { e.enemy.X = r.X - e.enemy.Width; e.Speed *= -1; e.UpdatePosition(); } } // låda for (int i = 0; i < bl.Count; i++) { if ((bl[i].box.Intersects(r) || bl[i].box.X <0 || bl[i].box.X> ww - bl[i].box.Width) && bl[i].BT == true) { bl.RemoveAt(i); } else if (bl[i].box.Intersects(r) && bl[i].BP == false) { bl[i].box.Y = r.Y - bl[i].box.Height; bl[i].FS = 0; bl[i].F = false; bl[i].UpdateAura(); } else { bl[i].F = true; } } // loot foreach (var l in ll) { if (l.drop.Intersects(r)) { l.drop.Y = r.Y - l.drop.Height; l.S = 0; l.FS = 0; l.F = false; } } // skått for (int i = 0; i < pl.Count; i++) { if (pl[i].bullet.Intersects(r) || pl[i].bullet.X <0 || pl[i].bullet.X> ww - pl[i].bullet.Width) { pl.RemoveAt(i); } } } //kollar ifall spelaren försöker lämna fönstret if (p.P.X > ww - p.P.Width) { p.RectangleStuff(ww - p.P.Width, p.P.Y, p.P.Width, p.P.Height); } else if (p.P.X < 0) { p.RectangleStuff(0, p.P.Y, p.P.Width, p.P.Height); } else if (p.P.Y > wh) { p.HP -= 1; p.sw.Start(); p.UpdateHealth(ww); p.RectangleStuff(0, p.P.Y, p.P.Width, p.P.Height); p.RectangleStuff(p.P.X, wh - wh / 24 - p.P.Height, p.P.Width, p.P.Height); p.JS = 0; } // kollar ifall något kolliderar med en låda foreach (var b in bl) { if (b.BP == true) { b.box.X = p.P.X - (b.box.Width - p.P.Width) / 2; b.box.Y = p.P.Y - b.box.Height; } //kollar ifall spelaern står nära lådan för att kunna plocka upp den if (p.P.Intersects(b.aura) && b.BP == false && b.BT == false) { b.E = true; } else { b.E = false; } if (p.P.Intersects(b.aura) && kstate.IsKeyDown(Keys.E) && oldstate.IsKeyUp(Keys.E) && b.BP == false && b.BT == false) { b.BP = true; p.GB = true; b.BT = false; } else if (kstate.IsKeyDown(Keys.E) && oldstate.IsKeyUp(Keys.E) && b.BP == true) { if (p.D == 1) { b.box.X = p.P.X + p.P.Width; } else { b.box.X = p.P.X - b.box.Width; } b.BP = false; p.GB = false; b.box.Y = p.P.Y + p.P.Height - b.box.Height; b.UpdateAura(); } // spelare if (b.BP == false) { if (p.pBot.Intersects(b.box)) { p.DJ = false; p.J = false; p.RectangleStuff(p.P.X, b.box.Y - p.P.Height, p.P.Width, p.P.Height); p.UpdatePosition(ww); p.JS = 0; } if (p.pRight.Intersects(b.box)) { p.RectangleStuff(b.box.X + b.box.Width, p.P.Y, p.P.Width, p.P.Height); p.UpdatePosition(ww); } if (p.pLeft.Intersects(b.box)) { p.RectangleStuff(b.box.X - p.P.Width, p.P.Y, p.P.Width, p.P.Height); p.UpdatePosition(ww); } } // fiende foreach (var e in el) { if (e.eBot.Intersects(b.box)) { e.F = false; e.FS = 0; e.enemy.Y = b.box.Y - e.enemy.Height; e.UpdatePosition(); } else { e.F = true; } if (e.eRight.Intersects(b.box)) { if (b.BT == true) { e.Dead = true; } b.BT = false; e.enemy.X = b.box.X + b.box.Width; e.Speed *= -1; e.UpdatePosition(); } if (e.eLeft.Intersects(b.box)) { if (b.BT == true) { e.Dead = true; } b.BT = false; e.enemy.X = b.box.X - e.enemy.Width; e.Speed *= -1; e.UpdatePosition(); } } // skått for (int i = 0; i < pl.Count; i++) { if (pl[i].bullet.Intersects(b.box)) { pl.RemoveAt(i); } } } // kistor foreach (var c in cl) { if (p.P.Intersects(c.chest) && el.Count == 0 && c.Open == false) { c.E = true; } else { c.E = false; } if (p.P.Intersects(c.chest) && kstate.IsKeyDown(Keys.E) && oldstate.IsKeyUp(Keys.E) && c.Open == false) { c.Open = true; ll.Add(new Loot(c.inside.X + c.inside.Width / 3, c.chest.Y, ww, c.Loot)); } } // kollar ifall skått eller spelar kolliderar med en fiende foreach (var e in el) { // skått for (int i = 0; i < pl.Count; i++) { if (pl[i].bullet.Intersects(e.enemy) && pl[i].F == true) { e.Dead = true; pl.RemoveAt(i); } // kollar ifall skått kolliderar med spelaren else if (pl[i].bullet.Intersects(p.P) && pl[i].F == false) { pl.RemoveAt(i); p.HP--; p.sw.Start(); p.UpdateHealth(ww); } } // spelare if (e.eTop.Intersects(p.pBot)) { e.Dead = true; p.JS = p.JP * 3 / 4; } else if (e.enemy.Intersects(p.P) && p.sw.ElapsedMilliseconds == 0) { p.HP--; p.sw.Start(); p.UpdateHealth(ww); } // kollar ifall spelaren blir sedd av en fiende if (e.sight.Intersects(p.P) && e.sw.ElapsedMilliseconds == 0) { if (e.GG == true) { e.sw.Start(); pl.Add(new Bullet(e.gun.X, e.gun.Y + 1, e.Speed, ww, wh, "e")); } else if (e.GG == false && e.F == false && kstate.IsKeyDown(Keys.Space)) { e.F = true; e.FS = e.JP; } } // kollar ifall fiender lämnar fönstret if (e.enemy.Y > wh) { e.Dead = true; } } // kollar ifall spelaren kolliderar med loot for (int i = 0; i < ll.Count; i++) { if (p.P.Intersects(ll[i].drop) && p.HP < p.MHP && ll[i].T == "hp") { p.HP++; p.UpdateHealth(ww); ll.RemoveAt(i); } else if (p.P.Intersects(ll[i].drop) && p.Ammo < p.MAmmo && ll[i].T == "ammo") { p.Ammo++; p.UpdateAmmo(ww); ll.RemoveAt(i); } else if (p.P.Intersects(ll[i].drop) && ll[i].T != "ammo" && ll[i].T != "hp") { ll.RemoveAt(i); p.StatUpgrade(ww); } } }
protected override void Update(GameTime gameTime) { if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape)) { Exit(); } // TODO: Add your update logic here KeyboardState kstate = Keyboard.GetState(); if (end == false) { if (kstate.IsKeyDown(Keys.D)) { p.RectangleStuff(p.P.X + p.Speed, p.P.Y, p.P.Width, p.P.Height); p.UpdatePosition(ww); p.D = 1; } if (kstate.IsKeyDown(Keys.A)) { p.RectangleStuff(p.P.X - p.Speed, p.P.Y, p.P.Width, p.P.Height); p.UpdatePosition(ww); p.D = -1; } if (kstate.IsKeyDown(Keys.S) && oldstate.IsKeyUp(Keys.S)) { if (p.S == false) { p.S = true; p.Speed /= 2; } else if (p.S == true) { p.RectangleStuff(p.P.X, p.P.Y - p.P.Height, p.P.Width, p.P.Height); p.S = false; p.Speed *= 2; } p.UpdatePosition(ww); } if (kstate.IsKeyDown(Keys.Space) && p.J == false) { p.J = true; p.JS = p.JP; } else if (kstate.IsKeyDown(Keys.Space) && p.J == true && p.DJ == false && oldstate.IsKeyUp(Keys.Space)) { p.DJ = true; p.JS = p.JP; } if (kstate.IsKeyDown(Keys.Enter) && oldstate.IsKeyUp(Keys.Enter)) { if (p.GB == true) { p.GB = false; foreach (var b in bl) { if (b.BP == true) { if (p.D > 0) { b.BS = 10; b.box.X = p.P.X + p.P.Width; } else { b.BS = -10; b.box.X = p.P.X - b.box.Width; } b.BP = false; b.BT = true; b.box.Y = p.P.Y; } } } else if (p.Ammo > 0) { p.Ammo--; p.UpdateAmmo(ww); pl.Add(new Bullet(p.gun.X, p.gun.Y + 1, p.D, ww, wh, "p")); } } // spelaren faller if (p.J == true || p.F == true) { p.RectangleStuff(p.P.X, p.P.Y - p.JS, p.P.Width, p.P.Height); p.UpdatePosition(ww); p.JS -= g; } // flyttar alla lådor ifall dom kastas eller släpps foreach (var b in bl) { b.Update(p, g); } // gör så att skåtten rör sig foreach (var p in pl) { p.Update(); } if (room.Next(p, el, kstate) == true) { room.Generate(ref p, ref rlg, ref bl, ref cl, ref ll, ref el, ww, wh); } // kollar ifall något kollidrerar c.Check(p, ref room, rlg, ref bl, ref cl, ref pl, ref el, ref ll, g, kstate, oldstate, ww, wh); // Uppdaterar fienderna for (int i = 0; i < el.Count; i++) { // kollar ifall en fiende har dött if (el[i].Dead == true) { rh = r.Next(10); if (rh == 0) { rh = r.Next(2); if (rh == 0) { ll.Add(new Loot(el[i].enemy.X, el[i].enemy.Y, ww, "ammo")); } else { ll.Add(new Loot(el[i].enemy.X, el[i].enemy.Y, ww, "hp")); } } el.RemoveAt(i); } else { el[i].Move(ww); if (el[i].F == true) { el[i].enemy.Y -= el[i].FS; el[i].FS -= g; } el[i].UpdatePosition(); if (el[i].sw.ElapsedMilliseconds > 1000) { el[i].sw.Reset(); } } } foreach (var l in ll) { l.Update(g); } // Gör så att spelaren kan ta skada igen if (p.sw.ElapsedMilliseconds > 500) { p.sw.Reset(); } // kollar ifall spelaren har förlorat if (p.HP < 1) { end = true; } } else if (end == true && kstate.IsKeyDown(Keys.Space)) { end = false; p.MHP = 3; p.HP = p.MHP; p.MAmmo = 5; p.Ammo = p.MAmmo; p.UpdateHealth(ww); p.UpdateAmmo(ww); p.Speed = p.BS; p.SP = 100; el.Clear(); bl.Clear(); cl.Clear(); ll.Clear(); room.RC = -1; room.Generate(ref p, ref rlg, ref bl, ref cl, ref ll, ref el, ww, wh); } oldstate = Keyboard.GetState(); base.Update(gameTime); }
/// <summary> /// Skapar ett nytt rum /// </summary> public void Generate(ref Player p, ref List <Rectangle> rlg, ref List <Box> bl, ref List <Chest> cl, ref List <Loot> ll, ref List <Enemy> el, int ww, int wh) { roomCounter++; if (roomCounter == b) { b *= 10; a++; } p.RectangleStuff(0, p.P.Y, p.P.Width, p.P.Height); p.RectangleStuff(p.P.X, ground.Y - p.P.Height, p.P.Width, p.P.Height); rlg.Clear(); el.Clear(); bl.Clear(); cl.Clear(); ll.Clear(); ec = r.Next(1, 6); rh = r.Next(2); if ((roomCounter % 5 == 0 || rh > 0) && roomCounter > 0) { cl.Add(new Chest(ww / 2 - ww / 20 / 2, Ground.Y - gap * 2 - ww / 26, ww)); } if (roomCounter == 0) { rlg.Add(ground); } else if (roomCounter == 10) { rlg.Add(ground); rlg.Add(new Rectangle(s, ground.Y - gap, s, t)); bl.Add(new Box(m - ww / 26, ground.Y - gap - ww / 26, ww)); rlg.Add(new Rectangle(l - s, ground.Y - gap, s, t)); bl.Add(new Box(l - ww / 26, ground.Y - gap - ww / 26, ww)); rlg.Add(new Rectangle(ww - m - s, ground.Y - gap, s, t)); bl.Add(new Box(l + m - ww / 26, ground.Y - gap - ww / 26, ww)); rlg.Add(new Rectangle(ww - s, ground.Y - gap, s, t)); rlg.Add(new Rectangle(0, ground.Y - gap * 2, l, t)); rlg.Add(new Rectangle(l, ground.Y - gap * 2, m, t)); rlg.Add(new Rectangle(0, ground.Y - gap * 3, s, t)); rlg.Add(new Rectangle(ww - l, ground.Y - gap * 4, l, t)); rlg.Add(new Rectangle(m, ground.Y - gap * 4, m, t)); rlg.Add(new Rectangle(ww - s, ground.Y - gap * 5, s, t)); cl.Add(new Chest(ww - ww / 20, Ground.Y - gap * 5 - ww / 26, ww)); for (int i = 0; i < 11; i++) { el.Add(new Enemy(ww - i * 2 * ww / 25, ground.Y - gap * 4 - wh / 10, ww, wh)); } } else { rh = r.Next(2); // two holes if (rh == 0) { rlg.Add(new Rectangle(0, ground.Y, m, t)); rlg.Add(new Rectangle(0 + m + s, ground.Y, m, t)); bl.Add(new Box(ww / 2 - ww / 26 / 2, ground.Y - ww / 26, ww)); rlg.Add(new Rectangle(0 + m * 2 + s * 2, ground.Y, m, t)); rlg.Add(new Rectangle(0, ground.Y - gap, s, t)); rlg.Add(new Rectangle(ww - s, ground.Y - gap, s, t)); rlg.Add(new Rectangle(0, ground.Y - gap * 3, m, t)); rlg.Add(new Rectangle(ww - m, ground.Y - gap * 3, m, t)); el.Add(new Enemy(0, ground.Y - gap * 3 - wh / 10, ww, wh)); el.Add(new Enemy(ww - ww / 25, ground.Y - gap * 3 - wh / 10, ww, wh)); bl.Add(new Box(s, ground.Y - gap * 3 - ww / 26, ww)); bl.Add(new Box(ww - s - ww / 26, ground.Y - gap * 3 - ww / 26, ww)); rh = r.Next(2); if (rh == 0) { rlg.Add(new Rectangle(ww / 2 - (l) / 2, ground.Y - gap * 2, l, t)); bl.Add(new Box(m + s - ww / 26, ground.Y - gap * 2 - ww / 26, ww)); bl.Add(new Box(ww - m - s, ground.Y - gap * 2 - ww / 26, ww)); if (ec > 1) { el.Add(new Enemy(ww / 2, ground.Y - gap * 2 - wh / 10, ww, wh)); } rh = r.Next(2); if (rh == 0) { rlg.Add(new Rectangle(ww / 2 - (m) / 2, ground.Y - gap * 4, m, t)); } else { rlg.Add(new Rectangle(ww / 2 - (s) / 2, ground.Y - gap * 4, s, t)); } } else { rlg.Add(new Rectangle(m, ground.Y - gap * 2, s, t)); rlg.Add(new Rectangle(ww / 2 - (s) / 2, ground.Y - gap * 2, s, t)); rlg.Add(new Rectangle(ww - m - s, ground.Y - gap * 2, s, t)); rlg.Add(new Rectangle(ww / 2 - (m) / 2, ground.Y - gap * 4, m, t)); bl.Add(new Box(ww / 2 - ww / 26 / 2, ground.Y - gap * 4 - ww / 26, ww)); } } // one hole else { rlg.Add(new Rectangle(0, ground.Y, m, t)); rlg.Add(new Rectangle(m, ground.Y, s, t)); rlg.Add(new Rectangle(ww - m, ground.Y, m, t)); rlg.Add(new Rectangle(ww - m - s, ground.Y, s, t)); rlg.Add(new Rectangle(s, ground.Y - gap * 4, m, t)); rlg.Add(new Rectangle(ww - m - s, ground.Y - gap * 4, m, t)); el.Add(new Enemy(s, ground.Y - gap * 4 - wh / 10, ww, wh)); el.Add(new Enemy(ww - m - s, ground.Y - gap * 4 - wh / 10, ww, wh)); bl.Add(new Box(s, ground.Y - gap * 4 - ww / 26, ww)); bl.Add(new Box(ww - s - ww / 26, ground.Y - gap * 4 - ww / 26, ww)); rh = r.Next(2); if (rh == 0) { rlg.Add(new Rectangle(0, ground.Y - gap, s, t)); rlg.Add(new Rectangle(ww - s, ground.Y - gap, s, t)); } else { rlg.Add(new Rectangle(0, ground.Y - gap, m, t)); rlg.Add(new Rectangle(ww - m, ground.Y - gap, m, t)); bl.Add(new Box(s, ground.Y - gap - ww / 26, ww)); bl.Add(new Box(ww - s - ww / 26, ground.Y - gap - ww / 26, ww)); rh = r.Next(2); if (rh == 0) { rlg.Add(new Rectangle(0, ground.Y - gap * 3, s, t)); rlg.Add(new Rectangle(ww - s, ground.Y - gap * 3, s, t)); } } rh = r.Next(2); if (rh == 0) { rlg.Add(new Rectangle(ww / 2 - (s) / 2, ground.Y - gap, s, t)); bl.Add(new Box(ww / 2 - ww / 26 / 2, ground.Y - gap - ww / 26, ww)); } rh = r.Next(2); if (rh == 0) { rlg.Add(new Rectangle(ww / 2 - (l) / 2, ground.Y - gap * 2, l, t)); bl.Add(new Box(m + s, ground.Y - gap * 2 - ww / 26, ww)); bl.Add(new Box(ww - m - s - ww / 26, ground.Y - gap * 2 - ww / 26, ww)); } else { rlg.Add(new Rectangle(ww / 2 - (m) / 2, ground.Y - gap * 2, m, t)); if (ec > 3) { el.Add(new Enemy(ww / 2, ground.Y - gap * 2 - wh / 10, ww, wh)); } } } } }