/Code/src/AIBehaviours/ChaseClosestCursorStrategy.java

https://bitbucket.org/DeveloperUX/behaviortree · Java · 61 lines · 39 code · 7 blank · 15 comment · 5 complexity · 3d45c0c08b3e65f9597f092a746e0871 MD5 · raw file

  1. package AIBehaviours;
  2. import com.game.Player;
  3. import com.game.Preferences;
  4. import com.game.Vec2;
  5. import com.game.Scenes.PlayScene;
  6. /**
  7. * Strategy that chases the closest cursor
  8. * @deprecated
  9. * @see Task
  10. * @author Ying
  11. *
  12. */
  13. public class ChaseClosestCursorStrategy extends Strategy
  14. {
  15. /**
  16. * Creates a new instance of the ChaseClosestCursorStrategy
  17. * @param sceneRef Reference to the PlayScene
  18. * @param playerRef Reference to the Player
  19. */
  20. public ChaseClosestCursorStrategy(PlayScene sceneRef, Player playerRef)
  21. {
  22. super(sceneRef,playerRef,0.5f);
  23. }
  24. @Override public void Start()
  25. {}
  26. /**
  27. * Updates the strategy
  28. */
  29. @Override protected void Update()
  30. {
  31. Vec2 cursorPos = playerRef.GetCursor().GetPosition();
  32. Vec2 enemyPos = null;
  33. Vec2 destination = new Vec2();
  34. Vec2 vecToEnemy = new Vec2();
  35. float minLenght = Preferences.Get().mapWidth*2; // Really large number
  36. for(int i= 0; i < sceneRef.GetPlayers().size(); i++)
  37. {
  38. if(sceneRef.GetPlayers().elementAt(i).GetID() != this.playerRef.GetID())
  39. {
  40. if(sceneRef.GetPlayers().elementAt(i).GetTotalDensity() > 0)
  41. {
  42. enemyPos = sceneRef.GetPlayers().elementAt(i).GetCursor().GetPosition();
  43. vecToEnemy.Set(enemyPos.X()-cursorPos.X(), enemyPos.Y()-cursorPos.Y());
  44. if(vecToEnemy.Length() < minLenght)
  45. {
  46. minLenght = (float) vecToEnemy.Length();
  47. destination.Set(vecToEnemy.X(), vecToEnemy.Y());
  48. }
  49. }
  50. }
  51. }
  52. this.playerRef.GetCursor().MoveInDirection(destination);
  53. }
  54. }