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

Last change on this file since bdd63ba 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: 1.8 KB
Line 
1package com.medievaltech.advancewars;
2
3import java.io.*;
4
5import com.medievaltech.unit.Unit;
6
7
8import android.graphics.*;
9
10public class Map {
11 private Tile[][] grid;
12 public Point offset;
13
14 public Map(int width, int height, Point offset) {
15 grid = new Tile[width][height];
16 this.offset = offset;
17 }
18
19 public Map(Tile t, int width, int height, Point offset) {
20 grid = new Tile[width][height];
21 this.offset = offset;
22
23 for(int x=0; x<getWidth(); x++)
24 for(int y=0; y<getHeight(); y++)
25 grid[x][y] = new Tile(t, new Point(x, y));
26 }
27
28 public int getWidth() {
29 return grid.length;
30 }
31
32 public int getHeight() {
33 return grid[0].length;
34 }
35
36 public Tile getTile(int x, int y) {
37 return grid[x][y];
38 }
39
40 public Tile getTile(Point point) {
41 return grid[point.x][point.y];
42 }
43
44 public void setTile(int x, int y, Tile t) {
45 grid[x][y] = t;
46 }
47
48 public void save(PrintWriter p) {
49 p.println(getWidth());
50 p.println(getHeight());
51 p.println(offset.x+"x"+offset.y);
52
53 for(int x=0; x<getWidth(); x++) {
54 p.print(grid[x][0].type.ordinal());
55 for(int y=1; y<getHeight(); y++)
56 p.print(","+grid[x][y].type.ordinal());
57 p.println();
58 }
59
60 for(int x=0; x<getWidth(); x++) {
61 for(int y=1; y<getHeight(); y++) {
62 if(grid[x][y].currentUnit != null) {
63 Unit u = grid[x][y].currentUnit;
64 //p.println(u.type);
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}
Note: See TracBrowser for help on using the repository browser.