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
RevLine 
[113d7cf]1package com.medievaltech.unit;
[a0f5455]2
[113d7cf]3import java.util.*;
[fea4b77]4
[113d7cf]5import android.graphics.*;
[a0f5455]6
[fea4b77]7import com.medievaltech.advancewars.Enum.*;
[15ddb57]8
[fea4b77]9public abstract class Unit {
[1a1e8c7]10 private Paint p;
11
[fea4b77]12 public UnitType type;
[113d7cf]13 //public Player owner;
[15ddb57]14
[a0f5455]15 public int maxHealth;
16 public int currentHealth;
17
18 public int maxFuel;
19 public int currentFuel;
20
21 public int sightRange;
[ebaddd9]22 protected int move;
[a0f5455]23
24 public int minAttackRange;
25 public int maxAttackRange;
26 public Point location;
27
[78d3c6f]28 public Unit(Paint p)
29 {
[1a1e8c7]30 this.p = p;
[78d3c6f]31 maxHealth = 10;
32 currentHealth = 10;
33
[1a1e8c7]34 }
35
[6a639f7]36 public abstract void move(Point point);
37 public abstract void attack(Point point);
[ebaddd9]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 }
[b97a618]59 if(p.x<5 && p.x<location.x+move && !visited[p.x-location.x+move+1][p.y-location.y+move]) {
[ebaddd9]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 }
[b97a618]67 if(p.y<7 && p.y<location.y+move && !visited[p.x-location.x+move][p.y-location.y+move+1]) {
[ebaddd9]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 }
[a0f5455]81
82 public abstract List<Point> getAttackRange();
83
[1a1e8c7]84 public void die() {
[a0f5455]85
86 }
87
[1a1e8c7]88 public void draw(Canvas c, int x, int y) {
89 c.drawCircle(x, y, 20, p);
90 }
[a0f5455]91}
Note: See TracBrowser for help on using the repository browser.