source: advance-wars/src/com/medievaltech/game/Unit.java@ 78d3c6f

Last change on this file since 78d3c6f was 78d3c6f, checked in by aluthra <devnull@…>, 14 years ago

Minor changes to Unit and Soldier constructors

  • Property mode set to 100644
File size: 2.3 KB
Line 
1package com.medievaltech.game;
2
3import java.util.LinkedList;
4import java.util.List;
5
6import android.graphics.Canvas;
7import android.graphics.Paint;
8import android.graphics.Point;
9
10public abstract class Unit
11{
12 public enum Type
13 {
14 LAND,SEA
15 }
16
17 private Paint p;
18
19 public Type type;
20 public Player owner;
21
22 public int maxHealth;
23 public int currentHealth;
24
25 public int maxFuel;
26 public int currentFuel;
27
28 public int sightRange;
29 protected int move;
30
31 public int minAttackRange;
32 public int maxAttackRange;
33 public Point location;
34
35 public Unit(Paint p)
36 {
37 this.p = p;
38 maxHealth = 10;
39 currentHealth = 10;
40
41 }
42
43 public abstract void move(Point point);
44 public abstract void attack(Point point);
45
46 public List<Point> getMovementRange() {
47 List<Point> l = new LinkedList<Point>();
48 List<Point> prev = new LinkedList<Point>();
49 List<Point> cur = new LinkedList<Point>();
50 boolean[][] visited = new boolean[move*2+1][move*2+1];
51
52 for(int x=0; x<=move*2; x++)
53 for(int y=0; y<=move*2; y++)
54 visited[x][y] = false;
55
56 prev.add(new Point(location));
57 l.addAll(prev);
58 visited[move][move] = true;
59
60 for(int dist=1; dist <= move; dist++) {
61 for(Point p : prev) {
62 if(p.x>0 && p.x>location.x-move && !visited[p.x-location.x+move-1][p.y-location.y+move]) {
63 cur.add(new Point(p.x-1, p.y));
64 visited[p.x-location.x+move-1][p.y-location.y+move] = true;
65 }
66 if(p.x<5 && p.x<location.x+move && !visited[p.x-location.x+move+1][p.y-location.y+move]) {
67 cur.add(new Point(p.x+1, p.y));
68 visited[p.x-location.x+move+1][p.y-location.y+move] = true;
69 }
70 if(p.y>0 && p.y>location.y-move && !visited[p.x-location.x+move][p.y-location.y+move-1]) {
71 cur.add(new Point(p.x, p.y-1));
72 visited[p.x-location.x+move][p.y-location.y+move-1] = true;
73 }
74 if(p.y<7 && p.y<location.y+move && !visited[p.x-location.x+move][p.y-location.y+move+1]) {
75 cur.add(new Point(p.x, p.y+1));
76 visited[p.x-location.x+move][p.y-location.y+move+1] = true;
77 }
78 }
79
80 l.addAll(cur);
81 prev.clear();
82 prev.addAll(cur);
83 cur.clear();
84 }
85
86 return l;
87 }
88
89 public abstract List<Point> getAttackRange();
90
91 public void die() {
92
93 }
94
95 public void draw(Canvas c, int x, int y) {
96 c.drawCircle(x, y, 20, p);
97 }
98}
Note: See TracBrowser for help on using the repository browser.