Source code: org/meowers/cide/data/Map.java
1 /*
2 * Map.java
3 *
4 * Created on January 18, 2002, 12:54 AM
5 */
6
7 package org.meowers.cide.data;
8
9 import java.util.*;
10
11 /**
12 * Represents and entire map, made up of tiles.
13 *
14 * @author Adam Miezianko
15 * @version %I%, %G%
16 */
17 public class Map extends GameObject {
18
19 private ArrayList cells = new ArrayList();
20 private int width;
21 private int height;
22 private String name;
23
24 /**
25 * Creates a new empty Map which has one cell.
26 *
27 */
28 public Map() {
29 type = GameObject.MAP;
30 width = 1;
31 height = 1;
32 cells.add(new Tile());
33 }
34
35 /**
36 * Returns the width of the map.
37 *
38 * @return width of the map
39 */
40 public int getWidth() {
41 return width;
42 }
43
44 /**
45 * Returns the height of the map.
46 *
47 * @return height of the map
48 */
49 public int getHeight() {
50 return height;
51 }
52
53 /**
54 * Resize the map to a new size. If the map is being enlarged, fill those
55 * cells with empty tiles. If the map is being shrunk, just drop the
56 * affected cells.
57 *
58 * NOTE: Does not support shrinking yet. And is destructive right now.
59 *
60 * @param width New width of the map
61 * @param height New height of the map
62 */
63 public void resize(int width, int height) {
64 cells.ensureCapacity(width*height);
65 this.width = width;
66 this.height = height;
67 for (int i = 0; i < width*height; i++) {
68 cells.add(i, new Tile());
69 }
70
71 /* this map has been changed */
72 dirty = true;
73 }
74
75 /**
76 * Increases or decrease the width of the <code>Map</code>. The new cells
77 * are created with empty <code>Tile</code> contents.
78 *
79 * @param size how much to increase or decrease the width by.
80 */
81 public void addWidth(int size) {
82 cells.ensureCapacity((width+1) * height);
83 for (int j = 0; j < size; j++) {
84 for (int i = height; i > 0; i--) {
85 cells.add((width*i), new Tile());
86 }
87 width++;
88 }
89
90 /* this map has been changed */
91 dirty = true;
92 }
93
94 /**
95 * Increase or decrease the height of the <code>Map</code>. If new cells
96 * are created, they contain new, emtpy <code>Tile</code> objects.
97 *
98 * @param size how much to increase or decrease the height by.
99 */
100 public void addHeight(int size) {
101 cells.ensureCapacity(width * (height + 1));
102 for (int j = 0; j < size; j++) {
103 for (int i = 1; i <= width; i++) {
104 cells.add(new Tile());
105 }
106 height++;
107 }
108
109 /* this map has been changed */
110 dirty = true;
111 }
112
113 /**
114 * Set the <code>Tile</code> at the map cell specified to the specified
115 * <code>Tile</code>.
116 *
117 * @param x Horizontal position of tile.
118 * @param y Vertical postion of tile.
119 * @param t The tile to place at the position.
120 */
121 public void setTileAt(int x, int y, Tile t) {
122 int location = y*width+x;
123 if (location <= cells.size()) {
124 cells.set(y*width+x, t);
125 }
126
127 /* this map has been changed */
128 dirty = true;
129 }
130
131 /**
132 * Gets the <code>Tile</code> at the specified position.
133 *
134 * @param x Vertical position.
135 * @param y Horizontal position.
136 * @returns the <code>Tile</code> at specified position.
137 */
138 public Tile getTileAt(int x, int y) {
139 int idx = y*width+x;
140 if (idx < cells.size())
141 return (Tile) cells.get(y*width+x);
142
143 return null;
144 }
145
146 }