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

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

Added code to draw a unit's possible movement options on the map.

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