using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Input; using System.Collections.Generic; using System; namespace Asteroids; public class AllAst { public List As; private List EX; public List Pow; public Texture2D[] asTextures; private Player PL; private int wait; public AllAst(Player PL) { this.PL = PL; As = new List(); Pow = new List(); EX = new List(); } public void Update(SpriteBatch _spriteBatch) { foreach (var item in As) { bullet rm = null; foreach (var it in PL.Bullets) { if (item.Rect.Intersects(it.Rect)) { item.Health--; AudioEn.playsound(4); rm = it; break; } } if (rm != null) { PL.Bullets.Remove(rm); } } As.RemoveAll(pred); Pow.RemoveAll(PowPred); EX.RemoveAll(EXpred); wait--; if (wait <= 0) { Random r = new Random(); float maxpoints = 1000f; float t = ((float)vars.Points/maxpoints); wait = r.Next((int)MathHelper.Lerp(120, 10, t), (int)MathHelper.Lerp(300, 60, t)); As.Add(new asteroid(asTextures[r.Next(asTextures.Length)], _spriteBatch)); } } private bool pred(asteroid ase) { if (ase.Health <= 0) { vars.Points++; Random r = new Random(); if(r.Next(0, 10) == 2) { Pow.Add(new powerupbox(ase.pos)); } EX.Add(new Explosion(ase.Rect)); return true; } if (ase.Rect.Intersects(PL.Rect)) { PL.Health--; EX.Add(new Explosion(ase.Rect)); return true; } if (!ase.Rect.Intersects(vars.PlayArea)) { return true; } return false; } private bool PowPred(powerupbox PO) { if(PO.Rect.Top >= vars.PlayArea.Bottom) { return true; } if(PO.Rect.Intersects(PL.Rect)) { PL.Ups.Add(new powerup()); vars.Points += 5; return true; } return false; } private bool EXpred(Explosion E) { if(E.frame == E.mframe) { return true; } return false; } public void Draw(SpriteBatch _spriteBatch, GameTime gameTime) { foreach (var item in As) { item.Draw(_spriteBatch); } foreach (var item in Pow) { item.Draw(_spriteBatch); } foreach (var item in EX) { item.draw(_spriteBatch, gameTime); } } } public class asteroid { public Texture2D Texture; public Rectangle Rect; public Vector2 pos; public float speed; public float rot; public float rots; public int Health; public hpbar bar; private float size; private int flydir; public asteroid(Texture2D Texture, SpriteBatch _spriteBatch) { this.Texture = Texture; Random r = new Random(); speed = (float)((float)r.Next(5, 30) / 10f); rots = (float)((float)r.Next(5, 30) / 10f); flydir = r.Next(-25, 25); pos = new Vector2(r.Next(vars.PlayArea.X, vars.PlayArea.Right-Texture.Width), 100); Health = r.Next(5, 15); size = (float)((float)r.Next(50, 300)/100f); Rect = new Rectangle((int)pos.X, (int)pos.Y, (int)(Texture.Width*size), (int)(Texture.Height*size)); bar = new hpbar(Health, _spriteBatch.GraphicsDevice); } public void Draw(SpriteBatch _spriteBatch) { Vector2 e = new Vector2(MathF.Cos(MathHelper.ToRadians(flydir)+1.5707963268f) * speed, MathF.Sin(MathHelper.ToRadians(flydir)+1.5707963268f) * speed); rot += rots; pos += e; bar.pos = new Vector2(Rect.Center.X, Rect.Center.Y); Rect = new Rectangle((int)pos.X, (int)pos.Y, Rect.Width, Rect.Height); _spriteBatch.Draw(Texture, Rect.Center.ToVector2(), null, Color.White, MathHelper.ToRadians(rot), new Vector2(Texture.Width / 2, Texture.Height / 2), new Vector2(size, size), SpriteEffects.None, 0f); if(bar.max != Health) { bar.Draw(_spriteBatch, Health); } } } public class hpbar { public Texture2D ThePixel; public float max; public Vector2 pos; public hpbar(float max, GraphicsDevice gd) { this.max = max; ThePixel = new Texture2D(gd, 1, 1); ThePixel.SetData(new Color[] { Color.White}); } public void Draw(SpriteBatch _spriteBatch, float v) { int size = (int)((v/max) * 38); Rectangle[] rects = new Rectangle[]{ new Rectangle((int)pos.X-20, (int)pos.Y+20, 40, 10), new Rectangle((int)pos.X-19, (int)pos.Y+20, 38, 8), new Rectangle((int)pos.X-19, (int)pos.Y+20, size, 8) }; Color[] cs = new Color[]{ Color.Black, Color.Red, Color.Green, }; for (int i = 0; i < rects.Length; i++) { _spriteBatch.Draw(ThePixel, rects[i], null, cs[i]); } } } public class powerupbox { public Texture2D Texture; public Vector2 pos; public Rectangle Rect; public powerupbox(Vector2 pos) { this.pos = pos; Random r = new Random(); Texture = vars.PowerUpTex[r.Next(0, vars.PowerUpTex.Length)]; Rect = new Rectangle((int)pos.X, (int)pos.Y, Texture.Width, Texture.Height); } public void Draw(SpriteBatch _sprtieBatch) { pos.Y += 1f; Rect = new Rectangle((int)pos.X, (int)pos.Y, Rect.Width, Rect.Height); _sprtieBatch.Draw(Texture, pos, null, Color.White); } } public class Explosion { private Rectangle rect; private Rectangle source; private float time; public int mframe; public int frame; public Explosion(Rectangle ase) { rect = new Rectangle(ase.X-ase.Width/2, ase.Y-ase.Height/2, 100, 100); source = new Rectangle(0, 0, 100, 100); frame = 1; mframe = 81; } public void draw(SpriteBatch _spriteBatch, GameTime gameTime) { frame += 2; if(frame == 17) { AudioEn.playsound(3); } source.X = source.Width*(frame-1); _spriteBatch.Draw(vars.explosionsheet, rect, source, Color.White); time += (float)gameTime.ElapsedGameTime.TotalSeconds; } }