Android Open Source - libgdx-demo-pax-britannica Fighter 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.fighter;
//  w  w  w .ja  va2  s .  c o m
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Vector2;

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

public class FighterAI {
  // shot range
  private float shot_range = 200;

  // try to stay this far away when you're out of ammo
  private float run_distance = 200;

  // true when we've shot everything and want to make a distance, false means
  // we're approaching to attack
  private boolean running = false;

  public Ship target;
  private boolean on_screen = true;
  
  //recycle vars
  Vector2 to_target = new Vector2();

  private Fighter fighter;

  public FighterAI(Fighter fighter) {
    this.fighter = fighter;
    retarget();
  }

  public void retarget() {
    target = Targeting.getNearestOfType(fighter, 1);
    if (target == null) {
      target = Targeting.getNearestOfType(fighter, 0);
    }
    if (target == null) {
      target = Targeting.getNearestOfType(fighter, 2);
    }
    if (target == null) {
      target = Targeting.getNearestOfType(fighter, 3);
    }
  }

  public void update() {
    // if we go from on to off screen, retarget
    boolean new_on_screen = Targeting.onScreen(fighter.collisionCenter);
    if (on_screen && !new_on_screen || target == null || !target.alive || MathUtils.random() < 0.005f) {
      retarget();
    }
    on_screen = new_on_screen;

    if (target != null) {
      to_target.set(target.collisionCenter.x - fighter.collisionCenter.x, target.collisionCenter.y - fighter.collisionCenter.y);
      float dist_squared = to_target.dot(to_target);

      if (running) {
        // run away until you have full ammo and are far enough away
        boolean too_close = dist_squared < Math.pow(run_distance, 2);
        // if you're too close to the target then turn away
        if (too_close) {
          fighter.goAway(target.collisionCenter, true);
        } else {
          fighter.thrust();
        }

        if (!fighter.isEmpty() && !too_close) {
          running = false;
        }
      } else {
        // go towards the target and attack!
        fighter.goTowards(target.collisionCenter, true);

        // maybe shoot
        if (fighter.isReadyToShoot()) {
          if (dist_squared <= shot_range * shot_range && to_target.dot(fighter.facing) > 0 && Math.pow(to_target.dot(fighter.facing), 2) > 0.97 * dist_squared) {
            fighter.shoot();
          }
        }

        // if out of shots then run away
        if (fighter.isEmpty()) {
          running = 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