wobblyhead hg > minor_monsters
comparison game/js/game.js @ 11:a77857594150
adjusted tower building, added preview dragging
| author | ark |
|---|---|
| date | 2026-10-19 17:56 +0200 |
| parents | 2c4fed8baed9 |
| children | 8df95050fc8b |
comparison
equal
deleted
inserted
replaced
| 10:22b604680c5a | 11:a77857594150 |
|---|---|
| 154 this.selected = null; | 154 this.selected = null; |
| 155 this.preview = null; | 155 this.preview = null; |
| 156 this.shop = this.shop === type ? null : type; | 156 this.shop = this.shop === type ? null : type; |
| 157 }; | 157 }; |
| 158 | 158 |
| 159 // a tap on the map at map position x, y. hitRadius is how close the tap | 159 // a press on the map at map position x, y. hitRadius is how close the |
| 160 // must be to the preview to build. returns false when no legal position | 160 // press must be to the preview to count as a press on it. returns |
| 161 // was found for a preview. | 161 // "preview" for a press on the preview, "place" for any other press |
| 162 Game.prototype.tapMap = function (x, y, hitRadius) { | 162 // while a shop tower is chosen, and null when the press selected or |
| 163 // deselected a built tower. | |
| 164 Game.prototype.pressMap = function (x, y, hitRadius) { | |
| 165 var onPreview = this.preview && Math.hypot(this.preview.x - x, this.preview.y - y) <= hitRadius; | |
| 163 var t = this.towerAt(x, y); | 166 var t = this.towerAt(x, y); |
| 164 this.sellArmed = false; | 167 this.sellArmed = false; |
| 165 if (t && !(this.preview && Math.hypot(this.preview.x - x, this.preview.y - y) <= hitRadius)) { | 168 if (t && !onPreview) { |
| 166 this.shop = null; | 169 this.shop = null; |
| 167 this.preview = null; | 170 this.preview = null; |
| 168 this.selected = t === this.selected ? null : t; | 171 this.selected = t === this.selected ? null : t; |
| 169 return true; | 172 return null; |
| 170 } | 173 } |
| 171 this.selected = null; | 174 this.selected = null; |
| 172 if (!this.shop) return true; | 175 if (!this.shop) return null; |
| 173 if (this.preview && Math.hypot(this.preview.x - x, this.preview.y - y) <= hitRadius) { | 176 return onPreview ? "preview" : "place"; |
| 174 return this.build(); | 177 }; |
| 175 } | 178 |
| 176 this.preview = this.map.snap(x, y, this.towers); | 179 // move the preview to the nearest legal position to map position x, y. |
| 177 if (!this.preview) return false; | 180 // where there is none, the preview stays where it was. returns false |
| 181 // when no legal position was found. | |
| 182 Game.prototype.movePreview = function (x, y) { | |
| 183 if (!this.shop) return false; | |
| 184 var pv = this.map.snap(x, y, this.towers); | |
| 185 if (!pv) return false; | |
| 186 if (this.preview && this.preview.x === pv.x && this.preview.y === pv.y) return true; | |
| 178 // t0 draws spikes only towards the path, judged by its top level range | 187 // t0 draws spikes only towards the path, judged by its top level range |
| 179 var def = cfg.tower[this.shop]; | 188 var def = cfg.tower[this.shop]; |
| 180 if (def.projectile_speed === 0) { | 189 if (def.projectile_speed === 0) { |
| 181 this.preview.sides = this.map.facingSides(this.preview.x, this.preview.y, | 190 pv.sides = this.map.facingSides(pv.x, pv.y, def.range[def.range.length - 1]); |
| 182 def.range[def.range.length - 1]); | 191 } |
| 183 } | 192 this.preview = pv; |
| 184 return true; | 193 return true; |
| 185 }; | 194 }; |
| 186 | 195 |
| 196 // build the shop tower at the preview. returns false when there is no | |
| 197 // preview or the tower cannot be afforded. | |
| 187 Game.prototype.build = function () { | 198 Game.prototype.build = function () { |
| 199 if (!this.shop || !this.preview) return false; | |
| 188 var cost = cfg.tower[this.shop].cost; | 200 var cost = cfg.tower[this.shop].cost; |
| 189 if (!this.canAfford(cost)) return false; | 201 if (!this.canAfford(cost)) return false; |
| 190 this.pay(cost); | 202 this.pay(cost); |
| 191 var pv = this.preview; | 203 var pv = this.preview; |
| 192 var t = MM.tower.create(this.shop, pv.x, pv.y, pv.sides); | 204 var t = MM.tower.create(this.shop, pv.x, pv.y, pv.sides); |
