aboutsummaryrefslogtreecommitdiff
path: root/src/sim/index.html
blob: c3f7926dfa7dcee713f17787e731d20f5e22ab01 (plain)
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
<!doctype html>
<html lang="en">

<head>
    <title>Sim</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <link rel="stylesheet" type="text/css" href="/sim/styles.css">
</head>

<body style="padding:0; margin:0;">
    <div>
        <div>Population: <span id="population"></span></div>
        <div>Gold: <span id="gold"></span></div>
        <div>Resources: <span id="resources"></span></div>
        <!-- <button onclick="update()">Update</button> -->
    </div>
    <div style="display: flex">
        <canvas style="padding: 1em;" id="canvas"></canvas>
        <p>Towns create population, Mountains mine gold, Forests farm resources</p>
    </div>
    <div id="shopDiv"></div>
    <div style="overflow: scroll; height: 10em; border: 1px solid black"><ul id="status"></ul></div>
    <script src="/highscore.js"></script>
    <script src="/sim/tiles.js"></script>
    <script src="/sim/items.js"></script>
    <script>
        var score;
        var username = undefined

        var game
        var shopItems = [];

        var width = 10
        var height = 10
        var tileSize = 40;

        var t
        var eventQueue, messages, bindings

        var level
        window.onload = function () {
            canvas = document.getElementById("canvas");
            canvas.width = width * tileSize
            canvas.height = height * tileSize
            ctx = canvas.getContext("2d");
            document.addEventListener("keydown", keyPush);
            document.addEventListener("click", mouseClick);
            init();
            draw()
        }
        function init() {
            score = 0
            

            game = {}
            game.population = 10
            game.gold = 5
            game.resources = 0
            game.level = []
            for (var i = 0; i < width; i++) {
                var row = []
                for (var j = 0; j < height; j++) {
                    row.push(undefined)
                }
                game.level.push(row)
            }
            game.level[Math.floor(width / 2)][Math.floor(height / 2)] = new Town()

            messages = []

            bindings = []
            bindTo(document.getElementById("population"), () => Math.floor(game.population))
            bindTo(document.getElementById("gold"), () => Math.floor(game.gold))
            bindTo(document.getElementById("resources"), () => Math.floor(game.resources))
            var statusEl = document.getElementById('status')
            bindTo(statusEl, ()=>{
                if(messages.length > 0){
                    var string = "<li>" + messages.join("</li><li>") + "</li>"
                    messages = []
                    statusEl.scrollTop = 0
                    return string + statusEl.innerHTML
                } else {
                    return statusEl.innerHTML
                }
            });

            t = 0
            eventQueue = {
                5: getScout(),
                15: getScout(),
                20: getScout(),
                30: getScout(),
            }
            addShopItem(getScout())

            gameInterval = setInterval(update, 1000)
        }
        function draw() {
            ctx.fillStyle = "#009900"
            ctx.fillRect(0, 0, width * tileSize, height * tileSize)
            for (var i = 0; i < width; i++) {
                var row = game.level[i]
                for (var j = 0; j < height; j++) {
                    if (row[j]) {
                        row[j].draw(i, j, tileSize)
                    }
                }
            }
            for (var i = 0; i <= height; i++) {
                ctx.strokeStyle = "#aaa"
                ctx.moveTo(0, i * tileSize)
                ctx.lineTo(tileSize * width, i * tileSize)
                ctx.stroke()
            }
            for (var i = 0; i <= width; i++) {
                ctx.strokeStyle = "#aaa"
                ctx.moveTo(i * tileSize, 0)
                ctx.lineTo(i * tileSize, tileSize * height)
                ctx.stroke()
            }
        }
        function update() {
            t++
            for (var i = 0; i < width; i++) {
                var row = game.level[i]
                for (var j = 0; j < height; j++) {
                    if (row[j]) {
                        row[j].update(game)
                    }
                }
            }
            if (eventQueue[t]) {
                addShopItem(eventQueue[t])
            }
            applyBindings()
            draw()
        }
        function keyPush(e) {
        }
        function mouseClick(e) {
        }
        function addShopItem(item) {
            shopItems.push(item)

            var newButton = document.createElement("button")
            newButton.innerText = item.text + ": " + item.cost
            newButton.id = item.id
            newButton.onclick = function () {
                if (item.cost <= game.gold) {
                    shopItems = shopItems.filter(it => it.id != item.id)
                    shopDiv.removeChild(newButton)
                    game.gold -= item.cost
                    item.callback(game)
                } else {
                    message.push("not enough gold")
                }
                update()
            }
            shopDiv.appendChild(newButton)
            update()
        }
        function bindTo(element, callback) {
            bindings.push([element, callback])
        }
        function applyBindings() {
            bindings.forEach(binding => {
                binding[0].innerHTML = binding[1]()
            })
        }
    </script>
</body>

</html>