private void playerCollision()
{
if (isInUse && attachedToPlayer)
{
PlayerClass[] players = spearOwner.getPlayers();
foreach (PlayerClass player in players)
{
if (player != null && !player.Equals(spearOwner) && !player.dead && player.blinked == spearOwner.blinked)
{
Rectangle adjustedRect = new Rectangle(spear.Location, spear.Size);
adjustedRect.X -= 27;
bool hit = VectorMath.rectCollision(adjustedRect, orientation, player.getPlayerRect(), 0, new Point(16, 8));
if (hit)//player.getPlayerRect().Intersects(this.spear))
{
if (player.shield)
{
player.shield = false;
}
else
{
player.setDead(true, this.spearOwner, "SPEAR");
Hit_Player_Sound.Play();
}
//Wrap-around collisions
if(this.spear.X <= spear.Width)
{
Rectangle tempRect = new Rectangle((int)(spear.X + SCREENSIZE.X), (int)spear.Y, spear.Width, spear.Height);
if (VectorMath.rectCollision(tempRect, orientation, player.getPlayerRect(), 0, new Point(16, 8)))//player.getPlayerRect().Intersects(tempRect))
{
player.setDead(true, this.spearOwner, "SPEAR");
Hit_Player_Sound.Play();
}
}
else if(this.spear.X >= SCREENSIZE.X - this.spear.Width)
{
Rectangle tempRect = new Rectangle((int)(spear.X - SCREENSIZE.X - 32), (int)spear.Y, spear.Width, spear.Height);
if (VectorMath.rectCollision(tempRect, orientation, player.getPlayerRect(), 0, new Point(16, 8)))//player.getPlayerRect().Intersects(tempRect))
{
player.setDead(true, this.spearOwner, "SPEAR");
Hit_Player_Sound.Play();
}
}
}
}
}
if (!attachedToPlayer)
{
foreach (PlayerClass p in players)
{
if(p != null) {
if (!attachedToPlayer && !throwing)
{
//Rectangle inter = Rectangle.Intersect(p.getPlayerRect(), spear);//new Rectangle((int)spear.X,(int)spear.Y, spear.Width, spear.Height));
Rectangle adjustedRect = new Rectangle(spear.Location, spear.Size);
adjustedRect.X -= 27;
bool hit = VectorMath.rectCollision(adjustedRect, orientation+(float)(Math.PI/2), p.getPlayerRect(), 0, new Point(16, 8));
if (!hit && spear.X > SCREENSIZE.X - spear.Width / 2)
{
Rectangle dupePos = new Rectangle((int)(adjustedRect.X - SCREENSIZE.X), adjustedRect.Y, adjustedRect.Width, adjustedRect.Height);
hit = VectorMath.rectCollision(dupePos, orientation - (float)(Math.PI / 2), p.getPlayerRect(), 0, new Point(16, 8));
}
if (!hit && spear.Y > SCREENSIZE.Y - spear.Width / 2)
{
Rectangle dupePos = new Rectangle((int)(adjustedRect.X), (int)(adjustedRect.Y - SCREENSIZE.Y), adjustedRect.Width, adjustedRect.Height);
hit = VectorMath.rectCollision(dupePos, orientation, p.getPlayerRect(), 0, new Point(16, 8));
}
if (hit && !p.hasSpear && !throwing && !p.dead)
{
gravityEffect = 0;
attachedToPlayer = true;
setOwner(p);
spearOwner.setSpear(this);
isInUse = false;
throwing = false;
p.hasSpear = true;
dropped = false;
}
}
if (!atRest && thrownBy != null && p.blinked == thrownBy.blinked)
{
Rectangle adjustedRect = new Rectangle(spear.Location, spear.Size);
adjustedRect.X -= 27;
bool hit = VectorMath.rectCollision(adjustedRect, orientation, p.getPlayerRect(), 0, new Point(16, 8));
if (!hit && spear.X > SCREENSIZE.X - spear.Width / 2)
{
Rectangle dupePos = new Rectangle((int)(adjustedRect.X - SCREENSIZE.X), adjustedRect.Y, adjustedRect.Width, adjustedRect.Height);
hit = VectorMath.rectCollision(dupePos, orientation - (float)(Math.PI / 2), p.getPlayerRect(), 0, new Point(16, 8));
}
if (!hit && spear.Y > SCREENSIZE.Y - spear.Width / 2)
{
Rectangle dupePos = new Rectangle((int)(adjustedRect.X), (int)(adjustedRect.Y - SCREENSIZE.Y), adjustedRect.Width, adjustedRect.Height);
hit = VectorMath.rectCollision(dupePos, orientation, p.getPlayerRect(), 0, new Point(16, 8));
}
if (hit && spearOwner != p && !p.dead && !dropped)
{
if (p.shield)
{
p.shield = false;
}
//ask david about how this should work
else if (p.spearCatch && !p.hasSpear)
{
p.spearCatch = false;
//pickup spear
gravityEffect = 0;
attachedToPlayer = true;
setOwner(p);
spearOwner.setSpear(this);
isInUse = false;
throwing = false;
p.hasSpear = true;
}
else
{
if(bomb)
{
bombExplosion();
bomb = false;
}
p.setDead(true, thrownBy, "SPEAR");
Hit_Player_Sound.Play();
}
//When the spear first leaves the one who threw its hitbox, make it live.
if(!hit && spearOwner != null && spearOwner == p)
{
isInUse = false;
attachedToPlayer = false;
if(spearOwner.spear == this)
{
spearOwner.hasSpear = false;
spearOwner.setSpear(null);
}
setOwner(null);
}
}
}
}
}
}
}
private void mapCollision()
{
Rectangle correctedBox = new Rectangle(spear.Location, spear.Size);
correctedBox.X -= 27;
Vector2[] verts = VectorMath.rectVerts(correctedBox, orientation);
HashSet<int> xVals = new HashSet<int>();
HashSet<int> yVals = new HashSet<int>();
//gather values the spear might be passing through
foreach(Vector2 vertex in verts)
{
xVals.Add((int)Math.Floor(vertex.X));
yVals.Add((int)Math.Floor(vertex.Y));
}
//Using the dot product, find collidable rectangles and test.
bool colliding = false;
foreach(int x in xVals)
{
foreach(int y in yVals)
{
int squareData = m.blockInfo(new Vector2(x, y));
if(thrownBy == null && squareData >= 10)
{
Rectangle r = new Rectangle(x, y, 32, 32);
if (VectorMath.rectCollision(correctedBox, orientation, r, 0))
{
velocity.X = 0;
velocity.Y = 0;
atRest = true;
throwing = false;
Hit_Wall_Sound.Play();
colliding = true;
break;
if(bomb)
{
bombExplosion();
bomb = false;
}
}
}//This is messy, I know.
else if((squareData >= 10 && !thrownBy.blinked) || (squareData >= 10 && squareData < 20 && thrownBy.blinked))
{
Rectangle r = new Rectangle(x, y, 32, 32);
if (VectorMath.rectCollision(correctedBox, orientation, r, 0))
{
velocity.X = 0;
velocity.Y = 0;
atRest = true;
throwing = false;
Hit_Wall_Sound.Play();
colliding = true;
break;
}
private void bombExplosion()
{
foreach (PlayerClass p in players)
{
int x_dist = p.getPlayerRect().X - spear.X;
int y_dist = p.getPlayerRect().Y - spear.Y;
int dist = x_dist * x_dist + y_dist * y_dist;
// if within radius
if (dist < EXPLODE_RADIUS*EXPLODE_RADIUS)
{
p.setDead(true, spearOwner, "BOMBSPEAR");
}
}
}
//Handle throw physics
public void throwSpear()
{
Throw_Sound.Play();
this.attachedToPlayer = false;
this.throwing = true;
this.isInUse = false;
this.spearOwner.hasSpear = false;
//velocity.X = spearOwner.velocity.X;
this.spear.X = spearOwner.getPlayerRect().X;
this.atRest = false;
Vector2 move = new Vector2(spearVector.X, spearVector.Y);
move *= THROW_V;
this.velocity.X = move.X;
this.velocity.Y = -move.Y;
spear.X += (int)(velocity.X * thrownBy.getMulti());
spear.Y += (int)(velocity.Y * thrownBy.getMulti());
if(spearOwner.bombSpear)
{
spearOwner.bombSpear = false;
bomb = true;
}
/*switch (spearOrientation)
switch (spearOrientation)
{
case 0:
velocity.X = -20;
spear.X += (int)velocity.X;
spear.Y = spearOwner.getPlayerRect().Y + spearOwner.getPlayerRect().Height / 3;
break;
case 2:
velocity.Y = -20;
spear.Y += (int)velocity.Y;
spear.Y = spearOwner.getPlayerRect().Y;
break;
case 4:
velocity.X = 20;
spear.X = spearOwner.getPlayerRect().Width + spearOwner.getPlayerRect().X - spear.Width;
spear.X += (int)velocity.X;
spear.Y = spearOwner.getPlayerRect().Y + spearOwner.getPlayerRect().Height / 3;
break;
case 6:
velocity.Y = 20;
spear.Y += (int)velocity.Y;
spear.Y = spearOwner.getPlayerRect().Y + spearOwner.getPlayerRect().Height / 2;
break;
}*/
//correctHitBox();
}