Compare commits

..

3 Commits

Author SHA1 Message Date
Taizo 'Tsa6' Simpson
6456ef73c7 Randomly generated maps inherit waves from the first map 2017-08-22 13:18:00 -04:00
Taizo 'Tsa6' Simpson
5d7e697525 Merge branch 'master' of https://github.com/LunaSquee/html5-tower-defense-experiment into random-map-generation 2017-08-22 13:05:35 -04:00
Taizo 'Tsa6' Simpson
e4b40c9010 Added random map generation 2017-08-21 16:24:26 -04:00

192
index.js
View File

@ -70,7 +70,7 @@ window.onload = function () {
tough: {
speed: 5,
node: 1,
health: 100,
health: 80,
reward: 20,
frequency: 40,
icon: '#f40'
@ -124,39 +124,6 @@ window.onload = function () {
}
}
/*
Tiles:
* 0 - grass
* 1 - path
* 2 - spawn
* 3 - end
Wave layout:
[] - optional
<> - value type
{
type - 'recurring', 'once-every' or 'once'
* recurring
waveLow <int> - wave in which this entry will start spawning enemies
[waveHigh] <int> - wave in which this entry will stop spawning enemies
* once-every
every <int> - every x wave this entry will spawn enemies
* once
wave <int> - wave in which this entry will spawn enemies
[oneAfterAnother] <bool> - If true, enemy types specified below will be spawned one-after-another
enemies: [{
type <string> - type of enemy
count <int> - base count to spawn
[incrementCount] <bool> - increments count by wave number
[countFactor] <int> - this number is multiplied by the wave and then added to count only if incrementCount is true
[incrementHealth] <bool> - increments health by wave number times healthFactor (default is 5)
[healthFactor] <int> - this number is multiplied by the wave and then added to health only if incrementHealth is true (default is 5)
}]
}
*/
let Maps = {
width: 20, // Width of the map
height: 20, // Height of the map
@ -208,8 +175,8 @@ window.onload = function () {
enemies: [{
type: 'basic',
count: 5,
incrementCount: true,
incrementHealth: true
inclCount: true,
inclHealth: true
}]
},
{
@ -220,14 +187,14 @@ window.onload = function () {
enemies: [{
type: 'basic',
count: 5,
incrementCount: true,
incrementHealth: true
inclCount: true,
inclHealth: true
},
{
type: 'speedy',
count: 10,
incrementCount: true,
incrementHealth: true
inclCount: true,
inclHealth: true
}]
},
{
@ -237,16 +204,14 @@ window.onload = function () {
enemies: [{
type: 'basic',
count: 5,
incrementCount: true,
incrementHealth: true,
healthFactor: 10
inclCount: true,
inclHealth: true
},
{
type: 'speedy',
count: 10,
incrementCount: true,
incrementHealth: true,
healthFactor: 10
inclCount: true,
inclHealth: true
}]
},
{
@ -256,8 +221,8 @@ window.onload = function () {
enemies: [{
type: 'tough',
count: 5,
incrementCount: true,
incrementHealth: true
inclCount: true,
inclHealth: true
}]
},
{
@ -265,10 +230,119 @@ window.onload = function () {
wave: 3,
enemies: [{
type: 'tough',
count: 1
count: 2
}]
}
]
},
generate: function(targetLength) {
//Initialize variables
cursor = {x: 1, y:2, end: false}
totalLength = 0
targetLength = targetLength || 66
var pathgen = [Object.assign({}, cursor)]
var tiles = []
for(var i = 0; i < this.height; i++) {
var row = Array(this.width)
row.fill(0)
tiles.push(row)
}
//Checks if a certain point is a valid next point for pathgen
//Requirements:
// No direct line shorter than three spaces to the nearest path
// Not within 1 tile of the border
function checkValidNode(point, map) {
if(point.x > 0 && point.x < map.width - 1 && point.y > 0 && point.y < map.height - 1) {
for(var x = Math.max(point.x - 3, 0); x <= Math.min(map.width-1, point.x + 3); x++) {
if(tiles[point.y][x]) {
return false;
}
}
for(var y = Math.max(point.y - 3, 0); y <= Math.min(map.height-1, point.y + 3); y++) {
if(tiles[y][point.x]) {
return false;
}
}
return true
}else{
return false
}
}
//Add populate map with nodes to the path
while(totalLength < targetLength) {
//Determine tiles available for expansion by checking each direction
directions = [
{dx: 1, dy: 0},
{dx: -1, dy: 0},
{dx: 0, dy: 1},
{dx: 0, dy: -1},
]
distances = Array(directions.length)
distances.fill(3)
for(var d = 0; d < directions.length; d++) {
while(checkValidNode({
x: cursor.x + directions[d].dx * (distances[d] + 1),
y: cursor.y + directions[d].dy * (distances[d] + 1)
}, this)){
distances[d]++
}
if(!checkValidNode({
x: cursor.x + directions[d].dx * distances[d],
y: cursor.y + directions[d].dy * distances[d]
},this)) {
directions.splice(d, 1)
distances.splice(d--, 1)
}
}
//Pick a valid tile at random
if(directions.length == 0) {
return this.generate(); //If stuck, start over
}
d = Math.floor(directions.length * Math.random())
direction = directions[d]
distance = distances[d]
amt = Math.floor(Math.random() * (distance - 3)) + 3
tile = {
x: cursor.x + direction.dx * amt,
y: cursor.y + direction.dy * amt,
end: false
}
//Load tile
for(var i = 0; i < amt; i++) {
tiles[cursor.y][cursor.x] = 1
totalLength++
cursor.x += direction.dx
cursor.y += direction.dy
}
pathgen.push(tile)
}
//Set the first and last nodes to special values
first = pathgen[0]
last = pathgen[pathgen.length-1]
tiles[first.y][first.x] = 2
tiles[last.y][last.x] = 3
last.end = true
//Debug and flatten array
tiles_out = []
console.log('Tiles: ')
for(var i = 0; i < tiles.length; i++) {
console.log(tiles[i].join(' ') + ' /' + i)
tiles_out = tiles_out.concat(tiles[i])
}
console.log('Pathgen: ')
console.log(pathgen)
console.log('Length: ',totalLength)
//Return
//Note: Currently inherits waves from the first map
return {tiles:tiles_out, pathgen:pathgen, waves:Maps.first.waves}
}
}
@ -667,16 +741,16 @@ window.onload = function () {
let eHealthIncl = 0
let multiply = wv.oneAfterAnother != null ? wv.oneAfterAnother : false
if (e.incrementCount === true) {
eCount += Game.wave * (e.countFactor != null ? e.countFactor : 1)
if (e.inclCount === true) {
eCount += Game.wave
}
if (e.baseHealth) {
eHealthIncl = e.baseHealth
}
if (e.incrementHealth === true) {
eHealthIncl = Game.wave * (e.healthFactor != null ? e.healthFactor : 5)
if (e.inclHealth === true) {
eHealthIncl = Game.wave * 5
if (eHealthIncl > 500) {
eHealthIncl = 500
}
@ -903,7 +977,7 @@ window.onload = function () {
if (can === false) break
let tower = Game.towers[j]
// Tower placement restriction around the tower
// tower placement restriction visualization
for (let i = 0; i < 4; i++) {
if (can === false) break
let ax = tower.x
@ -1062,13 +1136,11 @@ window.onload = function () {
// Draw enemies
for (let i in Game.enemies) {
let margin = .25 //A ratio of the width of a tile. .25 margins with 32 px tiles leave a 8 px margin on all sides, with the body being 16px x 16px
let enemy = Game.enemies[i]
let rx = (enemy.x + margin) * mt
let ry = (enemy.y + margin) * mt
let w = mt * (1 - margin * 2)
let rx = (enemy.x * mt) + mt / 8
let ry = (enemy.y * mt) + mt / 8
ctx.fillStyle = enemy.icon
ctx.fillRect(rx, ry, w, w)
ctx.fillRect(rx, ry, 16, 16)
// health bars
let hx = rx - 6
@ -1191,7 +1263,7 @@ window.onload = function () {
}
function initialize () {
Game.map = Maps.first
Game.map = Maps.generate()
// Next wave button
Components.wave = new ButtonComponent('Next Wave', '#fff', '#11f', 650, 570, 200, 60, () => {