-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEnemy.java
301 lines (237 loc) · 10.2 KB
/
Enemy.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
package mkat.apps.spacewars;
import org.andengine.engine.handler.IUpdateHandler;
import org.andengine.engine.handler.timer.ITimerCallback;
import org.andengine.engine.handler.timer.TimerHandler;
import org.andengine.entity.modifier.MoveModifier;
import org.andengine.entity.modifier.IEntityModifier.IEntityModifierListener;
import org.andengine.entity.primitive.Line;
import org.andengine.entity.sprite.AnimatedSprite;
import org.andengine.entity.sprite.Sprite;
import org.andengine.entity.sprite.AnimatedSprite.IAnimationListener;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.opengl.vbo.VertexBufferObjectManager;
import org.andengine.util.math.MathUtils;
import org.andengine.util.modifier.LoopModifier;
import android.util.Log;
public class Enemy extends Sprite{
MoveModifier moveEnemy1;
IEntityModifierListener entityModifierListener;
/*Define Move Modifiers*/
int counter, eLife = 0;
float duration = GameLevel.getInstance().duration;
float distance;
float fromX;
float toX;
float fromY;
float toY;
float rotateX;
float rotateY;
float directionX;
float directionY;
float rotationAngle;
AnimatedSprite animatedSprite;
Enemy sprite = null;
Line direction = null;
Sprite bullet = null;
boolean recycleBullet = true;
boolean explodeAnimation = false;
boolean bulletLifeEnd, readyToFireEnemyLaser = false;
//boolean beenHit = false;
EnemyLaserPool bulletPool;
TimerHandler enemyBulletTimerHandler;
//Body enemyPhysics;
//final FixtureDef enemyFixtureDef = PhysicsFactory.createFixtureDef(1f, 0.5f, 0.9f);
//public FixedStepPhysicsWorld mPhysicsWorld;
// These variables are needed for the objects allocated by the pool
ITextureRegion mTextureRegion;
VertexBufferObjectManager mVertexBufferObjectManager;
public Enemy(ITextureRegion pTextureRegion) {
super((float)(0 + (int)(Math.random() * ((1024 - 0) + 1))),(float)(0 + (int)(Math.random() * ((640 - 0) + 1))),pTextureRegion, ResourceManager.getInstance().engine.getVertexBufferObjectManager());
//super(200,200, pTextureRegion,ResourceManager.getInstance().engine.getVertexBufferObjectManager());
sprite = this;
//enemyPhysics = PhysicsFactory.createBoxBody(GameLevel.getInstance().mPhysicsWorld, sprite, BodyType.StaticBody, enemyFixtureDef);;
fromX = sprite.getX();
fromY = sprite.getY();
//Handle rotation of every sprite
//sprite.setRotation(MathUtils.radToDeg(rotationAngle));
setUpEnemy();
//sprite = new
// TODO Auto-generated constructor stub
}
public TimerHandler getTimerHandler(){
return enemyBulletTimerHandler;
}
private void setUpEnemy() {
//beenHit = false;
sprite.setVisible(true);
sprite.setIgnoreUpdate(false);
sprite.setColor(1, 1, 1);
//GameLevel.getInstance().attachChild(sprite);
bulletPool = new EnemyLaserPool(1);
//PLAYER LASER SETUP
//Create new laser
Log.v("","calls for a enemy laser");
if (GameLevel.getInstance().waveCounter-1 == 0) {
eLife = 1;
} else if (GameLevel.getInstance().waveCounter-1 == 1){
eLife = 2;
} else if (GameLevel.getInstance().waveCounter-1 == 2) {
eLife = 3;
} else if (GameLevel.getInstance().waveCounter-1 == 3) {
eLife = 4;
} else if (GameLevel.getInstance().waveCounter-1 == 4) {
eLife = 5;
}
Log.v("Sprite1", sprite.getX() + " " + sprite.getY() );
moveEnemy1 = new MoveModifier(duration, fromX, fromY, toX, toY);
sprite.setCullingEnabled(true);
sprite.registerEntityModifier(moveEnemy1);
//set up compass pointer
//add the pointer to the scene where the direction line intersects the compass, and update it at that position.
direction = new Line(sprite.getX(), sprite.getY(), sprite.getX()+1, sprite.getY()+1, ResourceManager.getInstance().engine.getVertexBufferObjectManager());
direction.setAlpha(1f);
//GameLevel.getInstance().attachChild(direction);
//GameLevel.getInstance().attachChild(pointer);
//SETUP ENEMY LASER
ResourceManager.getInstance().engine.registerUpdateHandler(enemyBulletTimerHandler = new TimerHandler(GameLevel.getInstance().fireRateEnemyStart, new ITimerCallback()
{
@Override
public void onTimePassed(TimerHandler pTimerHandler) {
// TODO Auto-generated method stub
enemyBulletTimerHandler.setTimerSeconds(GameLevel.getInstance().fireRateEnemy);
enemyBulletTimerHandler.reset();
//Log.v("", "time runs every second");
if (readyToFireEnemyLaser == true && GameLevel.getInstance().readyToFireEnemyLaser == true) {
//Log.v(""," Enemy recycledOrNot? " + recycleBullet);
if (recycleBullet == true){
if (bulletPool.getAvailableItemCount()>0){
bullet = bulletPool.obtainPoolItem(sprite.getX(), sprite.getY(),sprite.getRotation(), sprite);
//Log.v(""," After get a new bullet on scene " + recycleBullet);
//GameLevel.getInstance().EnemyLaser = bullet;
bulletLifeEnd = false;
explodeAnimation = false;
recycleBullet = false;
}
} else {
bulletPool.recyclePoolItem((EnemyLaser)bullet);
ResourceManager.getInstance().engine.runOnUpdateThread(new Runnable() {
@Override
public void run() {
if (bullet!= null){
bullet.detachSelf();
bulletLifeEnd = true;
//Log.v("","Removes bullet from scene");
recycleBullet = true;
//Log.v(""," After detach bullet from Scene? " + recycleBullet);
}
}
});
}
}
}
}));
//END SETUP ENEMY LASER
GameLevel.getInstance().registerUpdateHandler(new IUpdateHandler() {
@Override
public void onUpdate(float pSecondsElapsed) {
//Pointer for Compass
if (GameLevel.getInstance().PlayerReady){
//direction.
//direction.setPosition(GameLevel.getInstance().mPlayer);
//line 1 is direction
//line 2 is is the player compass
direction.setPosition(sprite.getX(), sprite.getY(), GameLevel.getInstance().mPlayer.getX(), GameLevel.getInstance().mPlayer.getY());
//direction.setPosition(sprite.getX(), sprite.getY(), GameLevel.getInstance().mPlayer.compass.getX(), GameLevel.getInstance().mPlayer.compass.getY());
//if (direction.collidesWith(GameLevel.getInstance().mPlayer.compass)){
//pointer.unregisterEntityModifier(mm);
//pointer.detachSelf();
//mm = new MoveModifier(1, GameLevel.getInstance().mPlayer.getX(), GameLevel.getInstance().mPlayer.getY(), sprite.getX(),sprite.getY());
//pointer.registerEntityModifier(mm);
//}
}
//End Pointer for Compass
if (GameLevel.getInstance().PlayerReady && bullet != null ){
if (bullet.collidesWith(GameLevel.getInstance().mPlayer)&& explodeAnimation == false && bulletLifeEnd == false){
explodeAnimation = true;
bulletPool.recyclePoolItem((EnemyLaser)bullet);
ResourceManager.getInstance().engine.runOnUpdateThread(new Runnable() {
@Override
public void run() {
if (bullet!= null){
bullet.detachSelf();
GameLevel.getInstance().playerHit = true;
bulletLifeEnd = true;
//Log.v("","Removes bullet from scene");
recycleBullet = true;
//Log.v(""," After detach bullet from Scene? " + recycleBullet);
}
}
});
//ANIMATED SPRITE BEGINS
//if (bulletPool.getUnrecycledItemCount()>0){
// bulletPool.recyclePoolItem((EnemyLaser)bullet);
// ResourceManager.getInstance().engine.runOnUpdateThread(new Runnable() {
// @Override
// public void run() {
// if (bullet!= null){
// bullet.detachSelf();
// //Log.v("","Removes bullet from scene");
// recycleBullet = true;
// //Log.v(""," After detach bullet from Scene? " + recycleBullet);
// }
// }
// });
//}
//Create a new animated sprite in the center of the scene
animatedSprite = new AnimatedSprite(GameLevel.getInstance().mPlayer.getX(), GameLevel.getInstance().mPlayer.getY(), ResourceManager.getInstance().mTiledTextureRegionExplosion, ResourceManager.getInstance().engine.getVertexBufferObjectManager());
//Length to play each frame before moving on to the next
long frameDuration[] = {75,75,75,75,75,75,75,75,75,75,75,75,75,75,75,75,75,75,75,75,75,75,75,75};
// we can define the indices of the animation to play between
int firstTileIndex = 0;
int lastTileIndex = ResourceManager.getInstance().mTiledTextureRegionExplosion.getTileCount()-1;
//Log.i("MODIFIER", Integer.toString(lastTileIndex));
boolean loopAnimation = false;
//GameLevel.getInstance().mPlayer.clearEntityModifiers();
//GameLevel.getInstance().mPlayer.clearUpdateHandlers();
//GameLevel.getInstance().mPlayer.detachSelf();
//GameLevel.getInstance().mPlayer = null;
//Animate the sprite with the data as set defined above
animatedSprite.animate(frameDuration, firstTileIndex, lastTileIndex, loopAnimation, new IAnimationListener() {
@Override
public void onAnimationStarted(AnimatedSprite pAnimatedSprite, int pInitialLoopCount){
//Fired when the animation first begins to run
//counter++;
}
@Override
public void onAnimationFrameChanged(AnimatedSprite pAniamtedSprite, int pOldFrameIndex, int pNewIndex) {
// Fired every time a new frame is selected to display
}
@Override
public void onAnimationLoopFinished(AnimatedSprite pAnimatedSprite, int pRemainingLoopCount, int pInitialLoopCount){
// Fired when an animation loop ends (from first to last frame
ResourceManager.getInstance().engine.runOnUpdateThread(new Runnable() {
@Override
public void run() {
GameLevel.getInstance().detachChild(animatedSprite);
}
});
Log.v("","Gets past sprite animation");
}
@Override
public void onAnimationFinished(AnimatedSprite pAnimatedSprite)
{
//SceneManager.getInstance().showMainMenu();
}
//ANIMATED SPRITE ENDS
});
GameLevel.getInstance().attachChild(animatedSprite);
}
}
}
@Override
public void reset() {
// TODO Auto-generated method stub
}
});
}
}