Menu.js (raw)
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 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 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 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 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 |
// Menu.js & uStrat (c) Derek Stevens <nilix@nilfm.cc> // this file contains the menu system and definitions for // individual menus window.menuSys = {}; menuSys.cursor = 0; menuSys.menu = {}; menuSys.gfx = {} menuSys.gfx.border = new Image(); menuSys.gfx.border.src = "assets/ui/menuEdge.png"; menuSys.gfx.body = new Image(); menuSys.gfx.body.src = "assets/ui/menuMid.png"; menuSys.gfx.cursor = new Image(); menuSys.gfx.cursor.src = "assets/ui/menuCursor.png"; menuSys.ticker = new Date(); menuSys.lastMove = new Date(); menuSys.input = function() { this.ticker = new Date(); if (this.ticker - this.lastMove >= 300) { if (controller.up) { this.cursor--; } if (controller.down) { this.cursor++; } if (controller.x) { this.ascend(); } if (controller.y) { this.menu.select(); } if (menuSys.cursor >= menuSys.menu.items.length) { menuSys.cursor = 0;} if (menuSys.cursor < 0) { menuSys.cursor = menuSys.menu.items.length - 1; } if (controller.up || controller.down || controller.x || controller.y) { this.lastMove = new Date() }; } } menuSys.ascend = function() { switch (gameState.flow) { case "mainMenu": gameState.flow = "freeLook"; menuSys.cursor = 0; break; case "buildMenu": case "fabricateMenu": case "equipmentMenu": generateMainMenu(); break; } } function resetFlow() { gameState.flow = "freeLook"; controller.x = false; controller.y = false; } function menuItem(text, func) { this.text = text; this.f = func; } function blankMenu() { menuSys.lastMove = new Date(); this.items = new Array(); this.child = {}; this.addItem = function(text, func, mode) { this.items.push(new menuItem(text, func, mode)); } this.select = function() { this.items[menuSys.cursor].f(); menuSys.cursor = 0; controller.y = false; } } menuSys.draw = function() { screen.font = "8px console"; screen.textBaseline = 'top'; screen.fillStyle = "white"; screen.drawImage(menuSys.gfx.border, 155, 70); let i; for (i = 0; i < menuSys.menu.items.length; i++) { screen.drawImage(menuSys.gfx.body, 155, 71 + 12*i); screen.fillText(menuSys.menu.items[i].text, 167, 71 + 12*i + 1); } screen.drawImage(menuSys.gfx.border, 155, 71 + 12*menuSys.menu.items.length); screen.drawImage(menuSys.gfx.cursor, 157, 71 + 12*menuSys.cursor); } function generateMainMenu() { menuSys.menu = mainMenu(); gameState.flow = "mainMenu"; } function mainMenu() { let thisCell = map.data[mapCursor.x][mapCursor.y]; let self = new blankMenu(); if (gameState.phase == "cpu") { self.addItem("End Phase", endPhase); return self; } if (thisCell.structure == null && dist(mapCursor, teams.p1.structs[0])*15 <= teams.p1.energy) { self.addItem("Build", generateBuildMenu); } if (thisCell.structure !== null && thisCell.structure.name == "p1 factory") { self.addItem("Fabricate", generateFabricateMenu); } if (thisCell.unit !== null && thisCell.structure !== null && thisCell.structure.name == "p1 armory") { self.addItem("Equipment", generateEquipmentMenu); } if (dist(mapCursor, teams.p1.structs[0])*15 <= teams.p1.energy && thisCell.structure === null) { self.addItem("Slash&Burn", burnCell); } if (thisCell.structure !== null && thisCell.structure.name.startsWith("p1")) { // self.addItem("Demolish", startDemolishMode); } if (thisCell.unit !== null && !thisCell.unit.hasMoved && thisCell.unit.name.startsWith("p1")) { self.addItem("Move", startMoveMode); } if (thisCell.unit !== null && thisCell.unit.name.startsWith("p1") && !thisCell.unit.hasAttacked && thisCell.unit.equipment !== null) { self.addItem("Attack", attack.init); } self.addItem("End Phase", endPhase); return self; } function buildMenu() { let thisCell = map.data[mapCursor.x][mapCursor.y]; let self = new blankMenu(); if (teams.p1.energy >= 20 && thisCell.type != "water" && thisCell.type != "forest") { self.addItem("E20 Solar Plant", buildSolar); } if (teams.p1.energy >= 30 && thisCell.type == "water") { self.addItem("E30 Tidal Plant", buildTidal); } if (teams.p1.energy >= 30 && thisCell.type == "mountain") { self.addItem("E30 Mine", buildMine); } if (teams.p1.energy >= 40 && thisCell.type != "water" && thisCell.type != "forest" && thisCell.type != "city" && thisCell.type != "mountain") { self.addItem("E40 Factory", buildFactory); } if (teams.p1.energy >= 40 && thisCell.type != "water" && thisCell.type != "forest" && thisCell.type != "city" && thisCell.type != "mountain") { self.addItem("E40 Armory", buildArmory); } if (self.items.length == 0) { self.addItem("Nothing", resetFlow); } return self } function generateBuildMenu() { menuSys.menu = buildMenu(); gameState.flow = "buildMenu"; } function generateFabricateMenu() { menuSys.menu = fabricateMenu(); gameState.flow = "fabricateMenu"; } function fabricateMenu() { let thisCell = map.data[mapCursor.x][mapCursor.y]; let self = new blankMenu(); if (teams.p1.energy >= 30) { self.addItem("E30 Ranger", buildRanger); } if (teams.p1.energy >= 50) { self.addItem("E50 Knight", buildKnight); } if (teams.p1.energy >= 80) { self.addItem("E80 Ace", buildAce); } if (teams.p1.energy >= 120) { self.addItem("E120 Battle Angel", buildBattleAngel); } if (teams.p1.energy >= 200) { self.addItem("E200 BlitzWalker", buildBlitzWalker); } if (self.items.length == 0) { self.addItem("Nothing", resetFlow); } return self; } function generateEquipmentMenu() { menuSys.menu = equipmentMenu(); gameState.flow = "equipmentMenu"; } function equipmentMenu() { let thisCell = map.data[mapCursor.x][mapCursor.y]; let self = new blankMenu(); if (teams.p1.energy >= 15) { self.addItem("E15 Beam Sabre", equipOrUpgradeBeamSabreMKI); } if (teams.p1.energy >= 12) { self.addItem("E12 Rifle", equipOrUpgradeRifleMKI); } if (teams.p1.energy >= 45) { self.addItem("E45 Beam Cannon", equipOrUpgradeBeamCannon); } if (teams.p1.energy >= 80) { self.addItem("E80 Hell Bomb", equipOrUpgradeHellBomb); } if (self.items.length == 0) { self.addItem("Nothing", resetFlow); } return self; } function startMoveMode() { gameState.flow = "moveUnit"; movement.init(map.data[mapCursor.x][mapCursor.y].unit); } function burnCell() { map.degradeCell(map.data[mapCursor.x][mapCursor.y]); gameState.ruin++; gameState.flow="freeLook"; } |