Android Open Source - libgdx-demo-pax-britannica Missile A I






From Project

Back to project page libgdx-demo-pax-britannica.

License

The source code is released under:

Copyright (c) 2010 Ben Abraham, Renaud B?dard, Henk Boom, Daniel Burton, Matthew Gallant Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated ...

If you think the Android project libgdx-demo-pax-britannica listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package de.swagner.paxbritannica.frigate;
//w w w  .ja v  a 2  s .co m
import com.badlogic.gdx.math.Vector2;

import de.swagner.paxbritannica.GameInstance;
import de.swagner.paxbritannica.Ship;
import de.swagner.paxbritannica.Targeting;

public class MissileAI {
  private float MAX_LIFETIME = 5; // 5 seconds to auto-destruct

  private Ship target;

  private Missile missile;
  
  Vector2 relativeVel = new Vector2();
  Vector2 toTarget = new Vector2();

  public MissileAI(Missile missile) {
    this.missile = missile;
    retarget();
  }

  public void retarget() {
    target = Targeting.getTypeInRange(missile, 0, 500);
    if (target == null) {
      target = Targeting.getTypeInRange(missile, 1, 500);
    } else
      return;
    if (target == null) {
      target = Targeting.getTypeInRange(missile, 2, 500);
    } else
      return;
    if (target == null) {
      target = Targeting.getNearestOfType(missile, 1);
    } else
      return;
    if (target == null) {
      target = Targeting.getNearestOfType(missile, 3);
    } else
      target = null;
  }

  public void selfDestruct() {
    // EXPLODE!
    missile.alive = false;
    GameInstance.getInstance().explosionParticles.addTinyExplosion(missile.collisionCenter);
  }

  public Vector2 predict() {
    relativeVel.set(missile.velocity).sub(target.velocity);
    toTarget.set(target.collisionCenter).sub(missile.collisionCenter);
    if (missile.velocity.dot(toTarget) != 0) {
      float time_to_target = toTarget.dot(toTarget) / relativeVel.dot(toTarget);
      return new Vector2(target.collisionCenter).sub(relativeVel.scl(Math.max(0, time_to_target)));
    } else {
      return target.collisionCenter;
    }
  }

  public void update() {
    if (target == null || missile.aliveTime > MAX_LIFETIME) {
      selfDestruct();
    } else if (!target.alive) {
      retarget();
    } else {
      missile.goTowards(predict(), true);
    }
  }
}




Java Source Code List

de.swagner.paxbritannica.Bullet.java
de.swagner.paxbritannica.Collision.java
de.swagner.paxbritannica.Constants.java
de.swagner.paxbritannica.DefaultScreen.java
de.swagner.paxbritannica.GameInstance.java
de.swagner.paxbritannica.GameScreen.java
de.swagner.paxbritannica.IOSLauncher.java
de.swagner.paxbritannica.PaxBritannica.java
de.swagner.paxbritannica.Resources.java
de.swagner.paxbritannica.Ship.java
de.swagner.paxbritannica.Targeting.java
de.swagner.paxbritannica.android.AndroidLauncher.java
de.swagner.paxbritannica.background.BackgroundFXRenderer.java
de.swagner.paxbritannica.background.Debris.java
de.swagner.paxbritannica.background.Fish.java
de.swagner.paxbritannica.bomber.Bomb.java
de.swagner.paxbritannica.bomber.BomberAI.java
de.swagner.paxbritannica.bomber.Bomber.java
de.swagner.paxbritannica.client.HtmlLauncher.java
de.swagner.paxbritannica.desktop.DesktopLauncher.java
de.swagner.paxbritannica.factory.EasyEnemyProduction.java
de.swagner.paxbritannica.factory.FactoryProduction.java
de.swagner.paxbritannica.factory.HardEnemyProduction.java
de.swagner.paxbritannica.factory.MediumEnemyProduction.java
de.swagner.paxbritannica.factory.PlayerProduction.java
de.swagner.paxbritannica.factory.Production.java
de.swagner.paxbritannica.fighter.FighterAI.java
de.swagner.paxbritannica.fighter.Fighter.java
de.swagner.paxbritannica.fighter.Laser.java
de.swagner.paxbritannica.frigate.FrigateAI.java
de.swagner.paxbritannica.frigate.Frigate.java
de.swagner.paxbritannica.frigate.MissileAI.java
de.swagner.paxbritannica.frigate.Missile.java
de.swagner.paxbritannica.help.Help.java
de.swagner.paxbritannica.mainmenu.Countdown.java
de.swagner.paxbritannica.mainmenu.FactorySelector.java
de.swagner.paxbritannica.mainmenu.MainMenu.java
de.swagner.paxbritannica.particlesystem.BigBubbleParticleEmitter.java
de.swagner.paxbritannica.particlesystem.BubbleParticleEmitter.java
de.swagner.paxbritannica.particlesystem.ExplosionParticleEmitter.java
de.swagner.paxbritannica.particlesystem.ParticleEmitter.java
de.swagner.paxbritannica.particlesystem.Particle.java
de.swagner.paxbritannica.particlesystem.SparkParticleEmitter.java
de.swagner.paxbritannica.settings.Settings.java