source: advance-wars/src/com/medievaltech/unit/Unit.java@ 99433bb

Last change on this file since 99433bb was fea4b77, checked in by dportnoy <devnull@…>, 13 years ago

Implemented turn and support for computer-controller movement of units. Right now, the computer moves a specific unit down one square when its his turn.

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