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

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

Each planet is colored based on the faction that controls it and the ship count is centered on the planet and increases over time.

  • Property mode set to 100644
File size: 2.2 KB
RevLine 
[159eef8]1package com.example.helloandroid;
2
[8a4e64f]3import java.util.ArrayList;
4
[b6a9b5f]5import android.graphics.Canvas;
6import android.graphics.Color;
7import android.graphics.Paint;
8import android.graphics.Paint.FontMetrics;
9import android.util.Log;
10
[159eef8]11public 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;
18
19 public Planet(int radius, int x, int y) {
20 this.radius = radius;
21 this.x = x;
22 this.y = y;
23 faction = 0;
24 numShips = 0;
25
26 regenRate = 0; //change this to some expression / funcion call
27 }
28
29 public int getX() {
30 return x;
31 }
32
33 public int getY() {
34 return y;
35 }
36
[9b87c8d]37 public int getRadius() {
38 return radius;
39 }
40
[8a4e64f]41 public int getNumShips() {
42 return numShips;
43 }
44
45 public void setNumShips(int num) {
46 numShips = num;
47 }
[b6a9b5f]48
[1291908]49 public void setFaction(int faction) {
50 this.faction = faction;
51 }
52
[b6a9b5f]53 public void draw(Canvas canvas, Paint linePaint, Paint textPaint) {
54 FontMetrics metrics = textPaint.getFontMetrics();
55
56 int c, prevC = linePaint.getColor();
57
58 switch(faction) {
59 case 0:
60 c = Color.argb(255, 100, 100, 100);
61 break;
62 case 1:
63 c = Color.argb(255, 255, 0, 0);
64 break;
65 case 2:
66 c = Color.argb(255, 0, 255, 0);
67 break;
68 case 3:
69 c = Color.argb(255, 0, 0, 255);
70 break;
71 case 4:
72 c = Color.argb(255, 255, 255, 0);
73 break;
74 default:
75 c = prevC;
76 }
77
78 linePaint.setColor(c);
79
80 canvas.drawCircle(x, y, getRadius(), linePaint);
81 canvas.drawText(Integer.toString(numShips), x-textPaint.measureText(Integer.toString(numShips))/2, y-(metrics.ascent+metrics.descent)/2, textPaint);
82
83 linePaint.setColor(prevC);
84 }
85
[159eef8]86 public void update() {
[b6a9b5f]87 if(faction != 0)
88 numShips++;
89
[159eef8]90 }
91
92 public void sendFleet(Planet p, int numShips) {
93
94 }
[8a4e64f]95
96 public boolean collides(Planet p) {
97 double dist = Math.sqrt(Math.pow(this.x-p.x, 2) + Math.pow(this.y-p.y, 2));
98
99 return dist <= this.radius + p.radius;
100 }
101
102 public static boolean collisionDetected(Planet p, ArrayList<Planet> curPlanets) {
103 for(Planet p2 : curPlanets) {
104 if(p.collides(p2))
105 return true;
106 }
107
108 return false;
109 }
[159eef8]110}
Note: See TracBrowser for help on using the repository browser.