| 1 | package com.example.helloandroid;
|
|---|
| 2 |
|
|---|
| 3 | import java.util.ArrayList;
|
|---|
| 4 |
|
|---|
| 5 | import android.graphics.Bitmap;
|
|---|
| 6 | import android.graphics.Canvas;
|
|---|
| 7 | import android.graphics.Color;
|
|---|
| 8 | import android.graphics.Paint;
|
|---|
| 9 | import android.graphics.Paint.FontMetrics;
|
|---|
| 10 |
|
|---|
| 11 | public class Planet {
|
|---|
| 12 | int radius;
|
|---|
| 13 | int regenRate; // ships per second
|
|---|
| 14 | private int x;
|
|---|
| 15 | private int y;
|
|---|
| 16 | int faction;
|
|---|
| 17 | int numShips;
|
|---|
| 18 | boolean selected;
|
|---|
| 19 | private Bitmap selection;
|
|---|
| 20 |
|
|---|
| 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;
|
|---|
| 27 | selected = false;
|
|---|
| 28 |
|
|---|
| 29 | regenRate = 0; //change this to some expression / funcion call
|
|---|
| 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));
|
|---|
| 41 | c.drawCircle(size, size, getRadius()+9, p);
|
|---|
| 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 | }
|
|---|
| 52 | }
|
|---|
| 53 |
|
|---|
| 54 | public int getX() {
|
|---|
| 55 | return x;
|
|---|
| 56 | }
|
|---|
| 57 |
|
|---|
| 58 | public int getY() {
|
|---|
| 59 | return y;
|
|---|
| 60 | }
|
|---|
| 61 |
|
|---|
| 62 | public int getRadius() {
|
|---|
| 63 | return radius;
|
|---|
| 64 | }
|
|---|
| 65 |
|
|---|
| 66 | public int getFaction() {
|
|---|
| 67 | return faction;
|
|---|
| 68 | }
|
|---|
| 69 |
|
|---|
| 70 | public int getNumShips() {
|
|---|
| 71 | return numShips;
|
|---|
| 72 | }
|
|---|
| 73 |
|
|---|
| 74 | public boolean isSelected() {
|
|---|
| 75 | return selected;
|
|---|
| 76 | }
|
|---|
| 77 |
|
|---|
| 78 | public void setNumShips(int num) {
|
|---|
| 79 | numShips = num;
|
|---|
| 80 | }
|
|---|
| 81 |
|
|---|
| 82 | public void setFaction(int faction) {
|
|---|
| 83 | this.faction = faction;
|
|---|
| 84 | }
|
|---|
| 85 |
|
|---|
| 86 | public void select() {
|
|---|
| 87 | selected = true;
|
|---|
| 88 | }
|
|---|
| 89 |
|
|---|
| 90 | public void unselect() {
|
|---|
| 91 | selected = false;
|
|---|
| 92 | }
|
|---|
| 93 |
|
|---|
| 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:
|
|---|
| 107 | c = Color.argb(255, 0, 180, 0);
|
|---|
| 108 | break;
|
|---|
| 109 | case 3:
|
|---|
| 110 | c = Color.argb(255, 0, 0, 255);
|
|---|
| 111 | break;
|
|---|
| 112 | case 4:
|
|---|
| 113 | c = Color.argb(255, 150, 150, 0);
|
|---|
| 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 |
|
|---|
| 127 | public void drawSelectionCircle(Canvas canvas) {
|
|---|
| 128 | int size = getRadius()+15;
|
|---|
| 129 |
|
|---|
| 130 | canvas.drawBitmap(selection, x-size, y-size, null);
|
|---|
| 131 | }
|
|---|
| 132 |
|
|---|
| 133 | public void update() {
|
|---|
| 134 | //if(faction != 0)
|
|---|
| 135 | //numShips++;
|
|---|
| 136 |
|
|---|
| 137 | }
|
|---|
| 138 |
|
|---|
| 139 | public void sendFleet(Planet p, int numShips) {
|
|---|
| 140 |
|
|---|
| 141 | }
|
|---|
| 142 |
|
|---|
| 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 |
|
|---|
| 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 | }
|
|---|
| 163 | }
|
|---|