Spaces:
Running
Running
Update index.html
Browse files- index.html +289 -387
index.html
CHANGED
|
@@ -1,415 +1,317 @@
|
|
| 1 |
<!DOCTYPE html>
|
| 2 |
<html lang="ko">
|
| 3 |
<head>
|
| 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 |
-
</style>
|
| 48 |
</head>
|
| 49 |
<body>
|
| 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 |
-
let units = []; // 전체 유닛 객체를 보관
|
| 106 |
-
let currentTeam = RED; // 현재 턴의 팀
|
| 107 |
-
let selectedUnit = null; // 유저가 선택한 유닛(RED 턴 중)
|
| 108 |
-
|
| 109 |
-
/********************************************************
|
| 110 |
-
* 2. 유닛 클래스
|
| 111 |
-
********************************************************/
|
| 112 |
-
class Unit {
|
| 113 |
-
constructor(typeKey, team, x, y) {
|
| 114 |
-
const data = UNIT_TYPES[typeKey];
|
| 115 |
-
this.type = typeKey; // "INFANTRY", "ARCHER", "CAVALRY"
|
| 116 |
-
this.team = team; // "RED" 또는 "BLUE"
|
| 117 |
-
this.hp = data.hp;
|
| 118 |
-
this.attack = data.attack;
|
| 119 |
-
this.defense = data.defense;
|
| 120 |
-
this.move = data.move;
|
| 121 |
-
this.range = data.range;
|
| 122 |
-
this.advantage = data.advantage;
|
| 123 |
-
this.x = x;
|
| 124 |
-
this.y = y;
|
| 125 |
-
this.hasActed = false; // 이 턴 중 이동/공격 여부를 단순 체크(확장 가능)
|
| 126 |
-
}
|
| 127 |
-
get isAlive() {
|
| 128 |
-
return this.hp > 0;
|
| 129 |
-
}
|
| 130 |
-
get displayName() {
|
| 131 |
-
// 예: 'I25' (Infantry, HP 25)
|
| 132 |
-
return this.type[0] + this.hp;
|
| 133 |
-
}
|
| 134 |
-
}
|
| 135 |
-
|
| 136 |
-
/********************************************************
|
| 137 |
-
* 3. 초기화: 유닛 배치
|
| 138 |
-
********************************************************/
|
| 139 |
-
function initUnits() {
|
| 140 |
-
units = [];
|
| 141 |
-
// RED - 보병(6), 궁수(3), 기병(3) / 예시로 좌측에 배치
|
| 142 |
-
for (let i = 0; i < 6; i++) {
|
| 143 |
-
units.push(new Unit("INFANTRY", RED, 0, i % HEIGHT));
|
| 144 |
-
}
|
| 145 |
-
for (let i = 0; i < 3; i++) {
|
| 146 |
-
units.push(new Unit("ARCHER", RED, 1, i));
|
| 147 |
-
}
|
| 148 |
-
for (let i = 0; i < 3; i++) {
|
| 149 |
-
units.push(new Unit("CAVALRY", RED, 2, i + 3));
|
| 150 |
-
}
|
| 151 |
-
// BLUE - 보병(6), 궁수(3), 기병(3) / 예시로 우측에 배치
|
| 152 |
-
for (let i = 0; i < 6; i++) {
|
| 153 |
-
units.push(new Unit("INFANTRY", BLUE, WIDTH - 1, i % HEIGHT));
|
| 154 |
-
}
|
| 155 |
-
for (let i = 0; i < 3; i++) {
|
| 156 |
-
units.push(new Unit("ARCHER", BLUE, WIDTH - 2, i));
|
| 157 |
-
}
|
| 158 |
-
for (let i = 0; i < 3; i++) {
|
| 159 |
-
units.push(new Unit("CAVALRY", BLUE, WIDTH - 3, i + 3));
|
| 160 |
-
}
|
| 161 |
-
}
|
| 162 |
-
|
| 163 |
-
/********************************************************
|
| 164 |
-
* 4. 화면 렌더링
|
| 165 |
-
********************************************************/
|
| 166 |
-
function renderBoard() {
|
| 167 |
-
const board = document.getElementById("gameBoard");
|
| 168 |
-
board.innerHTML = "";
|
| 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 |
-
document.getElementById("turnInfo").textContent = `현재 턴: ${currentTeam}`;
|
| 194 |
-
if (selectedUnit) {
|
| 195 |
-
document.getElementById("selectedInfo").textContent =
|
| 196 |
-
`선택 유닛: ${selectedUnit.type} (HP: ${selectedUnit.hp})`;
|
| 197 |
-
} else {
|
| 198 |
-
document.getElementById("selectedInfo").textContent = `선택 유닛: 없음`;
|
| 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 |
-
selectedUnit = null;
|
| 234 |
}
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
|
|
|
|
|
|
| 239 |
}
|
| 240 |
-
}
|
| 241 |
-
} else {
|
| 242 |
-
// 빈 칸 클릭 -> 이동 시도
|
| 243 |
-
const dist = Math.abs(selectedUnit.x - x) + Math.abs(selectedUnit.y - y);
|
| 244 |
-
// (간단히 맨해튼 거리로만 계산. 대각 이동/장애물은 고려 안 함)
|
| 245 |
-
if (dist <= selectedUnit.move) {
|
| 246 |
-
selectedUnit.x = x;
|
| 247 |
-
selectedUnit.y = y;
|
| 248 |
-
// 이동만 하고 싶으면 여기서 hasActed를 true로 하거나,
|
| 249 |
-
// 이동 후 공격을 허용할지 여부는 규칙에 따라 결정
|
| 250 |
-
// 여기선 이동해도 공격 가능하도록 hasActed = false 유지
|
| 251 |
-
}
|
| 252 |
}
|
| 253 |
-
}
|
| 254 |
-
renderBoard();
|
| 255 |
-
}
|
| 256 |
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 277 |
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 284 |
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
// 모든 유닛의 hasActed 초기화
|
| 292 |
-
units.forEach(u => {
|
| 293 |
-
if (u.team === RED) {
|
| 294 |
-
u.hasActed = false;
|
| 295 |
-
}
|
| 296 |
});
|
| 297 |
-
// 턴 교체
|
| 298 |
-
currentTeam = BLUE;
|
| 299 |
-
selectedUnit = null;
|
| 300 |
-
renderBoard();
|
| 301 |
-
// AI 수행
|
| 302 |
-
setTimeout(() => performAiTurn(), 500);
|
| 303 |
-
}
|
| 304 |
-
}
|
| 305 |
-
|
| 306 |
-
// AI 간단 로직
|
| 307 |
-
function performAiTurn() {
|
| 308 |
-
// 1. 행동할 수 있는 BLUE 유닛 목록
|
| 309 |
-
const blueUnits = units.filter(u => u.team === BLUE && u.isAlive);
|
| 310 |
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
|
| 314 |
-
|
| 315 |
-
|
| 316 |
-
|
| 317 |
-
|
| 318 |
-
|
| 319 |
-
|
| 320 |
-
|
| 321 |
-
|
| 322 |
-
|
| 323 |
-
|
| 324 |
-
|
|
|
|
| 325 |
}
|
| 326 |
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
|
| 330 |
-
|
| 331 |
-
|
| 332 |
-
} else {
|
| 333 |
-
// 범위 밖이면 이동(가장 간단히, 가까워지도록 x,y를 1칸 이동)
|
| 334 |
-
const moveStep = getSimpleMoveToward(aiUnit, closestEnemy);
|
| 335 |
-
if (moveStep) {
|
| 336 |
-
aiUnit.x = clamp(aiUnit.x + moveStep.dx, 0, WIDTH - 1);
|
| 337 |
-
aiUnit.y = clamp(aiUnit.y + moveStep.dy, 0, HEIGHT - 1);
|
| 338 |
-
// 이동 후, 사거리 안이면 다시 공격
|
| 339 |
-
const distAfterMove = getDistance(aiUnit, closestEnemy);
|
| 340 |
-
if (distAfterMove <= aiUnit.range) {
|
| 341 |
-
attack(aiUnit, closestEnemy);
|
| 342 |
}
|
| 343 |
-
}
|
| 344 |
-
}
|
| 345 |
-
}
|
| 346 |
-
// AI 턴 종료
|
| 347 |
-
currentTeam = RED;
|
| 348 |
-
// AI 유닛 hasActed 초기화
|
| 349 |
-
units.forEach(u => {
|
| 350 |
-
if (u.team === BLUE) {
|
| 351 |
-
u.hasActed = false;
|
| 352 |
}
|
| 353 |
-
});
|
| 354 |
-
renderBoard();
|
| 355 |
-
checkWinCondition();
|
| 356 |
-
}
|
| 357 |
-
|
| 358 |
-
/********************************************************
|
| 359 |
-
* 7. 보조 함수들
|
| 360 |
-
********************************************************/
|
| 361 |
-
// 두 유닛 간 거리(간단히 맨해튼 거리)
|
| 362 |
-
function getDistance(u1, u2) {
|
| 363 |
-
return Math.abs(u1.x - u2.x) + Math.abs(u1.y - u2.y);
|
| 364 |
-
}
|
| 365 |
-
|
| 366 |
-
// AI 이동용: 목표 적 유닛에 조금 더 다가가는 방향 계산
|
| 367 |
-
function getSimpleMoveToward(aiUnit, target) {
|
| 368 |
-
// 이동력만큼 세밀히 계산하기보다는, 1칸만 전진(또는 2칸) 하는 식
|
| 369 |
-
const dx = target.x - aiUnit.x;
|
| 370 |
-
const dy = target.y - aiUnit.y;
|
| 371 |
-
let stepX = 0;
|
| 372 |
-
let stepY = 0;
|
| 373 |
-
|
| 374 |
-
if (Math.abs(dx) > Math.abs(dy)) {
|
| 375 |
-
stepX = dx > 0 ? 1 : -1;
|
| 376 |
-
} else {
|
| 377 |
-
stepY = dy > 0 ? 1 : -1;
|
| 378 |
-
}
|
| 379 |
|
| 380 |
-
|
| 381 |
-
|
| 382 |
-
|
| 383 |
-
|
| 384 |
-
|
| 385 |
-
function clamp(value, min, max) {
|
| 386 |
-
return Math.max(min, Math.min(max, value));
|
| 387 |
-
}
|
| 388 |
-
|
| 389 |
-
// 승리 조건 체크
|
| 390 |
-
function checkWinCondition() {
|
| 391 |
-
const redAlive = units.some(u => u.team === RED && u.isAlive);
|
| 392 |
-
const blueAlive = units.some(u => u.team === BLUE && u.isAlive);
|
| 393 |
-
|
| 394 |
-
if (!redAlive) {
|
| 395 |
-
showMessage("BLUE 승리!");
|
| 396 |
-
} else if (!blueAlive) {
|
| 397 |
-
showMessage("RED 승리!");
|
| 398 |
-
}
|
| 399 |
-
}
|
| 400 |
|
| 401 |
-
|
| 402 |
-
|
| 403 |
-
|
| 404 |
-
|
| 405 |
-
|
| 406 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 407 |
|
| 408 |
-
|
| 409 |
-
|
| 410 |
-
|
| 411 |
-
|
| 412 |
-
});
|
| 413 |
-
</script>
|
| 414 |
</body>
|
| 415 |
-
</html>
|
|
|
|
| 1 |
<!DOCTYPE html>
|
| 2 |
<html lang="ko">
|
| 3 |
<head>
|
| 4 |
+
<meta charset="UTF-8">
|
| 5 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 6 |
+
<title>턴제 전략 시뮬레이션</title>
|
| 7 |
+
<style>
|
| 8 |
+
body {
|
| 9 |
+
margin: 0;
|
| 10 |
+
padding: 20px;
|
| 11 |
+
background-color: #f0f0f0;
|
| 12 |
+
font-family: Arial, sans-serif;
|
| 13 |
+
}
|
| 14 |
+
|
| 15 |
+
#gameContainer {
|
| 16 |
+
display: flex;
|
| 17 |
+
flex-direction: column;
|
| 18 |
+
align-items: center;
|
| 19 |
+
}
|
| 20 |
+
|
| 21 |
+
#gameCanvas {
|
| 22 |
+
border: 2px solid #333;
|
| 23 |
+
box-shadow: 0 0 10px rgba(0,0,0,0.2);
|
| 24 |
+
background-color: #fff;
|
| 25 |
+
}
|
| 26 |
+
|
| 27 |
+
#gameInfo {
|
| 28 |
+
margin-top: 20px;
|
| 29 |
+
padding: 10px;
|
| 30 |
+
background-color: #fff;
|
| 31 |
+
border: 1px solid #ccc;
|
| 32 |
+
border-radius: 5px;
|
| 33 |
+
}
|
| 34 |
+
|
| 35 |
+
.controls {
|
| 36 |
+
margin-top: 10px;
|
| 37 |
+
display: flex;
|
| 38 |
+
gap: 10px;
|
| 39 |
+
}
|
| 40 |
+
|
| 41 |
+
button {
|
| 42 |
+
padding: 5px 10px;
|
| 43 |
+
cursor: pointer;
|
| 44 |
+
}
|
| 45 |
+
</style>
|
|
|
|
|
|
|
| 46 |
</head>
|
| 47 |
<body>
|
| 48 |
+
<div id="gameContainer">
|
| 49 |
+
<canvas id="gameCanvas"></canvas>
|
| 50 |
+
<div id="gameInfo">
|
| 51 |
+
<div id="turnInfo">현재 턴: 적색군</div>
|
| 52 |
+
<div id="selectedUnit">선택된 유닛: 없음</div>
|
| 53 |
+
<div class="controls">
|
| 54 |
+
<button id="endTurn">턴 종료</button>
|
| 55 |
+
<button id="resetGame">게임 재시작</button>
|
| 56 |
+
</div>
|
| 57 |
+
</div>
|
| 58 |
+
</div>
|
| 59 |
+
|
| 60 |
+
<script>
|
| 61 |
+
// 게임 상수
|
| 62 |
+
const GRID_SIZE = 80;
|
| 63 |
+
const COLS = 10;
|
| 64 |
+
const ROWS = 6;
|
| 65 |
+
|
| 66 |
+
// Canvas 설정
|
| 67 |
+
const canvas = document.getElementById('gameCanvas');
|
| 68 |
+
const ctx = canvas.getContext('2d');
|
| 69 |
+
canvas.width = GRID_SIZE * COLS;
|
| 70 |
+
canvas.height = GRID_SIZE * ROWS;
|
| 71 |
+
|
| 72 |
+
// 지형 데이터
|
| 73 |
+
const elevationData = [
|
| 74 |
+
[1, 1, 2, 2, 2, 2, 1, 1, 1, 1],
|
| 75 |
+
[1, 2, 2, 3, 3, 2, 2, 1, 1, 1],
|
| 76 |
+
[1, 2, 3, 3, 2, 2, 2, 2, 1, 1],
|
| 77 |
+
[1, 2, 2, 2, 2, 2, 3, 2, 2, 1],
|
| 78 |
+
[1, 1, 2, 2, 2, 3, 2, 2, 1, 1],
|
| 79 |
+
[1, 1, 1, 2, 2, 2, 2, 1, 1, 1]
|
| 80 |
+
];
|
| 81 |
+
|
| 82 |
+
// 유닛 클래스
|
| 83 |
+
class Unit {
|
| 84 |
+
constructor(type, team, x, y) {
|
| 85 |
+
this.type = type;
|
| 86 |
+
this.team = team;
|
| 87 |
+
this.x = x;
|
| 88 |
+
this.y = y;
|
| 89 |
+
this.hp = this.getInitialHP();
|
| 90 |
+
this.moved = false;
|
| 91 |
+
this.attacked = false;
|
| 92 |
+
|
| 93 |
+
// 유닛 타입별 스탯
|
| 94 |
+
const stats = {
|
| 95 |
+
'Infantry': { move: 2, attack: 3, defense: 3, range: 1 },
|
| 96 |
+
'Archer': { move: 2, attack: 2, defense: 2, range: 3 },
|
| 97 |
+
'Cavalry': { move: 3, attack: 4, defense: 2, range: 1 }
|
| 98 |
+
};
|
| 99 |
+
|
| 100 |
+
Object.assign(this, stats[type]);
|
| 101 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
|
| 103 |
+
getInitialHP() {
|
| 104 |
+
const hpValues = {
|
| 105 |
+
'Infantry': 25,
|
| 106 |
+
'Archer': 20,
|
| 107 |
+
'Cavalry': 22
|
| 108 |
+
};
|
| 109 |
+
return hpValues[this.type];
|
| 110 |
+
}
|
| 111 |
|
| 112 |
+
draw() {
|
| 113 |
+
const x = this.x * GRID_SIZE;
|
| 114 |
+
const y = this.y * GRID_SIZE;
|
| 115 |
+
|
| 116 |
+
// 유닛 기본 모양
|
| 117 |
+
ctx.fillStyle = this.team === 'red' ? '#ff0000' : '#0000ff';
|
| 118 |
+
ctx.beginPath();
|
| 119 |
+
ctx.arc(x + GRID_SIZE/2, y + GRID_SIZE/2, GRID_SIZE/3, 0, Math.PI * 2);
|
| 120 |
+
ctx.fill();
|
| 121 |
+
|
| 122 |
+
// 유닛 타입 표시
|
| 123 |
+
ctx.fillStyle = '#ffffff';
|
| 124 |
+
ctx.font = '14px Arial';
|
| 125 |
+
ctx.textAlign = 'center';
|
| 126 |
+
ctx.fillText(this.type[0], x + GRID_SIZE/2, y + GRID_SIZE/2 + 5);
|
| 127 |
+
|
| 128 |
+
// HP 바
|
| 129 |
+
this.drawHealthBar(x, y);
|
| 130 |
+
}
|
| 131 |
|
| 132 |
+
drawHealthBar(x, y) {
|
| 133 |
+
const maxHP = this.getInitialHP();
|
| 134 |
+
const barWidth = GRID_SIZE * 0.8;
|
| 135 |
+
const barHeight = 5;
|
| 136 |
+
const barX = x + GRID_SIZE * 0.1;
|
| 137 |
+
const barY = y + GRID_SIZE * 0.8;
|
| 138 |
+
|
| 139 |
+
// 배경
|
| 140 |
+
ctx.fillStyle = '#ff0000';
|
| 141 |
+
ctx.fillRect(barX, barY, barWidth, barHeight);
|
| 142 |
+
|
| 143 |
+
// 현재 체력
|
| 144 |
+
ctx.fillStyle = '#00ff00';
|
| 145 |
+
ctx.fillRect(barX, barY, (this.hp / maxHP) * barWidth, barHeight);
|
| 146 |
+
}
|
| 147 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 148 |
|
| 149 |
+
// 게임 상태
|
| 150 |
+
let gameState = {
|
| 151 |
+
units: [],
|
| 152 |
+
selectedUnit: null,
|
| 153 |
+
currentTurn: 'red',
|
| 154 |
+
turnCount: 1
|
| 155 |
+
};
|
| 156 |
+
|
| 157 |
+
// 게임 초기화
|
| 158 |
+
function initGame() {
|
| 159 |
+
gameState.units = [];
|
| 160 |
+
gameState.currentTurn = 'red';
|
| 161 |
+
gameState.turnCount = 1;
|
| 162 |
+
gameState.selectedUnit = null;
|
| 163 |
+
|
| 164 |
+
// 적색군 유닛 배치
|
| 165 |
+
for(let i = 0; i < 6; i++) {
|
| 166 |
+
gameState.units.push(new Unit('Infantry', 'red', 0, i));
|
| 167 |
+
}
|
| 168 |
+
for(let i = 0; i < 3; i++) {
|
| 169 |
+
gameState.units.push(new Unit('Archer', 'red', 1, i*2));
|
| 170 |
+
gameState.units.push(new Unit('Cavalry', 'red', 2, i*2));
|
| 171 |
+
}
|
| 172 |
|
| 173 |
+
// 청색군 유닛 배치
|
| 174 |
+
for(let i = 0; i < 6; i++) {
|
| 175 |
+
gameState.units.push(new Unit('Infantry', 'blue', 9, i));
|
| 176 |
+
}
|
| 177 |
+
for(let i = 0; i < 3; i++) {
|
| 178 |
+
gameState.units.push(new Unit('Archer', 'blue', 8, i*2));
|
| 179 |
+
gameState.units.push(new Unit('Cavalry', 'blue', 7, i*2));
|
| 180 |
+
}
|
| 181 |
}
|
| 182 |
+
|
| 183 |
+
// 격자 그리기
|
| 184 |
+
function drawGrid() {
|
| 185 |
+
ctx.strokeStyle = '#ccc';
|
| 186 |
+
ctx.lineWidth = 1;
|
| 187 |
+
|
| 188 |
+
for(let x = 0; x <= canvas.width; x += GRID_SIZE) {
|
| 189 |
+
ctx.beginPath();
|
| 190 |
+
ctx.moveTo(x, 0);
|
| 191 |
+
ctx.lineTo(x, canvas.height);
|
| 192 |
+
ctx.stroke();
|
|
|
|
| 193 |
}
|
| 194 |
+
|
| 195 |
+
for(let y = 0; y <= canvas.height; y += GRID_SIZE) {
|
| 196 |
+
ctx.beginPath();
|
| 197 |
+
ctx.moveTo(0, y);
|
| 198 |
+
ctx.lineTo(canvas.width, y);
|
| 199 |
+
ctx.stroke();
|
| 200 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 201 |
}
|
|
|
|
|
|
|
|
|
|
| 202 |
|
| 203 |
+
// 지형 그리기
|
| 204 |
+
function drawTerrain() {
|
| 205 |
+
// 배경
|
| 206 |
+
ctx.fillStyle = '#e8e8d0';
|
| 207 |
+
ctx.fillRect(0, 0, canvas.width, canvas.height);
|
| 208 |
+
|
| 209 |
+
// 높낮이 표시
|
| 210 |
+
elevationData.forEach((row, y) => {
|
| 211 |
+
row.forEach((height, x) => {
|
| 212 |
+
ctx.fillStyle = `rgba(0,0,0,${height * 0.1})`;
|
| 213 |
+
ctx.fillRect(x * GRID_SIZE, y * GRID_SIZE, GRID_SIZE, GRID_SIZE);
|
| 214 |
+
});
|
| 215 |
+
});
|
| 216 |
+
|
| 217 |
+
// 강
|
| 218 |
+
ctx.strokeStyle = '#4040ff';
|
| 219 |
+
ctx.lineWidth = 20;
|
| 220 |
+
ctx.beginPath();
|
| 221 |
+
ctx.moveTo(0, canvas.height/2);
|
| 222 |
+
ctx.bezierCurveTo(
|
| 223 |
+
canvas.width/4, canvas.height/2 - 40,
|
| 224 |
+
canvas.width*3/4, canvas.height/2 + 40,
|
| 225 |
+
canvas.width, canvas.height/2
|
| 226 |
+
);
|
| 227 |
+
ctx.stroke();
|
| 228 |
+
}
|
| 229 |
|
| 230 |
+
// 게임 화면 갱신
|
| 231 |
+
function render() {
|
| 232 |
+
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
| 233 |
+
|
| 234 |
+
drawTerrain();
|
| 235 |
+
drawGrid();
|
| 236 |
+
|
| 237 |
+
// 유닛 그리기
|
| 238 |
+
gameState.units.forEach(unit => unit.draw());
|
| 239 |
+
|
| 240 |
+
// 선택된 유닛 하이라이트
|
| 241 |
+
if(gameState.selectedUnit) {
|
| 242 |
+
const x = gameState.selectedUnit.x * GRID_SIZE;
|
| 243 |
+
const y = gameState.selectedUnit.y * GRID_SIZE;
|
| 244 |
+
ctx.strokeStyle = '#ffff00';
|
| 245 |
+
ctx.lineWidth = 3;
|
| 246 |
+
ctx.strokeRect(x, y, GRID_SIZE, GRID_SIZE);
|
| 247 |
+
}
|
| 248 |
+
|
| 249 |
+
// 턴 정보 업데이트
|
| 250 |
+
document.getElementById('turnInfo').textContent =
|
| 251 |
+
`현재 턴: ${gameState.currentTurn === 'red' ? '적색군' : '청색군'} (${gameState.turnCount}턴)`;
|
| 252 |
+
|
| 253 |
+
document.getElementById('selectedUnit').textContent =
|
| 254 |
+
gameState.selectedUnit ?
|
| 255 |
+
`선택된 유닛: ${gameState.selectedUnit.type} (HP: ${gameState.selectedUnit.hp})` :
|
| 256 |
+
'선택된 유닛: 없음';
|
| 257 |
+
}
|
| 258 |
|
| 259 |
+
// 이벤트 리스너
|
| 260 |
+
canvas.addEventListener('click', handleClick);
|
| 261 |
+
document.getElementById('endTurn').addEventListener('click', endTurn);
|
| 262 |
+
document.getElementById('resetGame').addEventListener('click', () => {
|
| 263 |
+
initGame();
|
| 264 |
+
render();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 265 |
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 266 |
|
| 267 |
+
function handleClick(e) {
|
| 268 |
+
const rect = canvas.getBoundingClientRect();
|
| 269 |
+
const x = Math.floor((e.clientX - rect.left) / GRID_SIZE);
|
| 270 |
+
const y = Math.floor((e.clientY - rect.top) / GRID_SIZE);
|
| 271 |
+
|
| 272 |
+
const clickedUnit = gameState.units.find(unit => unit.x === x && unit.y === y);
|
| 273 |
+
|
| 274 |
+
if(clickedUnit && clickedUnit.team === gameState.currentTurn) {
|
| 275 |
+
gameState.selectedUnit = clickedUnit;
|
| 276 |
+
} else if(gameState.selectedUnit) {
|
| 277 |
+
// 이동 또는 공격 로직
|
| 278 |
+
moveUnit(gameState.selectedUnit, x, y);
|
| 279 |
+
}
|
| 280 |
+
|
| 281 |
+
render();
|
| 282 |
}
|
| 283 |
|
| 284 |
+
function moveUnit(unit, targetX, targetY) {
|
| 285 |
+
if(!unit.moved && isValidMove(unit, targetX, targetY)) {
|
| 286 |
+
unit.x = targetX;
|
| 287 |
+
unit.y = targetY;
|
| 288 |
+
unit.moved = true;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 289 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 290 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 291 |
|
| 292 |
+
function isValidMove(unit, targetX, targetY) {
|
| 293 |
+
const dx = Math.abs(targetX - unit.x);
|
| 294 |
+
const dy = Math.abs(targetY - unit.y);
|
| 295 |
+
return dx + dy <= unit.move;
|
| 296 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 297 |
|
| 298 |
+
function endTurn() {
|
| 299 |
+
gameState.currentTurn = gameState.currentTurn === 'red' ? 'blue' : 'red';
|
| 300 |
+
if(gameState.currentTurn === 'red') gameState.turnCount++;
|
| 301 |
+
|
| 302 |
+
// 유닛 상태 초기화
|
| 303 |
+
gameState.units.forEach(unit => {
|
| 304 |
+
unit.moved = false;
|
| 305 |
+
unit.attacked = false;
|
| 306 |
+
});
|
| 307 |
+
|
| 308 |
+
gameState.selectedUnit = null;
|
| 309 |
+
render();
|
| 310 |
+
}
|
| 311 |
|
| 312 |
+
// 게임 시작
|
| 313 |
+
initGame();
|
| 314 |
+
render();
|
| 315 |
+
</script>
|
|
|
|
|
|
|
| 316 |
</body>
|
| 317 |
+
</html>
|