-
Notifications
You must be signed in to change notification settings - Fork 0
/
building-placer.html
180 lines (179 loc) · 5.77 KB
/
building-placer.html
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
<!DOCTYPE html>
<html>
<head>
<title>Building Placer - Lame City</title>
<meta charset="UTF-8">
<meta name="description" content="Place buildings on an isometric grid."/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<link rel="stylesheet" type="text/css" href="css/canvas.css">
<script src="js/canvas.js" charset="utf-8"></script>
<script src="js/mouse.js" charset="utf-8"></script>
<style>
body {
background-color: #007f12;
}
img {
display: none;
}
.desc {
font-size: 16px;
font-family: sans-serif;
width: 100%;
text-align: center;
height: 1em;
padding: 1em 0;
margin: 0;
color: rgba(255,255,255,0.7);
position: fixed;
left: 0;
bottom: 0;
background-image: linear-gradient(0deg, rgba(0,0,0,0.5), transparent);
opacity: 0.5;
transition: opacity .2s;
}
.desc:hover {
opacity: 1;
}
</style>
</head>
<body>
<canvas id="canvas"></canvas>
<img src="images/housesimple.svg" id="simplehouse">
<img src="images/tree.svg" id="tree">
<img src="images/pond.svg" id="pond">
<p class="desc">Click to place; left/right arrow keys to select building</p>
<script>
const canvas = document.querySelector("#canvas"),
c = canvas.getContext("2d"),
tileSize = 40,
tileWidth = tileSize * Math.sqrt(3) / 2,
tileHeight = tileSize / 2,
buildings = {
"simple-house": {
image: document.querySelector("#simplehouse"),
xSize: 1,
ySize: 1
},
"pine-tree": {
image: document.querySelector("#tree"),
xSize: 1,
ySize: 1
},
"rock-pond": {
image: document.querySelector("#pond"),
xSize: 2,
ySize: 2
}
},
buildingTypes = Object.keys(buildings);
buildingTypes.push("_remove_");
let mouse = {x: 0, y: 0, down : false},
scroll = {x: 0, y: 0},
placedBuildings = {},
selectedBuilding = "simple-house";
pointerFns(
() => mouse.down = true,
(x, y) => (mouse.x = x, mouse.y = y),
() => mouse.down = false,
false
);
document.addEventListener("keydown", e => {
switch (e.keyCode) {
case 37:
selectedBuilding = buildingTypes[(buildingTypes.indexOf(selectedBuilding) + buildingTypes.length - 1) % buildingTypes.length];
break;
case 39:
selectedBuilding = buildingTypes[(buildingTypes.indexOf(selectedBuilding) + 1) % buildingTypes.length];
break;
}
}, false);
function isoToMouse(isoX, isoY) {
return [(isoX - isoY) * tileWidth - scroll.x, (isoX + isoY) * tileHeight - scroll.y];
}
function mouseToIso(mouseX, mouseY) {
let x = (mouseX + scroll.x) / tileWidth, y = (mouseY + scroll.y) / tileHeight;
return [(y + x) / 2, (y - x) / 2];
}
function drawBuilding(buildingType, isoX, isoY) {
let buildingInfo = buildings[buildingType],
img = buildingInfo.image,
left = isoToMouse(isoX, isoY + buildingInfo.ySize)[0],
bottom = isoToMouse(isoX + buildingInfo.xSize, isoY + buildingInfo.ySize)[1],
width = isoToMouse(isoX + buildingInfo.xSize, isoY)[0] - left,
height = width / img.width * img.height;
c.drawImage(img, left, bottom - height, width, height);
}
function drawBuildings() {
let [initIsoX, initIsoY] = mouseToIso(0, 0).map(Math.floor),
width = Math.ceil(window.innerWidth / tileWidth / 2),
height = Math.ceil(window.innerHeight / tileHeight);
for (let y = 0, isoX, isoY, stringPos; y < height; y++) {
for (let x = 0; x < width; x++) {
isoX = initIsoX + x + Math.floor(y / 2) + y % 2;
isoY = initIsoY - x + Math.floor(y / 2);
stringPos = `${isoX},${isoY}`;
if (placedBuildings[stringPos] && !placedBuildings[stringPos].partOf) {
drawBuilding(placedBuildings[stringPos].type, isoX, isoY);
}
}
}
}
function noBuildingsInWay(isoX, isoY, xSize, ySize) {
for (let x = 0; x < xSize; x++) {
for (let y = 0; y < ySize; y++) {
if (placedBuildings[`${isoX + x},${isoY + y}`]) return false;
}
}
return true;
}
function draw() {
c.clearRect(0, 0, innerWidth, innerHeight);
if (mouse.x / window.innerWidth <= 0.1) scroll.x -= 5;
else if (mouse.x / window.innerWidth >= 0.9) scroll.x += 5;
if (mouse.y / window.innerHeight <= 0.1) scroll.y -= 5;
else if (mouse.y / window.innerHeight >= 0.9) scroll.y += 5;
drawBuildings();
if (selectedBuilding) {
let [isoX, isoY] = mouseToIso(mouse.x, mouse.y).map(Math.floor);
if (selectedBuilding === "_remove_") {
c.fillStyle = "rgba(255,0,0,0.3)";
c.beginPath();
c.moveTo(...isoToMouse(isoX, isoY));
c.lineTo(...isoToMouse(isoX + 1, isoY));
c.lineTo(...isoToMouse(isoX + 1, isoY + 1));
c.lineTo(...isoToMouse(isoX, isoY + 1));
c.fill();
let tile = placedBuildings[`${isoX},${isoY}`];
if (mouse.down && tile) {
if (tile.partOf) {
[isoX, isoY] = tile.partOf;
tile = placedBuildings[tile.partOf.join(",")];
}
for (let x = 0; x < buildings[tile.type].xSize; x++) {
for (let y = 0; y < buildings[tile.type].ySize; y++) {
delete placedBuildings[`${isoX + x},${isoY + y}`];
}
}
}
} else {
if (mouse.down && noBuildingsInWay(isoX, isoY, buildings[selectedBuilding].xSize, buildings[selectedBuilding].ySize)) {
placedBuildings[`${isoX},${isoY}`] = {type: selectedBuilding};
for (let x = 0; x < buildings[selectedBuilding].xSize; x++) {
for (let y = 0; y < buildings[selectedBuilding].ySize; y++) {
if (x === 0 && y === 0) continue;
placedBuildings[`${isoX + x},${isoY + y}`] = {partOf: [isoX, isoY]};
}
}
}
c.globalAlpha = 0.5;
drawBuilding(selectedBuilding, isoX, isoY);
c.globalAlpha = 1;
}
}
window.requestAnimationFrame(draw);
}
draw();
</script>
</body>
<!-- MADE BY SEAN -->
</html>