source: lost-perception/main/MapEditor.java@ e5d2936

Last change on this file since e5d2936 was a10d422, checked in by Dmitry Portnoy <dmitry.portnoy@…>, 5 years ago

Create a working makefile target for MapEditor

  • Property mode set to 100644
File size: 56.1 KB
RevLine 
[ebd3538]1package main;
2
3import java.awt.GraphicsEnvironment;
4import java.io.OutputStream;
5import java.io.PrintStream;
6import java.io.FileOutputStream;
7import java.awt.event.KeyEvent;
8import java.awt.event.MouseEvent;
9import java.util.Iterator;
10import java.awt.geom.Point2D;
11import java.util.Random;
12import java.awt.image.ImageObserver;
13import java.awt.Image;
14import java.awt.Color;
15import java.awt.geom.Area;
16import java.awt.geom.Ellipse2D;
17import java.awt.Shape;
18import collision.Bound;
19import java.awt.geom.Rectangle2D;
20import utils.DynamicImage;
21import gamegui.Button;
22import gamegui.Align;
23import gamegui.Member;
24import gamegui.Label;
25import java.awt.image.BufferStrategy;
26import java.awt.GraphicsConfiguration;
27import utils.Utils;
28import java.awt.GraphicsDevice;
29import java.awt.Point;
30import java.util.ArrayList;
31import java.util.HashMap;
32import java.awt.Font;
33import gamegui.Textbox;
34import gamegui.ScrollBar;
35import gamegui.Window;
36import java.awt.Frame;
37import java.awt.image.BufferedImage;
38import java.awt.Graphics;
39import java.awt.event.MouseListener;
40import java.awt.event.KeyListener;
41
[a10d422]42public class MapEditor implements KeyListener, MouseListener {
[ebd3538]43 public static final int RES_X = 1024;
44 public static final int RES_Y = 768;
45 public static final int MAP_OFFSET_X = 204;
46 public static final int MAP_OFFSET_Y = 148;
47 private static final boolean RUNNING_FROM_JAR = false;
48 public State state;
49 public AuxState auxState;
50 boolean started;
51 boolean done;
52 boolean showFps;
53 int frameCount;
54 int lastFrameCount;
55 long lastFpsUpdate;
56 Graphics g;
57 BufferedImage mapBuffer;
58 Graphics mapG;
59 public static Frame frmMain;
60 public static Map map;
61 Window wndMain;
62 Window wndEdit;
63 Window wndEditPalette;
64 Window wndEditOptions;
65 Window wndDiagnostics;
66 Window wndLoadMap;
67 ScrollBar horBar;
68 ScrollBar verBar;
69 Window wndMessage;
70 Textbox selectedText;
71 Font font11;
72 Font font12;
73 Font font14;
74 Font font24;
75 Font fontTT;
76 static final int LEVEL_ARROWS_OFFSET = 0;
77 static final int SET_ARROWS_OFFSET = 50;
78 HashMap<Integer, Tile> imageMap;
79 HashMap<Integer, Creature> crMap;
80 HashMap<Integer, GroundType[]> tileBreakdowns;
81 ArrayList<Tile>[] tileByBreakdown;
82 ArrayList<ArrayList<Tile>> imgSets;
83 ArrayList<Tile> activeSet;
84 int setIndex;
85 Tile curTile;
86 int curLevel;
87 Tile selectedObject;
88 Creature selectedCreature;
89 String mapName;
90 ArrayList<Creature> crSet;
91 Creature curCr;
92 boolean bounds;
93 boolean passable;
94 boolean showCr;
95 boolean highLevelPlacement;
96 Point playerLoc;
97
98 public MapEditor(final GraphicsDevice device) {
99 this.started = false;
100 try {
101 GraphicsConfiguration gc = device.getDefaultConfiguration();
102 (MapEditor.frmMain = new Frame(gc)).setUndecorated(true);
103 MapEditor.frmMain.setIgnoreRepaint(true);
104 device.setFullScreenWindow(MapEditor.frmMain);
105 if (device.isDisplayChangeSupported()) {
106 Utils.chooseBestDisplayMode(device, 1024, 768);
107 }
108 MapEditor.frmMain.addMouseListener(this);
109 MapEditor.frmMain.addKeyListener(this);
110 MapEditor.frmMain.createBufferStrategy(2);
111 final BufferStrategy bufferStrategy = MapEditor.frmMain.getBufferStrategy();
112 this.g = bufferStrategy.getDrawGraphics();
113 this.mapBuffer = gc.createCompatibleImage(800, 600);
114 this.mapG = this.mapBuffer.getGraphics();
[a10d422]115 Utils.init(gc, RUNNING_FROM_JAR);
[ebd3538]116 this.state = State.Main;
117 this.auxState = AuxState.None;
118 this.done = false;
119 this.showFps = false;
120 this.frameCount = 0;
121 this.lastFrameCount = 0;
122 this.lastFpsUpdate = System.nanoTime();
123 this.setIndex = 0;
124 this.curTile = null;
125 this.curLevel = 0;
126 this.selectedObject = null;
127 this.selectedCreature = null;
128 this.curCr = null;
129 this.bounds = true;
130 this.passable = false;
131 this.showCr = false;
132 this.highLevelPlacement = false;
133 this.loadMap();
134 this.loadCreatures();
135 this.loadGUI();
136 this.started = true;
137 while (!this.done) {
138 this.g = bufferStrategy.getDrawGraphics();
139 this.handleEvents();
140 this.render(this.g);
141 this.g.dispose();
142 bufferStrategy.show();
143 ++this.frameCount;
144 if (System.nanoTime() - 1000000000L >= this.lastFpsUpdate) {
145 this.lastFpsUpdate = System.nanoTime();
146 this.lastFrameCount = this.frameCount;
147 this.frameCount = 0;
148 }
149 }
[a10d422]150 } catch (Exception e) {
[ebd3538]151 e.printStackTrace();
152 return;
[a10d422]153 } finally {
[ebd3538]154 device.setFullScreenWindow(null);
155 }
156 device.setFullScreenWindow(null);
157 }
158
159 private void loadGUI() {
160 this.font11 = new Font("Arial", 0, 11);
161 this.font12 = new Font("Arial", 0, 12);
162 this.font24 = new Font("Arial", 0, 24);
163 this.fontTT = new Font("Courier New", 0, 11);
164 (this.wndMain = new Window("main", 0, 0, 1024, 768, true)).add(new Label("title", 256, 0, 512, 95, "Lost Haven Map Editor", this.font24));
165 this.wndMain.add(new Button("new map", 668, 140, 200, 40, "New Map", this.font12, Align.Center));
166 this.wndMain.add(new Button("load map", 668, 240, 200, 40, "Load Map", this.font12, Align.Center));
167 this.wndMain.add(new Button("quit", 668, 340, 200, 40, "Quit", this.font12, Align.Center));
168 this.wndEdit = new Window("edit", 0, 0, 1024, 768, true);
169 (this.wndEditPalette = new Window("editPalette", 0, 0, 203, 767, false)).add(new Label("selected", 62, 10, 80, 20, "Selected Tile:", this.font12));
170 this.wndEditPalette.add(new Label("level", 62, 125, 80, 20, "Level " + this.curLevel, this.font12));
171 this.wndEditPalette.add(new Label("switch", 62, 175, 80, 20, "Switch", this.font12));
172 this.wndEdit.add(this.wndEditPalette);
173 (this.wndEditOptions = new Window("editOptions", 203, 0, 820, 147, false)).add(new Textbox("name", 65, 25, 160, 20, "Name:", this.font12, false));
174 this.wndEditOptions.add(new Textbox("x", 65, 65, 80, 20, "Width:", this.font12, false));
175 this.wndEditOptions.add(new Textbox("y", 65, 105, 80, 20, "Height:", this.font12, false));
176 this.wndEditOptions.add(new Button("new", 400, 108, 80, 20, "New", this.font12));
177 this.wndEditOptions.add(new Button("save", 500, 108, 80, 20, "Save", this.font12));
178 this.wndEditOptions.add(new Button("load", 600, 108, 80, 20, "Load", this.font12));
179 this.wndEditOptions.add(new Button("back", 700, 108, 80, 20, "Back", this.font12));
180 this.wndEdit.add(this.wndEditOptions);
181 this.horBar = new ScrollBar("horScroll", 204, 748, 800, 19, 8, true);
182 this.verBar = new ScrollBar("verScroll", 1004, 147, 19, 601, 8, false);
183 this.wndEdit.add(this.horBar);
184 this.wndEdit.add(this.verBar);
185 (this.wndMessage = new Window("message", 402, 202, 220, 160)).add(new Label("label", 20, 15, 180, 12, "none", this.font12));
186 this.wndMessage.add(new Button("button", 70, 115, 80, 30, "OK", this.font12));
187 (this.wndLoadMap = new Window("load map", 402, 202, 220, 160)).add(new Textbox("map_name", 80, 15, 120, 20, "Map Name:", this.font12, false));
188 this.wndLoadMap.add(new Button("load", 20, 115, 80, 30, "Load", this.font12, Align.Center));
189 this.wndLoadMap.add(new Button("back", 120, 115, 80, 30, "Back", this.font12, Align.Center));
190 this.wndDiagnostics = new Window("diagnostics", 0, 0, 80, 40, true);
191 }
192
193 private void loadMap() {
194 this.imageMap = new HashMap<Integer, Tile>();
195 this.crMap = new HashMap<Integer, Creature>();
196 this.tileBreakdowns = new HashMap<Integer, GroundType[]>();
197 this.tileByBreakdown = (ArrayList<Tile>[])new ArrayList[81];
198 (this.imgSets = new ArrayList<ArrayList<Tile>>()).add(new ArrayList<Tile>());
199 this.imgSets.add(new ArrayList<Tile>());
200 this.imgSets.add(new ArrayList<Tile>());
201 this.imgSets.add(new ArrayList<Tile>());
202 this.imgSets.add(new ArrayList<Tile>());
203 this.imgSets.add(new ArrayList<Tile>());
204 this.imgSets.add(new ArrayList<Tile>());
205 for (int x = 0; x < 16; ++x) {
206 this.imgSets.get(0).add(new Tile(new MapImage(new DynamicImage("tiles/ground" + (x + 1) + ".png"), MapType.Ground)));
207 this.imageMap.put(MapImage.lastKey, this.imgSets.get(0).get(x));
208 }
209 for (int x = 0; x < 12; ++x) {
210 this.imgSets.get(1).add(new Tile(new MapImage(new DynamicImage("tiles/cliff" + (x + 1) + ".png"), MapType.Ground)));
211 this.imageMap.put(MapImage.lastKey, this.imgSets.get(1).get(x));
212 }
213 int last = 15;
214 this.imageMap.get(last + 1).getImg().setDrawOffset(-40, 0);
215 this.imageMap.get(last + 3).getImg().setDrawOffset(-40, 0);
216 this.imageMap.get(last + 5).getImg().setDrawOffset(0, -40);
217 this.imageMap.get(last + 6).getImg().setDrawOffset(0, -40);
218 this.imageMap.get(last + 7).getImg().setDrawOffset(-40, -40);
219 this.imageMap.get(last + 8).getImg().setDrawOffset(0, -40);
220 this.imageMap.get(last + 9).getImg().setDrawOffset(-40, 0);
221 this.imageMap.get(last + 1).getImg().setSortOffset(-18, 0);
222 this.imageMap.get(last + 2).getImg().setSortOffset(62, 0);
223 this.imageMap.get(last + 3).getImg().setSortOffset(-18, 0);
224 this.imageMap.get(last + 4).getImg().setSortOffset(62, 0);
225 this.imageMap.get(last + 5).getImg().setSortOffset(0, 32);
226 this.imageMap.get(last + 6).getImg().setSortOffset(0, 32);
227 this.imageMap.get(last + 7).getImg().setSortOffset(-18, 32);
228 this.imageMap.get(last + 8).getImg().setSortOffset(62, 32);
229 this.imageMap.get(last + 9).getImg().setSortOffset(-18, 75);
230 this.imageMap.get(last + 10).getImg().setSortOffset(62, 75);
231 this.imageMap.get(last + 11).getImg().setSortOffset(0, 75);
232 this.imageMap.get(last + 12).getImg().setSortOffset(0, 75);
233 this.imageMap.get(last + 1).setBound(new Bound(new Rectangle2D.Double(20.0, 0.0, 15.0, 40.0)));
234 this.imageMap.get(last + 2).setBound(new Bound(new Rectangle2D.Double(45.0, 0.0, 21.0, 40.0)));
235 this.imageMap.get(last + 3).setBound(new Bound(new Rectangle2D.Double(20.0, 0.0, 15.0, 40.0)));
236 this.imageMap.get(last + 4).setBound(new Bound(new Rectangle2D.Double(45.0, 0.0, 21.0, 40.0)));
237 this.imageMap.get(last + 5).setBound(new Bound(new Rectangle2D.Double(0.0, 20.0, 40.0, 15.0)));
238 this.imageMap.get(last + 6).setBound(new Bound(new Rectangle2D.Double(0.0, 20.0, 40.0, 15.0)));
239 this.imageMap.get(last + 11).setBound(new Bound(new Rectangle2D.Double(0.0, 45.0, 40.0, 27.0)));
240 this.imageMap.get(last + 12).setBound(new Bound(new Rectangle2D.Double(0.0, 45.0, 40.0, 27.0)));
241 Bound b1 = new Bound(new Ellipse2D.Double(20.0, -48.0, 120.0, 120.0));
242 b1.intersect(new Bound(new Rectangle2D.Double(20.0, 12.0, 60.0, 60.0)));
243 b1.add(new Bound(new Rectangle2D.Double(20.0, 0.0, 15.0, 12.0)));
244 b1.subtract(new Bound(new Ellipse2D.Double(35.0, -21.0, 66.0, 66.0)));
245 b1.subtract(new Bound(new Rectangle2D.Double(68.0, 35.0, 12.0, 10.0)));
246 final Bound b2 = new Bound(new Ellipse2D.Double(-66.0, -60.0, 132.0, 132.0));
247 b2.intersect(new Bound(new Rectangle2D.Double(0.0, 6.0, 66.0, 66.0)));
248 b2.add(new Bound(new Rectangle2D.Double(45.0, 0.0, 21.0, 6.0)));
249 b2.subtract(new Bound(new Ellipse2D.Double(-45.0, -45.0, 90.0, 90.0)));
250 final Bound b3 = new Bound(new Ellipse2D.Double(-54.0, 20.0, 120.0, 120.0));
251 b3.intersect(new Bound(new Rectangle2D.Double(6.0, 20.0, 60.0, 60.0)));
252 b3.add(new Bound(new Rectangle2D.Double(0.0, 20.0, 6.0, 15.0)));
253 b3.subtract(new Bound(new Ellipse2D.Double(-33.0, 35.0, 78.0, 78.0)));
254 b3.subtract(new Bound(new Rectangle2D.Double(39.0, 74.0, 6.0, 6.0)));
255 final Bound b4 = new Bound(new Ellipse2D.Double(20.0, 20.0, 120.0, 120.0));
256 b4.intersect(new Bound(new Rectangle2D.Double(20.0, 20.0, 60.0, 60.0)));
257 b4.subtract(new Bound(new Ellipse2D.Double(35.0, 35.0, 90.0, 90.0)));
258 this.imageMap.get(last + 7).setBound(b4);
259 this.imageMap.get(last + 8).setBound(b3);
260 this.imageMap.get(last + 9).setBound(b1);
261 this.imageMap.get(last + 10).setBound(b2);
262 this.imageMap.get(last + 7).getImg().setNeedsBase(true);
263 this.imageMap.get(last + 8).getImg().setNeedsBase(true);
264 for (int x2 = 0; x2 < 6; ++x2) {
265 this.imgSets.get(2).add(new Tile(new MapImage(new DynamicImage("tiles/rock" + (x2 + 1) + ".png"), MapType.Ground)));
266 this.imageMap.put(MapImage.lastKey, this.imgSets.get(2).get(x2));
267 }
268 last = 33;
269 this.imgSets.get(3).add(new Tile(new MapImage(new DynamicImage("objects/rock1.png"), MapType.Object)));
270 this.imageMap.put(MapImage.lastKey, this.imgSets.get(3).get(0));
271 this.imgSets.get(3).add(new Tile(new MapImage(new DynamicImage("objects/rock2.png"), MapType.Object)));
272 this.imageMap.put(MapImage.lastKey, this.imgSets.get(3).get(1));
273 this.imgSets.get(3).add(new Tile(new MapImage(new DynamicImage("objects/tree1.png"), MapType.Object)));
274 this.imageMap.put(MapImage.lastKey, this.imgSets.get(3).get(2));
275 this.imgSets.get(3).add(new Tile(new MapImage(new DynamicImage("objects/tree2.png"), MapType.Object)));
276 this.imageMap.put(MapImage.lastKey, this.imgSets.get(3).get(3));
277 this.imageMap.get(last + 1).getImg().setDrawOffset(-22, -28);
278 this.imageMap.get(last + 2).getImg().setDrawOffset(-16, -50);
279 this.imageMap.get(last + 3).getImg().setDrawOffset(-36, -100);
280 this.imageMap.get(last + 4).getImg().setDrawOffset(-58, -83);
281 this.imageMap.get(last + 1).setBound(new Bound(new Ellipse2D.Double(-6.0, -6.0, 12.0, 12.0)));
282 this.imageMap.get(last + 2).setBound(new Bound(new Ellipse2D.Double(-6.0, -6.0, 12.0, 12.0)));
283 this.imageMap.get(last + 3).setBound(new Bound(new Ellipse2D.Double(-16.0, -16.0, 32.0, 32.0)));
284 this.imageMap.get(last + 4).setBound(new Bound(new Ellipse2D.Double(-30.0, -30.0, 60.0, 60.0)));
285 for (int x2 = 0; x2 < 19; ++x2) {
286 this.imgSets.get(4).add(new Tile(new MapImage(new DynamicImage("tiles/pave" + (x2 + 1) + ".png"), MapType.Ground)));
287 this.imageMap.put(MapImage.lastKey, this.imgSets.get(4).get(x2));
288 }
289 for (int x2 = 0; x2 < 13; ++x2) {
290 this.imgSets.get(5).add(new Tile(new MapImage(new DynamicImage("tiles/fence" + (x2 + 1) + ".png"), MapType.Structure)));
291 this.imageMap.put(MapImage.lastKey, this.imgSets.get(5).get(x2));
292 }
293 last = 56;
294 this.imageMap.get(last + 1).getImg().setDrawOffset(-19, -85);
295 this.imageMap.get(last + 2).getImg().setDrawOffset(-20, -85);
296 this.imageMap.get(last + 3).getImg().setDrawOffset(-20, -97);
297 this.imageMap.get(last + 4).getImg().setDrawOffset(-16, -72);
298 this.imageMap.get(last + 5).getImg().setDrawOffset(-20, -81);
299 this.imageMap.get(last + 6).getImg().setDrawOffset(-13, -45);
300 this.imageMap.get(last + 7).getImg().setDrawOffset(-20, -67);
301 this.imageMap.get(last + 8).getImg().setDrawOffset(-13, -51);
302 this.imageMap.get(last + 9).getImg().setDrawOffset(-17, -95);
303 this.imageMap.get(last + 10).getImg().setDrawOffset(-22, -95);
304 this.imageMap.get(last + 11).getImg().setDrawOffset(-19, -86);
305 this.imageMap.get(last + 12).getImg().setDrawOffset(-20, -67);
306 this.imageMap.get(last + 13).getImg().setDrawOffset(-20, -84);
307 b1 = new Bound(new Rectangle2D.Double(-14.0, -40.0, 34.0, 40.0));
308 b1.add(new Bound(new Rectangle2D.Double(-20.0, -25.0, 6.0, 25.0)));
309 this.imageMap.get(last + 1).setBound(new Bound(new Rectangle2D.Double(-14.0, -25.0, 34.0, 25.0)));
310 this.imageMap.get(last + 2).setBound(new Bound(new Rectangle2D.Double(-20.0, -25.0, 40.0, 25.0)));
311 this.imageMap.get(last + 3).setBound(new Bound(new Rectangle2D.Double(-20.0, -25.0, 40.0, 25.0)));
312 this.imageMap.get(last + 5).setBound(b1);
313 this.imageMap.get(last + 6).setBound(new Bound(new Rectangle2D.Double(-14.0, -40.0, 34.0, 40.0)));
314 this.imageMap.get(last + 7).setBound(new Bound(new Rectangle2D.Double(-20.0, -25.0, 40.0, 25.0)));
315 this.imageMap.get(last + 8).setBound(new Bound(new Rectangle2D.Double(-14.0, -40.0, 34.0, 40.0)));
316 this.imageMap.get(last + 9).setBound(new Bound(new Rectangle2D.Double(-20.0, -25.0, 40.0, 25.0)));
317 this.imageMap.get(last + 10).setBound(new Bound(new Rectangle2D.Double(-20.0, -25.0, 40.0, 25.0)));
318 this.imageMap.get(last + 11).setBound(new Bound(new Rectangle2D.Double(-14.0, -40.0, 34.0, 40.0)));
319 this.imageMap.get(last + 12).setBound(new Bound(new Rectangle2D.Double(-20.0, -25.0, 40.0, 25.0)));
320 this.imgSets.get(6).add(this.imgSets.get(0).get(0));
321 this.imgSets.get(6).add(this.imgSets.get(0).get(9));
322 this.imgSets.get(6).add(this.imgSets.get(4).get(0));
323 this.setBreakdown(this.imgSets.get(0).get(0), GroundType.Grass, GroundType.Grass, GroundType.Grass, GroundType.Grass);
324 this.setBreakdown(this.imgSets.get(0).get(1), GroundType.Dirt, GroundType.Dirt, GroundType.Dirt, GroundType.Grass);
325 this.setBreakdown(this.imgSets.get(0).get(2), GroundType.Dirt, GroundType.Dirt, GroundType.Grass, GroundType.Dirt);
326 this.setBreakdown(this.imgSets.get(0).get(3), GroundType.Dirt, GroundType.Grass, GroundType.Grass, GroundType.Grass);
327 this.setBreakdown(this.imgSets.get(0).get(4), GroundType.Dirt, GroundType.Dirt, GroundType.Grass, GroundType.Grass);
328 this.setBreakdown(this.imgSets.get(0).get(5), GroundType.Grass, GroundType.Dirt, GroundType.Grass, GroundType.Grass);
329 this.setBreakdown(this.imgSets.get(0).get(6), GroundType.Dirt, GroundType.Grass, GroundType.Dirt, GroundType.Dirt);
330 this.setBreakdown(this.imgSets.get(0).get(7), GroundType.Grass, GroundType.Dirt, GroundType.Dirt, GroundType.Dirt);
331 this.setBreakdown(this.imgSets.get(0).get(8), GroundType.Dirt, GroundType.Grass, GroundType.Dirt, GroundType.Grass);
332 this.setBreakdown(this.imgSets.get(0).get(9), GroundType.Dirt, GroundType.Dirt, GroundType.Dirt, GroundType.Dirt);
333 this.setBreakdown(this.imgSets.get(0).get(10), GroundType.Grass, GroundType.Dirt, GroundType.Grass, GroundType.Dirt);
334 this.setBreakdown(this.imgSets.get(0).get(11), GroundType.Grass, GroundType.Grass, GroundType.Dirt, GroundType.Grass);
335 this.setBreakdown(this.imgSets.get(0).get(12), GroundType.Grass, GroundType.Grass, GroundType.Dirt, GroundType.Dirt);
336 this.setBreakdown(this.imgSets.get(0).get(13), GroundType.Grass, GroundType.Grass, GroundType.Grass, GroundType.Dirt);
337 this.setBreakdown(this.imgSets.get(0).get(14), GroundType.Dirt, GroundType.Grass, GroundType.Grass, GroundType.Dirt);
338 this.setBreakdown(this.imgSets.get(0).get(15), GroundType.Grass, GroundType.Dirt, GroundType.Dirt, GroundType.Grass);
339 this.setBreakdown(this.imgSets.get(4).get(0), GroundType.Stone, GroundType.Stone, GroundType.Stone, GroundType.Stone);
340 this.setBreakdown(this.imgSets.get(4).get(1), GroundType.Stone, GroundType.Stone, GroundType.Stone, GroundType.Stone);
341 this.setBreakdown(this.imgSets.get(4).get(2), GroundType.Stone, GroundType.Stone, GroundType.Stone, GroundType.Stone);
342 this.setBreakdown(this.imgSets.get(4).get(3), GroundType.Stone, GroundType.Stone, GroundType.Stone, GroundType.Stone);
343 this.setBreakdown(this.imgSets.get(4).get(4), GroundType.Dirt, GroundType.Dirt, GroundType.Dirt, GroundType.Stone);
344 this.setBreakdown(this.imgSets.get(4).get(5), GroundType.Dirt, GroundType.Dirt, GroundType.Stone, GroundType.Stone);
345 this.setBreakdown(this.imgSets.get(4).get(6), GroundType.Dirt, GroundType.Dirt, GroundType.Stone, GroundType.Dirt);
346 this.setBreakdown(this.imgSets.get(4).get(7), GroundType.Dirt, GroundType.Stone, GroundType.Stone, GroundType.Stone);
347 this.setBreakdown(this.imgSets.get(4).get(8), GroundType.Stone, GroundType.Dirt, GroundType.Stone, GroundType.Stone);
348 this.setBreakdown(this.imgSets.get(4).get(9), GroundType.Dirt, GroundType.Stone, GroundType.Dirt, GroundType.Stone);
349 this.setBreakdown(this.imgSets.get(4).get(10), GroundType.Stone, GroundType.Stone, GroundType.Stone, GroundType.Stone);
350 this.setBreakdown(this.imgSets.get(4).get(11), GroundType.Stone, GroundType.Dirt, GroundType.Stone, GroundType.Dirt);
351 this.setBreakdown(this.imgSets.get(4).get(12), GroundType.Stone, GroundType.Stone, GroundType.Dirt, GroundType.Stone);
352 this.setBreakdown(this.imgSets.get(4).get(13), GroundType.Stone, GroundType.Stone, GroundType.Stone, GroundType.Dirt);
353 this.setBreakdown(this.imgSets.get(4).get(14), GroundType.Dirt, GroundType.Stone, GroundType.Dirt, GroundType.Dirt);
354 this.setBreakdown(this.imgSets.get(4).get(15), GroundType.Stone, GroundType.Stone, GroundType.Dirt, GroundType.Dirt);
355 this.setBreakdown(this.imgSets.get(4).get(16), GroundType.Stone, GroundType.Dirt, GroundType.Dirt, GroundType.Dirt);
356 this.setBreakdown(this.imgSets.get(4).get(17), GroundType.Dirt, GroundType.Stone, GroundType.Stone, GroundType.Dirt);
357 this.setBreakdown(this.imgSets.get(4).get(18), GroundType.Stone, GroundType.Dirt, GroundType.Dirt, GroundType.Stone);
358 this.setIndex = 0;
359 this.activeSet = this.imgSets.get(this.setIndex);
360 this.curTile = this.activeSet.get(0);
361 }
362
363 private void loadCreatures() {
364 this.crSet = new ArrayList<Creature>();
365 Utils.loadCreatures(this.crMap, this.crSet);
366 this.curCr = this.crSet.get(0);
367 }
368
369 private boolean createNewMap() {
370 final String name = ((Textbox)this.wndEditOptions.getMember("name")).getText();
371 final String strX = ((Textbox)this.wndEditOptions.getMember("x")).getText();
372 final String strY = ((Textbox)this.wndEditOptions.getMember("y")).getText();
373 boolean passed = false;
374 if (name.equals("")) {
375 this.showMessage("You didn't enter a name for the map");
376 }
377 else if (strX.equals("")) {
378 this.showMessage("You didn't enter a width");
379 }
380 else if (strY.equals("")) {
381 this.showMessage("You didn't enter a height");
382 }
383 else {
384 int x = 0;
385 int y = 0;
386 passed = true;
387 try {
388 x = Integer.parseInt(strX);
389 if (x <= 0) {
390 passed = false;
391 this.showMessage("The width must be positive");
392 }
393 }
394 catch (NumberFormatException nfe) {
395 passed = false;
396 this.showMessage("Your width is not a valid integer");
397 }
398 try {
399 y = Integer.parseInt(strY);
400 if (y <= 0) {
401 passed = false;
402 this.showMessage("The height must be positive");
403 }
404 }
405 catch (NumberFormatException nfe) {
406 passed = false;
407 this.showMessage("Your height is not a valid integer");
408 }
409 if (passed) {
410 this.playerLoc = new Point(400, 300);
411 MapEditor.map = new Map(x, y, this.imgSets.get(0).get(0), this.imgSets.get(0).get(9));
412 this.mapName = name;
413 this.horBar.setSize(800 * this.horBar.getMaxSize() / (MapEditor.map.getWidth() * 40 + 800));
414 this.verBar.setSize(600 * this.verBar.getMaxSize() / (MapEditor.map.getHeight() * 40 + 600));
415 this.horBar.setPosition(this.horBar.getMaxSize() * this.playerLoc.x / (40 * MapEditor.map.getWidth() + 800));
416 this.verBar.setPosition(this.verBar.getMaxSize() * this.playerLoc.y / (40 * MapEditor.map.getHeight() + 600));
417 this.showMessage(MapEditor.map.message);
418 }
419 }
420 return passed;
421 }
422
423 private ArrayList<Tile> getTilesByBreakdown(final GroundType[] t) {
424 return this.tileByBreakdown[t[0].ordinal() * 27 + t[1].ordinal() * 9 + t[2].ordinal() * 3 + t[3].ordinal()];
425 }
426
427 private GroundType[] getBreakdown(final Tile t) {
428 return this.tileBreakdowns.get(t.getImg().key);
429 }
430
431 private void setBreakdown(final Tile t, final GroundType t1, final GroundType t2, final GroundType t3, final GroundType t4) {
432 final GroundType[] type = { t1, t2, t3, t4 };
433 this.tileBreakdowns.put(t.getImg().key, type);
434 final int index = t1.ordinal() * 27 + t2.ordinal() * 9 + t3.ordinal() * 3 + t4.ordinal();
435 if (this.tileByBreakdown[index] == null) {
436 this.tileByBreakdown[index] = new ArrayList<Tile>();
437 }
438 this.tileByBreakdown[index].add(t);
439 }
440
441 private synchronized void handleEvents() {
442 switch (this.state) {
443 }
444 }
445
446 private synchronized void render(final Graphics g) {
447 g.setColor(Color.black);
448 g.fillRect(0, 0, 1024, 768);
449 this.mapG.setColor(Color.black);
450 this.mapG.fillRect(0, 0, 800, 600);
451 switch (this.state) {
452 case Main: {
453 this.wndMain.draw(g);
454 break;
455 }
456 case Edit: {
457 this.wndEdit.draw(g);
458 this.drawPalette(g);
459 MapEditor.map.draw(this.mapG, this.playerLoc, this.bounds, this.passable);
460 g.drawImage(this.mapBuffer, 204, 148, null);
461 if (this.selectedObject != null) {
462 g.setColor(Color.blue);
463 final int selectedX = 400 - this.playerLoc.x + 204 + this.selectedObject.loc.x;
464 final int selectedY = 300 - this.playerLoc.y + 148 + this.selectedObject.loc.y;
465 g.drawOval(selectedX - 25, selectedY - 33, 50, 50);
466 }
467 if (this.selectedCreature != null) {
468 g.setColor(Color.blue);
469 final int selectedX = 400 - this.playerLoc.x + 204 + this.selectedCreature.loc.x;
470 final int selectedY = 300 - this.playerLoc.y + 148 + this.selectedCreature.loc.y;
471 g.drawOval(selectedX - 25, selectedY - 33, 50, 50);
472 break;
473 }
474 break;
475 }
476 }
477 switch (this.auxState) {
478 case MsgBox: {
479 this.wndMessage.draw(g);
480 break;
481 }
482 case LoadMap: {
483 this.wndLoadMap.draw(g);
484 break;
485 }
486 }
487 if (this.showFps) {
488 this.drawDiagnostics(g);
489 }
490 }
491
492 private void drawPalette(final Graphics g) {
493 if (this.showCr) {
494 final DynamicImage curImg = this.curCr.getModel().getAnimation(Direction.South, Action.Standing).frames.get(0);
495 curImg.draw(g, (204 - curImg.getWidth()) / 2, 40);
496 int yOffset = 215;
497 for (int x = 0; x < this.crSet.size(); x += 2) {
498 final DynamicImage img1 = this.crSet.get(x).getModel().getAnimation(Direction.South, Action.Standing).frames.get(0);
499 img1.draw(g, 97 - img1.getWidth(), yOffset);
500 if (this.crSet.size() != x + 1) {
501 final DynamicImage img2 = this.crSet.get(x + 1).getModel().getAnimation(Direction.South, Action.Standing).frames.get(0);
502 img2.draw(g, 107, yOffset);
503 yOffset += Math.max(img1.getHeight(), img2.getHeight()) + 10;
504 }
505 }
506 this.drawArrows(g, 0, this.curLevel, 5);
507 this.drawArrows(g, 50, this.imgSets.size(), this.imgSets.size() - 1);
508 return;
509 }
510 this.curTile.getImg().drawNoOffset(g, (204 - this.curTile.getImg().getWidth()) / 2, 40);
511 int yOffset2 = 215;
512 for (int x2 = 0; x2 < this.activeSet.size(); x2 += 2) {
513 this.activeSet.get(x2).getImg().drawNoOffset(g, 97 - this.activeSet.get(x2).getImg().getWidth(), yOffset2);
514 if (this.activeSet.size() != x2 + 1) {
515 this.activeSet.get(x2 + 1).getImg().drawNoOffset(g, 107, yOffset2);
516 yOffset2 += Math.max(this.activeSet.get(x2).getImg().getHeight(), this.activeSet.get(x2 + 1).getImg().getHeight()) + 10;
517 }
518 }
519 this.drawArrows(g, 0, this.curLevel, 5);
520 this.drawArrows(g, 50, this.setIndex, this.imgSets.size() - 1);
521 }
522
523 private void drawArrows(final Graphics g, final int arrowShift, final int curIndex, final int maxIndex) {
524 if (curIndex > 0) {
525 g.setColor(Color.yellow);
526 }
527 else {
528 g.setColor(new Color(127, 127, 0));
529 }
530 g.drawLine(67, 146 + arrowShift, 87, 146 + arrowShift);
531 g.drawLine(67, 152 + arrowShift, 87, 152 + arrowShift);
532 g.drawLine(87, 146 + arrowShift, 87, 152 + arrowShift);
533 g.drawLine(60, 149 + arrowShift, 67, 142 + arrowShift);
534 g.drawLine(67, 142 + arrowShift, 67, 146 + arrowShift);
535 g.drawLine(60, 149 + arrowShift, 67, 156 + arrowShift);
536 g.drawLine(67, 156 + arrowShift, 67, 152 + arrowShift);
537 if (curIndex < maxIndex) {
538 g.setColor(Color.yellow);
539 }
540 else {
541 g.setColor(new Color(127, 127, 0));
542 }
543 g.drawLine(117, 146 + arrowShift, 137, 146 + arrowShift);
544 g.drawLine(117, 152 + arrowShift, 137, 152 + arrowShift);
545 g.drawLine(117, 146 + arrowShift, 117, 152 + arrowShift);
546 g.drawLine(144, 149 + arrowShift, 137, 142 + arrowShift);
547 g.drawLine(137, 142 + arrowShift, 137, 146 + arrowShift);
548 g.drawLine(144, 149 + arrowShift, 137, 156 + arrowShift);
549 g.drawLine(137, 156 + arrowShift, 137, 152 + arrowShift);
550 }
551
552 private void drawDiagnostics(final Graphics g) {
553 this.wndDiagnostics.draw(g);
554 g.setColor(Color.green);
555 g.setFont(this.fontTT);
556 g.drawString("FPS: " + this.lastFrameCount, 0, 15);
557 }
558
559 private boolean changeLevel(final int x, final int y) {
560 if (142 < y && y < 156) {
561 if (60 <= x && x <= 87) {
562 if (this.curLevel > 0) {
563 --this.curLevel;
564 ((Label)this.wndEditPalette.getMember("level")).setText("Level " + this.curLevel);
565 }
566 return true;
567 }
568 if (117 <= x && x <= 144) {
569 if (this.curLevel < 5) {
570 ++this.curLevel;
571 ((Label)this.wndEditPalette.getMember("level")).setText("Level " + this.curLevel);
572 }
573 return true;
574 }
575 }
576 return false;
577 }
578
579 private boolean changeSet(final int x, final int y) {
580 if (192 < y && y < 206) {
581 if (60 <= x && x <= 87) {
582 if (this.setIndex > 0) {
583 --this.setIndex;
584 this.activeSet = this.imgSets.get(this.setIndex);
585 this.showCr = false;
586 }
587 return true;
588 }
589 if (117 <= x && x <= 144) {
590 if (this.setIndex < this.imgSets.size() - 1) {
591 ++this.setIndex;
592 this.activeSet = this.imgSets.get(this.setIndex);
593 }
594 else if (this.setIndex == this.imgSets.size() - 1) {
595 ++this.setIndex;
596 this.showCr = true;
597 }
598 return true;
599 }
600 }
601 return false;
602 }
603
604 private boolean selectNewImage(final int x, final int y) {
605 boolean foundImage = false;
606 int index = 0;
607 int yOffset = 215;
608 if (this.showCr) {
609 DynamicImage img2 = null;
610 while (index < this.crSet.size() && !foundImage) {
611 final DynamicImage img3 = this.crSet.get(index).getModel().getAnimation(Direction.South, Action.Standing).frames.get(0);
612 int rowHeight;
613 if (this.crSet.size() == index + 1) {
614 rowHeight = img3.getHeight();
615 }
616 else {
617 img2 = this.crSet.get(index + 1).getModel().getAnimation(Direction.South, Action.Standing).frames.get(0);
618 rowHeight = Math.max(img3.getHeight(), img2.getHeight());
619 }
620 if (yOffset <= y && y < yOffset + rowHeight) {
621 if (97 - img3.getWidth() <= x && x < 97) {
622 if (y < yOffset + img3.getHeight()) {
623 foundImage = true;
624 }
625 }
626 else if (107 <= x && this.crSet.size() > index + 1 && x < 107 + img2.getWidth() && y < yOffset + img2.getHeight()) {
627 foundImage = true;
628 ++index;
629 }
630 }
631 else {
632 yOffset += rowHeight + 10;
633 }
634 if (!foundImage) {
635 index += 2;
636 }
637 }
638 if (foundImage) {
639 this.curCr = this.crSet.get(index);
640 }
641 return foundImage;
642 }
643 while (index < this.activeSet.size() && !foundImage) {
644 int rowHeight;
645 if (this.activeSet.size() == index + 1) {
646 rowHeight = this.activeSet.get(index).getImg().getHeight();
647 }
648 else {
649 rowHeight = Math.max(this.activeSet.get(index).getImg().getHeight(), this.activeSet.get(index + 1).getImg().getHeight());
650 }
651 if (yOffset <= y && y < yOffset + rowHeight) {
652 if (97 - this.activeSet.get(index).getImg().getWidth() <= x && x < 97) {
653 if (y < yOffset + this.activeSet.get(index).getImg().getHeight()) {
654 foundImage = true;
655 }
656 }
657 else if (107 <= x && this.activeSet.size() > index + 1 && x < 107 + this.activeSet.get(index + 1).getImg().getWidth() && y < yOffset + this.activeSet.get(index + 1).getImg().getHeight()) {
658 foundImage = true;
659 ++index;
660 }
661 }
662 else {
663 yOffset += rowHeight + 10;
664 }
665 if (!foundImage) {
666 index += 2;
667 }
668 }
669 if (foundImage) {
670 this.curTile = this.activeSet.get(index);
671 }
672 this.highLevelPlacement = (this.activeSet == this.imgSets.get(6));
673 return foundImage;
674 }
675
676 private void placeTile(int x, int y) {
677 x -= 204;
678 y -= 148;
679 if (x >= 0 && x < 800 && y >= 0 && y < 600) {
680 x += this.playerLoc.x - 400;
681 y += this.playerLoc.y - 300;
682 if (x >= 0 && x < 40 * MapEditor.map.getWidth() && y >= 0 && y < 40 * MapEditor.map.getHeight()) {
683 if (this.showCr) {
684 MapEditor.map.addCreature(x / 40, y / 40, this.curCr.copy(new Point(x, y)));
685 }
686 else if (this.curTile.getImg().getType() == MapType.Ground) {
687 if (this.highLevelPlacement) {
688 this.placeGroup(this.curTile, x, y);
689 }
690 else {
691 MapEditor.map.setGround(x / 40, y / 40, new Tile(this.curTile, x - x % 40, y - y % 40, this.curLevel));
692 }
693 }
694 else if (this.curTile.getImg().getType() == MapType.Object) {
695 MapEditor.map.addObject(x / 40, y / 40, new Tile(this.curTile, x, y, this.curLevel));
696 System.out.println("place at (" + x / 40 + ", " + y / 40 + ")");
697 }
698 else if (this.curTile.getImg().getType() == MapType.Structure) {
699 MapEditor.map.addObject(x / 40, y / 40, new Tile(this.curTile, x - x % 40 + 20, y - y % 40 + 39, this.curLevel));
700 }
701 }
702 }
703 }
704
705 private boolean containsGroundType(final GroundType[] arr, final GroundType t) {
706 return arr[0] == t || arr[1] == t || arr[2] == t || arr[3] == t;
707 }
708
709 private void placeGroup(final Tile tile, final int x, final int y) {
710 final int xMap = x / 40;
711 final int yMap = y / 40;
[6d094e4]712 final ArrayList<Tile>[] tiles = new ArrayList[9];
[ebd3538]713 final GroundType[] middle = this.getBreakdown(tile);
714 final GroundType[] breakdown = new GroundType[4];
715 if (this.containsGroundType(middle, GroundType.Stone)) {
716 if (xMap > 0 && yMap > 0 && this.containsGroundType(this.getBreakdown(MapEditor.map.getLoc(xMap - 1, yMap - 1).getGround()), GroundType.Grass)) {
717 this.placeGroup(this.imgSets.get(6).get(1), x - 40, y - 40);
718 }
719 if (yMap > 0 && this.containsGroundType(this.getBreakdown(MapEditor.map.getLoc(xMap, yMap - 1).getGround()), GroundType.Grass)) {
720 this.placeGroup(this.imgSets.get(6).get(1), x, y - 40);
721 }
722 if (xMap < MapEditor.map.getWidth() - 1 && yMap > 0 && this.containsGroundType(this.getBreakdown(MapEditor.map.getLoc(xMap + 1, yMap - 1).getGround()), GroundType.Grass)) {
723 this.placeGroup(this.imgSets.get(6).get(1), x + 40, y - 40);
724 }
725 if (xMap < MapEditor.map.getWidth() - 1 && this.containsGroundType(this.getBreakdown(MapEditor.map.getLoc(xMap + 1, yMap).getGround()), GroundType.Grass)) {
726 this.placeGroup(this.imgSets.get(6).get(1), x + 40, y);
727 }
728 if (xMap < MapEditor.map.getWidth() - 1 && yMap < MapEditor.map.getHeight() - 1 && this.containsGroundType(this.getBreakdown(MapEditor.map.getLoc(xMap + 1, yMap + 1).getGround()), GroundType.Grass)) {
729 this.placeGroup(this.imgSets.get(6).get(1), x + 40, y + 40);
730 }
731 if (yMap < MapEditor.map.getHeight() - 1 && this.containsGroundType(this.getBreakdown(MapEditor.map.getLoc(xMap, yMap + 1).getGround()), GroundType.Grass)) {
732 this.placeGroup(this.imgSets.get(6).get(1), x, y + 40);
733 }
734 if (xMap > 0 && yMap < MapEditor.map.getHeight() - 1 && this.containsGroundType(this.getBreakdown(MapEditor.map.getLoc(xMap - 1, yMap + 1).getGround()), GroundType.Grass)) {
735 this.placeGroup(this.imgSets.get(6).get(1), x - 40, y + 40);
736 }
737 if (xMap > 0 && this.containsGroundType(this.getBreakdown(MapEditor.map.getLoc(xMap - 1, yMap).getGround()), GroundType.Grass)) {
738 this.placeGroup(this.imgSets.get(6).get(1), x - 40, y);
739 }
740 }
741 else if (this.containsGroundType(middle, GroundType.Grass)) {
742 if (xMap > 0 && yMap > 0 && this.containsGroundType(this.getBreakdown(MapEditor.map.getLoc(xMap - 1, yMap - 1).getGround()), GroundType.Stone)) {
743 this.placeGroup(this.imgSets.get(6).get(1), x - 40, y - 40);
744 }
745 if (yMap > 0 && this.containsGroundType(this.getBreakdown(MapEditor.map.getLoc(xMap, yMap - 1).getGround()), GroundType.Stone)) {
746 this.placeGroup(this.imgSets.get(6).get(1), x, y - 40);
747 }
748 if (xMap < MapEditor.map.getWidth() - 1 && yMap > 0 && this.containsGroundType(this.getBreakdown(MapEditor.map.getLoc(xMap + 1, yMap - 1).getGround()), GroundType.Stone)) {
749 this.placeGroup(this.imgSets.get(6).get(1), x + 40, y - 40);
750 }
751 if (xMap < MapEditor.map.getWidth() - 1 && this.containsGroundType(this.getBreakdown(MapEditor.map.getLoc(xMap + 1, yMap).getGround()), GroundType.Stone)) {
752 this.placeGroup(this.imgSets.get(6).get(1), x + 40, y);
753 }
754 if (xMap < MapEditor.map.getWidth() - 1 && yMap < MapEditor.map.getHeight() - 1 && this.containsGroundType(this.getBreakdown(MapEditor.map.getLoc(xMap + 1, yMap + 1).getGround()), GroundType.Stone)) {
755 this.placeGroup(this.imgSets.get(6).get(1), x + 40, y + 40);
756 }
757 if (yMap < MapEditor.map.getHeight() - 1 && this.containsGroundType(this.getBreakdown(MapEditor.map.getLoc(xMap, yMap + 1).getGround()), GroundType.Stone)) {
758 this.placeGroup(this.imgSets.get(6).get(1), x, y + 40);
759 }
760 if (xMap > 0 && yMap < MapEditor.map.getHeight() - 1 && this.containsGroundType(this.getBreakdown(MapEditor.map.getLoc(xMap - 1, yMap + 1).getGround()), GroundType.Stone)) {
761 this.placeGroup(this.imgSets.get(6).get(1), x - 40, y + 40);
762 }
763 if (xMap > 0 && this.containsGroundType(this.getBreakdown(MapEditor.map.getLoc(xMap - 1, yMap).getGround()), GroundType.Stone)) {
764 this.placeGroup(this.imgSets.get(6).get(1), x - 40, y);
765 }
766 }
767 tiles[4] = this.getTilesByBreakdown(middle);
768 if (xMap > 0 && yMap > 0) {
769 final GroundType[] old = this.getBreakdown(MapEditor.map.getLoc(xMap - 1, yMap - 1).getGround());
770 System.arraycopy(old, 0, breakdown, 0, 4);
771 breakdown[3] = middle[0];
772 tiles[0] = this.getTilesByBreakdown(breakdown);
773 this.addTile(x - 40, y - 40, tiles[0].get(new Random().nextInt(tiles[0].size())));
774 }
775 if (yMap > 0) {
776 final GroundType[] old = this.getBreakdown(MapEditor.map.getLoc(xMap, yMap - 1).getGround());
777 System.arraycopy(old, 0, breakdown, 0, 4);
778 breakdown[2] = middle[0];
779 breakdown[3] = middle[1];
780 tiles[1] = this.getTilesByBreakdown(breakdown);
781 this.addTile(x, y - 40, tiles[1].get(new Random().nextInt(tiles[1].size())));
782 }
783 if (xMap < MapEditor.map.getWidth() - 1 && yMap > 0) {
784 final GroundType[] old = this.getBreakdown(MapEditor.map.getLoc(xMap + 1, yMap - 1).getGround());
785 System.arraycopy(old, 0, breakdown, 0, 4);
786 breakdown[2] = middle[1];
787 tiles[2] = this.getTilesByBreakdown(breakdown);
788 this.addTile(x + 40, y - 40, tiles[2].get(new Random().nextInt(tiles[2].size())));
789 }
790 if (xMap < MapEditor.map.getWidth() - 1) {
791 final GroundType[] old = this.getBreakdown(MapEditor.map.getLoc(xMap + 1, yMap).getGround());
792 System.arraycopy(old, 0, breakdown, 0, 4);
793 breakdown[0] = middle[1];
794 breakdown[2] = middle[3];
795 tiles[5] = this.getTilesByBreakdown(breakdown);
796 this.addTile(x + 40, y, tiles[5].get(new Random().nextInt(tiles[5].size())));
797 }
798 if (xMap < MapEditor.map.getWidth() - 1 && yMap < MapEditor.map.getHeight() - 1) {
799 final GroundType[] old = this.getBreakdown(MapEditor.map.getLoc(xMap + 1, yMap + 1).getGround());
800 System.arraycopy(old, 0, breakdown, 0, 4);
801 breakdown[0] = middle[3];
802 tiles[8] = this.getTilesByBreakdown(breakdown);
803 this.addTile(x + 40, y + 40, tiles[8].get(new Random().nextInt(tiles[8].size())));
804 }
805 if (yMap < MapEditor.map.getHeight() - 1) {
806 final GroundType[] old = this.getBreakdown(MapEditor.map.getLoc(xMap, yMap + 1).getGround());
807 System.arraycopy(old, 0, breakdown, 0, 4);
808 breakdown[0] = middle[2];
809 breakdown[1] = middle[3];
810 tiles[7] = this.getTilesByBreakdown(breakdown);
811 this.addTile(x, y + 40, tiles[7].get(new Random().nextInt(tiles[7].size())));
812 }
813 if (xMap > 0 && yMap < MapEditor.map.getHeight() - 1) {
814 final GroundType[] old = this.getBreakdown(MapEditor.map.getLoc(xMap - 1, yMap + 1).getGround());
815 System.arraycopy(old, 0, breakdown, 0, 4);
816 breakdown[1] = middle[2];
817 tiles[6] = this.getTilesByBreakdown(breakdown);
818 this.addTile(x - 40, y + 40, tiles[6].get(new Random().nextInt(tiles[6].size())));
819 }
820 if (xMap > 0) {
821 final GroundType[] old = this.getBreakdown(MapEditor.map.getLoc(xMap - 1, yMap).getGround());
822 System.arraycopy(old, 0, breakdown, 0, 4);
823 breakdown[1] = middle[0];
824 breakdown[3] = middle[2];
825 tiles[3] = this.getTilesByBreakdown(breakdown);
826 this.addTile(x - 40, y, tiles[3].get(new Random().nextInt(tiles[3].size())));
827 }
828 this.addTile(x, y, tiles[4].get(new Random().nextInt(tiles[4].size())));
829 }
830
831 private void addTile(final int x, final int y, final Tile t) {
832 MapEditor.map.setGround(x / 40, y / 40, new Tile(t, x - x % 40, y - y % 40, this.curLevel));
833 }
834
835 private void selectObject(int x, int y) {
836 x -= 204;
837 y -= 148;
838 if (x >= 0 && x < 800 && y >= 0 && y < 600) {
839 x += this.playerLoc.x - 400;
840 y += this.playerLoc.y - 300;
841 final Iterator<Tile> iter = MapEditor.map.getLoc(x / 40, y / 40).getObjects().iterator();
842 Tile closest = null;
843 final Point curPoint = new Point(x, y);
844 if (!iter.hasNext()) {
845 this.selectedObject = null;
846 return;
847 }
848 closest = iter.next();
849 while (iter.hasNext()) {
850 final Tile cur = iter.next();
851 if (cur.loc.distance(curPoint) < closest.loc.distance(curPoint)) {
852 closest = cur;
853 }
854 }
855 this.selectedObject = closest;
856 }
857 else {
858 this.selectedObject = null;
859 }
860 }
861
862 private void selectCreature(int x, int y) {
863 x -= 204;
864 y -= 148;
865 if (x >= 0 && x < 800 && y >= 0 && y < 600) {
866 x += this.playerLoc.x - 400;
867 y += this.playerLoc.y - 300;
868 final Iterator<Creature> iter = MapEditor.map.getLoc(x / 40, y / 40).getCreatures().iterator();
869 Creature closest = null;
870 final Point curPoint = new Point(x, y);
871 if (!iter.hasNext()) {
872 this.selectedCreature = null;
873 return;
874 }
875 closest = iter.next();
876 while (iter.hasNext()) {
877 final Creature cur = iter.next();
878 if (cur.loc.distance(curPoint) < closest.loc.distance(curPoint)) {
879 closest = cur;
880 }
881 }
882 if (this.selectedObject == null || closest.loc.distance(curPoint) < this.selectedObject.loc.distance(curPoint)) {
883 this.selectedCreature = closest;
884 }
885 }
886 else {
887 this.selectedCreature = null;
888 }
889 }
890
891 public void showMessage(final String text) {
892 this.auxState = AuxState.MsgBox;
893 ((Label)this.wndMessage.getMember("label")).setText(text);
894 }
895
896 private void selectText(final Textbox text) {
897 if (this.selectedText != null) {
898 this.selectedText.setSelected(false);
899 }
900 if ((this.selectedText = text) != null) {
901 text.setSelected(true);
902 }
903 }
904
905 private synchronized void handleMouseInput(final MouseEvent e) {
906 if (!this.started) {
907 return;
908 }
909 this.selectText(null);
910 switch (this.auxState) {
911 case None: {
912 switch (this.state) {
913 case Main: {
914 if (this.wndMain.getMember("new map").isClicked(e.getX(), e.getY())) {
915 MapEditor.map = new Map(40, 40, this.activeSet.get(0));
916 this.playerLoc = new Point(400, 300);
917 this.mapName = "mapSave.txt";
918 this.horBar.setSize(800 * this.horBar.getMaxSize() / (MapEditor.map.getWidth() * 40 + 800));
919 this.verBar.setSize(600 * this.verBar.getMaxSize() / (MapEditor.map.getHeight() * 40 + 600));
920 this.horBar.setPosition(this.horBar.getMaxSize() * this.playerLoc.x / (40 * MapEditor.map.getWidth() + 800));
921 this.verBar.setPosition(this.verBar.getMaxSize() * this.playerLoc.y / (40 * MapEditor.map.getHeight() + 600));
922 this.state = State.Edit;
923 break;
924 }
925 if (this.wndMain.getMember("load map").isClicked(e.getX(), e.getY())) {
926 this.auxState = AuxState.LoadMap;
927 ((Textbox)this.wndLoadMap.getMember("map_name")).setText("mapSave.txt");
928 break;
929 }
930 if (this.wndMain.getMember("quit").isClicked(e.getX(), e.getY())) {
931 this.done = true;
932 break;
933 }
934 break;
935 }
936 case Edit: {
937 if (this.wndEditOptions.getMember("name").isClicked(e.getX(), e.getY())) {
938 this.selectText((Textbox)this.wndEditOptions.getMember("name"));
939 break;
940 }
941 if (this.wndEditOptions.getMember("x").isClicked(e.getX(), e.getY())) {
942 this.selectText((Textbox)this.wndEditOptions.getMember("x"));
943 break;
944 }
945 if (this.wndEditOptions.getMember("y").isClicked(e.getX(), e.getY())) {
946 this.selectText((Textbox)this.wndEditOptions.getMember("y"));
947 break;
948 }
949 if (this.wndEditOptions.getMember("new").isClicked(e.getX(), e.getY())) {
950 this.createNewMap();
951 break;
952 }
953 if (this.wndEditOptions.getMember("save").isClicked(e.getX(), e.getY())) {
954 MapEditor.map.save(this.mapName);
955 this.showMessage(MapEditor.map.message);
956 break;
957 }
958 if (this.wndEditOptions.getMember("load").isClicked(e.getX(), e.getY())) {
959 this.auxState = AuxState.LoadMap;
960 ((Textbox)this.wndLoadMap.getMember("map_name")).setText("mapSave.txt");
961 break;
962 }
963 if (this.wndEditOptions.getMember("back").isClicked(e.getX(), e.getY())) {
964 this.state = State.Main;
965 break;
966 }
967 if (this.wndEdit.handleEvent(e)) {
968 this.playerLoc.x = (MapEditor.map.getWidth() * 40 + 800) * this.horBar.getPosition() / this.horBar.getMaxSize();
969 this.playerLoc.y = (MapEditor.map.getHeight() * 40 + 600) * this.verBar.getPosition() / this.verBar.getMaxSize();
970 break;
971 }
972 if (this.selectNewImage(e.getX(), e.getY()) || this.changeSet(e.getX(), e.getY()) || this.changeLevel(e.getX(), e.getY())) {
973 break;
974 }
975 if (e.isShiftDown()) {
976 this.selectObject(e.getX(), e.getY());
977 this.selectCreature(e.getX(), e.getY());
978 break;
979 }
980 this.placeTile(e.getX(), e.getY());
981 break;
982 }
983 }
984 break;
985 }
986 case MsgBox: {
987 if (this.wndMessage.getMember("button").isClicked(e.getX(), e.getY())) {
988 this.auxState = AuxState.None;
989 break;
990 }
991 break;
992 }
993 case LoadMap: {
994 if (this.wndLoadMap.getMember("map_name").isClicked(e.getX(), e.getY())) {
995 this.selectText((Textbox)this.wndLoadMap.getMember("map_name"));
996 break;
997 }
998 if (this.wndLoadMap.getMember("load").isClicked(e.getX(), e.getY())) {
999 final String strName = ((Textbox)this.wndLoadMap.getMember("map_name")).getText();
1000 MapEditor.map = null;
1001 final Map newMap = Map.load(strName, MapEditor.map, this.imageMap, this.crMap);
1002 if (newMap.valid) {
1003 MapEditor.map = newMap;
1004 this.playerLoc = new Point(400, 300);
1005 this.mapName = strName;
1006 this.horBar.setSize(800 * this.horBar.getMaxSize() / (MapEditor.map.getWidth() * 40 + 800));
1007 this.verBar.setSize(600 * this.verBar.getMaxSize() / (MapEditor.map.getHeight() * 40 + 600));
1008 this.horBar.setPosition(this.horBar.getMaxSize() * this.playerLoc.x / (40 * MapEditor.map.getWidth() + 800));
1009 this.verBar.setPosition(this.verBar.getMaxSize() * this.playerLoc.y / (40 * MapEditor.map.getHeight() + 600));
1010 this.state = State.Edit;
1011 }
1012 this.showMessage(newMap.message);
1013 break;
1014 }
1015 if (this.wndLoadMap.getMember("back").isClicked(e.getX(), e.getY())) {
1016 this.auxState = AuxState.None;
1017 break;
1018 }
1019 break;
1020 }
1021 }
1022 }
1023
1024 public void handleKeyboardInput(final KeyEvent e) {
1025 if (this.selectedText != null) {
1026 this.selectedText.handleEvent(e);
1027 }
1028 else if (e.getKeyCode() == 83) {
1029 this.showFps = !this.showFps;
1030 }
1031 else if (e.getKeyCode() == 27) {
1032 if (this.state == State.Edit) {
1033 this.state = State.Main;
1034 }
1035 else {
1036 this.done = true;
1037 }
1038 }
1039 else if (e.getKeyCode() == 127) {
1040 if (this.state == State.Edit && this.selectedObject != null) {
1041 MapEditor.map.removeObject(this.selectedObject.getMapX(), this.selectedObject.getMapY(), this.selectedObject);
1042 this.selectedObject = null;
1043 }
1044 if (this.state == State.Edit && this.selectedCreature != null) {
1045 MapEditor.map.removeCreature(this.selectedCreature.getMapX(), this.selectedCreature.getMapY(), this.selectedCreature);
1046 this.selectedCreature = null;
1047 }
1048 }
1049 else if (e.getKeyCode() == 90 && this.state == State.Edit) {
1050 this.bounds = !this.bounds;
1051 }
1052 else if (e.getKeyCode() == 88 && this.state == State.Edit) {
1053 this.passable = !this.passable;
1054 }
1055 }
1056
1057 @Override
1058 public void mousePressed(final MouseEvent e) {
1059 this.handleMouseInput(e);
1060 }
1061
1062 @Override
1063 public void mouseReleased(final MouseEvent e) {
1064 }
1065
1066 @Override
1067 public void mouseEntered(final MouseEvent e) {
1068 }
1069
1070 @Override
1071 public void mouseExited(final MouseEvent e) {
1072 }
1073
1074 @Override
1075 public void mouseClicked(final MouseEvent e) {
1076 }
1077
1078 @Override
1079 public void keyTyped(final KeyEvent e) {
1080 }
1081
1082 @Override
1083 public void keyPressed(final KeyEvent e) {
1084 this.handleKeyboardInput(e);
1085 }
1086
1087 @Override
1088 public void keyReleased(final KeyEvent e) {
1089 }
1090
1091 public static void main(final String[] args) {
1092 try {
1093 final PrintStream st = new PrintStream(new FileOutputStream("errMapEditor.txt", true));
1094 System.setErr(st);
1095 System.setOut(st);
1096 System.out.println("-----[ Session started on " + Utils.dateString() + " ]-----");
1097 final GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
1098 final GraphicsDevice device = env.getDefaultScreenDevice();
1099 new MapEditor(device);
[a10d422]1100 } catch (Exception e) {
[ebd3538]1101 e.printStackTrace();
1102 }
1103 System.exit(0);
1104 }
1105
1106 public enum AuxState
1107 {
1108 None("None", 0),
1109 MsgBox("MsgBox", 1),
1110 LoadMap("LoadMap", 2);
1111
1112 private AuxState(final String s, final int n) {
1113 }
1114 }
1115
1116 private enum GroundType
1117 {
1118 Grass("Grass", 0),
1119 Dirt("Dirt", 1),
1120 Stone("Stone", 2);
1121
1122 private GroundType(final String s, final int n) {
1123 }
1124 }
1125
1126 public enum State
1127 {
1128 Main("Main", 0),
1129 Edit("Edit", 1);
1130
1131 private State(final String s, final int n) {
1132 }
1133 }
1134}
Note: See TracBrowser for help on using the repository browser.