MagneticOrb.java
001 /*
002  *
003  * Created: Jun  7 2006
004  *
005  * Copyright (C) 1999-2000 Fabien Sanglard
006  
007  * This program is free software; you can redistribute it and/or
008  * modify it under the terms of the GNU General Public License
009  * as published by the Free Software Foundation; either version 2
010  * of the License, or (at your option) any later version.
011  
012  * This program is distributed in the hope that it will be useful,
013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
015  * GNU General Public License for more details.
016  
017  * You should have received a copy of the GNU General Public License
018  * along with this program; if not, write to the Free Software
019  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
020  */
021 
022 package rtype.entity;
023 
024 import java.util.ArrayList;
025 
026 import rtype.Prototyp;
027 
028 public class MagneticOrb extends Orb
029 {
030   
031   
032   boolean attracting = false;
033   
034   public MagneticOrb(PlayerShip player)
035   {
036     this.playerShip = player;
037     this.type = RED_ORB;
038     init();
039     animationSpeed = 20f;
040     chargeAnimationSpeed = 0.9f;
041     setRatio(0.35f);
042     
043   }
044 
045 
046   @Override
047   public void fire(float chargePercentage)
048   {
049     attractionDuration = 10 * chargePercentage;
050     attracting = true;
051   }
052 
053   // Duration for attaction process
054   float attractionDuration = 10f;
055   float attractionTickCounter = 0;
056   float attractionStrengh = 300f;
057   
058   // Once attraction has started, the orb is not supposed to
059   // move anymore...
060   boolean startedAttraction = false;
061   @Override
062   public void update()
063   {
064     // TODO Auto-generated method stub
065     
066     if (attracting)
067     {
068       
069       if (attractionTickCounter > attractionDuration)
070       {
071         attractionTickCounter = 0;
072         attracting = false;
073         startedAttraction = false;
074       }
075       else
076       {
077         calculateDistanceFromShip();
078         if (startedAttraction || distanceFromShip > 250 )
079         {
080           startedAttraction = true;
081           ArrayList<Entity> enemies = Prototyp.enemies.entities;
082           Entity enemy = null;
083           float diffX =0;
084           float diffY =0;
085           float diffXcarre = 0;
086           float diffYcarre = 0;
087           float distanceFromOrb = 0;
088           attractionTickCounter += tick ;
089           for (int i = ; i < enemies.size() ; i++)
090           {
091             enemy = enemies.get(i);
092             
093             diffX = this.position.x - enemy.position.x ;
094             diffY = this.position.y - enemy.position.y ;
095             
096             diffXcarre = diffX * diffX;
097             if (diffXcarre > 40000)
098               continue;
099             
100             diffYcarre = diffY * diffY;
101             if (diffYcarre > 40000)
102               continue;
103             
104             distanceFromOrb = (float)Math.sqrt(diffXcarre  + diffYcarre);
105             
106             if (distanceFromOrb < 200)
107             {
108               diffX *= attractionStrengh * 1/distanceFromOrb * tick ;
109               diffY *= attractionStrengh * 1/distanceFromOrb * tick ;
110             
111               enemy.speed.x += diffX;
112               enemy.speed.y += diffY;
113             }
114           }
115         }
116         else
117         {
118           speed.x = -xDiffWithPlayerShip * 2;
119           speed.y = -yDiffWithPlayerShip * 2;
120           interpolate(position,speed);
121           updateOrbAngle();
122         }
123       }
124     }
125     else
126       super.update();
127   }  
128 }