Created
January 3, 2016 22:40
-
-
Save nanonull/0c82ddb88b1a15a75059 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.artemis.Entity; | |
import com.badlogic.gdx.math.Vector2; | |
import conversion7.gdxg.engine.utils.MathUtilsExt; | |
import conversion7.spashole.game.artemis_odb.box2d.Box2dBodyComponent; | |
import conversion7.spashole.game.artemis_odb.box2d.Box2dBodySystem; | |
import conversion7.spashole.game.utils.SpasholeUtils; | |
public class AiAttackSystem { | |
public static final Vector2 posDiff = new Vector2(); | |
public static final float CHASE_FROM_DISTANCE = SpasholeUtils.sceneToWorld(200); | |
public static final float DELTA_STEP = AiStateMachineSystem.DELTA_STEP; | |
public static final float MAKE_SHOT_ON_ANGLE = MathUtilsExt.toRadians(15); | |
private static Vector2 moveDirectionWip = new Vector2(); | |
public static void eval(Entity attackerE, int targetE) { | |
Position2dComponent attackerPos = Position2dSystem.components.get(attackerE); | |
Position2dComponent targetPos = Position2dSystem.components.get(targetE); | |
posDiff.set(targetPos.pos).sub(attackerPos.pos); | |
float angleRadToTarget = MathUtilsExt.absRadians(posDiff.angleRad()); | |
Box2dBodyComponent attackerBody = Box2dBodySystem.components.get(attackerE); | |
float attackerAngle = MathUtilsExt.absRadians(attackerBody.body.getAngle()); | |
float angleDiff = MathUtilsExt.diffAbsoluteRadians(attackerAngle, angleRadToTarget); | |
if (Math.abs(angleDiff) < MAKE_SHOT_ON_ANGLE) { | |
GunComponent gunComponent = GunSystem.components.get(attackerE); | |
gunComponent.makeShot(); | |
} else { | |
float torque; | |
if (angleDiff < 0) { | |
torque = -MovementConstant.SHIP_PARAMS.torqueForce; | |
} else { | |
torque = MovementConstant.SHIP_PARAMS.torqueForce; | |
} | |
// TODO movement constants as component | |
Box2dBodySystem.rotate(attackerBody | |
, torque | |
, DELTA_STEP); | |
} | |
if (posDiff.len() > CHASE_FROM_DISTANCE) { | |
// TODO movement constants as component | |
MathUtilsExt.angleRadiansToVector(attackerBody.body.getAngle(), moveDirectionWip); | |
Box2dBodySystem.move(attackerBody | |
, moveDirectionWip | |
, MovementConstant.SHIP_PARAMS.moveMltpl | |
, DELTA_STEP | |
, true); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment