void PlayerMovement() { HelperObject ho = (HelperObject)ObjectManager.GetObjectByName("HelperObject"); if (ho.xVel != 0) { playerVelX += ho.xVel; playerIsOnGround = false; } if (ho.yVel != 0) { playerVelY += ho.yVel; } if (playerIsOnGround == true) { playerVelY = 0; playerVelX *= 0.9f; if (InputManager.IsPressed(Keys.A) && canGoLeft == true && head.IsDead == false) { playerVelX -= movementSpeed; } if (InputManager.IsPressed(Keys.D) && canGoRight == true && head.IsDead == false) { playerVelX += movementSpeed; } if (InputManager.IsTriggered(Keys.W) && head.IsDead == false) { playerVelY = PersistentData.jumpPower; } if (Math.Abs(playerVelX) > 0.5f) { isWalking = true; } else { isWalking = false; } } else { playerVelY -= 0.16f; if (InputManager.IsPressed(Keys.A) && canGoLeft == true && head.IsDead == false) { playerVelX -= 0.02f; playerPosX -= 0.7f; } if (InputManager.IsPressed(Keys.D) && canGoRight == true && head.IsDead == false) { playerVelX += 0.02f; playerPosX += 0.7f; } } if (playerIsOnGround == true) { if (isWalking == true) { playerBodyFrameTime -= FrameRateController.FrameTime; if (playerBodyFrameTime <= 0) { playerBodyFrameNum++; playerBodyFrameTime = 0.1f; } if (playerBodyFrameNum >= 8) { playerBodyFrameNum = 0; } } else { playerBodyFrameNum = 0; } } else { playerBodyFrameNum = 2; } body.Position.X = playerPosX; body.Position.Y = playerPosY + 46; head.Position.X = playerPosX; head.Position.Y = playerPosY + 76; gun.Position.X = playerPosX; gun.Position.Y = playerPosY + 66; playerPosX += playerVelX; playerPosY += playerVelY; //float mousePosX = Cursor.Position.X - 965 + Camera.Position.X ; //float mousePosY = (Cursor.Position.Y * -1) + 500 + Camera.Position.Y; float mousePosX = Cursor.Position.X - 960 + gun.Position.X; float mousePosY = (Cursor.Position.Y * -1) + 540 + gun.Position.Y; float xdiff = mousePosX - gun.Position.X; float ydiff = mousePosY - gun.Position.Y; float xsquared = xdiff * xdiff; float ysquared = ydiff * ydiff; float length = (float)Math.Sqrt(xsquared + ysquared); xdiff /= length; ydiff /= length; //firing if (weapon1Cooldown > 0) { weapon1Cooldown -= FrameRateController.FrameTime; ho.bullet1CoolDown = weapon1Cooldown; } if (weapon2Cooldown > 0) { weapon2Cooldown -= FrameRateController.FrameTime; ho.bullet2CoolDown = weapon2Cooldown; } if (weapon3Cooldown > 0) { weapon3Cooldown -= FrameRateController.FrameTime; ho.bullet3CoolDown = weapon3Cooldown; } if (weapon4Cooldown > 0) { weapon4Cooldown -= FrameRateController.FrameTime; ho.bullet4CoolDown = weapon4Cooldown; } if (grenade1Cooldown > 0) { grenade1Cooldown -= FrameRateController.FrameTime; ho.grenade1CoolDown = grenade1Cooldown; } if (grenade2Cooldown > 0) { grenade2Cooldown -= FrameRateController.FrameTime; ho.grenade2CoolDown = grenade2Cooldown; } if (grenade3Cooldown > 0) { grenade3Cooldown -= FrameRateController.FrameTime; ho.grenade3CoolDown = grenade3Cooldown; } if (grenade4Cooldown > 0) { grenade4Cooldown -= FrameRateController.FrameTime; ho.grenade4CoolDown = grenade4Cooldown; } if (InputManager.IsTriggered(Keys.LButton) && head.IsDead == false) { if (PersistentData.weaponType == 1 && weapon1Cooldown <= 0) { GameObject b = new bullet1(xdiff * 10, ydiff * 10); b.Position.X = gun.Position.X + xdiff * 30; b.Position.Y = gun.Position.Y + ydiff * 30; weapon1Cooldown = PersistentData.weapon1DefaultCooldown; } if (PersistentData.weaponType == 2 && weapon2Cooldown <= 0) { GameObject b = new bullet2(xdiff * 10, ydiff * 10); b.Position.X = gun.Position.X + xdiff * 30; b.Position.Y = gun.Position.Y + ydiff * 30; weapon2Cooldown = PersistentData.weapon2DefaultCooldown; } if (PersistentData.weaponType == 3 && weapon3Cooldown <= 0) { GameObject b = new bullet3(xdiff * 10, ydiff * 10); b.Position.X = gun.Position.X + xdiff * 30; b.Position.Y = gun.Position.Y + ydiff * 30; weapon3Cooldown = PersistentData.weapon3DefaultCooldown; } if (PersistentData.weaponType == 4 && weapon4Cooldown <= 0) { GameObject b = new bullet4(xdiff * 10, ydiff * 10); b.Position.X = gun.Position.X + xdiff * 30; b.Position.Y = gun.Position.Y + ydiff * 30; weapon4Cooldown = PersistentData.weapon4DefaultCooldown; } } if (head.IsDead == false) { if (InputManager.IsTriggered(Keys.Z) && grenade1Cooldown <= 0 && PersistentData.grenade1 == true) { GameObject g = new grenade1(xdiff * 5, ydiff * 5); g.Position.X = gun.Position.X + xdiff * 5; g.Position.Y = gun.Position.Y + ydiff * 5; grenade1Cooldown = PersistentData.grenade1DefaultCooldown; } if (InputManager.IsTriggered(Keys.X) && grenade2Cooldown <= 0 && PersistentData.grenade2 == true) { GameObject g = new grenade2(xdiff * 5, ydiff * 5); g.Position.X = gun.Position.X + xdiff * 5; g.Position.Y = gun.Position.Y + ydiff * 5; grenade2Cooldown = PersistentData.grenade2DefaultCooldown; } if (InputManager.IsTriggered(Keys.C) && grenade3Cooldown <= 0 && PersistentData.grenade3 == true) { GameObject g = new grenade3(xdiff * 7, ydiff * 7); g.Position.X = gun.Position.X + xdiff * 5; g.Position.Y = gun.Position.Y + ydiff * 5; grenade3Cooldown = PersistentData.grenade3DefaultCooldown; } if (InputManager.IsTriggered(Keys.V) && grenade4Cooldown <= 0 && PersistentData.grenade4 == true) { GameObject g = new grenade4(xdiff * 7, ydiff * 7); g.Position.X = gun.Position.X + xdiff * 5; g.Position.Y = gun.Position.Y + ydiff * 5; grenade4Cooldown = PersistentData.grenade4DefaultCooldown; } } float radians = (float)(Math.Atan2(ydiff, xdiff)); float degrees = (float)(radians * 180 / Math.PI); gun.Rotation = degrees; int FrameOffset = 0; if (degrees < 90 && degrees > -90 && head.IsDead == false) { gun.AnimationData.GoToFrame(0); head.AnimationData.GoToFrame(0); body.Rotation = 0; FrameOffset = 0; deadBodyFrameOffset = 0; } else if (head.IsDead == false) { gun.AnimationData.GoToFrame(1); head.AnimationData.GoToFrame(1); body.Rotation = 180; FrameOffset = 8; deadBodyFrameOffset = 12; } if (degrees > -60 || degrees < -120) { head.Rotation = degrees; } else if (degrees > -90) { head.Rotation = -60; } else { head.Rotation = -120; } body.AnimationData.GoToFrame((uint)(playerBodyFrameNum + FrameOffset)); }
void EnemyMovement() { if (isOnGround == true) { velY = 0; velX *= 0.9f; if (Math.Abs(velX) > 0.5f) { isWalking = true; body.AnimationData.Play(); } else { isWalking = false; } } else { velY -= 0.16f; } if (isOnGround == true) { if (isWalking == true) { bodyFrameTime -= FrameRateController.FrameTime; if (bodyFrameTime <= 0) { bodyFrameNum++; bodyFrameTime = 0.1f; } if (bodyFrameNum >= 8) { bodyFrameNum = 0; } } else { bodyFrameNum = 0; } } else { bodyFrameNum = 2; } body.Position.X = Position.X; body.Position.Y = Position.Y + 46; head.Position.X = Position.X; head.Position.Y = Position.Y + 76; gun.Position.X = Position.X; gun.Position.Y = Position.Y + 66; Position.X += velX; Position.Y += velY; GameObject playerbody = ObjectManager.GetObjectByName("PlayerBody"); if (playerbody != null && head.IsDead == false && LOSWithPlayer == true) { playerLastSeenPosX = playerbody.Position.X; playerLastSeenPosY = playerbody.Position.Y; isAwareOfPlayer = true; float mousePosX = playerbody.Position.X; float mousePosY = playerbody.Position.Y; float xdiff = mousePosX - gun.Position.X; float ydiff = mousePosY - gun.Position.Y + (Math.Abs(xdiff) / 11); if (weapontype == 3) { ydiff = mousePosY - gun.Position.Y + (Math.Abs(xdiff) / 3); } float xsquared = xdiff * xdiff; float ysquared = ydiff * ydiff; float length = (float)Math.Sqrt(xsquared + ysquared); xdiff /= length; ydiff /= length; if (timer <= 0) { if (weapontype == 1) { GameObject b = new bullet1(xdiff * 10, ydiff * 10); b.Position.X = gun.Position.X + xdiff * 30; b.Position.Y = gun.Position.Y + ydiff * 30; timer = PersistentData.weapon1DefaultCooldown * 1.8f; } if (weapontype == 2) { GameObject b = new bullet2(xdiff * 10, ydiff * 10); b.Position.X = gun.Position.X + xdiff * 30; b.Position.Y = gun.Position.Y + ydiff * 30; timer = PersistentData.weapon2DefaultCooldown * 0.7f; } if (weapontype == 3) { GameObject b = new bullet3(xdiff * 10, ydiff * 10); b.Position.X = gun.Position.X + xdiff * 30; b.Position.Y = gun.Position.Y + ydiff * 30; timer = PersistentData.weapon3DefaultCooldown * 0.9f; } if (weapontype == 4) { GameObject b = new bullet4(xdiff * 10, ydiff * 10); b.Position.X = gun.Position.X + xdiff * 30; b.Position.Y = gun.Position.Y + ydiff * 30; timer = PersistentData.weapon4DefaultCooldown * 0.7f; } } float radians = (float)(Math.Atan2(ydiff, xdiff)); degrees = (float)(radians * 180 / Math.PI); gun.Rotation = degrees; if (degrees < 90 && degrees > -90) { gun.AnimationData.GoToFrame(0); head.AnimationData.GoToFrame(0); body.Rotation = 0; FrameOffset = 0; deadBodyFrameOffset = 0; } else { gun.AnimationData.GoToFrame(1); head.AnimationData.GoToFrame(1); body.Rotation = 180; FrameOffset = 8; deadBodyFrameOffset = 12; } if (degrees > -60 || degrees < -120) { head.Rotation = degrees; } else if (degrees > -90) { head.Rotation = -60; } else { head.Rotation = -120; } } else if (playerbody != null && head.IsDead == false && LOSWithPlayer == false && isAwareOfPlayer == true) { if (isOnGround) { if (playerLastSeenPosX > body.Position.X) { velX += PersistentData.movementSpeed; } if (playerLastSeenPosX < body.Position.X) { velX -= PersistentData.movementSpeed; } if (canGoLeft == false || canGoRight == false) { velY = PersistentData.jumpPower * 1.35f; Position.Y += 1; isOnGround = false; } } else { if (playerLastSeenPosX > body.Position.X) { velX += 0.03f; } if (playerLastSeenPosX < body.Position.X) { velX -= 0.03f; } } } body.AnimationData.GoToFrame((uint)(bodyFrameNum + FrameOffset)); }