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.Random;
025
026 import org.lwjgl.opengl.GL11;
027 import org.lwjgl.util.vector.Vector2f;
028
029 import rtype.Layer;
030 import rtype.Prototyp;
031 import rtype.Texture;
032
033 public abstract class Entity implements IEntity
034 {
035 // This is used by the crystal Orb
036 public float distanceFromOrb = 0;
037
038 public int original_width;
039 public int original_height;
040
041 private float ratio = 1f;
042 public float width ;
043 public float height ;
044
045 public float rotation = 0;
046 protected float rotationSpeed = 0;
047
048 protected float damage = 1;
049 protected float life = 1;
050 protected Explosion ex = null;
051
052 public void setRatio(float newRatio)
053 {
054 this.ratio = newRatio;
055 this.width = this.original_width * ratio;
056 this.height = this.original_height * ratio;
057 }
058
059 public Vector2f speed = new Vector2f();
060 public Vector2f position = new Vector2f();
061
062
063 protected Texture texture;
064 public int type;
065
066
067 protected float textureUp = 1;
068 protected float textureDown = 0;
069 protected float textureLeft = 0;
070 protected float textureRight = 1;
071
072 protected Layer layer = null;
073
074 protected void init()
075 {
076 this.texture = Prototyp.textureLoader.getTexture(this.type);
077 this.original_width = this.texture.getTextureWidth();
078 this.original_height = this.texture.getTextureHeight();
079 this.width = this.original_width * ratio;
080 this.height = this.original_height * ratio;
081
082 }
083
084 public void spawn(Vector2f position,Vector2f speed,Layer layer )
085 {
086 this.position.x = position.x;
087 this.position.y = position.y;
088
089 this.speed.y = speed.y;
090 this.speed.x = speed.x;
091
092 this.layer = layer;
093 this.layer.add(this);
094 }
095
096 public void spawn(Vector2f position,Vector2f speed, float rotationSpeed,Layer layer )
097 {
098 spawn(position,speed,layer);
099 this.rotationSpeed = rotationSpeed;
100 }
101
102 public void unSpawn()
103 {
104 layer.remove(this);
105 }
106
107 public void draw()
108 {
109 GL11.glLoadIdentity();
110 GL11.glTranslatef(position.x,position.y,Prototyp.DEFAULT_Z); // Translate Into/Out Of The Screen By z
111 GL11.glRotatef(this.rotation,0f,0f,1f);
112
113 GL11.glBindTexture(GL11.GL_TEXTURE_2D, this.texture.getTextureId() );
114
115 GL11.glBegin(GL11.GL_QUADS);
116 {
117 GL11.glTexCoord2f(textureRight,textureUp); //Upper right
118 GL11.glVertex2f(width, -height);
119
120 GL11.glTexCoord2f(textureLeft,textureUp); //Upper left
121 GL11.glVertex2f(-width, -height);
122
123 GL11.glTexCoord2f(textureLeft,textureDown); //Lower left
124 GL11.glVertex2f(-width,height);
125
126 GL11.glTexCoord2f(textureRight,textureDown); // Lower right
127 GL11.glVertex2f(width,height);
128 }
129 GL11.glEnd();
130 }
131
132
133
134 protected Vector2f interpolate(Vector2f old_position,Vector2f speed)
135 {
136 old_position.x = old_position.x + tick * speed.x;
137 old_position.y = old_position.y + tick * speed.y;
138
139 return old_position;
140 }
141
142 static public void dumpEntity(Entity ent)
143 {
144
145 Logger.log("--------------------------");
146 Logger.log("px"+ent.position.x);
147 Logger.log("py"+ent.position.y);
148 Logger.log("dx"+ent.speed.x);
149 Logger.log("dy"+ent.speed.y);
150 Logger.log("width"+ent.original_width);
151 Logger.log("height"+ent.original_height);
152 Logger.log("--------------------------");
153
154 }
155
156 public void flipYAxis()
157 {
158 float tmp = textureLeft;
159 this.textureLeft = textureRight;
160 textureRight = tmp;
161 }
162
163 public void flipXAxis()
164 {
165 float tmp = textureUp;
166 this.textureUp = textureDown;
167 textureDown = tmp;
168 }
169
170 public void update()
171 {
172 interpolate(position,speed);
173 this.rotation += rotationSpeed * tick ;
174 if (this.position.x - this.width > (Prototyp.SCREEN_WIDTH / 2) || this.position.x + this.width < - (Prototyp.SCREEN_WIDTH / 2))
175 {
176 unSpawn();
177 if (Logger.isLogActivate) Logger.log(this.getClass().getName()+" died");
178 return;
179 }
180 }
181
182
183 private static EnemyPiece ep1 = null;
184 private static EnemyPiece ep2 = null;
185 private static EnemyPiece ep3 = null;
186 private static EnemyPiece ep4 = null;
187 public static Vector2f speedNull = new Vector2f(0,0);
188 private static Vector2f speedUp = new Vector2f(30,20);
189 private static Vector2f speedUp2 = new Vector2f(60,35);
190 private static Vector2f speedDown = new Vector2f(30,-23);
191 private static Vector2f speedDown2 = new Vector2f(60,-24);
192
193 private static final int ROTATION_SPEED = 260;
194
195 // Return true is the current Entity is destroyed.
196 public boolean collided(Entity entity)
197 {
198 if (entity == this)
199 return false;
200
201 this.life -= entity.damage;
202 if (life < 0)
203 {
204 this.unSpawn();
205
206 ex = new Explosion(Prototyp.random.nextInt(2)+IEntity.EXPLOSION1);
207 ex.spawn(this.position,this.speed,Prototyp.random.nextInt(ROTATION_SPEED),Prototyp.frontground);
208
209 ep1 = new EnemyPiece(Prototyp.random.nextInt(8)+IEntity.ENEMY_PIECE_1);
210 ep1.spawn(this.position,speedUp2,Prototyp.random.nextInt(ROTATION_SPEED),Prototyp.frontground);
211
212 ep2 = new EnemyPiece(Prototyp.random.nextInt(8)+IEntity.ENEMY_PIECE_1);
213 ep2.spawn(this.position,speedDown,Prototyp.random.nextInt(ROTATION_SPEED),Prototyp.frontground);
214
215 ep3 = new EnemyPiece(Prototyp.random.nextInt(8)+IEntity.ENEMY_PIECE_1);
216 ep3.spawn(this.position,new Vector2f(entity.speed.x,this.speed.y),Prototyp.random.nextInt(ROTATION_SPEED),Prototyp.frontground);
217
218 ep4 = new EnemyPiece(Prototyp.random.nextInt(8)+IEntity.ENEMY_PIECE_1);
219 ep4 .spawn(this.position,new Vector2f(entity.speed.x,-this.speed.y),Prototyp.random.nextInt(ROTATION_SPEED),Prototyp.frontground);
220
221 return true;
222 }
223 return false;
224 }
225
226 protected float tick;
227 private float frozenTickCounter=0;
228 private float freezeDuration = 0;
229 private float freezeSpeed = 0;
230 private float freezingPercentage = 0;
231
232 // This part has been added
233 public void updateTick()
234 {
235 if (freezing)
236 {
237
238 if (frozen)
239 {
240 // You may wonder why I multiply by 10.
241 // It is because on very "fast" computer, fps
242 // may be so high and tick so small that
243 // frozenTickCounter get no updated and
244 // unfreeze never happen
245 frozenTickCounter += tick * 10;
246
247 if (frozenTickCounter > freezeDuration)
248 {
249 frozen = false;
250 frozenTickCounter = 0;
251 // Have to start to defroze
252 freezeSpeed = -freezeSpeed;
253 }
254 else
255 {
256 tick = 0;
257 }
258 }
259 else
260 {
261 // Update tick according to freeze Speed
262 // Note this can be the froze or defroze process...
263 freezingPercentage += Prototyp.tick * freezeSpeed;
264 tick = Prototyp.tick - freezingPercentage * Prototyp.tick;
265 if (tick < 0 )
266 {
267 this.frozen = true;
268 this.unSpawn();
269 this.spawn(this.position,this.speed,Prototyp.bullets);
270 //this.life = 50;
271
272 }
273 if (tick > Prototyp.tick)
274 {
275 freezingPercentage = 0;
276 this.freezing = false;
277
278 }
279 }
280 }
281 else
282 tick = Prototyp.tick;
283
284 }
285
286 protected boolean frozen = false;
287 protected boolean freezing = false;
288
289 public void freeze(float freezeDuration, float freezeSpeed)
290 {
291 this.freezeDuration = freezeDuration;
292 this.freezeSpeed = freezeSpeed;
293 this.freezing = true ;
294 }
295
296 public Layer getLayer() {
297 return layer;
298 }
299
300 public void setLayer(Layer layer) {
301 this.layer = layer;
302 }
303
304 }
|