2017-08-20 19:52:59 +00:00
|
|
|
window.onload = function () {
|
2017-08-21 08:46:54 +00:00
|
|
|
/*
|
|
|
|
My programming practice notes:
|
|
|
|
* every major feature as part of updating the game (such as towers, enemies)
|
|
|
|
are handeled in a separate function
|
|
|
|
* I use Object.assign to copy objects in order to eliminate references when spawning enemies
|
|
|
|
and to get the ability to modify them individually
|
|
|
|
* Components such as buttons or selections within the canvas are classes
|
|
|
|
*/
|
|
|
|
|
2017-08-20 19:52:59 +00:00
|
|
|
let canvas = document.getElementById('canvas')
|
|
|
|
let ctx = canvas.getContext('2d')
|
|
|
|
|
|
|
|
let fps = 0
|
|
|
|
let fpsDraw = 0
|
|
|
|
let fpsCount = 0
|
|
|
|
|
|
|
|
// mouse
|
|
|
|
let mX = 0
|
|
|
|
let mY = 0
|
|
|
|
let mXr = 0
|
|
|
|
let mYr = 0
|
|
|
|
|
|
|
|
let Game = {
|
|
|
|
state: 2,
|
|
|
|
enemies: [],
|
|
|
|
towers: [],
|
|
|
|
particles: [],
|
|
|
|
map: null,
|
|
|
|
health: 100,
|
|
|
|
money: 100,
|
|
|
|
enemySpawn: 0,
|
|
|
|
pace: 1,
|
|
|
|
wave: 0,
|
|
|
|
waveTimer: 0,
|
|
|
|
tower: 'simple'
|
|
|
|
}
|
|
|
|
|
2017-08-21 08:46:54 +00:00
|
|
|
/**
|
|
|
|
speed - movement speed multiplier - higher: faster
|
|
|
|
node - always 1
|
|
|
|
health - health of the enemy
|
|
|
|
reward - money earned when killed
|
|
|
|
frequency - milliseconds to spawn in
|
|
|
|
icon - currently color of the enemy
|
|
|
|
*/
|
2017-08-20 19:52:59 +00:00
|
|
|
let Enemies = {
|
|
|
|
basic: {
|
|
|
|
speed: 10,
|
|
|
|
node: 1,
|
|
|
|
health: 50,
|
2017-08-21 08:46:54 +00:00
|
|
|
reward: 10,
|
|
|
|
frequency: 1000,
|
|
|
|
icon: '#f00'
|
2017-08-20 19:52:59 +00:00
|
|
|
},
|
|
|
|
speedy: {
|
|
|
|
speed: 20,
|
|
|
|
node: 1,
|
|
|
|
health: 60,
|
2017-08-21 08:46:54 +00:00
|
|
|
reward: 15,
|
|
|
|
frequency: 500,
|
|
|
|
icon: '#f11'
|
2017-08-20 19:52:59 +00:00
|
|
|
},
|
|
|
|
tough: {
|
|
|
|
speed: 5,
|
|
|
|
node: 1,
|
|
|
|
health: 100,
|
2017-08-21 08:46:54 +00:00
|
|
|
reward: 20,
|
|
|
|
frequency: 1000,
|
|
|
|
icon: '#f40'
|
2017-08-20 19:52:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let Towers = {
|
|
|
|
simple: {
|
2017-08-21 10:10:56 +00:00
|
|
|
range: 5, // range in tiles
|
2017-08-21 08:46:54 +00:00
|
|
|
damage: 15, // damage to deal to enemies when hit
|
|
|
|
rate: 20, // rate of fire, higher - slower
|
|
|
|
name: 'Simple', // name of the tower
|
2017-08-21 10:10:56 +00:00
|
|
|
description: 'Basic tower',
|
2017-08-21 08:46:54 +00:00
|
|
|
speed: 30, // bullet speed, higher - faster
|
|
|
|
cost: 50, // cost to place
|
|
|
|
icon: '#333' // currently color
|
2017-08-21 10:10:56 +00:00
|
|
|
},
|
|
|
|
rapid: {
|
|
|
|
range: 3,
|
|
|
|
damage: 5,
|
|
|
|
rate: 5,
|
|
|
|
name: 'Rapid',
|
|
|
|
description: 'Rapid-firing tower',
|
|
|
|
speed: 30,
|
|
|
|
cost: 250,
|
|
|
|
icon: '#303'
|
|
|
|
},
|
|
|
|
snipe: {
|
|
|
|
range: 5,
|
|
|
|
damage: 150,
|
|
|
|
rate: 100,
|
|
|
|
name: 'Sniper',
|
|
|
|
description: 'Slow but powerful shots',
|
|
|
|
speed: 50,
|
|
|
|
cost: 500,
|
|
|
|
icon: '#4f3'
|
2017-08-20 19:52:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let Maps = {
|
2017-08-21 08:46:54 +00:00
|
|
|
width: 20, // Width of the map
|
|
|
|
height: 20, // Height of the map
|
|
|
|
tile: 32, // Tile size in pixels (each coordinate is multiplied by this number in rendering)
|
2017-08-20 19:52:59 +00:00
|
|
|
first: {
|
2017-08-21 08:46:54 +00:00
|
|
|
// map rendering data
|
2017-08-20 19:52:59 +00:00
|
|
|
tiles: [
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
0, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0,
|
|
|
|
0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0,
|
|
|
|
0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0,
|
|
|
|
0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0,
|
|
|
|
0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0,
|
|
|
|
0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0,
|
|
|
|
0, 3, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
],
|
2017-08-21 08:46:54 +00:00
|
|
|
// enemy follow path
|
2017-08-20 19:52:59 +00:00
|
|
|
pathgen: [
|
|
|
|
{x: 1, y: 2, end: false},
|
|
|
|
{x: 14, y: 2, end: false},
|
|
|
|
{x: 14, y: 6, end: false},
|
|
|
|
{x: 8, y: 6, end: false},
|
|
|
|
{x: 8, y: 10, end: false},
|
|
|
|
{x: 17, y: 10, end: false},
|
|
|
|
{x: 17, y: 17, end: false},
|
|
|
|
{x: 5, y: 17, end: false},
|
|
|
|
{x: 5, y: 12, end: false},
|
|
|
|
{x: 1, y: 12, end: false},
|
|
|
|
{x: 1, y: 17, end: true},
|
|
|
|
]
|
2017-08-21 01:12:50 +00:00
|
|
|
},
|
|
|
|
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
|
|
|
|
return {tiles:tiles_out, pathgen:pathgen}
|
2017-08-20 19:52:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-21 10:10:56 +00:00
|
|
|
let Components = {}
|
|
|
|
|
|
|
|
class Component {
|
|
|
|
constructor (x, y) {
|
|
|
|
this.x = x
|
|
|
|
this.y = y
|
|
|
|
}
|
|
|
|
|
|
|
|
draw () {}
|
|
|
|
update() {}
|
|
|
|
}
|
|
|
|
|
|
|
|
class ButtonComponent extends Component {
|
|
|
|
constructor (text, textColor, color, x, y, w, h, fn) {
|
|
|
|
super(x, y)
|
|
|
|
this.w = w
|
|
|
|
this.h = h
|
|
|
|
this.fn = () => {
|
|
|
|
fn.apply(this, [])
|
|
|
|
}
|
|
|
|
this.text = text
|
|
|
|
this.textColor = textColor
|
|
|
|
this.color = color
|
|
|
|
this.disabled = false
|
|
|
|
this.hovered = false
|
|
|
|
}
|
|
|
|
|
|
|
|
draw () {
|
|
|
|
if (this.font) ctx.font = this.font
|
|
|
|
ctx.fillStyle = this.color
|
2017-08-20 19:52:59 +00:00
|
|
|
ctx.fillRect(this.x, this.y, this.w, this.h)
|
2017-08-21 10:10:56 +00:00
|
|
|
ctx.fillStyle = this.textColor
|
|
|
|
let txtMeasure = ctx.measureText(this.text)
|
2017-08-20 19:52:59 +00:00
|
|
|
let tx = this.x + (this.w / 2 - txtMeasure.width / 2)
|
|
|
|
let ty = this.y + (this.h / 2) * 1.2
|
|
|
|
|
2017-08-21 10:10:56 +00:00
|
|
|
ctx.fillText(this.text, tx, ty)
|
2017-08-20 19:52:59 +00:00
|
|
|
|
|
|
|
if (this.disabled) {
|
|
|
|
ctx.fillStyle = 'rgba(0, 0, 0, 0.45)'
|
|
|
|
ctx.fillRect(this.x, this.y, this.w, this.h)
|
|
|
|
} else if (this.hovered) {
|
|
|
|
ctx.fillStyle = 'rgba(255, 255, 255, 0.25)'
|
|
|
|
ctx.fillRect(this.x, this.y, this.w, this.h)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-21 10:10:56 +00:00
|
|
|
setDisabled (disable) {
|
|
|
|
this.disabled = typeof disabled === 'boolean' ? disabled : !this.disabled
|
2017-08-20 19:52:59 +00:00
|
|
|
}
|
2017-08-21 10:10:56 +00:00
|
|
|
}
|
2017-08-20 19:52:59 +00:00
|
|
|
|
2017-08-21 10:10:56 +00:00
|
|
|
class TowerButton extends ButtonComponent {
|
|
|
|
constructor (tower, i) {
|
|
|
|
super()
|
|
|
|
this.active = false
|
|
|
|
this.w = 50
|
|
|
|
this.h = 50
|
|
|
|
this.x = (Maps.width * Maps.tile) + (i % 4) * (this.w + 4) + 4
|
|
|
|
this.y = 80 + Math.floor(i / 4) * (this.h + 4)
|
|
|
|
this.tower = tower
|
|
|
|
this.towerObj = Towers[this.tower]
|
|
|
|
this.costText = '$' + this.towerObj.cost
|
|
|
|
this.text = this.towerObj.name
|
|
|
|
this.textColor = '#fff'
|
|
|
|
this.color = '#995522'
|
|
|
|
this.fn = this.select
|
|
|
|
this.font = '14px Helvetica'
|
|
|
|
}
|
|
|
|
|
|
|
|
select () {
|
|
|
|
Game.tower = this.tower
|
|
|
|
}
|
|
|
|
|
|
|
|
draw () {
|
|
|
|
if (this.active) {
|
|
|
|
ctx.fillStyle = '#afa'
|
|
|
|
ctx.fillRect(this.x - 2, this.y - 2, this.w + 4, this.h + 4)
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.font = '14px Helvetica'
|
|
|
|
ctx.fillStyle = this.color
|
|
|
|
ctx.fillRect(this.x, this.y, this.w, this.h)
|
|
|
|
ctx.fillStyle = this.textColor
|
|
|
|
let txtMeasure = ctx.measureText(this.text)
|
|
|
|
let tx = this.x + (this.w / 2 - txtMeasure.width / 2)
|
|
|
|
let ty = this.y + (this.h / 2) * 1
|
|
|
|
|
|
|
|
ctx.fillText(this.text, tx, ty)
|
|
|
|
|
|
|
|
ctx.font = '10px Helvetica'
|
|
|
|
if (Game.money >= this.towerObj.cost) {
|
|
|
|
ctx.fillStyle = '#0f0'
|
|
|
|
} else {
|
|
|
|
ctx.fillStyle = '#f11'
|
|
|
|
}
|
|
|
|
txtMeasure = ctx.measureText(this.costText)
|
|
|
|
tx = this.x + (this.w / 2 - txtMeasure.width / 2)
|
|
|
|
ty = this.y + (this.h / 2) * 1.6
|
|
|
|
ctx.fillText(this.costText, tx, ty)
|
|
|
|
|
|
|
|
if (this.disabled) {
|
|
|
|
ctx.fillStyle = 'rgba(0, 0, 0, 0.45)'
|
|
|
|
ctx.fillRect(this.x, this.y, this.w, this.h)
|
|
|
|
} else if (this.hovered) {
|
|
|
|
ctx.fillStyle = 'rgba(255, 255, 255, 0.25)'
|
|
|
|
ctx.fillRect(this.x, this.y, this.w, this.h)
|
|
|
|
}
|
2017-08-20 19:52:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function clickBtn () {
|
2017-08-21 10:10:56 +00:00
|
|
|
for (let i in Components) {
|
|
|
|
let btn = Components[i]
|
|
|
|
if (!(btn instanceof ButtonComponent)) continue
|
|
|
|
if (btn.disabled) continue
|
2017-08-20 19:52:59 +00:00
|
|
|
if (mXr > btn.x && mYr > btn.y && mXr < btn.x + btn.w && mYr < btn.y + btn.h) {
|
|
|
|
btn.fn()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-21 08:46:54 +00:00
|
|
|
// Use this function to spawn enemies depending on round
|
2017-08-20 19:52:59 +00:00
|
|
|
function nextWave () {
|
|
|
|
Game.wave++
|
|
|
|
|
2017-08-21 08:46:54 +00:00
|
|
|
if (Game.wave < 5) {
|
|
|
|
addEnemies(10 + Game.wave, Enemies.basic)
|
|
|
|
} else {
|
2017-08-20 19:52:59 +00:00
|
|
|
addEnemies(10 + Game.wave, Enemies.speedy)
|
|
|
|
}
|
2017-08-21 08:46:54 +00:00
|
|
|
|
|
|
|
if (Game.wave > 10) {
|
|
|
|
addEnemies(Game.wave - 5, Enemies.tough)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Game.wave % 5 === 0) {
|
|
|
|
addEnemies(Game.wave / 5, Enemies.tough)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Use this function to modify the enemies spawned each round
|
|
|
|
function waveEnemyModifer (enemy, round) {
|
|
|
|
// Reduce the time between enemy spawns
|
|
|
|
let fr = enemy.frequency - 2 * round
|
|
|
|
if (fr < 100) {
|
|
|
|
fr = 100
|
|
|
|
}
|
|
|
|
|
|
|
|
enemy.frequency = fr
|
|
|
|
|
|
|
|
// Increase enemy health
|
|
|
|
enemy.health += round * 5
|
|
|
|
|
|
|
|
return enemy
|
2017-08-20 19:52:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function getTileIn (map, x, y) {
|
|
|
|
let tile = x + y * Maps.width
|
|
|
|
return map[tile]
|
|
|
|
}
|
|
|
|
|
|
|
|
function updateGameState (gst) {
|
|
|
|
if (Game.state !== -1 && Game.health <= 0) {
|
|
|
|
Game.health = 0
|
|
|
|
Game.state = -1
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Game.state === 2 && gst === 1) {
|
|
|
|
nextWave()
|
|
|
|
}
|
|
|
|
|
|
|
|
if (gst === 2 && Game.state === 1) {
|
|
|
|
Game.waveTimer = 0
|
|
|
|
}
|
|
|
|
|
|
|
|
if (gst != null) {
|
|
|
|
Game.state = gst
|
|
|
|
}
|
|
|
|
|
2017-08-21 10:10:56 +00:00
|
|
|
Components.wave.disabled = (Game.state !== 2)
|
2017-08-20 19:52:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function updateEnemyMovements () {
|
|
|
|
for (let i in Game.enemies) {
|
|
|
|
let enemy = Game.enemies[i]
|
|
|
|
let enemyTrackTarget = Game.map.pathgen[enemy.node]
|
|
|
|
if (enemyTrackTarget) {
|
|
|
|
let tx = enemyTrackTarget.x
|
|
|
|
let ty = enemyTrackTarget.y
|
|
|
|
let vexLen = Math.sqrt(Math.pow(tx - enemy.x, 2) + Math.pow(ty - enemy.y, 2))
|
|
|
|
let velX = (tx - enemy.x) / Math.abs(vexLen)
|
|
|
|
let velY = (ty - enemy.y) / Math.abs(vexLen)
|
|
|
|
enemy.velocity = {x: velX, y: velY, dist: Math.abs(vexLen)}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (enemy.velocity.dist > 0.1) {
|
|
|
|
enemy.x += (enemy.velocity.x * 0.01) * enemy.speed * Game.pace
|
|
|
|
enemy.y += (enemy.velocity.y * 0.01) * enemy.speed * Game.pace
|
|
|
|
} else {
|
|
|
|
if (Game.map.pathgen[enemy.node + 1]) {
|
|
|
|
enemy.node += 1
|
|
|
|
} else if (enemyTrackTarget.end === true) {
|
|
|
|
Game.enemies.splice(i, 1)
|
|
|
|
Game.health -= Math.floor(enemy.dmg / 2)
|
|
|
|
if (Game.health < 0) {
|
|
|
|
Game.health = 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Game.state === 1 && Game.enemies.length === 0 && Game.enemySpawn === 0) {
|
|
|
|
updateGameState(2)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function towerFire (tower) {
|
|
|
|
let enemiesProxima = []
|
|
|
|
let target = null
|
|
|
|
|
|
|
|
for (let i in Game.enemies) {
|
|
|
|
let enemy = Game.enemies[i]
|
|
|
|
let proxi = Math.abs(Math.sqrt(Math.pow(enemy.x - tower.x, 2) + Math.pow(enemy.y - tower.y, 2)))
|
|
|
|
if (proxi > tower.range) continue
|
|
|
|
enemiesProxima.push(Object.assign({dist: proxi}, enemy))
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!enemiesProxima.length) return
|
|
|
|
|
|
|
|
enemiesProxima.sort((a, b) => {
|
|
|
|
return a.dist - b.dist
|
|
|
|
})
|
|
|
|
|
|
|
|
if (tower.setting === 1) {
|
|
|
|
target = enemiesProxima[0]
|
|
|
|
} else {
|
|
|
|
target = enemiesProxima[enemiesProxima.length - 1]
|
|
|
|
}
|
|
|
|
|
|
|
|
Game.particles.push({
|
|
|
|
x: tower.x,
|
|
|
|
y: tower.y,
|
|
|
|
velX: (target.x - tower.x) / target.dist * 1.24,
|
|
|
|
velY: (target.y - tower.y) / target.dist * 1.24,
|
|
|
|
dmg: tower.damage,
|
|
|
|
speed: tower.speed,
|
|
|
|
life: 100
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
function tickTowers () {
|
|
|
|
for (let i in Game.towers) {
|
|
|
|
let tower = Game.towers[i]
|
|
|
|
|
|
|
|
// Tick towers
|
|
|
|
tower.tick++
|
|
|
|
tower.tick %= tower.rate
|
|
|
|
|
|
|
|
// fire
|
|
|
|
if (tower.tick === 0) {
|
|
|
|
towerFire(tower)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function tickParticles () {
|
|
|
|
for (let i in Game.particles) {
|
|
|
|
let parti = Game.particles[i]
|
|
|
|
parti.x += parti.velX * 0.01 * parti.speed
|
|
|
|
parti.y += parti.velY * 0.01 * parti.speed
|
|
|
|
|
|
|
|
parti.life--
|
|
|
|
if (parti.life <= 0) {
|
|
|
|
Game.particles.splice(i, 1)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
for (let j in Game.enemies) {
|
|
|
|
let enemy = Game.enemies[j]
|
|
|
|
|
|
|
|
if (parti.x >= enemy.x - 0.25 && parti.y >= enemy.y - 0.25 &&
|
|
|
|
parti.x <= enemy.x + 0.5 && parti.y <= enemy.y + 0.5) {
|
|
|
|
// damage enemy
|
|
|
|
enemy.dmg -= parti.dmg
|
|
|
|
|
|
|
|
if (enemy.dmg <= 0) {
|
|
|
|
Game.enemies.splice(j, 1)
|
|
|
|
Game.money += 10
|
|
|
|
}
|
|
|
|
|
|
|
|
// remove particle
|
|
|
|
Game.particles.splice(i, 1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-21 10:10:56 +00:00
|
|
|
// Total enemy spawn count is used to determine that the round is over
|
|
|
|
// Local (in-function) determines how many there are left to spawn as ordered by the function call
|
2017-08-20 19:52:59 +00:00
|
|
|
function addEnemies (cnt, type) {
|
2017-08-21 10:10:56 +00:00
|
|
|
Game.enemySpawn += cnt // Total amount of enemies to spawn
|
|
|
|
let enemies = cnt // Local amount of enemies to spawn
|
2017-08-21 08:46:54 +00:00
|
|
|
|
|
|
|
let path = Game.map.pathgen[0]
|
2017-08-21 10:10:56 +00:00
|
|
|
// Copy the enemy and add x and y coordinates
|
2017-08-21 08:46:54 +00:00
|
|
|
let enemyCopy = Object.assign({
|
|
|
|
x: path.x,
|
|
|
|
y: path.y
|
|
|
|
}, type)
|
|
|
|
|
2017-08-21 10:10:56 +00:00
|
|
|
// Modify the enemy according to wave settings
|
2017-08-21 08:46:54 +00:00
|
|
|
enemyCopy = waveEnemyModifer(enemyCopy, Game.wave)
|
|
|
|
enemyCopy.dmg = enemyCopy.health
|
|
|
|
|
2017-08-21 10:10:56 +00:00
|
|
|
// Copy the enemy at an interval and spawn it
|
2017-08-20 19:52:59 +00:00
|
|
|
let ect = setInterval(() => {
|
2017-08-21 10:10:56 +00:00
|
|
|
if (enemies === 0) return clearInterval(ect)
|
|
|
|
Game.enemySpawn-- // Reduce total spawn count
|
|
|
|
enemies-- // Reduce local spawn count
|
2017-08-20 19:52:59 +00:00
|
|
|
|
2017-08-21 08:46:54 +00:00
|
|
|
Game.enemies.push(Object.assign({}, enemyCopy))
|
|
|
|
}, enemyCopy.frequency)
|
2017-08-20 19:52:59 +00:00
|
|
|
}
|
|
|
|
|
2017-08-21 08:46:54 +00:00
|
|
|
function getTowerAt (x, y) {
|
2017-08-20 19:52:59 +00:00
|
|
|
for (let i in Game.towers) {
|
|
|
|
let tower = Game.towers[i]
|
|
|
|
if (tower.x === x && tower.y === y) return tower
|
|
|
|
}
|
|
|
|
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
2017-08-21 08:46:54 +00:00
|
|
|
function canPlaceTowerAt (x, y) {
|
|
|
|
let tileAt = getTileIn(Game.map.tiles, x, y)
|
|
|
|
if (tileAt !== 0) return false
|
|
|
|
|
|
|
|
// Do not overlap towers
|
|
|
|
if (getTowerAt(x, y) !== null) return false
|
|
|
|
|
|
|
|
// Prevent towers from being placed right next to each-other
|
|
|
|
let can = true
|
|
|
|
for (let i in Game.towers) {
|
|
|
|
if (can === false) break
|
|
|
|
let tower = Game.towers[i]
|
|
|
|
|
|
|
|
// tower placement restriction visualization
|
|
|
|
for (let i = 0; i < 4; i++) {
|
|
|
|
if (can === false) break
|
|
|
|
let ax = tower.x
|
|
|
|
let ay = tower.y
|
|
|
|
if (i == 0) {
|
|
|
|
ax -= 1
|
|
|
|
} else if (i == 1) {
|
|
|
|
ax += 1
|
|
|
|
} else if (i == 2) {
|
|
|
|
ay -= 1
|
|
|
|
} else if (i == 3) {
|
|
|
|
ay += 1
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ax < 0 || ay < 0 || ay > Maps.height || ax > Maps.width) continue
|
|
|
|
if (ax === x && ay === y) can = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return can
|
|
|
|
}
|
|
|
|
|
2017-08-20 19:52:59 +00:00
|
|
|
function placeTower (tower, x, y) {
|
|
|
|
if (tower.cost > Game.money) return // no money
|
|
|
|
|
2017-08-21 08:46:54 +00:00
|
|
|
if (!canPlaceTowerAt(x, y)) return
|
2017-08-20 19:52:59 +00:00
|
|
|
|
|
|
|
Game.money -= tower.cost
|
|
|
|
Game.towers.push(Object.assign({
|
|
|
|
x: x,
|
|
|
|
y: y,
|
|
|
|
tick: tower.rate,
|
|
|
|
setting: 1
|
|
|
|
}, tower))
|
|
|
|
}
|
|
|
|
|
|
|
|
function update (dt) {
|
|
|
|
fpsCount++
|
|
|
|
fpsCount %= 20
|
|
|
|
if (fpsCount === 0) {
|
|
|
|
fpsDraw = fps
|
|
|
|
}
|
|
|
|
|
|
|
|
tickTowers()
|
|
|
|
updateEnemyMovements()
|
|
|
|
tickParticles()
|
|
|
|
|
2017-08-21 10:10:56 +00:00
|
|
|
for (let i in Components) {
|
|
|
|
let btn = Components[i]
|
|
|
|
if (btn instanceof ButtonComponent) {
|
|
|
|
if (mXr > btn.x && mYr > btn.y && mXr < btn.x + btn.w && mYr < btn.y + btn.h) {
|
|
|
|
btn.hovered = true
|
|
|
|
} else if (btn.hovered) {
|
|
|
|
btn.hovered = false
|
|
|
|
}
|
|
|
|
|
|
|
|
if (btn instanceof TowerButton) {
|
|
|
|
btn.disabled = btn.towerObj.cost > Game.money
|
|
|
|
btn.active = Game.tower === i
|
|
|
|
}
|
2017-08-20 19:52:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
updateGameState()
|
|
|
|
if (Game.state === 1) {
|
|
|
|
Game.waveTimer++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function render () {
|
|
|
|
let mt = Maps.tile
|
|
|
|
ctx.clearRect(0, 0, canvas.width, canvas.height)
|
|
|
|
|
|
|
|
for (let i in Game.map.tiles) {
|
|
|
|
let tile = Game.map.tiles[i]
|
|
|
|
let index = parseInt(i)
|
|
|
|
let y = Math.floor(index / Maps.width)
|
|
|
|
let x = Math.floor(index % Maps.height)
|
|
|
|
|
|
|
|
if (tile === 1) {
|
|
|
|
ctx.fillStyle = '#fdd'
|
|
|
|
} else if (tile === 2) {
|
|
|
|
ctx.fillStyle = '#aaf'
|
|
|
|
} else if (tile === 3) {
|
|
|
|
ctx.fillStyle = '#f3a'
|
|
|
|
} else {
|
|
|
|
ctx.fillStyle = '#0fa'
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.fillRect(x * mt, y * mt, mt, mt)
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
for (let i in Game.map.pathgen) {
|
|
|
|
let node = Game.map.pathgen[i]
|
|
|
|
ctx.fillStyle = '#00f'
|
|
|
|
ctx.fillRect((node.x * mt) + mt / 3, (node.y * mt) + mt / 3, 8, 8)
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
for (let i in Game.towers) {
|
|
|
|
let tower = Game.towers[i]
|
2017-08-21 08:46:54 +00:00
|
|
|
ctx.fillStyle = tower.icon
|
2017-08-20 19:52:59 +00:00
|
|
|
ctx.fillRect(tower.x * mt + 2, tower.y * mt + 2, 28, 28)
|
2017-08-21 08:46:54 +00:00
|
|
|
|
|
|
|
if (Game.state === 2 && Game.tower) {
|
|
|
|
// tower placement restriction visualization
|
|
|
|
for (let i = 0; i < 4; i++) {
|
|
|
|
let ax = tower.x
|
|
|
|
let ay = tower.y
|
|
|
|
if (i == 0) {
|
|
|
|
ax -= 1
|
|
|
|
} else if (i == 1) {
|
|
|
|
ax += 1
|
|
|
|
} else if (i == 2) {
|
|
|
|
ay -= 1
|
|
|
|
} else if (i == 3) {
|
|
|
|
ay += 1
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ax < 0 || ay < 0 || ay > Maps.height || ax > Maps.width) continue
|
|
|
|
if (getTileIn(Game.map.tiles, ax, ay) !== 0) continue
|
|
|
|
ctx.fillStyle = 'rgba(255, 0, 0, 0.45)'
|
|
|
|
ctx.fillRect(ax * mt, ay * mt, mt, mt)
|
|
|
|
}
|
|
|
|
}
|
2017-08-20 19:52:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (let i in Game.enemies) {
|
|
|
|
let enemy = Game.enemies[i]
|
|
|
|
let rx = (enemy.x * mt) + mt / 8
|
|
|
|
let ry = (enemy.y * mt) + mt / 8
|
2017-08-21 08:46:54 +00:00
|
|
|
ctx.fillStyle = enemy.icon
|
2017-08-20 19:52:59 +00:00
|
|
|
ctx.fillRect(rx, ry, 16, 16)
|
|
|
|
|
|
|
|
// health bars
|
|
|
|
let hx = rx - 6
|
|
|
|
let hy = ry - 12
|
|
|
|
ctx.fillStyle = '#f00'
|
|
|
|
ctx.fillRect(hx, hy, 16 + 12, 5)
|
|
|
|
|
|
|
|
ctx.fillStyle = '#0f0'
|
|
|
|
ctx.fillRect(hx, hy, (16 + 12) * enemy.dmg / enemy.health, 5)
|
|
|
|
}
|
|
|
|
|
|
|
|
for (let i in Game.particles) {
|
|
|
|
let tower = Game.particles[i]
|
|
|
|
ctx.fillStyle = '#f33'
|
|
|
|
ctx.fillRect(tower.x * mt + mt / 16, tower.y * mt + mt / 16, 8, 8)
|
|
|
|
}
|
|
|
|
|
2017-08-21 08:46:54 +00:00
|
|
|
// tower placement
|
2017-08-20 19:52:59 +00:00
|
|
|
if (Game.state === 2 && Game.tower && mX < Maps.width && mY < Maps.height) {
|
2017-08-21 08:46:54 +00:00
|
|
|
// tower range visualization
|
2017-08-20 19:52:59 +00:00
|
|
|
let towerData = Towers[Game.tower]
|
2017-08-21 08:46:54 +00:00
|
|
|
if (towerData.cost <= Game.money && canPlaceTowerAt (mX, mY)) {
|
|
|
|
let towerData = Towers[Game.tower]
|
|
|
|
ctx.strokeStyle = '#ddd'
|
|
|
|
ctx.fillStyle = 'rgba(200, 200, 200, 0.25)'
|
|
|
|
ctx.beginPath()
|
|
|
|
ctx.arc(mX * mt + mt / 2, mY * mt + mt / 2, towerData.range * mt, 0, 2 * Math.PI)
|
|
|
|
ctx.stroke()
|
|
|
|
ctx.fill()
|
|
|
|
ctx.closePath()
|
|
|
|
}
|
2017-08-20 19:52:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ctx.fillStyle = '#996633'
|
|
|
|
ctx.fillRect(640, 0, 240, 640)
|
|
|
|
|
|
|
|
ctx.font = '20px Helvetica'
|
|
|
|
ctx.fillStyle = '#fff'
|
|
|
|
ctx.fillText('FPS: ' + fpsDraw.toFixed(2), 0, 20)
|
|
|
|
ctx.fillText('Wave: ' + Game.wave, 645, 25)
|
|
|
|
ctx.fillText('Health: ' + Game.health, 645, 45)
|
|
|
|
ctx.fillText('Money: ' + Game.money, 645, 65)
|
|
|
|
|
2017-08-21 10:10:56 +00:00
|
|
|
for (let i in Components) {
|
|
|
|
let btn = Components[i]
|
2017-08-20 19:52:59 +00:00
|
|
|
btn.draw()
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Game.state === -1) {
|
|
|
|
ctx.font = '80px Helvetica'
|
|
|
|
ctx.fillStyle = '#f00'
|
|
|
|
ctx.fillText('Game Over', 100, canvas.height / 2 - 80 / 2)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mX < Maps.width && mY < Maps.height) {
|
|
|
|
ctx.fillStyle = 'rgba(255, 0, 0, 0.24)'
|
|
|
|
ctx.fillRect(mX * mt, mY * mt, mt, mt)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let lastTime = Date.now()
|
2017-08-21 14:21:09 +00:00
|
|
|
let now
|
|
|
|
let fpsRes = 50
|
2017-08-20 19:52:59 +00:00
|
|
|
|
2017-08-21 14:21:09 +00:00
|
|
|
function gameLoop () {
|
|
|
|
update()
|
2017-08-20 19:52:59 +00:00
|
|
|
render()
|
|
|
|
|
2017-08-21 14:21:09 +00:00
|
|
|
let cfps = 1000 / ((now = new Date) - lastTime)
|
|
|
|
if (now != lastTime) {
|
|
|
|
fps += (cfps - fps) / fpsRes
|
|
|
|
lastTime = now
|
|
|
|
}
|
|
|
|
|
|
|
|
requestAnimationFrame(gameLoop)
|
2017-08-20 19:52:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function initialize () {
|
2017-08-21 01:12:50 +00:00
|
|
|
Game.map = Maps.generate()
|
2017-08-20 19:52:59 +00:00
|
|
|
|
2017-08-21 10:10:56 +00:00
|
|
|
Components.wave = new ButtonComponent('Next Wave', '#fff', '#11f', 650, 570, 200, 60, () => {
|
2017-08-20 19:52:59 +00:00
|
|
|
updateGameState(1)
|
|
|
|
})
|
|
|
|
|
2017-08-21 10:10:56 +00:00
|
|
|
let index = 0
|
|
|
|
for (let i in Towers) {
|
|
|
|
Components[i] = new TowerButton(i, index)
|
|
|
|
index++
|
|
|
|
}
|
|
|
|
|
2017-08-20 19:52:59 +00:00
|
|
|
gameLoop()
|
|
|
|
}
|
|
|
|
|
|
|
|
canvas.addEventListener('click', (e) => {
|
|
|
|
if (Game.state === 2 && mX < Maps.width && mY < Maps.height && Game.tower) {
|
|
|
|
placeTower(Towers[Game.tower], mX, mY)
|
|
|
|
}
|
|
|
|
|
|
|
|
clickBtn()
|
|
|
|
})
|
|
|
|
|
|
|
|
canvas.addEventListener('mousemove', (e) => {
|
|
|
|
if (e.changedTouches) {
|
|
|
|
let touch = e.changedTouches[0]
|
|
|
|
if (touch) {
|
|
|
|
e.pageX = touch.pageX
|
|
|
|
e.pageY = touch.pageY
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (e.pageX || e.pageY) {
|
|
|
|
mXr = e.pageX
|
|
|
|
mYr = e.pageY
|
|
|
|
} else {
|
|
|
|
mXr = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft
|
|
|
|
mYr = e.clientY + document.body.scrollTop + document.documentElement.scrollTop
|
|
|
|
}
|
|
|
|
|
|
|
|
mXr -= canvas.offsetLeft
|
|
|
|
mYr -= canvas.offsetTop
|
|
|
|
|
|
|
|
mX = Math.floor(mXr / Maps.tile)
|
|
|
|
mY = Math.floor(mYr / Maps.tile)
|
|
|
|
})
|
|
|
|
|
|
|
|
initialize()
|
|
|
|
}
|