-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
175 lines (175 loc) · 5.54 KB
/
index.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
<html>
<head>
<script type="module">
import Map, {Building, Tile, TileResult} from "./map.js";
import Topdown, {Spritesheet} from "./renderers/topdownsprite.js"
import Controller from "./controller.js"
import {Unit} from "./player.js"
window.c = new Controller(new Map({
length: 12,
width: 12,
abundance: 1,
smoothIterations: 3
}), document.getElementById("screen"), Topdown, 64, new Spritesheet("./res/spritesheet.png", 32, {
"terrains.base.field": {
x: 0,
y: 0,
},
"terrains.base.forest": {
x: 1,
y: 0,
},
"terrains.base.mountain": {
x: 2,
y: 0,
},
"buildings.base.city": {
x: 0,
y: 1,
},
"units.base.warrior": {
x: 2,
y: 1,
},
"availablemove": {
x: 0,
y: 2,
},
"availableattack": {
x: 1,
y: 2,
},
}))
c.map.addPlayers([
{
name: "Player 1",
style: "orange"
},
{
name: "Player 2",
style: "yellow"
},
{
name: "Player 3",
style: "olive"
},
{
name: "Player 4",
style: "red"
}
]);
c.map.createOcean();
c.renderer.render(c.map);
c.start();
displayPlayer();
document.getElementById("turnsleft").innerText = `${c.turnsLeft} turns`;
function canvasClick(event) {
debugger;
var canvas = document.getElementById("screen")
var rect = canvas.getBoundingClientRect();
var x = Math.floor((event.clientX - rect.left) * c.map.length / canvas.width);
var y = Math.floor((event.clientY - rect.top) * c.map.width / canvas.height);
c.handleClick(x, y);
displaySelected();
}
function displaySelected() {
var selected = document.getElementById("selected")
document.getElementById("capture").disabled = true;
document.getElementById("createunit").disabled = true;
switch(c.selected.constructor) {
case Unit:
selected.innerText = c.selected.name;
selected.style.color = c.selectedParent.style;
if (c.canCapture(c.selected)) {
document.getElementById("capture").disabled = false;
}
break;
case Building:
selected.innerText = c.selected.type.name;
selected.style.color = (c.selectedParent !== undefined) ? c.selected.owner.style : "white";
if (c.canCreateUnit()) {
document.getElementById("createunit").disabled = false;
}
break;
case TileResult:
selected.innerText = c.selected.tile.terrain.name;
selected.style.color = (c.selectedParent !== undefined) ? c.selectedParent.style : "white";
break;
}
}
function displayPlayer() {
document.getElementById("currentplayer").innerText = `${c.currentPlayer.name} - ${c.currentPlayer.resources} Resources`;
document.getElementById("currentplayer").style.color = c.currentPlayer.style;
document.getElementById("selected").innerText = "None Selected"
document.getElementById("selected").style.color = "white";
}
function nextTurn() {
c.endTurn();
document.getElementById("turnsleft").innerText = `${c.turnsLeft} turns`;
if (c.turnsLeft > 0) {
displayPlayer();
} else {
document.getElementById("currentplayer").innerText = "";
document.getElementById("nextturn").style.visibility = "hidden";
var winner = c.map.players.reduce((winner, player) => c.map.cityCount(player) > c.map.cityCount(player) ? player : winner);
document.getElementById("turnsleft").innerText = `Game ended! ${winner.name} is the winner!`;
document.getElementById("turnsleft").style.color = winner.style;
document.getElementById("selected").innerText = "";
}
}
document.getElementById("capture").addEventListener('click', (ev) => {c.capture(); ev.srcElement.disabled = true;}, false);
document.getElementById("createunit").addEventListener('click', (ev) => {c.createUnit(); ev.srcElement.disabled = true;displaySelected()}, false);
document.getElementById("nextturn").addEventListener('click', nextTurn, false);
document.getElementById("screen").addEventListener('click', canvasClick, false);
document.getElementById("capture").disabled = true;
document.getElementById("createunit").disabled = true;
</script>
</head>
<body style="background-color:#202020; color:white">
<div style="margin:auto; display: flex">
<div>
<canvas style="border-radius: 30px" id="screen" width="768" height="768" onclick=""></canvas>
</div>
<div style="margin-left:10px">
<h1>Clonytopia</h1>
<h2 id="turnsleft"></h2>
<h2 id="currentplayer"></h2>
<h2 id="selected">None selected</h2>
<button id="createunit">New Unit</button>
<button id="capture">Capture</button>
<button id="nextturn">End Turn</button>
<h2>
Guide
</h2>
<ul>
<li>
The player with the most cities at the end of 20 turns win.
</li>
<li>
To select something, click on it.
<ul>
<li>
If what you are looking for is under something else (like a building under a unit), click it again, and so on.
</li>
</ul>
</li>
<li>
If a unit is on a city that is not yours, you can capture it.
</li>
<li>
If you selected a unit, blue circles denoting the places that unit can move to will be shown. Red circles denote possible attacks.
</li>
<li>
Once a unit has moved, it cannot move for the rest of the turn.
</li>
<li>
You can create new units by selecting a city first. Each units cost 2 resources
</li>
<li>
At the start of each turn, each player receives the resources the same amount of cities they have.
</li>
</ul>
</div>
</div>
</body>
</html>