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 org.lwjgl.input.Keyboard;
025 import org.lwjgl.opengl.GL11;
026 import org.lwjgl.util.vector.Vector2f;
027 import org.xml.sax.ext.DeclHandler;
028
029 import rtype.EventManager;
030 import rtype.KeyListener;
031 import rtype.Layer;
032 import rtype.Prototyp;
033
034
035 public class PlayerShip extends AnimatedEntity
036 {
037 public float powerAccumulator = 0;
038 public Orb orb = null ;
039
040 public int hiscore=1;
041
042 private int fire1Key ;
043 private int leftKey ;
044 private int rightKey ;
045 private int fire2Key ;
046 private int upKey ;
047 private int downKey ;
048
049 float POWER_SPEED = 2f;
050 public float power = 0;
051 public static float MAX_POWER = 4f;
052 Blaster concentrateAnimation = new Blaster(this);
053
054 BitUpgrade booster1 = null;
055 BitUpgrade booster2 = null;
056
057 private final static float MAX_SPEED = 230;
058 private final static float ACCELLERATION = 1000;
059
060 private float DEFAULT_DECELERATION = 700;
061 private float x_deceleration = 0;
062 private float y_deceleration = 0;
063
064 private PlayerSpeed accelerateEntrity = new PlayerSpeed(this);
065
066 public PlayerShip()
067 {
068 this(Keyboard.KEY_UP,Keyboard.KEY_DOWN,Keyboard.KEY_LEFT,Keyboard.KEY_RIGHT,Keyboard.KEY_SPACE,Keyboard.KEY_X);
069 }
070
071 public PlayerShip(int up, int down,int left, int right, int fire1, int fire2)
072 {
073 upKey = up;
074 downKey = down;
075 leftKey = left;
076 rightKey = right;
077 fire1Key = fire1;
078 fire2Key = fire2;
079
080
081 concentrateAnimation.spawn(new Vector2f(0,0),new Vector2f(0,0),Prototyp.fx);
082 accelerateEntrity.spawn(new Vector2f(0,0),new Vector2f(0,0),Prototyp.fx);
083 this.type = PLAYER_SHIP;
084 init();
085 //setRatio(0.17f);
086 setRatio(0.25f);
087 //this.setOrb(new LightningOrb(this));
088 addEventListeners();
089
090 }
091
092
093
094 public void setOrb(Orb orb)
095 {
096 if (this.orb != null && this.orb.type == orb.type)
097 return;
098 if (this.orb != null )
099 this.orb.unSpawn();
100 this.orb = orb;
101 this.orb.spawn(new Vector2f(-430,0),new Vector2f(0,0),Prototyp.bullets);
102 }
103
104 public void addBooster()
105 {
106 addBooster(Prototyp.bullets);
107 }
108
109 public void addBooster(Layer layer)
110 {
111 if (booster1 == null)
112 {
113 booster1 = new BitUpgrade(this);
114 booster1.spawn(new Vector2f(5,20),new Vector2f(500,0),layer);
115 return;
116 }
117 if (booster2 == null)
118 {
119 booster2 = new BitUpgrade(this);
120 booster2.spawn(new Vector2f(5,-20),new Vector2f(500,0),layer);
121 return;
122 }
123 }
124
125 public void draw()
126 {
127
128
129 GL11.glLoadIdentity();
130 GL11.glPushMatrix();
131 GL11.glPopMatrix();
132
133 GL11.glTranslatef(position.x,position.y,Prototyp.DEFAULT_Z); // Translate Into/Out Of The Screen By z
134 int animationFrame = (int) ((speed.y*4)/MAX_SPEED);
135 animationFrame += 4;
136
137
138 GL11.glBindTexture(GL11.GL_TEXTURE_2D, this.animationTextures[animationFrame].getTextureId() );
139 //GL11.glBlendFunc(GL11.GL_SRC_ALPHA,GL11.GL_ONE_MINUS_SRC_ALPHA);
140 GL11.glColor4f(1,1,1,1f);
141 GL11.glBegin(GL11.GL_QUADS);
142 {
143 GL11.glTexCoord2f(textureRight,textureUp); //Upper right
144 GL11.glVertex2f(width, -height);
145
146 GL11.glTexCoord2f(textureLeft,textureUp); //Upper left
147 GL11.glVertex2f(-width, -height);
148
149 GL11.glTexCoord2f(textureLeft,textureDown); //Lower left
150 GL11.glVertex2f(-width,height);
151
152 GL11.glTexCoord2f(textureRight,textureDown); // Lower right
153 GL11.glVertex2f(width,height);
154 }
155 GL11.glEnd();
156
157
158 //accelerateEntrity .draw();
159 //concentrateAnimation.draw();
160
161 }
162
163
164
165 public boolean isAnimationStarted()
166 {
167 return this.displayAnimation;
168 }
169
170
171 public void addEventListeners() {
172
173 KeyListener fire2KeyEvent = new KeyListener()
174 {
175 public void onKeyDown(){};
176 public void keyPressed()
177 {
178 if (orb != null)
179 orb.setMove(Orb.ADJUDTING);
180 };
181 public void onKeyUp()
182 {
183 if (orb != null)
184 orb.setMove(Orb.STICKED);
185 };
186 };
187 EventManager.instance().addListener(fire2Key, fire2KeyEvent);
188
189
190
191 KeyListener upKeyEvent = new KeyListener()
192 {
193 public void keyPressed()
194 {
195 speed.y += ACCELLERATION * tick;
196 if (speed.y > MAX_SPEED)
197 speed.y = MAX_SPEED;
198 };
199
200 public void onKeyUp()
201 {
202 y_deceleration = -DEFAULT_DECELERATION;
203 };
204 };
205 EventManager.instance().addListener(upKey, upKeyEvent);
206
207 KeyListener downKeyEvent = new KeyListener()
208 {
209 public void onKeyDown()
210 {
211
212 };
213 public void keyPressed()
214 {
215 speed.y -= ACCELLERATION * tick;
216 if (speed.y < -MAX_SPEED)
217 speed.y = -MAX_SPEED;
218 };
219 public void onKeyUp()
220 {
221 y_deceleration = DEFAULT_DECELERATION;
222 };
223 };
224 EventManager.instance().addListener(downKey, downKeyEvent);
225
226
227 KeyListener leftKeyEvent = new KeyListener()
228 {
229 public void onKeyDown()
230 {
231
232 };
233 public void keyPressed()
234 {
235 speed.x -= ACCELLERATION * tick;
236 if (speed.x < -MAX_SPEED)
237 speed.x = -MAX_SPEED;
238 };
239 public void onKeyUp()
240 {
241 x_deceleration = DEFAULT_DECELERATION;
242 };
243 };
244 EventManager.instance().addListener(leftKey, leftKeyEvent);
245
246 KeyListener rightKeyEvent = new KeyListener()
247 {
248 public void onKeyDown()
249 {
250
251 };
252 public void keyPressed()
253 {
254 speed.x += ACCELLERATION * tick;
255 if (speed.x > MAX_SPEED)
256 speed.x = MAX_SPEED;
257 accelerateEntrity.startAnimation();
258 };
259 public void onKeyUp()
260 {
261 accelerateEntrity.stopAnimation();
262 x_deceleration = -DEFAULT_DECELERATION;
263 };
264 };
265 EventManager.instance().addListener(rightKey, rightKeyEvent);
266
267
268 KeyListener fire1KeyEvent = new KeyListener()
269 {
270 public void onKeyDown()
271 {
272 concentrateAnimation.fire(0.1f);
273 concentrateAnimation.startChargingAnimation();
274 if (orb != null)
275 orb.startChargingAnimation();
276
277
278 if (booster1 != null)
279 booster1.fire(0);
280
281 if (booster2 != null)
282 booster2.fire(0);
283 };
284 public void keyPressed()
285 {
286 power += POWER_SPEED * Prototyp.tick;
287 if (power > MAX_POWER)
288 power = MAX_POWER;
289 };
290 public void onKeyUp()
291 {
292 // Fire blast if power is high enought...
293 if (power > (MAX_POWER /10))
294 {
295 concentrateAnimation.fire(power/MAX_POWER);
296 if (orb != null)
297 orb.fire(power/MAX_POWER);
298
299 }
300 power = 0;
301 concentrateAnimation.stopChargingAnimation();
302
303 if (orb != null)
304 orb.stopChargingAnimation();
305 };
306 };
307 EventManager.instance().addListener(fire1Key, fire1KeyEvent);
308 }
309
310 @Override
311 public boolean collided(Entity entity)
312 {
313 return false;
314 }
315
316 @Override
317 protected Vector2f interpolate(Vector2f old_position,Vector2f speed)
318 {
319 old_position.x = old_position.x + tick * speed.x;
320
321 if (old_position.x > Prototyp.SCREEN_WIDTH /2)
322 old_position.x = Prototyp.SCREEN_WIDTH /2;
323 else
324 if (old_position.x < -Prototyp.SCREEN_WIDTH /2)
325 old_position.x = -Prototyp.SCREEN_WIDTH /2;
326
327
328 old_position.y = old_position.y + tick * speed.y;
329
330 if (old_position.y > Prototyp.SCREEN_HEIGHT /2)
331 old_position.y = Prototyp.SCREEN_HEIGHT /2;
332 else
333 if (old_position.y < -Prototyp.SCREEN_HEIGHT /2)
334 old_position.y = -Prototyp.SCREEN_HEIGHT /2;
335
336 return old_position;
337 }
338
339 @Override
340 public void update()
341 {
342 this.interpolate(position,speed);
343
344 float old_speed_x = speed.x;
345 speed.x += x_deceleration * tick;
346 if (old_speed_x * speed.x < 0)
347 {
348 x_deceleration = 0;
349 speed.x = 0;
350 }
351
352
353 float old_speed_y = speed.y;
354 speed.y += y_deceleration * tick;
355 if (old_speed_y * speed.y < 0)
356 {
357 y_deceleration = 0;
358 speed.y = 0;
359 }
360
361
362 }
363 }
|