-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEditor.as
256 lines (192 loc) · 5.79 KB
/
Editor.as
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
package
{
import net.flashpunk.*;
import net.flashpunk.graphics.*;
import net.flashpunk.masks.*;
import net.flashpunk.utils.*;
import flash.display.*;
import flash.geom.*;
import flash.utils.*;
public class Editor extends World
{
[Embed(source="images/tiles.png")]
public static const EditTilesGfx: Class;
public static var editTile:Spritemap = new Spritemap(EditTilesGfx, Main.TW, Main.TW);
public static var palette:Entity = createPalette();
public static var paletteClicked:Boolean = false;
public static var paletteMouseover:Stamp;
public static var tiles:Tilemap;
public static var data:LevelData;
public static var PERSISTENT:Boolean = false;
public static function init ():void
{
PERSISTENT = Main.devMode;
if (FP.stage.loaderInfo.parameters && FP.stage.loaderInfo.parameters.editor) {
PERSISTENT = false;
}
tiles = new Tilemap(EditTilesGfx, 640/32*Main.TW, 480/32*Main.TW, Main.TW, Main.TW);
data = new LevelData;
data.tiles = tiles;
var startLevel:String ='';
if (PERSISTENT && Main.so.data.editState) {
startLevel = Main.so.data.editState;
} else {
//startLevel = new DefaultRoom;
}
data.fromString(startLevel);
}
public override function update (): void
{
Input.mouseCursor = "auto";
Main.MenuClass = Editor;
History.buttonsVisible = 0;
if (Input.pressed(Key.SPACE)) {
togglePalette();
}
if (Input.pressed(Key.E)) {
FP.world = new Level(data, 0);
return;
}
// SPACE: Palette
// E: Test
// C: Clear
// 0-9: choose tile
if (Input.check(Key.SHIFT) && Input.pressed(Key.ESCAPE)) {
clear();
}
var i:int;
var j:int;
for (i = 0; i < 10; i++) {
if (Input.pressed(Key.DIGIT_1 + i)) {
editTile.frame = i;
}
}
//if (Input.mouseCursor != "auto") return;
var mx:int = mouseX / editTile.width;
var my:int = mouseY / editTile.height;
var overPalette:Boolean = palette.visible && palette.collidePoint(palette.x, palette.y, Input.mouseX, Input.mouseY);
if (overPalette) {
editTile.alpha = 0;
Input.mouseCursor = "button";
} else {
editTile.x = mx * editTile.width;
editTile.y = my * editTile.height;
editTile.originX = editTile.originY = 0;
editTile.alpha = 0.5;
}
if (palette.visible) {
if (overPalette) {
mx = Input.mouseX - palette.x;
my = Input.mouseY - palette.y;
mx /= editTile.width;
my /= editTile.height;
paletteMouseover.x = -1 + mx * editTile.width;
paletteMouseover.y = -1 + my * editTile.height;
} else {
paletteMouseover.x = -1 + int(editTile.frame % 4) * editTile.width;
paletteMouseover.y = -1 + int(editTile.frame / 4) * editTile.height;
}
}
if (Input.mouseDown || Input.mousePressed) {
if (overPalette && Input.mousePressed) {
editTile.frame = mx + (palette.width / editTile.width) * my;
paletteClicked = true;
}
if (! overPalette && ! paletteClicked) {
var id:int = getTile(mx, my);
if (id != editTile.frame) {
if(Input.check(Key.B))
{
for(i = mx-1; i<mx+2; i++)
for(j = my-1; j<my+2; j++)
setTile(i, j, editTile.frame);
}
else
{
setTile(mx, my, editTile.frame);
}
}
}
palette.visible = false;
} else {
paletteClicked = false;
}
}
public function clear ():void
{
tiles.setRect(0, 0, tiles.columns, tiles.rows, 0);
changed();
}
public function getTile (i:int, j:int): int
{
return tiles.getTile(i, j);
}
public function setTile (i:int, j:int, tile:int): void
{
if(i<0 || i>=tiles.columns || j<0 || j>=tiles.rows) return;
tiles.setTile(i, j, tile);
changed();
}
public override function render (): void
{
Draw.graphic(tiles);
Draw.graphic(editTile);
Draw.entity(palette, palette.x, palette.y);
}
public static function togglePalette ():void
{
palette.visible = ! palette.visible;
}
private static function createPalette ():Entity
{
var palette:Entity = new Entity;
palette.visible = false;
var tiles:Image = new Image(EditTilesGfx);
palette.width = tiles.width;
palette.height = tiles.height;
palette.x = int((FP.width - palette.width)*0.5);
palette.y = int((FP.height - palette.height)*0.5);
tiles.scrollX = tiles.scrollY = 0;
var border:Stamp = new Stamp(new BitmapData(palette.width+4, palette.height+4, false, 0xFF0000));
FP.rect.x = 2;
FP.rect.y = 2;
FP.rect.width = palette.width;
FP.rect.height = palette.height;
border.source.fillRect(FP.rect, 0x202020);
border.x = -2;
border.y = -2;
border.scrollX = border.scrollY = 0;
paletteMouseover = new Stamp(new BitmapData(editTile.width+2, editTile.height+2, true, 0xFFFFFFFF));
FP.rect.x = FP.rect.y = 1;
FP.rect.width = editTile.width;
FP.rect.height = editTile.height;
paletteMouseover.source.fillRect(FP.rect, 0x0);
paletteMouseover.x = -1;
paletteMouseover.y = -1;
paletteMouseover.scrollX = paletteMouseover.scrollY = 0;
palette.graphic = new Graphiclist(border, tiles, paletteMouseover);
return palette;
}
public static function GetTile(tiles:Tilemap, i:int, j:int):uint
{
//if(i<0 || i>=tiles.columns || j<0 || j>=tiles.rows) return WALL;
return tiles.getTile(i,j);
}
public static function clear ():void
{
tiles.setRect(0, 0, tiles.columns, tiles.rows, 0);
}
public function getWorldData (): *
{
return data.toString();
}
public function setWorldData (bytedata: ByteArray): void {
data.fromString(bytedata.toString());
changed();
}
private function changed (): void
{
Main.so.data.editState = getWorldData();
}
}
}