[159eef8] | 1 | package com.example.helloandroid;
|
---|
| 2 |
|
---|
[8a4e64f] | 3 | import java.util.ArrayList;
|
---|
| 4 |
|
---|
[5053d90] | 5 | import android.graphics.Bitmap;
|
---|
[b6a9b5f] | 6 | import android.graphics.Canvas;
|
---|
| 7 | import android.graphics.Color;
|
---|
| 8 | import android.graphics.Paint;
|
---|
| 9 | import android.graphics.Paint.FontMetrics;
|
---|
| 10 |
|
---|
[159eef8] | 11 | public class Planet {
|
---|
| 12 | int radius;
|
---|
[9b87c8d] | 13 | private int x;
|
---|
| 14 | private int y;
|
---|
[159eef8] | 15 | int faction;
|
---|
| 16 | int numShips;
|
---|
[5053d90] | 17 | boolean selected;
|
---|
| 18 | private Bitmap selection;
|
---|
[04a9a00] | 19 | private int frameCount, framesUntilSpawn;
|
---|
[5053d90] | 20 |
|
---|
[159eef8] | 21 | public Planet(int radius, int x, int y) {
|
---|
| 22 | this.radius = radius;
|
---|
| 23 | this.x = x;
|
---|
| 24 | this.y = y;
|
---|
| 25 | faction = 0;
|
---|
| 26 | numShips = 0;
|
---|
[5053d90] | 27 | selected = false;
|
---|
[159eef8] | 28 |
|
---|
[04a9a00] | 29 | frameCount = 0;
|
---|
| 30 | framesUntilSpawn = 100/radius;
|
---|
[5053d90] | 31 |
|
---|
| 32 | int size = getRadius()+15;
|
---|
| 33 |
|
---|
| 34 | selection = Bitmap.createBitmap(size*2, size*2, Bitmap.Config.ARGB_8888);
|
---|
| 35 | Canvas c = new Canvas(selection);
|
---|
| 36 | c.drawColor(Color.argb(0, 0, 0, 0));
|
---|
| 37 |
|
---|
| 38 | Paint p = new Paint();
|
---|
| 39 | p.setAntiAlias(true);
|
---|
| 40 |
|
---|
| 41 | p.setColor(Color.argb(255, 255, 255, 255));
|
---|
[c27abf4] | 42 | c.drawCircle(size, size, getRadius()+9, p);
|
---|
[5053d90] | 43 |
|
---|
| 44 | p.setColor(Color.argb(255, 100, 100, 100));
|
---|
| 45 | c.drawCircle(size, size, getRadius()+5, p);
|
---|
| 46 |
|
---|
| 47 | for(int i=0; i<size*2; i++) {
|
---|
| 48 | for(int j=0; j<size*2; j++) {
|
---|
| 49 | if(selection.getPixel(i,j) == Color.argb(255, 100, 100, 100))
|
---|
| 50 | selection.setPixel(i, j, Color.argb(0, 0, 0, 0));
|
---|
| 51 | }
|
---|
| 52 | }
|
---|
[159eef8] | 53 | }
|
---|
| 54 |
|
---|
| 55 | public int getX() {
|
---|
| 56 | return x;
|
---|
| 57 | }
|
---|
| 58 |
|
---|
| 59 | public int getY() {
|
---|
| 60 | return y;
|
---|
| 61 | }
|
---|
| 62 |
|
---|
[9b87c8d] | 63 | public int getRadius() {
|
---|
| 64 | return radius;
|
---|
| 65 | }
|
---|
| 66 |
|
---|
[95509e1] | 67 | public int getFaction() {
|
---|
| 68 | return faction;
|
---|
| 69 | }
|
---|
| 70 |
|
---|
[8a4e64f] | 71 | public int getNumShips() {
|
---|
| 72 | return numShips;
|
---|
| 73 | }
|
---|
| 74 |
|
---|
[5053d90] | 75 | public boolean isSelected() {
|
---|
| 76 | return selected;
|
---|
| 77 | }
|
---|
| 78 |
|
---|
[8a4e64f] | 79 | public void setNumShips(int num) {
|
---|
| 80 | numShips = num;
|
---|
| 81 | }
|
---|
[b6a9b5f] | 82 |
|
---|
[1291908] | 83 | public void setFaction(int faction) {
|
---|
| 84 | this.faction = faction;
|
---|
| 85 | }
|
---|
| 86 |
|
---|
[5053d90] | 87 | public void select() {
|
---|
| 88 | selected = true;
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 | public void unselect() {
|
---|
| 92 | selected = false;
|
---|
| 93 | }
|
---|
| 94 |
|
---|
[b6a9b5f] | 95 | public void draw(Canvas canvas, Paint linePaint, Paint textPaint) {
|
---|
| 96 | FontMetrics metrics = textPaint.getFontMetrics();
|
---|
| 97 |
|
---|
| 98 | int c, prevC = linePaint.getColor();
|
---|
| 99 |
|
---|
| 100 | switch(faction) {
|
---|
| 101 | case 0:
|
---|
| 102 | c = Color.argb(255, 100, 100, 100);
|
---|
| 103 | break;
|
---|
| 104 | case 1:
|
---|
| 105 | c = Color.argb(255, 255, 0, 0);
|
---|
| 106 | break;
|
---|
| 107 | case 2:
|
---|
[61c4fbc] | 108 | c = Color.argb(255, 0, 180, 0);
|
---|
[b6a9b5f] | 109 | break;
|
---|
| 110 | case 3:
|
---|
| 111 | c = Color.argb(255, 0, 0, 255);
|
---|
| 112 | break;
|
---|
| 113 | case 4:
|
---|
[61c4fbc] | 114 | c = Color.argb(255, 150, 150, 0);
|
---|
[b6a9b5f] | 115 | break;
|
---|
| 116 | default:
|
---|
| 117 | c = prevC;
|
---|
| 118 | }
|
---|
| 119 |
|
---|
| 120 | linePaint.setColor(c);
|
---|
| 121 |
|
---|
| 122 | canvas.drawCircle(x, y, getRadius(), linePaint);
|
---|
| 123 | canvas.drawText(Integer.toString(numShips), x-textPaint.measureText(Integer.toString(numShips))/2, y-(metrics.ascent+metrics.descent)/2, textPaint);
|
---|
| 124 |
|
---|
| 125 | linePaint.setColor(prevC);
|
---|
| 126 | }
|
---|
| 127 |
|
---|
[5053d90] | 128 | public void drawSelectionCircle(Canvas canvas) {
|
---|
| 129 | int size = getRadius()+15;
|
---|
| 130 |
|
---|
| 131 | canvas.drawBitmap(selection, x-size, y-size, null);
|
---|
| 132 | }
|
---|
| 133 |
|
---|
[159eef8] | 134 | public void update() {
|
---|
[04a9a00] | 135 | if(faction != 0) {
|
---|
| 136 | frameCount++;
|
---|
| 137 |
|
---|
| 138 | if(frameCount == framesUntilSpawn) {
|
---|
| 139 | frameCount = 0;
|
---|
| 140 | numShips++;
|
---|
| 141 | }
|
---|
| 142 | }
|
---|
[159eef8] | 143 | }
|
---|
| 144 |
|
---|
| 145 | public void sendFleet(Planet p, int numShips) {
|
---|
| 146 |
|
---|
| 147 | }
|
---|
[8a4e64f] | 148 |
|
---|
[5053d90] | 149 | public boolean contains(int x, int y) {
|
---|
| 150 | double dist = Math.sqrt(Math.pow(this.x-x, 2) + Math.pow(this.y-y, 2));
|
---|
| 151 |
|
---|
| 152 | return dist <= this.radius;
|
---|
| 153 | }
|
---|
| 154 |
|
---|
[8a4e64f] | 155 | public boolean collides(Planet p) {
|
---|
| 156 | double dist = Math.sqrt(Math.pow(this.x-p.x, 2) + Math.pow(this.y-p.y, 2));
|
---|
| 157 |
|
---|
| 158 | return dist <= this.radius + p.radius;
|
---|
| 159 | }
|
---|
| 160 |
|
---|
| 161 | public static boolean collisionDetected(Planet p, ArrayList<Planet> curPlanets) {
|
---|
| 162 | for(Planet p2 : curPlanets) {
|
---|
| 163 | if(p.collides(p2))
|
---|
| 164 | return true;
|
---|
| 165 | }
|
---|
| 166 |
|
---|
| 167 | return false;
|
---|
| 168 | }
|
---|
[159eef8] | 169 | }
|
---|