source: advance-wars/src/com/medievaltech/unit/Unit.java@ 113d7cf

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

Implemented saving/loading of the map and the units on it. Also did some package refactoring (renamed com.medievaltech.game to com.medievaltech.unit and moved some classes into com.medievaltech.advancewars)

  • Property mode set to 100644
File size: 2.2 KB
Line 
1package com.medievaltech.unit;
2
3import java.util.*;
4import android.graphics.*;
5
6public abstract class Unit
7{
8 public enum Type
9 {
10 LAND,SEA,AIR
11 }
12
13 private Paint p;
14
15 public Type type;
16 //public Player owner;
17
18 public int maxHealth;
19 public int currentHealth;
20
21 public int maxFuel;
22 public int currentFuel;
23
24 public int sightRange;
25 protected int move;
26
27 public int minAttackRange;
28 public int maxAttackRange;
29 public Point location;
30
31 public Unit(Paint p)
32 {
33 this.p = p;
34 maxHealth = 10;
35 currentHealth = 10;
36
37 }
38
39 public abstract void move(Point point);
40 public abstract void 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<5 && 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<7 && 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 }
84
85 public abstract List<Point> getAttackRange();
86
87 public void die() {
88
89 }
90
91 public void draw(Canvas c, int x, int y) {
92 c.drawCircle(x, y, 20, p);
93 }
94}
Note: See TracBrowser for help on using the repository browser.