Compare commits

..

5 Commits

Author SHA1 Message Date
558b77603b
wave documentation 2017-08-23 19:26:40 +03:00
b6736c4da0 Merge pull request #5 from Tsa6/fix-enemy-alignment
Fix enemy alignment
2017-08-22 20:13:43 +03:00
Taizo 'Tsa6' Simpson
3bcb14f3fb Merge branch 'master' of https://github.com/LunaSquee/html5-tower-defense-experiment into fix-enemy-alignment 2017-08-22 12:49:06 -04:00
Taizo 'Tsa6' Simpson
292afea46b Simplified expressions 2017-08-22 12:37:19 -04:00
Taizo 'Tsa6' Simpson
af7dcd70c8 Added margin system to enemy rendering, fixing alignment issues 2017-08-22 12:36:10 -04:00

192
index.js
View File

@ -70,7 +70,7 @@ window.onload = function () {
tough: { tough: {
speed: 5, speed: 5,
node: 1, node: 1,
health: 80, health: 100,
reward: 20, reward: 20,
frequency: 40, frequency: 40,
icon: '#f40' icon: '#f40'
@ -124,6 +124,39 @@ 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 = { let Maps = {
width: 20, // Width of the map width: 20, // Width of the map
height: 20, // Height of the map height: 20, // Height of the map
@ -175,8 +208,8 @@ window.onload = function () {
enemies: [{ enemies: [{
type: 'basic', type: 'basic',
count: 5, count: 5,
inclCount: true, incrementCount: true,
inclHealth: true incrementHealth: true
}] }]
}, },
{ {
@ -187,14 +220,14 @@ window.onload = function () {
enemies: [{ enemies: [{
type: 'basic', type: 'basic',
count: 5, count: 5,
inclCount: true, incrementCount: true,
inclHealth: true incrementHealth: true
}, },
{ {
type: 'speedy', type: 'speedy',
count: 10, count: 10,
inclCount: true, incrementCount: true,
inclHealth: true incrementHealth: true
}] }]
}, },
{ {
@ -204,14 +237,16 @@ window.onload = function () {
enemies: [{ enemies: [{
type: 'basic', type: 'basic',
count: 5, count: 5,
inclCount: true, incrementCount: true,
inclHealth: true incrementHealth: true,
healthFactor: 10
}, },
{ {
type: 'speedy', type: 'speedy',
count: 10, count: 10,
inclCount: true, incrementCount: true,
inclHealth: true incrementHealth: true,
healthFactor: 10
}] }]
}, },
{ {
@ -221,8 +256,8 @@ window.onload = function () {
enemies: [{ enemies: [{
type: 'tough', type: 'tough',
count: 5, count: 5,
inclCount: true, incrementCount: true,
inclHealth: true incrementHealth: true
}] }]
}, },
{ {
@ -230,119 +265,10 @@ window.onload = function () {
wave: 3, wave: 3,
enemies: [{ enemies: [{
type: 'tough', type: 'tough',
count: 2 count: 1
}] }]
} }
] ]
},
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}
} }
} }
@ -741,16 +667,16 @@ window.onload = function () {
let eHealthIncl = 0 let eHealthIncl = 0
let multiply = wv.oneAfterAnother != null ? wv.oneAfterAnother : false let multiply = wv.oneAfterAnother != null ? wv.oneAfterAnother : false
if (e.inclCount === true) { if (e.incrementCount === true) {
eCount += Game.wave eCount += Game.wave * (e.countFactor != null ? e.countFactor : 1)
} }
if (e.baseHealth) { if (e.baseHealth) {
eHealthIncl = e.baseHealth eHealthIncl = e.baseHealth
} }
if (e.inclHealth === true) { if (e.incrementHealth === true) {
eHealthIncl = Game.wave * 5 eHealthIncl = Game.wave * (e.healthFactor != null ? e.healthFactor : 5)
if (eHealthIncl > 500) { if (eHealthIncl > 500) {
eHealthIncl = 500 eHealthIncl = 500
} }
@ -977,7 +903,7 @@ window.onload = function () {
if (can === false) break if (can === false) break
let tower = Game.towers[j] let tower = Game.towers[j]
// tower placement restriction visualization // Tower placement restriction around the tower
for (let i = 0; i < 4; i++) { for (let i = 0; i < 4; i++) {
if (can === false) break if (can === false) break
let ax = tower.x let ax = tower.x
@ -1136,11 +1062,13 @@ window.onload = function () {
// Draw enemies // Draw enemies
for (let i in Game.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 enemy = Game.enemies[i]
let rx = (enemy.x * mt) + mt / 8 let rx = (enemy.x + margin) * mt
let ry = (enemy.y * mt) + mt / 8 let ry = (enemy.y + margin) * mt
let w = mt * (1 - margin * 2)
ctx.fillStyle = enemy.icon ctx.fillStyle = enemy.icon
ctx.fillRect(rx, ry, 16, 16) ctx.fillRect(rx, ry, w, w)
// health bars // health bars
let hx = rx - 6 let hx = rx - 6
@ -1263,7 +1191,7 @@ window.onload = function () {
} }
function initialize () { function initialize () {
Game.map = Maps.generate() Game.map = Maps.first
// Next wave button // Next wave button
Components.wave = new ButtonComponent('Next Wave', '#fff', '#11f', 650, 570, 200, 60, () => { Components.wave = new ButtonComponent('Next Wave', '#fff', '#11f', 650, 570, 200, 60, () => {