Advertisement
Guest User

Arrive

a guest
Feb 6th, 2016
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.12 KB | None | 0 0
  1. using Microsoft.Xna.Framework;
  2. using Microsoft.Xna.Framework.Graphics;
  3. using Microsoft.Xna.Framework.Input;
  4. using System;
  5.  
  6. namespace AICatMouse
  7. {
  8. class AgentSprites : Sprites
  9. {
  10. static string spriteName = "cat";
  11. static float linearVelocity = .125f;
  12. float maxLinearVelocity = .4f;
  13. float timeToTarget = .5f;
  14. float distance;
  15. Vector2 playerDistance;
  16. float radius = 56;
  17. float arrivalRadius = 16;
  18. Vector2 normalizedDistance;
  19. bool isCaught = false;
  20. Game ASgame;
  21. UserSprites user;
  22. public AgentSprites(Game game, SpriteBatch spriteBatch, UserSprites player) : base(game, spriteBatch, spriteName, linearVelocity)
  23. {
  24. ASgame = game;
  25. user = player;
  26. }
  27.  
  28. public override void Update(GameTime gameTime)
  29. {
  30. //Velocity and distance
  31. playerDistance = new Vector2(user.Position.X - base.Position.X, user.Position.Y - base.Position.Y);
  32. distance = Vector2.Distance(user.Position, base.Position);
  33.  
  34. //If cat is in slow area
  35. if (Math.Abs(distance) < radius)
  36. {
  37. base.Velocity = (base.Velocity * distance / radius) / timeToTarget;
  38. base.Position += base.Velocity;
  39. }
  40. else
  41. {
  42. normalizedDistance = playerDistance;
  43. normalizedDistance.Normalize();
  44. normalizedDistance *= maxLinearVelocity;
  45. base.Velocity = normalizedDistance;
  46. base.Position += base.Velocity * gameTime.ElapsedGameTime.Milliseconds;
  47. }
  48.  
  49. //If cat arrived
  50. if (Math.Abs(distance) < arrivalRadius)
  51. {
  52. isCaught = true;
  53. Position += Vector2.Zero;
  54. }
  55.  
  56. base.Update(gameTime);
  57. }
  58.  
  59. public bool Caught
  60. {
  61. get
  62. {
  63. return isCaught;
  64. }
  65. set
  66. {
  67. isCaught = value;
  68. }
  69. }
  70. }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement