Package com.mlarktar.spacewar.rules

Source Code of com.mlarktar.spacewar.rules.InterceptIncomming

/*
*  This file is part of jSpaceWar.
*
*  jSpaceWar is free software: you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation, either version 3 of the License, or
*  (at your option) any later version.
*
*  jSpaceWar is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*  GNU General Public License for more details.
*
*  You should have received a copy of the GNU General Public License
*  along with jSpaceWar.  If not, see <http://www.gnu.org/licenses/>.
*/
package com.mlarktar.spacewar.rules;

import java.awt.Rectangle;
import java.util.Collection;
import java.util.Iterator;

import com.mlarktar.spacewar.sprites.LaserBeam;
import com.mlarktar.spacewar.sprites.Misile;
import com.mlarktar.spacewar.sprites.ShipAI;
import com.mlarktar.spacewar.sprites.SpaceItem;

/**
* @author malar
*
*/
public class InterceptIncomming extends AIRule
  /**
   * @param ship
   */
  public InterceptIncomming(ShipAI ship) {
    super(ship);
    setPriority(2);
    setFunction(AIRule.WEAPONS);
  }

  /* (non-Javadoc)
   * @see AIRule#evaluate()
   */
  public void evaluate() {
    Rectangle myPos = ship.getBounds();

    Collection items =
      ship.getParent().getSpaceItems(
        new Rectangle(
          (int) (myPos.getCenterX() - LaserBeam.LASERLENGTH),
          (int) (myPos.getCenterY() - LaserBeam.LASERLENGTH),
          LaserBeam.LASERLENGTH * 2,
          LaserBeam.LASERLENGTH * 2));

    // removes self
    items.remove(ship);

    // sees if there are any INCOMMING misiles and fires a
    // laser at them
    Iterator i = items.iterator();
    while (i.hasNext()) {
      SpaceItem si = (SpaceItem) i.next();
      if (si instanceof Misile) {
        Misile misile = (Misile) si;
        if (LaserBeam.inRange(ship, misile) && misile.getOwner() != ship) {
          ship.addRule(this);
          break;
        }
      }
    }
  }

  /* (non-Javadoc)
   * @see AIRule#execute()
   */
  public void execute() {
    ship.fireLaser();
  }
}
TOP

Related Classes of com.mlarktar.spacewar.rules.InterceptIncomming

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.