|
1 | | -package com.miloshpetrov.sol2.game; |
2 | | - |
3 | | -import com.badlogic.gdx.math.Vector2; |
4 | | -import com.badlogic.gdx.physics.box2d.Fixture; |
5 | | -import com.badlogic.gdx.physics.box2d.RayCastCallback; |
6 | | -import com.miloshpetrov.sol2.TextureManager; |
7 | | -import com.miloshpetrov.sol2.game.input.Pilot; |
8 | | -import com.miloshpetrov.sol2.game.projectile.Projectile; |
9 | | -import com.miloshpetrov.sol2.game.ship.SolShip; |
10 | | - |
11 | | -import java.util.List; |
12 | | - |
13 | | -public class FractionMan { |
14 | | - |
15 | | - private final MyRayBack myRayBack; |
16 | | - |
17 | | - public FractionMan(TextureManager textureManager) { |
18 | | - myRayBack = new MyRayBack(); |
19 | | - } |
20 | | - |
21 | | - public SolShip getNearestEnemy(SolGame game, SolShip ship) { |
22 | | - Pilot pilot = ship.getPilot(); |
23 | | - float detectionDist = pilot.getDetectionDist(); |
24 | | - if (detectionDist <= 0) return null; |
25 | | - detectionDist += ship.getHull().config.getApproxRadius(); |
26 | | - Fraction f = pilot.getFraction(); |
27 | | - return getNearestEnemy(game, detectionDist, f, ship.getPos()); |
28 | | - } |
29 | | - |
30 | | - public SolShip getNearestEnemy(SolGame game, Projectile proj) { |
31 | | - return getNearestEnemy(game, game.getCam().getViewDist(), proj.getFraction(), proj.getPos()); |
32 | | - } |
33 | | - |
34 | | - public SolShip getNearestEnemy(SolGame game, float detectionDist, Fraction f, Vector2 pos) { |
35 | | - SolShip res = null; |
36 | | - float minDst = detectionDist; |
37 | | - List<SolObject> objs = game.getObjMan().getObjs(); |
38 | | - for (int i = 0, objsSize = objs.size(); i < objsSize; i++) { |
39 | | - SolObject o = objs.get(i); |
40 | | - if (!(o instanceof SolShip)) continue; |
41 | | - SolShip ship2 = (SolShip) o; |
42 | | - if (!areEnemies(f, ship2.getPilot().getFraction())) continue; |
43 | | - float dst = ship2.getPos().dst(pos) - ship2.getHull().config.getApproxRadius(); |
44 | | - if (minDst < dst) continue; |
45 | | - minDst = dst; |
46 | | - res = ship2; |
47 | | - } |
48 | | - return res; |
49 | | - } |
50 | | - |
51 | | - private boolean hasObstacles(SolGame game, SolShip shipFrom, SolShip shipTo) { |
52 | | - myRayBack.shipFrom = shipFrom; |
53 | | - myRayBack.shipTo = shipTo; |
54 | | - myRayBack.hasObstacle = false; |
55 | | - game.getObjMan().getWorld().rayCast(myRayBack, shipFrom.getPos(), shipTo.getPos()); |
56 | | - return myRayBack.hasObstacle; |
57 | | - } |
58 | | - |
59 | | - public boolean areEnemies(SolShip s1, SolShip s2) { |
60 | | - Fraction f1 = s1.getPilot().getFraction(); |
61 | | - Fraction f2 = s2.getPilot().getFraction(); |
62 | | - return areEnemies(f1, f2); |
63 | | - } |
64 | | - |
65 | | - public boolean areEnemies(Fraction f1, Fraction f2) { |
66 | | - return f1 != null && f2 != null && f1 != f2; |
67 | | - } |
68 | | - |
69 | | - private static class MyRayBack implements RayCastCallback { |
70 | | - public SolShip shipFrom; |
71 | | - public SolShip shipTo; |
72 | | - public boolean hasObstacle; |
73 | | - |
74 | | - @Override |
75 | | - public float reportRayFixture(Fixture fixture, Vector2 point, Vector2 normal, float fraction) { |
76 | | - SolObject o = (SolObject) fixture.getBody().getUserData(); |
77 | | - if (o == shipFrom || o == shipTo) { |
78 | | - return -1; |
79 | | - } |
80 | | - hasObstacle = true; |
81 | | - return 0; |
82 | | - } |
83 | | - } |
84 | | -} |
| 1 | +package com.miloshpetrov.sol2.game; |
| 2 | + |
| 3 | +import com.badlogic.gdx.math.Vector2; |
| 4 | +import com.badlogic.gdx.physics.box2d.Fixture; |
| 5 | +import com.badlogic.gdx.physics.box2d.RayCastCallback; |
| 6 | +import com.miloshpetrov.sol2.TextureManager; |
| 7 | +import com.miloshpetrov.sol2.game.input.Pilot; |
| 8 | +import com.miloshpetrov.sol2.game.projectile.Projectile; |
| 9 | +import com.miloshpetrov.sol2.game.ship.SolShip; |
| 10 | + |
| 11 | +import java.util.List; |
| 12 | + |
| 13 | +public class FactionMan { |
| 14 | + |
| 15 | + private final MyRayBack myRayBack; |
| 16 | + |
| 17 | + public FactionMan(TextureManager textureManager) { |
| 18 | + myRayBack = new MyRayBack(); |
| 19 | + } |
| 20 | + |
| 21 | + public SolShip getNearestEnemy(SolGame game, SolShip ship) { |
| 22 | + Pilot pilot = ship.getPilot(); |
| 23 | + float detectionDist = pilot.getDetectionDist(); |
| 24 | + if (detectionDist <= 0) return null; |
| 25 | + detectionDist += ship.getHull().config.getApproxRadius(); |
| 26 | + Faction f = pilot.getFaction(); |
| 27 | + return getNearestEnemy(game, detectionDist, f, ship.getPos()); |
| 28 | + } |
| 29 | + |
| 30 | + public SolShip getNearestEnemy(SolGame game, Projectile proj) { |
| 31 | + return getNearestEnemy(game, game.getCam().getViewDist(), proj.getFaction(), proj.getPos()); |
| 32 | + } |
| 33 | + |
| 34 | + public SolShip getNearestEnemy(SolGame game, float detectionDist, Faction f, Vector2 pos) { |
| 35 | + SolShip res = null; |
| 36 | + float minDst = detectionDist; |
| 37 | + List<SolObject> objs = game.getObjMan().getObjs(); |
| 38 | + for (int i = 0, objsSize = objs.size(); i < objsSize; i++) { |
| 39 | + SolObject o = objs.get(i); |
| 40 | + if (!(o instanceof SolShip)) continue; |
| 41 | + SolShip ship2 = (SolShip) o; |
| 42 | + if (!areEnemies(f, ship2.getPilot().getFaction())) continue; |
| 43 | + float dst = ship2.getPos().dst(pos) - ship2.getHull().config.getApproxRadius(); |
| 44 | + if (minDst < dst) continue; |
| 45 | + minDst = dst; |
| 46 | + res = ship2; |
| 47 | + } |
| 48 | + return res; |
| 49 | + } |
| 50 | + |
| 51 | + private boolean hasObstacles(SolGame game, SolShip shipFrom, SolShip shipTo) { |
| 52 | + myRayBack.shipFrom = shipFrom; |
| 53 | + myRayBack.shipTo = shipTo; |
| 54 | + myRayBack.hasObstacle = false; |
| 55 | + game.getObjMan().getWorld().rayCast(myRayBack, shipFrom.getPos(), shipTo.getPos()); |
| 56 | + return myRayBack.hasObstacle; |
| 57 | + } |
| 58 | + |
| 59 | + public boolean areEnemies(SolShip s1, SolShip s2) { |
| 60 | + Faction f1 = s1.getPilot().getFaction(); |
| 61 | + Faction f2 = s2.getPilot().getFaction(); |
| 62 | + return areEnemies(f1, f2); |
| 63 | + } |
| 64 | + |
| 65 | + public boolean areEnemies(Faction f1, Faction f2) { |
| 66 | + return f1 != null && f2 != null && f1 != f2; |
| 67 | + } |
| 68 | + |
| 69 | + private static class MyRayBack implements RayCastCallback { |
| 70 | + public SolShip shipFrom; |
| 71 | + public SolShip shipTo; |
| 72 | + public boolean hasObstacle; |
| 73 | + |
| 74 | + @Override |
| 75 | + public float reportRayFixture(Fixture fixture, Vector2 point, Vector2 normal, float fraction) { |
| 76 | + SolObject o = (SolObject) fixture.getBody().getUserData(); |
| 77 | + if (o == shipFrom || o == shipTo) { |
| 78 | + return -1; |
| 79 | + } |
| 80 | + hasObstacle = true; |
| 81 | + return 0; |
| 82 | + } |
| 83 | + } |
| 84 | +} |
0 commit comments