PageRenderTime 18ms CodeModel.GetById 1ms app.highlight 12ms RepoModel.GetById 1ms app.codeStats 1ms

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