home *** CD-ROM | disk | FTP | other *** search
/ AI Game Programming Wisdom / AIGameProgrammingWisdom.iso / SourceCode / 07 DecisionMaking Architectures / 01 Isla, Blumberg / Shield.java < prev    next >
Encoding:
Java Source  |  2001-09-25  |  1.9 KB  |  73 lines

  1. package bb;
  2.  
  3. import java.awt.*;
  4. import java.util.*;
  5. import javax.swing.*;
  6. import java.awt.geom.*;
  7.  
  8. /*
  9. The shield agent surrounds another agent, thereby absorbing most attacks.
  10.  
  11. @author naimad
  12. */
  13.  
  14. public class Shield extends Agent {
  15.  
  16.     class ShieldSkill extends Skill {
  17.     
  18.         public final static String SHIELD = "shield";
  19.     
  20.         public ShieldSkill(Agent a) {
  21.             super(SHIELD, a, 1.0);
  22.         }
  23.  
  24.         Vec2 shieldLocation = new Vec2();        
  25.         Vec2 direction = new Vec2();
  26.         public void apply(double time, Mission mission) {
  27.             if (mission==null) return;
  28.             ((LocatableMission)mission).getTargetPosition(shieldLocation);
  29.             
  30.             //move toward that location!
  31.             Vec2.sub(shieldLocation, agent.pos, direction);
  32.             agent.addForce(direction);
  33.         }
  34.     }
  35.  
  36.     public final static int COST = 100;
  37.     double radius = 9.0;
  38.  
  39.     public Shield(int team, Vec2 pos, Blackboard bb, World world) {
  40.         super(team, 0, pos, 3.0, 100.0, 50.0, bb, world);
  41.  
  42.         Skill protect = new ShieldSkill(this);
  43.         skills.put(protect.getName(), protect);
  44.  
  45.         defaultSkill = protect;
  46.         
  47.         mass = 20.0;
  48.     }
  49.  
  50.     public void getPosition(Agent askingAgent, Vec2 inplace) {
  51.         Vec2.sub(askingAgent.pos, this.pos, inplace);
  52.         double dist = inplace.mag();
  53.         dist -=  radius;
  54.         inplace.normalize();
  55.         inplace.scale(dist);
  56.         Vec2.add(askingAgent.pos, inplace, inplace);
  57.     }
  58.     
  59.     public void updatePhysics(double time) {
  60.         externalForce.scale(0.0);
  61.         super.updatePhysics(time);
  62.     }
  63.     
  64.     public Shape createShape() {
  65.         radius = 16.0;
  66.         return new Ellipse2D.Double(-radius, -radius, 2*radius, 2*radius);
  67.     }
  68.     
  69.     public void drawShape(double time, Graphics2D g2) {
  70.         g2.draw(drawShape);
  71.     }    
  72. }
  73.