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.
244 lines
6.6 KiB
244 lines
6.6 KiB
using Microsoft.Xna.Framework;
|
|
using Microsoft.Xna.Framework.Graphics;
|
|
using Microsoft.Xna.Framework.Input;
|
|
using System.Collections.Generic;
|
|
using System;
|
|
|
|
namespace Asteroids;
|
|
|
|
public class Player
|
|
{
|
|
public Texture2D Texture;
|
|
public Texture2D BTexture;
|
|
public List<bullet> Bullets;
|
|
public List<powerup> Ups;
|
|
public Rectangle Rect;
|
|
public Vector2 force;
|
|
public Vector2 pos;
|
|
public int wait;
|
|
public bool waitb;
|
|
public Rectangle[] Boxes;
|
|
public float boost;
|
|
public float maxboost;
|
|
public bool boosting;
|
|
public bool regboost;
|
|
public int Health;
|
|
public int maxHP;
|
|
public Player(Texture2D Texture, Texture2D BTexture)
|
|
{
|
|
this.Texture = Texture;
|
|
this.BTexture = BTexture;
|
|
Rect = new Rectangle(0,0,Texture.Width/2, Texture.Height/2);
|
|
pos = new Vector2(vars.PlayArea.X + vars.PlayArea.Width/2 - Rect.Width/2, vars.PlayArea.Y + vars.PlayArea.Height - 100);
|
|
Rect = new Rectangle((int)pos.X, (int)pos.Y, Rect.Width, Rect.Height);
|
|
Bullets = new List<bullet>();
|
|
Ups = new List<powerup>();
|
|
Health = 100;
|
|
maxHP = Health;
|
|
maxboost = 10;
|
|
boost = maxboost/2;
|
|
}
|
|
public void input(GameTime gameTime)
|
|
{
|
|
KeyboardState KEY = Keyboard.GetState();
|
|
if(KEY.IsKeyDown(Keys.W))
|
|
{
|
|
force.Y -= 0.1f;
|
|
}
|
|
if(KEY.IsKeyDown(Keys.S))
|
|
{
|
|
force.Y += 0.1f;
|
|
}
|
|
if(KEY.IsKeyDown(Keys.A))
|
|
{
|
|
force.X -= 0.1f;
|
|
}
|
|
if(KEY.IsKeyDown(Keys.D))
|
|
{
|
|
force.X += 0.1f;
|
|
}
|
|
if(KEY.IsKeyDown(Keys.LeftShift))
|
|
{
|
|
if(!regboost)
|
|
{
|
|
boosting = true;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
boosting = false;
|
|
}
|
|
if(KEY.IsKeyDown(Keys.Space))
|
|
{
|
|
wait--;
|
|
if(wait <= 0)
|
|
{
|
|
wait = 10;
|
|
bulletspawner();
|
|
}
|
|
}
|
|
else
|
|
{
|
|
wait = 0;
|
|
}
|
|
if(KEY.IsKeyDown(Keys.Escape) && waitb)
|
|
{
|
|
vars.Scens.Scene = 2;
|
|
vars.Scens.wait = false;
|
|
}
|
|
if(KEY.IsKeyUp(Keys.Escape)){waitb = true;}
|
|
//Checking boost numbs
|
|
if(!boosting)
|
|
{
|
|
boost += (float)gameTime.ElapsedGameTime.TotalSeconds;
|
|
}
|
|
if(boost <= 0)
|
|
{
|
|
regboost = true;
|
|
boosting = false;
|
|
boost = 0;
|
|
}
|
|
if(boost >= maxboost)
|
|
{
|
|
regboost = false;
|
|
boost = maxboost;
|
|
}
|
|
//exiting if no force applied
|
|
if(force == Vector2.Zero)
|
|
{
|
|
return;
|
|
}
|
|
force.X = MathHelper.Clamp(force.X, -1, 1);
|
|
force.Y = MathHelper.Clamp(force.Y, -1, 1);
|
|
if(force.Y > 0 && KEY.IsKeyUp(Keys.S)){force.Y -= 0.1f;}
|
|
if(force.Y < 0 && KEY.IsKeyUp(Keys.W)){force.Y += 0.1f;}
|
|
if(force.X > 0 && KEY.IsKeyUp(Keys.D)){force.X -= 0.1f;}
|
|
if(force.X < 0 && KEY.IsKeyUp(Keys.A)){force.X += 0.1f;}
|
|
force.Y = MathF.Round(force.Y, 1);
|
|
force.X = MathF.Round(force.X, 1);
|
|
if(boosting)
|
|
{
|
|
pos += force * 5;
|
|
boost -= (float)gameTime.ElapsedGameTime.TotalSeconds *3;
|
|
}
|
|
else{pos += force * 3;}
|
|
}
|
|
public void Draw(SpriteBatch _spriteBatch, GameTime gameTime)
|
|
{
|
|
foreach(var item in Ups)
|
|
{
|
|
item.Update(gameTime);
|
|
}
|
|
foreach(var item in Bullets)
|
|
{
|
|
item.Draw(_spriteBatch);
|
|
}
|
|
Rect = new Rectangle((int)pos.X, (int)pos.Y, Rect.Width, Rect.Height);
|
|
_spriteBatch.Draw(Texture, Rect, null, Color.White);
|
|
}
|
|
public void Collision(Rectangle[] Boxes)
|
|
{
|
|
this.Boxes = Boxes;
|
|
if(!Boxes[0].Contains(Rect))
|
|
{
|
|
if(Rect.Left < Boxes[0].Left)
|
|
{
|
|
pos.X += Boxes[0].Left - Rect.Left;
|
|
}
|
|
if(Rect.Right > Boxes[0].Right)
|
|
{
|
|
pos.X -= Rect.Right - Boxes[0].Right;
|
|
}
|
|
if(Rect.Top < Boxes[0].Top)
|
|
{
|
|
pos.Y += Boxes[0].Top - Rect.Top;
|
|
}
|
|
if(Rect.Bottom > Boxes[0].Bottom)
|
|
{
|
|
pos.Y -= Rect.Bottom - Boxes[0].Bottom;
|
|
}
|
|
}
|
|
Bullets.RemoveAll(pred);
|
|
Ups.RemoveAll(powPred);
|
|
}
|
|
public bool pred(bullet b)
|
|
{
|
|
return b.Collision(Boxes);
|
|
}
|
|
public bool powPred(powerup P)
|
|
{
|
|
if(P.TimeLeft <= 0)
|
|
{
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
public void bulletspawner()
|
|
{
|
|
Bullets.Add(new bullet(BTexture, new Vector2(pos.X + Rect.Width/2 - BTexture.Width/2, pos.Y), 10f, 0f, Color.White));
|
|
foreach(var item in Ups)
|
|
{
|
|
item.bullets(Bullets, this);
|
|
}
|
|
}
|
|
}
|
|
public class bullet
|
|
{
|
|
public Texture2D Texture;
|
|
public float Speed;
|
|
public Vector2 pos;
|
|
public Rectangle Rect;
|
|
public float Rot;
|
|
public Color C;
|
|
public bullet(Texture2D Texture, Vector2 pos, float Speed, float rot, Color C)
|
|
{
|
|
this.Texture = Texture;
|
|
this.pos = pos;
|
|
this.Speed = Speed;
|
|
this.Rot = rot;
|
|
this.C = C;
|
|
Rect = new Rectangle((int)pos.X, (int)pos.Y, Texture.Width, Texture.Height);
|
|
AudioEn.playsound(2);
|
|
}
|
|
public void Draw(SpriteBatch _spriteBatch)
|
|
{
|
|
Vector2 e = new Vector2(MathF.Cos(Rot - 1.5707963268f) * Speed, MathF.Sin(Rot - 1.5707963268f) * Speed);
|
|
pos += e;
|
|
Rect = new Rectangle((int)pos.X, (int)pos.Y, Texture.Width, Texture.Height);
|
|
_spriteBatch.Draw(Texture, Rect, null, C, Rot, Vector2.Zero, SpriteEffects.None, 0f);
|
|
}
|
|
public bool Collision(Rectangle[] Boxes)
|
|
{
|
|
if(!Boxes[0].Contains(Rect))
|
|
{
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
}
|
|
public class powerup
|
|
{
|
|
public float TimeLeft;
|
|
public float[] rots;
|
|
public powerup()
|
|
{
|
|
Random r = new Random();
|
|
TimeLeft = r.Next(5, 20);
|
|
rots = new float[r.Next(1, 2)];
|
|
for(int i = 0; i < rots.Length;i++)
|
|
{
|
|
rots[i] = r.Next(-45, 45);
|
|
}
|
|
}
|
|
public void Update(GameTime gameTime)
|
|
{
|
|
TimeLeft -= (float)gameTime.ElapsedGameTime.TotalSeconds;
|
|
}
|
|
public void bullets(List<bullet> Bullets, Player PL)
|
|
{
|
|
foreach(var item in rots)
|
|
{
|
|
Bullets.Add(new bullet(PL.BTexture, new Vector2(PL.pos.X + PL.Rect.Width/2 - PL.BTexture.Width/2, PL.pos.Y), 10f, MathHelper.ToRadians(item), Color.BlanchedAlmond));
|
|
}
|
|
}
|
|
} |