source: galcon-client/src/com/example/helloandroid/Planet.java@ c27abf4

Last change on this file since c27abf4 was c27abf4, checked in by dportnoy <devnull@…>, 15 years ago

Changed the width of the planet selection circle.

  • Property mode set to 100644
File size: 3.3 KB
Line 
1package com.example.helloandroid;
2
3import java.util.ArrayList;
4
5import android.graphics.Bitmap;
6import android.graphics.Canvas;
7import android.graphics.Color;
8import android.graphics.Paint;
9import android.graphics.Paint.FontMetrics;
10
11public 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 getNumShips() {
67 return numShips;
68 }
69
70 public boolean isSelected() {
71 return selected;
72 }
73
74 public void setNumShips(int num) {
75 numShips = num;
76 }
77
78 public void setFaction(int faction) {
79 this.faction = faction;
80 }
81
82 public void select() {
83 selected = true;
84 }
85
86 public void unselect() {
87 selected = false;
88 }
89
90 public void draw(Canvas canvas, Paint linePaint, Paint textPaint) {
91 FontMetrics metrics = textPaint.getFontMetrics();
92
93 int c, prevC = linePaint.getColor();
94
95 switch(faction) {
96 case 0:
97 c = Color.argb(255, 100, 100, 100);
98 break;
99 case 1:
100 c = Color.argb(255, 255, 0, 0);
101 break;
102 case 2:
103 c = Color.argb(255, 0, 255, 0);
104 break;
105 case 3:
106 c = Color.argb(255, 0, 0, 255);
107 break;
108 case 4:
109 c = Color.argb(255, 255, 255, 0);
110 break;
111 default:
112 c = prevC;
113 }
114
115 linePaint.setColor(c);
116
117 canvas.drawCircle(x, y, getRadius(), linePaint);
118 canvas.drawText(Integer.toString(numShips), x-textPaint.measureText(Integer.toString(numShips))/2, y-(metrics.ascent+metrics.descent)/2, textPaint);
119
120 linePaint.setColor(prevC);
121 }
122
123 public void drawSelectionCircle(Canvas canvas) {
124 int size = getRadius()+15;
125
126 canvas.drawBitmap(selection, x-size, y-size, null);
127 }
128
129 public void update() {
130 if(faction != 0)
131 numShips++;
132
133 }
134
135 public void sendFleet(Planet p, int numShips) {
136
137 }
138
139 public boolean contains(int x, int y) {
140 double dist = Math.sqrt(Math.pow(this.x-x, 2) + Math.pow(this.y-y, 2));
141
142 return dist <= this.radius;
143 }
144
145 public boolean collides(Planet p) {
146 double dist = Math.sqrt(Math.pow(this.x-p.x, 2) + Math.pow(this.y-p.y, 2));
147
148 return dist <= this.radius + p.radius;
149 }
150
151 public static boolean collisionDetected(Planet p, ArrayList<Planet> curPlanets) {
152 for(Planet p2 : curPlanets) {
153 if(p.collides(p2))
154 return true;
155 }
156
157 return false;
158 }
159}
Note: See TracBrowser for help on using the repository browser.