Android Open Source - libgdx-demo-pax-britannica Bomber 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.bomber;
// www .j av  a2s  .c  o  m
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Vector2;

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

public class BomberAI {
  private float APPROACH_DISTANCE = 210;
  private float COOLDOWN_DURATION = 0.6f;
  private float MAX_SHOTS = 4;

  private float cooldown_timer = 0;
  private float shots_counter = MAX_SHOTS;
  private int approach_sign = 1;
  float delta;

  // 0 = approach
  // 1 = turn
  // 2 = shoot
  // 3 = move_away
  private int state = 0;
  
  //recylce
  Vector2 target_direction = new Vector2();

  public Ship target;

  private Bomber bomber;

  public BomberAI(Bomber bomber) {
    this.bomber = bomber;
  }

  public void retarget() {
    target = Targeting.getNearestOfType(bomber, 2);
    if (target == null) {
      target = Targeting.getNearestOfType(bomber, 3);
    }
    // if (target == null) {
    // target = Targeting.get_nearest_of_type(bomber, "bomber");
    // } else
    // return;
    // if (target == null) {
    // target = Targeting.get_nearest_of_type(bomber, "fighter");
    // } else
    // return;
  }

  public void reviseApproach() {
    if (MathUtils.random() < 0.5) {
      approach_sign = 1;
    } else {
      approach_sign = -1;
    }
  }

  public void update() {
    delta = Math.min(0.06f, Gdx.graphics.getDeltaTime());
    
    if (target == null || !target.alive || MathUtils.random() < 0.005f) {
      Ship old_target = target;
      retarget();
      if (old_target != null && target != null && old_target.id != target.id) {
        reviseApproach();
      }
    }

    if (target != null) {
      float target_distance = target.collisionCenter.dst(bomber.collisionCenter);
      target_direction.set(target.collisionCenter).sub(bomber.collisionCenter).nor();

      float unit_factor;
      if (target instanceof Frigate) {
        unit_factor = 0.6f;
      } else {
        unit_factor = 1;
      }

      if (target_distance > (APPROACH_DISTANCE + 50) * unit_factor) {
        state = 0;
      }

      // approach
      if (state == 0) {
        bomber.goTowards(target.collisionCenter, true);
        if (target_distance < APPROACH_DISTANCE * unit_factor) {
          reviseApproach();
          state = 1;
        }
      }
      // turn
      else if (state == 1) {
        bomber.turn(-approach_sign);
        bomber.thrust(unit_factor * 0.75f);
        if (target_direction.dot(bomber.facing) < 0.5f) {
          state = 2;
        }
      }
      // shoot
      else if (state == 2) {
        bomber.turn(approach_sign * 0.05f);
        bomber.thrust(unit_factor * 0.75f);

        cooldown_timer -= delta;
        if (cooldown_timer <= 0) {
          bomber.shoot(approach_sign);
          cooldown_timer = COOLDOWN_DURATION;
          shots_counter -= 1;

          if (shots_counter == 0) {
            shots_counter = MAX_SHOTS;
            state = 3;
          }
        }
      }
      // move_away
      else if (state == 3) {
        bomber.goAway(target.collisionCenter, 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