-
Notifications
You must be signed in to change notification settings - Fork 0
/
leveledit.js
204 lines (178 loc) · 5.41 KB
/
leveledit.js
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
/*
By Simon Sarris
Art by Betsy Green, Sean DeVarney and Simon Sarris
Based on the old PC game Heartlight
This project will be given an open source license when complete
*/
// Level editor!
// Hold the state we need to make levels and test them
// All of this could be rolled into Game, but I wanted to keep them
// separate in case someone wanted to focus on just one or the other
function LevelEditor(game) {
this.game = game;
this.chosenTile = new Tile('G');
var editor = this;
this.editorClickEvent = function(e) { editor.editorClick(e) };
this.editorMouseMoveEvent = function(e) { editor.editorMouseMove(e) };
this.lastMouse = null;
var tiles = ['O', 'X', 'Y', 'V', 'G', 'B', 'R', 'H', 'P', 'D'];
var tileset = [];
var l = tiles.length;
for (var i = 0; i < l; i++) {
tileset.push(new Tile(tiles[i]));
}
this.tileset = tileset;
if (game.currentLevel === null) {
// Start with something blank
game.currentLevel =
'OOOOOOOOOOOOOOOOOOOO' +
'OOOOOOOOOOOOOOOOOOOO' +
'OOOOOOOOOOOOOOOOOOOO' +
'OOOOOOOOOOOOOOOOOOOO' +
'OOOOOOOOOOOOOOOOOOOO' +
'OOOOOOOOOOOOOOOOOOOO' +
'OOOOOOOOOOOOOOOOOOOO' +
'OOOOOOOOOOOOOOOOOOOO' +
'OOOOOOOOOOOOOOOOOOOO' +
'OOOOOOOOOOOOOOOOOOOO' +
'OOOOOOOOOOOOOOOOOOOO' +
'OOOOOOOOOOOOOOOOOOOO';
}
var level = game.currentLevel;
var tiles = []; // we want a 12x20 array
var k = 0;
for (var i = 0; i < 12; i++) {
tiles[i] = [];
for (var j = 0; j < 20; j++) {
var tile = new Tile(level[k++]);
tiles[i][j] = tile;
}
}
game.tiles = tiles;
}
// NYI
LevelEditor.prototype.save = function() {
var input = document.createElement("textarea");
input.style.position = "absolute";
input.style.zIndex = 100; // put it all the way to the front
input.style.width = '300px';
input.style.height = '200px';
input.style.fontFamily = 'monospace';
var level = this.game.currentLevel;
var result = '';
// lazy, too late at night to deal with regular expressions!
while (level.length > 0) {
result += level.substring(0, 20) + '\n';
level = level.substring(20);
}
input.value = result;
var div = document.getElementById('gameContainer');
div.appendChild(input); // NYI: some div to hold the level editor
input.addEventListener('blur', function(e) {
input.style.display = 'none';
}, false);
input.style.display = 'block';
input.focus();
}
// NYI
LevelEditor.prototype.load = function() {
}
// NYI
LevelEditor.prototype.test = function() {
}
LevelEditor.prototype.draw = function() {
var game = this.game; // kind of a weird way to do it but whatever
// The tiles take up 640x384
var ctx = game.ctx;
var can = game.canvas;
ctx.clearRect(0, 0, can.width, can.height);
// Draw the tiles
var tiles = game.tiles;
// we draw column by column
var rlen = tiles.length;
for (var row = 0; row < rlen; row++) {
var clen = tiles[row].length;
for (var col = 0; col < clen; col++) {
var e = tiles[row][col];
// rows represent y values
// columns represent x values
e.draw(ctx, col, row);
}
}
// gridlines to better see the level
ctx.fillStyle = 'rgba(255,255,255,.2)';
for (var row = 0; row < rlen; row++) {
ctx.fillRect(0, row*32, 640, 1);
}
for (var col = 0; col < clen; col++) {
ctx.fillRect(col*32, 0, 1, 384);
}
// Draw menu bar from 384 onwards
ctx.fillStyle = 'black';
ctx.fillRect(0,384, 640, 480-384);
ctx.fillStyle = 'rgb(158, 57, 50)';
ctx.font = '10px sans-serif';
ctx.fillText('Hearts left: ' + game.hearts, 10, 400);
var x = 0;
var tileset = this.tileset;
var l = tileset.length;
for (var i = 0; i < l; i++) {
tileset[i].drawXY(ctx, x, 420);
x += 32;
}
// If the mouse is over the game level we want to draw
// The currently selected tile with half transparency
var mouse = this.lastMouse;
if (mouse !== null && mouse.y < 384) {
ctx.save();
ctx.globalAlpha = 0.7;
this.chosenTile.drawXY(ctx, mouse.x - 16, mouse.y - 16);
ctx.restore();
}
};
LevelEditor.prototype.editorClick = function(e) {
var game = this.game;
if (e.targetTouches) e = e.targetTouches[0];
e.preventDefault();
e.stopPropagation();
var mouse = game.getMouse(e);
if (Debug) console.log('menu' + mouse.x + ',' + mouse.y + ' click!');
var chosen = this.chosenTile.type;
// See if we are picking a tile or placing it
if (mouse.y < 384) {
var x = Math.floor(mouse.x / 32);
var y = Math.floor(mouse.y / 32);
if (chosen === 'P' || chosen === 'D') {
// find and destroy any old players
var tiles = game.tiles;
var rlen = tiles.length;
for (var row = 0; row < rlen; row++) {
var clen = tiles[row].length;
for (var col = 0; col < clen; col++) {
if (chosen === 'P' && tiles[row][col].type === 'P')
tiles[row][col].type = 'O';
else if (chosen === 'D' && tiles[row][col].type === 'D')
tiles[row][col].type = 'O'
}
}
}
game.tiles[y][x].type = chosen;
} else {
// see if its over a tile
var x = 0;
var y = 420;
var tileset = this.tileset;
var l = tileset.length;
for (var i = 0; i < l; i++) {
if (Game.intersects(x, y, 32, 32, mouse.x, mouse.y)) {
this.chosenTile = tileset[i];
break;
}
x += 32;
}
}
};
LevelEditor.prototype.editorMouseMove = function(e) {
var game = this.game;
this.lastMouse = game.getMouse(e);
};