source: advance-wars/src/com/medievaltech/unit/Unit.java@ 331d180

Last change on this file since 331d180 was 41c11dd, checked in by dportnoy <devnull@…>, 13 years ago

Added cities to the game, moved the map to a new static class, and added incomplete support for capturing cities with soldiers.

  • Property mode set to 100644
File size: 2.3 KB
RevLine 
[113d7cf]1package com.medievaltech.unit;
[a0f5455]2
[113d7cf]3import java.util.*;
[fea4b77]4
[113d7cf]5import android.graphics.*;
[a0f5455]6
[41c11dd]7import com.medievaltech.advancewars.Player;
[fea4b77]8import com.medievaltech.advancewars.Enum.*;
[15ddb57]9
[fea4b77]10public abstract class Unit {
11 public UnitType type;
[c3ad11c]12 public Player owner;
[15ddb57]13
[a0f5455]14 public int maxHealth;
15 public int currentHealth;
16
17 public int maxFuel;
18 public int currentFuel;
19
20 public int sightRange;
[ebaddd9]21 protected int move;
[a0f5455]22
23 public int minAttackRange;
24 public int maxAttackRange;
25 public Point location;
26
[c3ad11c]27 public Unit(Player p)
[78d3c6f]28 {
[c3ad11c]29 p.addUnit(this);
30
31 owner = p;
[78d3c6f]32 maxHealth = 10;
33 currentHealth = 10;
[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) {
[c3ad11c]89 c.drawCircle(x, y, 20, owner.getColor());
[1a1e8c7]90 }
[a0f5455]91}
Note: See TracBrowser for help on using the repository browser.