Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added asteroids

  • Loading branch information...
commit c4f0c79bdba0062b52d6ffeb71e1b6e62e531bf8 1 parent ace9696
@sticksoft authored
View
BIN  assets/asteroid.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
25 move.dasm
@@ -0,0 +1,25 @@
+:forward
+set [0xad00], 0x1000
+
+set I, 500
+:loop1
+sub I, 1
+ifg I, 0
+set pc, loop1
+
+set [0xad00], 0
+set [0xad02], 0xd000
+
+
+ set I, 200
+:loop2
+sub I, 1
+ifg I, 0
+set pc, loop2
+
+set [0xad02], 0
+
+set pc, forward
+
+:crash
+set pc, crash
View
25 res/raw/move.dasm
@@ -0,0 +1,25 @@
+:forward
+set [0xad00], 0x1000
+
+set I, 500
+:loop1
+sub I, 1
+ifg I, 0
+set pc, loop1
+
+set [0xad00], 0
+set [0xad02], 0xd000
+
+
+ set I, 200
+:loop2
+sub I, 1
+ifg I, 0
+set pc, loop2
+
+set [0xad02], 0
+
+set pc, forward
+
+:crash
+set pc, crash
View
55 src/uk/co/sticksoft/adce/ShipView2D.java
@@ -1,14 +1,18 @@
package uk.co.sticksoft.adce;
+import java.io.InputStream;
import java.util.HashSet;
import java.util.Random;
import uk.co.sticksoft.adce.cpu.CPU;
import uk.co.sticksoft.adce.cpu.CPU.Observer;
import uk.co.sticksoft.adce.maths.Vector2;
+import uk.co.sticksoft.adce.ship2d.Asteroid;
import uk.co.sticksoft.adce.ship2d.PlayerShip;
import uk.co.sticksoft.adce.ship2d.Star;
import android.content.Context;
+import android.graphics.Bitmap;
+import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.view.View;
@@ -19,6 +23,7 @@
PlayerShip player;
float starBoxSize = 900;
HashSet<Star> stars;
+ HashSet<Asteroid> asteroids;
Random rand = new Random();
@@ -26,6 +31,7 @@ public ShipView2D(Context context, CPU cpu)
{
super(context);
player = new PlayerShip(context, cpu);
+
stars = new HashSet<Star>();
for (int i = 0; i < 200; i++)
stars.add(new Star(
@@ -33,9 +39,45 @@ public ShipView2D(Context context, CPU cpu)
Color.argb(128 + rand.nextInt(127), 240 + rand.nextInt(15), 240 + rand.nextInt(15), 240 + rand.nextInt(15)),
rand.nextFloat() + 0.5f));
+ Bitmap aster = loadBitmapAsset(context, "asteroid.png");
+ asteroids = new HashSet<Asteroid>();
+ for (int i = 0; i < 10; i++)
+ asteroids.add(new Asteroid(
+ new Vector2((rand.nextFloat()-0.5f) * starBoxSize * 2.0f, (rand.nextFloat()-0.5f) * starBoxSize * 2.0f),
+ aster,
+ Color.argb(128 + rand.nextInt(127), 240 + rand.nextInt(15), 240 + rand.nextInt(15), 240 + rand.nextInt(15)),
+ rand.nextFloat() + 0.5f));
+
cpu.addObserver(this);
}
+ private static Bitmap loadBitmapAsset(Context context, String name)
+ {
+ Bitmap bmp = null;
+ InputStream is = null;
+ try
+ {
+ bmp = BitmapFactory.decodeStream(is = context.getAssets().open(name));
+ }
+ catch (Exception ex)
+ {
+ ex.printStackTrace();
+ }
+ finally
+ {
+ try
+ {
+ if (is != null)
+ is.close();
+ }
+ catch (Exception ex)
+ {
+ ex.printStackTrace();
+ }
+ }
+ return bmp;
+ }
+
private boolean rendering = false;
@Override
@@ -62,15 +104,26 @@ protected void onDraw(Canvas canvas)
canvas.save();
canvas.translate(getMeasuredWidth() / 2, getMeasuredHeight() / 2);
- player.render(canvas);
+ canvas.save();
canvas.translate(-player.position.x(), -player.position.y());
+
for (Star s : stars)
{
s.update(0, player, starBoxSize);
s.render(canvas);
}
+ for (Asteroid a : asteroids)
+ {
+ a.update(1.0f / 30.0f, player, starBoxSize);
+ a.render(canvas);
+ }
+
+ canvas.restore();
+
+ player.render(canvas);
+
canvas.restore();
player.update(1.0f / 30.0f);
View
61 src/uk/co/sticksoft/adce/ship2d/Asteroid.java
@@ -0,0 +1,61 @@
+package uk.co.sticksoft.adce.ship2d;
+
+import uk.co.sticksoft.adce.maths.Vector2;
+import android.graphics.Bitmap;
+import android.graphics.Canvas;
+import android.graphics.Paint;
+import android.graphics.PorterDuff.Mode;
+import android.graphics.PorterDuffColorFilter;
+
+public class Asteroid
+{
+ public Vector2 position, velocity;
+ public float rotation, angularMomentum;
+
+ public int colour;
+ public float size;
+ private Paint paint;
+ private Bitmap bmp;
+
+ public Asteroid(Vector2 position, Bitmap bmp, int colour, float size)
+ {
+ this.position = position;
+ this.bmp = bmp;
+ this.colour = colour;
+ this.size = size;
+
+ this.velocity = new Vector2(100.0f * (float)(Math.random() - 0.5), 100.0f * (float)(Math.random() - 0.5));
+ this.rotation = (float)Math.random();
+ this.angularMomentum = (0.5f + (float)Math.random()) * (Math.random() < 0.5 ? 1 : -1);
+
+ paint = new Paint();
+ //paint.setColorFilter(new PorterDuffColorFilter(colour, Mode.MULTIPLY));
+ }
+
+ private Vector2 temp = new Vector2();
+ public void update(float seconds, PlayerShip ship, float boxSize)
+ {
+ position.add(velocity.mul(temp, seconds));
+ rotation += angularMomentum * seconds;
+
+ while (position.x() < ship.position.x() - boxSize)
+ position.add(boxSize*2, 0);
+ while (position.x() > ship.position.x() + boxSize)
+ position.sub(boxSize*2, 0);
+
+ while (position.y() < ship.position.y() - boxSize)
+ position.add(0, boxSize*2);
+ while (position.y() > ship.position.y() + boxSize)
+ position.sub(0, boxSize*2);
+ }
+
+
+ public void render(Canvas canvas)
+ {
+ canvas.save();
+ canvas.translate(position.x(), position.y());
+ canvas.rotate((float)Math.toDegrees(rotation));
+ canvas.drawBitmap(bmp, -bmp.getWidth()/2, -bmp.getHeight()/2, paint);
+ canvas.restore();
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.