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