Skip to content

Commit f93585b

Browse files
author
Paul Ruiz
committed
Added an Asteroids-esque Android TV game
1 parent fff0599 commit f93585b

File tree

12 files changed

+308
-1022
lines changed

12 files changed

+308
-1022
lines changed

AndroidTVAsteroidBelt/.gitignore

100644100755
File mode changed.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.ptrprograms.asteroidbelttv;
2+
3+
import android.app.Application;
4+
import android.test.ApplicationTestCase;
5+
6+
/**
7+
* <a href="http://d.android.com/tools/testing/testing_android.html">Testing Fundamentals</a>
8+
*/
9+
public class ApplicationTest extends ApplicationTestCase<Application> {
10+
public ApplicationTest() {
11+
super(Application.class);
12+
}
13+
}

AndroidTVAsteroidBelt/app/src/main/java/com/ptrprograms/asteroidbelttv/GameView.java

Lines changed: 60 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,19 @@
66
import android.opengl.GLSurfaceView;
77
import android.hardware.input.InputManager.InputDeviceListener;
88
import android.opengl.Matrix;
9-
import android.util.Log;
109
import android.view.KeyEvent;
1110
import android.view.MotionEvent;
1211

1312
import com.ptrprograms.asteroidbelttv.Objects.Asteroid;
13+
import com.ptrprograms.asteroidbelttv.Objects.Bullet;
1414
import com.ptrprograms.asteroidbelttv.Objects.Ship;
15-
import com.ptrprograms.asteroidbelttv.Particles.BaseParticle;
16-
import com.ptrprograms.asteroidbelttv.Particles.ParticleSystem;
1715
import com.ptrprograms.asteroidbelttv.Utils.Constants;
1816
import com.ptrprograms.asteroidbelttv.Utils.ShapeBuffer;
1917
import com.ptrprograms.asteroidbelttv.Utils.Utils;
2018

2119
import java.util.ArrayList;
2220
import java.util.List;
21+
import java.util.ListIterator;
2322

2423
import javax.microedition.khronos.egl.EGLConfig;
2524
import javax.microedition.khronos.opengles.GL10;
@@ -40,9 +39,8 @@ public class GameView extends GLSurfaceView implements GLSurfaceView.Renderer, I
4039
private int mWindowWidth;
4140
private int mWindowHeight;
4241
private List<Asteroid> mAsteroids;
43-
private static final int MAX_BULLET_PARTICLES = 100;
44-
45-
private static ParticleSystem mShots;
42+
public List<Bullet> mBullets;
43+
private int mLevel = 1;
4644

4745
private final float[] mMVPMatrix = new float[16];
4846

@@ -57,42 +55,78 @@ public GameView(Context context) {
5755

5856
mLastUpdateTimeMillis = System.currentTimeMillis();
5957

60-
mShip = new Ship( this, Utils.Color.WHITE );
58+
mShip = new Ship();
6159

6260
InputManager inputManager = (InputManager) context.getSystemService( Context.INPUT_SERVICE );
6361
inputManager.registerInputDeviceListener( this, null );
6462
mAsteroids = new ArrayList<Asteroid>();
65-
66-
mShots = new ParticleSystem(MAX_BULLET_PARTICLES, true);
67-
63+
mBullets = new ArrayList<Bullet>();
6864
initLevel();
69-
7065
}
7166

7267
public static GameView getInstance() {
7368
return mInstance;
7469
}
7570

