source: advance-wars/src/com/medievaltech/advancewars/Map.java@ 41c11dd

Last change on this file since 41c11dd 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.0 KB
Line 
1package com.medievaltech.advancewars;
2
3import java.io.*;
4
5import android.graphics.*;
6
7import com.medievaltech.unit.Unit;
8
9public class Map {
10 private Tile[][] grid;
11 public Point offset;
12
13 public Map(int width, int height, Point offset) {
14 grid = new Tile[width][height];
15 this.offset = offset;
16 }
17
18 public Map(Tile t, int width, int height, Point offset) {
19 grid = new Tile[width][height];
20 this.offset = offset;
21
22 for(int x=0; x<getWidth(); x++)
23 for(int y=0; y<getHeight(); y++)
24 grid[x][y] = new Tile(t, new Point(x, y));
25 }
26
27 public int getWidth() {
28 return grid.length;
29 }
30
31 public int getHeight() {
32 return grid[0].length;
33 }
34
35 public Tile getTile(int x, int y) {
36 return grid[x][y];
37 }
38
39 public Tile getTile(Point point) {
40 return grid[point.x][point.y];
41 }
42
43 public void setTile(int x, int y, Tile t) {
44 grid[x][y] = t;
45 }
46
47 public void save(PrintWriter p) {
48 p.println(getWidth());
49 p.println(getHeight());
50 p.println(offset.x+"x"+offset.y);
51
52 for(int x=0; x<getWidth(); x++) {
53 p.print(grid[x][0].type.ordinal());
54 for(int y=1; y<getHeight(); y++)
55 p.print(","+grid[x][y].type.ordinal());
56 p.println();
57 }
58
59 for(int x=0; x<getWidth(); x++) {
60 for(int y=1; y<getHeight(); y++) {
61 if(grid[x][y].currentUnit != null) {
62 Unit u = grid[x][y].currentUnit;
63 //p.println(u.type);
64 //we also need to save the owner of the unit
65 p.println(u.location.x+","+u.location.y);
66 }
67 }
68 }
69 }
70
71 public void draw(Canvas c) {
72 for(int x=0; x<getWidth(); x++)
73 for(int y=0; y<getHeight(); y++)
74 grid[x][y].draw(c, offset.x+50*x, offset.y+50*y);
75 }
76
77 public void drawUnits(Canvas c) {
78 for(int x=0; x<getWidth(); x++)
79 for(int y=0; y<getHeight(); y++)
80 grid[x][y].drawUnit(c, offset.x+50*x, offset.y+50*y);
81 }
82
83 public void drawBuildings(Canvas c) {
84 for(int x=0; x<getWidth(); x++)
85 for(int y=0; y<getHeight(); y++)
86 grid[x][y].drawBuilding(c, offset.x+50*x, offset.y+50*y);
87 }
88}
Note: See TracBrowser for help on using the repository browser.