diff options
Diffstat (limited to 'src/sim')
-rw-r--r-- | src/sim/index.html | 183 | ||||
-rw-r--r-- | src/sim/server.js | 10 | ||||
-rw-r--r-- | src/sim/static/items.js | 21 | ||||
-rw-r--r-- | src/sim/static/styles.css | 4 | ||||
-rw-r--r-- | src/sim/static/tiles.js | 73 |
5 files changed, 291 insertions, 0 deletions
diff --git a/src/sim/index.html b/src/sim/index.html new file mode 100644 index 0000000..b9330e0 --- /dev/null +++ b/src/sim/index.html @@ -0,0 +1,183 @@ +<!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; overflow:hidden;"> + <div> + <div>Population: <span id="population"></span></div> + <div>Gold: <span id="gold"></span></div> + <div>Resources: <span id="resources"></span></div> + <button onclick="status = ''; update()">Update</button> + </div> + <canvas id="canvas"></canvas> + <div id="status"></div> + <div id="shopDiv"></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 populationEl = document.getElementById("population") + var goldEl = document.getElementById("gold") + var shopEl = document.getElementById("shopDiv") + var statusEl = document.getElementById("status") + var resourceEl = document.getElementById("resources") + + var population, gold, resources; + var shopItems = []; + var queuedTile + + var width = 10 + var height = 10 + var tileSize = 40; + var status = "" + + var t + var eventQueue + + 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 + level = [] + + for (var i = 0; i < width; i++) { + var row = [] + for (var j = 0; j < height; j++) { + row.push(undefined) + } + level.push(row) + } + level[Math.floor(width / 2)][Math.floor(height / 2)] = new Town() + + population = 10 + gold = 5 + resources = 0 + + t = 0 + eventQueue = { + 5: getScout(), + 15: getScout(), + 20: getScout(), + 30: getScout(), + } + addShopItem(getScout()) + } + function draw() { + ctx.fillStyle = "#009900" + ctx.fillRect(0, 0, width * tileSize, height * tileSize) + for (var i = 0; i < width; i++) { + var row = 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() + } + + populationEl.innerText = population + goldEl.innerText = gold + statusEl.innerText = status + resourceEl.innerText = resources + } + function update() { + t++ + for (var i = 0; i < width; i++) { + var row = level[i] + for (var j = 0; j < height; j++) { + if (row[j]) { + processUpdate(row[j].update()) + } + } + } + if(eventQueue[t]){ + addShopItem(eventQueue[t]) + } + + draw() + } + function keyPush(e) { + status = "" + update() + } + function mouseClick(e) { + if(queuedTile){ + var x = Math.floor((e.x - canvas.offsetLeft) / tileSize) + var y = Math.floor((e.y - canvas.offsetTop) / tileSize) + if(x < 0 || width <= x || y < 0 || height <= y){ + return + } + if(level[x][y] == undefined){ + level[x][y] = queuedTile + status = `You placed the ${queuedTile.name}` + queuedTile = undefined + } else { + status = `You cannot place a tile there.` + } + update() + } + } + 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 <= gold){ + shopItems = shopItems.filter(it => it.id != item.id) + shopDiv.removeChild(newButton) + gold -= item.cost + processUpdate(item.callback()) + } else { + status = "not enough gold" + } + update() + } + shopDiv.appendChild(newButton) + update() + } + function processUpdate(response){ + if(response.tile){ + queuedTile = response.tile + } else if(response.gold){ + gold += response.gold + } else if(response.population){ + population += response.population + } else if(response.resources){ + resources += response.resources + } + } + </script> +</body> + +</html>
\ No newline at end of file diff --git a/src/sim/server.js b/src/sim/server.js new file mode 100644 index 0000000..5328b03 --- /dev/null +++ b/src/sim/server.js @@ -0,0 +1,10 @@ +function setUpRoutes(server, models, jwtFunctions, database) { + server.get('/sim', (req, res) => res.sendFile(__dirname + "/index.html")) + server.get('/sim/styles.css', (req, res) => res.sendFile(__dirname + "/static/styles.css")) + server.get('/sim/tiles.js', (req, res) => res.sendFile(__dirname + "/static/tiles.js")) + server.get('/sim/items.js', (req, res) => res.sendFile(__dirname + "/static/items.js")) +} + +module.exports = { + setUpRoutes +}; diff --git a/src/sim/static/items.js b/src/sim/static/items.js new file mode 100644 index 0000000..40f73f1 --- /dev/null +++ b/src/sim/static/items.js @@ -0,0 +1,21 @@ +tileMap = { + 0: Mountain, + 1: Forest, + 2: Town, +} + +function getScout() { + return { + id: 1, + text: "scout", + cost: 2, + callback: function () { + var type = tileMap[Math.floor(Math.random() * 3)] + var tile = new type() + status = `Your scout found a ${tile.name}. Click to place it.` + return { + tile: tile + } + } + } +}
\ No newline at end of file diff --git a/src/sim/static/styles.css b/src/sim/static/styles.css new file mode 100644 index 0000000..febe7a0 --- /dev/null +++ b/src/sim/static/styles.css @@ -0,0 +1,4 @@ +button { + background-color: lavender; + padding: 10px 20px; +}
\ No newline at end of file diff --git a/src/sim/static/tiles.js b/src/sim/static/tiles.js new file mode 100644 index 0000000..5b31d19 --- /dev/null +++ b/src/sim/static/tiles.js @@ -0,0 +1,73 @@ +function Mountain() { + this.name = "mountain" +} +Mountain.prototype.draw = function (x, y, size) { + ctx.fillStyle = "gray" + ctx.beginPath(); + ctx.moveTo(x * size + size / 2, y * size); + ctx.lineTo(x * size, (1 + y) * (size)); + ctx.lineTo((1 + x) * (size), (1 + y) * (size)); + ctx.stroke(); + ctx.fill(); +} +Mountain.prototype.update = function () { + var event = undefined + if(Math.random() < 0.1){ + event = function(){ + + } + } + return { + gold: 1, + event: event + } +} + +function Forest() { + this.name = "forest" +} +Forest.prototype.draw = function (x, y, size) { + ctx.fillStyle = "green" + ctx.beginPath(); + ctx.moveTo(x * size + size / 2, y * size); + ctx.lineTo(x * size, (1 + y) * (size)); + ctx.lineTo((1 + x) * (size), (1 + y) * (size)); + ctx.stroke(); + ctx.fill(); +} +Forest.prototype.update = function () { + var event = {} + if(Math.random() < 0.1){ + event.status = "A tree monster taks half of your" + } + return { + resources: Math.floor(Math.random() * 5), + event: event + } +} + +function Town() { + this.name = "town" +} +Town.prototype.draw = function (x, y, size) { + ctx.fillStyle = "brown" + ctx.stokeStyle = "brown" + ctx.beginPath(); + ctx.moveTo(x * size + size / 2, y * size); + ctx.lineTo(x * size, (1 + y) * (size)); + ctx.lineTo((1 + x) * (size), (1 + y) * (size)); + ctx.stroke(); + ctx.fill(); +} +Town.prototype.update = function () { + var event = undefined + if(Math.random() < 0.1){ + event = function(){ + + } + } + return { + population: Math.floor(Math.random() * 4) - 1, + event: event + } +} |