public virtual void Update() {
//does nothing if object not spawned
if (isSpawned)
{
//get state of all keyboards on device
var state = Keyboard.GetState();
//checks up key, if it is pressed it will check if location is valid then update location.
if (state[Key.Up])
{
//checks if accelerating and adds speed if it is
if (uAcc)
{
speed += acc;
}
else
{
speed = baseSpeed;
}
bool valid = true;
//checks if the new coordinates are valid
if (collided && uAcc)
{
//indicates invlaid if previous motion was in this direction, and there was a collision
//NOTE: assumes other objects stationary, can remove if assumption changes
valid = false;
}
else
{
//checks if collision after movement
if (intersects(speed, "u", segs))
{
valid = false;
}
}
//modifies object location if valid
if (valid)
{
//updates each relevant coordinate and segment location
for (int i = 0; i < loc.GetLength(0); i++)
{
loc[i].Y += speed;
segs[i].move(speed, "u");
}
maxY += speed;
minY += speed;
uAcc = true;
collided = false;
}
//else indicates collision was detected and cancels acceleration
else
{
collided = true;
uAcc = false;
}
//indicates that other directions are not accelerating
dAcc = false;
rAcc = false;
lAcc = false;
}
//checks if down key is pressed (see above comments)
if (state[Key.Down])
{
if (dAcc)
{
speed += acc;
}
else
{
speed = baseSpeed;
}
bool valid = true;
//checks if the new coordinates are valid
if (collided && dAcc)
{
//indicates invlaid if previous motion was in this direction, and there was a collision
//NOTE: assumes other objects stationary, can remove if assumption changes
valid = false;
}
else
{
if (intersects(speed, "d", segs))
{
valid = false;
}
}
if (valid)
{
for (int i = 0; i < loc.GetLength(0); i++)
{
loc[i].Y -= speed;
segs[i].move(speed, "d");
}
maxY -= speed;
minY -= speed;
dAcc = true;
collided = false;
}
else
{
collided = true;
dAcc = false;
}
uAcc = false;
rAcc = false;
lAcc = false;
}
//checks if left key is pressed (see above comments)
if (state[Key.Left])
{
if (lAcc)
{
speed += acc;
}
else
{
speed = baseSpeed;
}
bool valid = true;
//checks if the new coordinates are valid
if (collided && lAcc)
{
//indicates invlaid if previous motion was in this direction, and there was a collision
//NOTE: assumes other objects stationary, can remove if assumption changes
valid = false;
}
else
{
if (intersects(speed, "l", segs))
{
valid = false;
}
}
if (valid)
{
for (int i = 0; i < loc.GetLength(0); i++)
{
loc[i].X -= speed;
segs[i].move(speed, "l");
}
maxX -= speed;
minX -= speed;
lAcc = true;
collided = false;
}
else
{
collided = true;
lAcc = false;
}
uAcc = false;
rAcc = false;
dAcc = false;
}
//checks if right key is pressed (see above comments)
if (state[Key.Right])
{
if (rAcc)
{
speed += acc;
}
else
{
speed = baseSpeed;
}
bool valid = true;
//checks if the new coordinates are valid
if (collided && rAcc)
{
//indicates invlaid if previous motion was in this direction, and there was a collision
//NOTE: assumes other objects stationary, can remove if assumption changes
valid = false;
}
else
{
if (intersects(speed, "r", segs))
{
valid = false;
}
}
if (valid)
{
for (int i = 0; i < loc.GetLength(0); i++)
{
loc[i].X += speed;
segs[i].move(speed, "r");
}
maxX += speed;
minX += speed;
rAcc = true;
collided = false;
}
else
{
collided = true;
rAcc = false;
}
uAcc = false;
lAcc = false;
dAcc = false;
}
}
}