You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

229 lines
6.5 KiB

2 years ago
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<asteroid> As;
private List<Explosion> EX;
public List<powerupbox> Pow;
public Texture2D[] asTextures;
private Player PL;
private int wait;
public AllAst(Player PL)
{
this.PL = PL;
As = new List<asteroid>();
Pow = new List<powerupbox>();
EX = new List<Explosion>();
}
public void Update(SpriteBatch _spriteBatch)
{
foreach (var item in As)
{
bullet rm = null;
Rectangle cent = new Rectangle(item.Rect.X - item.Rect.Width / 2, item.Rect.Y - item.Rect.Height / 2, item.Rect.Width, item.Rect.Height);
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 = 10000f;
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.Top >= vars.PlayArea.Bottom)
{
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;
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);
pos = new Vector2(r.Next(vars.PlayArea.X, vars.PlayArea.Right-Texture.Width), 100);
Health = r.Next(5, 15);
Rect = new Rectangle((int)pos.X, (int)pos.Y, Texture.Width, Texture.Height);
bar = new hpbar(Health, _spriteBatch.GraphicsDevice);
}
public void Draw(SpriteBatch _spriteBatch)
{
rot += rots;
pos.Y += speed;
bar.pos = new Vector2(Rect.Center.X, Rect.Center.Y);
Rect = new Rectangle((int)pos.X, (int)pos.Y, Texture.Width, Texture.Height);
_spriteBatch.Draw(Texture, Rect.Center.ToVector2(), null, Color.White, MathHelper.ToRadians(rot), new Vector2(Rect.Width / 2, Rect.Height / 2), Vector2.One, 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<Color>(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;
}
}