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

Last change on this file since 9d030cb was 04a9a00, checked in by dportnoy <devnull@…>, 14 years ago

Framerate is now set to 20 fps and planets spawn ships at different rates based on their size. However, fleets now don't leave a planet they're going around at the proper time.

  • Property mode set to 100644
File size: 3.5 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 private int x;
14 private int y;
15 int faction;
16 int numShips;
17 boolean selected;
18 private Bitmap selection;
19 private int frameCount, framesUntilSpawn;
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 frameCount = 0;
30 framesUntilSpawn = 100/radius;
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));
42 c.drawCircle(size, size, getRadius()+9, p);
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 }
53 }
54
55 public int getX() {
56 return x;
57 }
58
59 public int getY() {
60 return y;
61 }
62
63 public int getRadius() {
64 return radius;
65 }
66
67 public int getFaction() {
68 return faction;
69 }
70
71 public int getNumShips() {
72 return numShips;
73 }
74
75 public boolean isSelected() {
76 return selected;
77 }
78
79 public void setNumShips(int num) {
80 numShips = num;
81 }
82
83 public void setFaction(int faction) {
84 this.faction = faction;
85 }
86
87 public void select() {
88 selected = true;
89 }
90
91 public void unselect() {
92 selected = false;
93 }
94
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:
108 c = Color.argb(255, 0, 180, 0);
109 break;
110 case 3:
111 c = Color.argb(255, 0, 0, 255);
112 break;
113 case 4:
114 c = Color.argb(255, 150, 150, 0);
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
128 public void drawSelectionCircle(Canvas canvas) {
129 int size = getRadius()+15;
130
131 canvas.drawBitmap(selection, x-size, y-size, null);
132 }
133
134 public void update() {
135 if(faction != 0) {
136 frameCount++;
137
138 if(frameCount == framesUntilSpawn) {
139 frameCount = 0;
140 numShips++;
141 }
142 }
143 }
144
145 public void sendFleet(Planet p, int numShips) {
146
147 }
148
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
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 }
169}
Note: See TracBrowser for help on using the repository browser.