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 CrystalOrb extends Orb
029 {
030
031
032 boolean freezing = false;
033
034 public CrystalOrb(PlayerShip player)
035 {
036 this.playerShip = player;
037 this.type = GREEN_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
050 freezing = true;
051 freezingRange = DEFAULT_FREEZING_RANGE * chargePercentage ;
052
053 }
054
055 // Duration for attaction process
056 float freezingDuration = 2;
057 float freezeTickCounter = 0;
058 private static final float DEFAULT_FREEZING_RANGE = 250;
059 private float freezingRange = 0;
060
061 // Once attraction has started, the orb is not supposed to
062 // move anymore...
063 boolean startedFreezeProcess = false;
064 private ArrayList<Entity> enemiesToFreeze = null;
065 @Override
066 public void update()
067 {
068 // TODO Auto-generated method stub
069
070 if (freezing)
071 {
072
073 if (freezeTickCounter > freezingDuration)
074 {
075 freezeTickCounter = 0;
076 freezing = false;
077 startedFreezeProcess = false;
078 enemiesToFreeze = null;
079 }
080 else
081 {
082 calculateDistanceFromShip();
083 if (startedFreezeProcess || distanceFromShip > 250 )
084 {
085 startedFreezeProcess = true;
086 if (enemiesToFreeze == null)
087 buildListOfEnemiesToFreeze();
088
089 freezeTickCounter += tick ;
090 for (int i = 0 ; i < enemiesToFreeze.size() ; i++)
091 {
092 enemiesToFreeze.get(i).freeze(0.3f,0.6f);
093 }
094 }
095 else
096 {
097 speed.x = -xDiffWithPlayerShip * 2;
098 speed.y = -yDiffWithPlayerShip * 2;
099 interpolate(position,speed);
100 updateOrbAngle();
101 }
102 }
103 }
104 else
105 super.update();
106 }
107
108
109 private void buildListOfEnemiesToFreeze()
110 {
111 // Used freezingRange to populate
112 enemiesToFreeze = new ArrayList();
113 Entity enemy = null;
114 float diffX =0;
115 float diffY =0;
116
117 ArrayList<Entity> enemies = Prototyp.enemies.entities;
118 for (int i = 0 ; i < enemies.size() ; i++)
119 {
120
121 enemy = enemies.get(i);
122
123 diffX = this.position.x - enemy.position.x ;
124 diffY = this.position.y - enemy.position.y ;
125
126 enemy.distanceFromOrb = (float)Math.sqrt(diffX * diffX + diffY * diffY);
127
128 if (enemy.distanceFromOrb < freezingRange)
129 {
130 insertInDescDistanceFromOrb(enemy);
131 }
132
133 }
134
135 }
136
137 private boolean toInserted = false;
138 private void insertInDescDistanceFromOrb(Entity enemy)
139 {
140 toInserted = false;
141 int i = 0;
142 while (i < enemiesToFreeze.size() && !toInserted)
143 {
144 if (enemiesToFreeze.get(i).distanceFromOrb > enemy.distanceFromOrb)
145 toInserted = true;
146 else
147 i++;
148 }
149 enemiesToFreeze.add(i,enemy);
150 }
151 }
|