7671
private void initLevel() {
77-
for( int i = 0; i < 1; i++ ) {
78-
mAsteroids.add(new Asteroid(Utils.Color.RED, Constants.ASTEROID_SIZE_LARGE));
79-
mAsteroids.add( new Asteroid(Utils.Color.RED, Constants.ASTEROID_SIZE_MEDIUM) );
80-
mAsteroids.add( new Asteroid(Utils.Color.RED, Constants.ASTEROID_SIZE_SMALL) );
72+
mShip.reset();
73+
if( mBullets != null ) {
74+
ListIterator<Bullet> iter = mBullets.listIterator();
75+
while( iter.hasNext() ) {
76+
iter.next();
77+
iter.remove();
78+
}
79+
}
80+
for( int i = 0; i < mLevel + 2; i++ ) {
81+
mAsteroids.add( new Asteroid(Utils.Color.RED, Constants.ASTEROID_SIZE_LARGE));
8182
}
8283
}
8384

8485
private void update( float delta ) {
86+
if( mAsteroids.isEmpty() ) {
87+
mLevel++;
88+
initLevel();
89+
return;
90+
}
91+
8592
mShip.update( delta );
86-
BaseParticle bullet = null;
8793
for( Asteroid asteroid : mAsteroids ) {
8894
asteroid.update( delta );
89-
bullet = mShots.checkForCollision( asteroid.mPositionX, asteroid.mPositionY, Constants.ASTEROID_SIZE_LARGE );
90-
if( bullet != null ) {
91-
Log.e("ASTEROIDS", "bullet hit something" );
92-
bullet = null;
95+
}
96+
97+
ListIterator<Bullet> bulletIter = mBullets.listIterator();
98+
ListIterator<Asteroid> asteroidIter = mAsteroids.listIterator();
99+
Bullet bullet;
100+
Asteroid asteroid;
101+
102+
while( bulletIter.hasNext() ) {
103+
bullet = bulletIter.next();
104+
bullet.update( delta );
105+
if( bullet.mLifeTimer == 0 ) {
106+
bulletIter.remove();
107+
}
108+
}
109+
110+
while( asteroidIter.hasNext() ) {
111+
asteroid = asteroidIter.next();
112+
bulletIter = mBullets.listIterator();
113+
while( bulletIter.hasNext() ) {
114+
bullet = bulletIter.next();
115+
if ((bullet.mPositionX > asteroid.mPositionX && bullet.mPositionX < (asteroid.mPositionX + (2 * asteroid.mAsteroidSize))
116+
&& (bullet.mPositionY > asteroid.mPositionY && bullet.mPositionY < (asteroid.mPositionY + (2 * asteroid.mAsteroidSize))))) {
117+
bulletIter.remove();
118+
asteroidIter.remove();
119+
if (asteroid.mAsteroidSize == Constants.ASTEROID_SIZE_LARGE) {
120+
asteroidIter.add(new Asteroid(Utils.Color.RED, Constants.ASTEROID_SIZE_MEDIUM, asteroid.mPositionX, asteroid.mPositionY));
121+
asteroidIter.add(new Asteroid(Utils.Color.RED, Constants.ASTEROID_SIZE_MEDIUM, asteroid.mPositionX, asteroid.mPositionY));
122+
} else if (asteroid.mAsteroidSize == Constants.ASTEROID_SIZE_MEDIUM) {
123+
asteroidIter.add(new Asteroid(Utils.Color.RED, Constants.ASTEROID_SIZE_SMALL, asteroid.mPositionX, asteroid.mPositionY));
124+
asteroidIter.add(new Asteroid(Utils.Color.RED, Constants.ASTEROID_SIZE_SMALL, asteroid.mPositionX, asteroid.mPositionY));
125+
}
126+
break;
127+
}
93128
}
94129
}
95-
mShots.update( delta );
96130
}
97131

98132
@Override
@@ -161,25 +195,21 @@ public void onSurfaceChanged(GL10 unused, int width, int height) {
161195
}
162196

163197
public void draw() {
164-
// Each world element adds triangles to the shape buffer. No OpenGl calls are made
165-
// until after the whole scene has been added to the shape buffer.
166198
mShapeBuffer.clear();
167-
if (mShip.isActive()) {
168-
mShip.draw(mShapeBuffer);
169-
}
199+
mShip.draw(mShapeBuffer);
170200
for( Asteroid asteroid : mAsteroids ) {
171201
asteroid.draw( mShapeBuffer );
172202
}
173-
mShots.draw( mShapeBuffer );
174-
// Prepare for rendering to the screen.
203+
for( Bullet bullet : mBullets ) {
204+
bullet.draw( mShapeBuffer );
205+
}
206+
175207
updateViewportAndProjection();
176208

177-
// Send the triangles to OpenGl.
178209
mShapeBuffer.draw( mMVPMatrix );
179210
}
180211

181212
private void updateViewportAndProjection() {
182-
// Assume a square viewport if the width and height haven't been initialized.
183213
float viewportAspectRatio = 1.0f;
184214
if ((mWindowWidth > 0) && (mWindowHeight > 0)) {
185215
viewportAspectRatio = (float) mWindowWidth / (float) mWindowHeight;
@@ -237,8 +267,4 @@ public boolean handleKeyEvent(KeyEvent keyEvent) {
237267
}
238268
return false;
239269
}
240-
241-
public static ParticleSystem getShots() {
242-
return mShots;
243-
}
244270
}

AndroidTVAsteroidBelt/app/src/main/java/com/ptrprograms/asteroidbelttv/Objects/Asteroid.java

Lines changed: 49 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
*/
1010
public class Asteroid {
1111

12-
private float mAsteroidSize;
12+
public float mAsteroidSize;
1313

1414
private Utils.Color mColor = new Utils.Color();
1515

@@ -20,14 +20,45 @@ public class Asteroid {
2020
public float mPositionY;
2121

2222
public Asteroid( Utils.Color color, float asteroidSize ) {
23-
this.mColor.set( color );
23+
mColor.set( color );
2424

25-
mVelocityX = (float) Math.random() * mVelocityMultiplier;
26-
mVelocityY = (float) Math.random() * mVelocityMultiplier;
25+
setRandomStartingPosition();
26+
setRandomVelocities();
2727

28+
mAsteroidSize = asteroidSize;
29+
}
30+
31+
public Asteroid( Utils.Color color, float asteroidSize, float positionX, float positionY ) {
32+
mPositionX = positionX;
33+
mPositionY = positionY;
34+
35+
setRandomVelocities();
36+
37+
mAsteroidSize = asteroidSize;
38+
39+
mColor.set( color );
40+
}
41+
42+
public void setRandomVelocities() {
43+
setVelocityX( (float) Math.random() );
44+
setVelocityY( (float) Math.random() );
45+
}
46+
47+
public void setRandomStartingPosition() {
2848
mPositionX = (float) Math.random() * Constants.MAP_RIGHT_COORDINATE;
49+
if( Math.random() < 0.5 )
50+
mPositionX = -mPositionX;
2951
mPositionY = (float) Math.random() * Constants.MAP_TOP_COORDINATE;
30-
mAsteroidSize = asteroidSize;
52+
if( Math.random() < 0.5 )
53+
mPositionY = -mPositionY;
54+
}
55+
56+
public void setVelocityX( float vel ) {
57+
mVelocityX = vel * mVelocityMultiplier;
58+
}
59+
60+
public void setVelocityY( float vel ) {
61+
mVelocityY = vel * mVelocityMultiplier;
3162
}
3263

3364
public void draw( ShapeBuffer sb ) {
@@ -40,40 +71,36 @@ private float[] getAsteroidVerticies() {
4071
vertices[1] = 0;
4172
vertices[2] = 0;
4273

43-
for(int i =1; i <361; i++){
44-
vertices[(i * 3)+ 0] = (float) (0.5 * Math.cos((3.14/180) * (float)i ) + vertices[0]);
45-
vertices[(i * 3)+ 1] = (float) (0.5 * Math.sin((3.14/180) * (float)i ) + vertices[1]);
74+
for(int i = 1; i < 361; i++){
75+
vertices[(i * 3)] = (float) ( Math.cos((3.14/180) * (float)i ) + vertices[0]);
76+
vertices[(i * 3)+ 1] = (float) ( Math.sin((3.14/180) * (float)i ) + vertices[1]);
4677
vertices[(i * 3)+ 2] = 0;
4778
}
4879

4980
return vertices;
5081
}
5182

5283
public void update( float delta ) {
53-
updateAsteroidPosition( delta );
54-
}
55-
56-
public void updateAsteroidPosition( float delta ) {
5784
setPositionX( mPositionX + ( mVelocityX * delta ) );
5885
setPositionY( mPositionY + ( mVelocityY * delta ) );
5986
}
6087

61-
public void setPositionX( float positionX ) {
62-
if( Constants.MAP_LEFT_COORDINATE - mAsteroidSize < positionX && positionX < Constants.MAP_RIGHT_COORDINATE + mAsteroidSize ) {
63-
mPositionX = positionX;
64-
} else if( positionX > Constants.MAP_RIGHT_COORDINATE ) {
88+
public void setPositionX( float position ) {
89+
if( Utils.isInXPlane( position, mAsteroidSize ) ) {
90+
mPositionX = position;
91+
} else if( Utils.isOffScreenToRight( position, mAsteroidSize ) ) {
6592
mPositionX = Constants.MAP_LEFT_COORDINATE;
66-
} else if( positionX < Constants.MAP_LEFT_COORDINATE ) {
93+
} else if( Utils.isOffScreenToLeft( position, mAsteroidSize ) ) {
6794
mPositionX = Constants.MAP_RIGHT_COORDINATE;
6895
}
6996
}
7097

71-
public void setPositionY( float positionY ) {
72-
if( Constants.MAP_BOTTOM_COORDINATE - mAsteroidSize < positionY && positionY < Constants.MAP_TOP_COORDINATE + mAsteroidSize ) {
73-
mPositionY = positionY;
74-
} else if( positionY > Constants.MAP_TOP_COORDINATE + mAsteroidSize ) {
98+
public void setPositionY( float position ) {
99+
if( Utils.isInYPlane( position, mAsteroidSize ) ) {
100+
mPositionY = position;
101+
} else if( Utils.isOffScreenAboveTop( position, mAsteroidSize ) ) {
75102
mPositionY = Constants.MAP_BOTTOM_COORDINATE;
76-
} else if( positionY < Constants.MAP_BOTTOM_COORDINATE - mAsteroidSize ) {
103+
} else if( Utils.isOffScreenBelowBottom( position, mAsteroidSize ) ) {
77104
mPositionY = Constants.MAP_TOP_COORDINATE;
78105
}
79106
}
Lines changed: 68 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,82 @@
11
package com.ptrprograms.asteroidbelttv.Objects;
22

3+
import com.ptrprograms.asteroidbelttv.Utils.Constants;
4+
import com.ptrprograms.asteroidbelttv.Utils.ShapeBuffer;
5+
import com.ptrprograms.asteroidbelttv.Utils.Utils;
6+
37
/**
48
* Created by PaulTR on 7/20/14.
59
*/
610
public class Bullet {
7-
private int bulletSpeed;
811

9-
public Bullet() {
12+
private Utils.Color mColor = new Utils.Color();
13+
14+
private float mBulletSize = 1.0f;
15+
private float mVelocityX;
16+
private float mVelocityY;
17+
public float mPositionX;
18+
public float mPositionY;
19+
public int mLifeTimer = 5 * 60;
20+
21+
public Bullet( Utils.Color color, float shotDx, float shotDy, float positionX, float positionY ) {
22+
this.mColor.set( color );
23+
24+
mVelocityX = shotDx;
25+
mVelocityY = shotDy;
1026

27+
mPositionX = positionX;
28+
mPositionY = positionY;
1129
}
1230

13-
public void setBulletSpeed( int bulletSpeed ) {
14-
this.bulletSpeed = bulletSpeed;
31+
public void draw( ShapeBuffer sb ) {
32+
sb.add2DShape( mPositionX, mPositionY, mColor, getBulletVerticies(), mBulletSize, mBulletSize, 0, 0 );
33+
}
34+
35+
public float[] getBulletVerticies() {
36+
float vertices[] = new float[ 361*3 ];
37+
vertices[0] = 0;
38+
vertices[1] = 0;
39+
vertices[2] = 0;
40+
41+
for(int i =1; i <361; i++){
42+
vertices[(i * 3)] = (float) (0.5 * Math.cos((3.14/180) * (float)i ) + vertices[0]);
43+
vertices[(i * 3)+ 1] = (float) (0.5 * Math.sin((3.14/180) * (float)i ) + vertices[1]);
44+
vertices[(i * 3)+ 2] = 0;
45+
}
46+
47+
return vertices;
1548
}
1649

17-
public int getBulletSpeed() {
18-
return bulletSpeed;
50+
public void update( float delta ) {
51+
if( mLifeTimer > 0 ) {
52+
updateBulletPosition(delta);
53+
mLifeTimer--;
54+
}
1955
}
56+
57+
public void updateBulletPosition( float delta ) {
58+
setPositionX( mPositionX + ( mVelocityX * delta ) );
59+
setPositionY( mPositionY + ( mVelocityY * delta ) );
60+
}
61+
62+
public void setPositionX( float positionX ) {
63+
if( Constants.MAP_LEFT_COORDINATE - mBulletSize < positionX && positionX < Constants.MAP_RIGHT_COORDINATE + mBulletSize ) {
64+
mPositionX = positionX;
65+
} else if( positionX > Constants.MAP_RIGHT_COORDINATE ) {
66+
mPositionX = Constants.MAP_LEFT_COORDINATE;
67+
} else if( positionX < Constants.MAP_LEFT_COORDINATE ) {
68+
mPositionX = Constants.MAP_RIGHT_COORDINATE;
69+
}
70+
}
71+
72+
public void setPositionY( float positionY ) {
73+
if( Constants.MAP_BOTTOM_COORDINATE - mBulletSize < positionY && positionY < Constants.MAP_TOP_COORDINATE + mBulletSize ) {
74+
mPositionY = positionY;
75+
} else if( positionY > Constants.MAP_TOP_COORDINATE + mBulletSize ) {
76+
mPositionY = Constants.MAP_BOTTOM_COORDINATE;
77+
} else if( positionY < Constants.MAP_BOTTOM_COORDINATE - mBulletSize ) {
78+
mPositionY = Constants.MAP_TOP_COORDINATE;
79+
}
80+
}
81+
2082
}

0 commit comments

Comments
 (0)