TNT and change goal calculation

This commit is contained in:
Evert Prants 2019-11-30 14:44:04 +02:00
parent 14f598c905
commit 546f891acd
Signed by: evert
GPG Key ID: 1688DA83D222D0B5
8 changed files with 113 additions and 20 deletions

View File

@ -1,13 +1,13 @@
import { canvas, ctx } from './canvas' import { canvas, ctx } from './canvas'
import { Level, Rock, Gold, Diamond, Lootbag } from './level' import { Level, Rock, Gold, Diamond, Lootbag, BarrelPiece } from './level'
import { randomi } from './utils' import { randomi } from './utils'
const MAP_CLEARANCE_PERCENTAGE = 80 const MAP_CLEARANCE_PERCENTAGE = 90
const REWARD_TABLE = { const REWARD_TABLE = {
rock: 16, rock: 16,
gold: [129, 543, 2399], gold: [229, 843, 1299],
diamond: 599, diamond: 1000,
lootbag: [5, 5000] lootbag: [5, 5000]
} }
@ -32,7 +32,7 @@ export class Game {
this.wait = 0 this.wait = 0
} }
static calculateScore (obj, skipLoot) { static calculateScore (obj) {
if (obj instanceof Rock) { if (obj instanceof Rock) {
return REWARD_TABLE.rock * obj.size return REWARD_TABLE.rock * obj.size
} }
@ -45,7 +45,9 @@ export class Game {
return REWARD_TABLE.diamond return REWARD_TABLE.diamond
} }
if (skipLoot) return 0 if (obj instanceof BarrelPiece) {
return 1
}
if (obj instanceof Lootbag) { if (obj instanceof Lootbag) {
let ssChance = randomi(0, 3) let ssChance = randomi(0, 3)
@ -61,7 +63,10 @@ export class Game {
static calculateLevelScore (level) { static calculateLevelScore (level) {
let total = 0 let total = 0
for (let i in level.objects) { for (let i in level.objects) {
total += Game.calculateScore(level.objects[i], true) let obj = level.objects[i]
if (obj instanceof Gold) {
total += REWARD_TABLE.gold[obj.size - 1]
}
} }
return total return total
} }
@ -125,6 +130,7 @@ export class Game {
return return
} }
this.level.update()
this.player.update(this.level) this.player.update(this.level)
} }

View File

@ -35,7 +35,8 @@ function start () {
async function loadAll () { async function loadAll () {
let images = ['static/hook_open.png', 'static/gold_1.png', 'static/gold_2.png', 'static/gold_3.png', let images = ['static/hook_open.png', 'static/gold_1.png', 'static/gold_2.png', 'static/gold_3.png',
'static/rock_1.png', 'static/rock_2.png', 'static/rock_3.png', 'static/diamond.png', 'static/loot.png'] 'static/rock_1.png', 'static/rock_2.png', 'static/rock_3.png', 'static/diamond.png', 'static/loot.png',
'static/tnt.png', 'static/barrel_piece.png', 'static/explosion.png']
for (let i in images) { for (let i in images) {
await RES.loadImage(images[i]) await RES.loadImage(images[i])
} }

View File

@ -1,8 +1,10 @@
import { ctx } from './canvas' import { ctx } from './canvas'
import { randomi } from './utils' import { randomi, distanceTo } from './utils'
import RES from './resource' import RES from './resource'
const offset = 50 const OFFSET = 50
const TNT_START = 5
const DIAMOND_START = 3
// Objects // Objects
@ -75,7 +77,46 @@ export class Lootbag extends GameObject {
} }
get weight () { get weight () {
return 0.56 return 0.45
}
}
export class BarrelPiece extends GameObject {
constructor (x, y) {
super(x, y, 30, 18, 'static/barrel_piece.png')
}
get weight () {
return 0.05
}
}
export class TNTBarrel extends GameObject {
constructor (x, y) {
super(x, y, 40, 55, 'static/tnt.png')
this.radius = 156
}
get weight () {
return 0.0
}
explode (level) {
for (let i in level.objects) {
let obj = level.objects[i]
if (distanceTo(obj, this) < this.radius) {
if (obj instanceof TNTBarrel && obj !== this && obj.physical) {
obj.explode(level)
continue
}
obj.destroy()
}
}
this.destroy()
level.placeExplosion(this.x, this.y, this.radius)
let b = new BarrelPiece(this.x, this.y)
level.objects.push(b)
return b
} }
} }
@ -85,51 +126,85 @@ export class Level {
constructor (id, objects) { constructor (id, objects) {
this.id = id this.id = id
this.objects = objects || [] this.objects = objects || []
this.explosions = []
}
placeExplosion (x, y, r) {
this.explosions.push({ x, y, time: 30, r })
} }
draw () { draw () {
for (let i in this.objects) { for (let i in this.objects) {
this.objects[i].draw() this.objects[i].draw()
} }
let expl = RES.loadImage('static/explosion.png', true)
for (let i in this.explosions) {
let ex = this.explosions[i]
let r = ex.r * 1.5
ctx.drawImage(expl, ctx.oX + ex.x - r / 2, ctx.oY + ex.y - r / 2, r, r)
}
}
update () {
let toKeep = []
for (let i in this.explosions) {
let ex = this.explosions[i]
if (ex.time > 0) {
ex.time--
ex.r -= randomi(-10, 10)
toKeep.push(ex)
continue
}
}
this.explosions = toKeep
} }
static create (index, my, width, height) { static create (index, my, width, height) {
let objects = [] let objects = []
let rocks = randomi(4, 12) let rocks = randomi(4, 12)
let gold = randomi(4, 12) let gold = randomi(4, 12)
let diamond = randomi(0, 2) let diamond = index >= DIAMOND_START ? randomi(0, 2) : 0
let loot = randomi(0, 2) let loot = randomi(0, 2)
let tnt = index >= TNT_START ? randomi(0, 4) : 0
// Add rocks // Add rocks
for (let r = 0; r < rocks; r++) { for (let r = 0; r < rocks; r++) {
let x = randomi(offset, width - offset) let x = randomi(OFFSET, width - OFFSET)
let y = randomi(offset + my, height - (offset + my)) let y = randomi(OFFSET + my, height - (OFFSET + my))
let size = randomi(1, 4) let size = randomi(1, 4)
objects.push(new Rock(x, y, size)) objects.push(new Rock(x, y, size))
} }
// Add gold // Add gold
for (let r = 0; r < gold; r++) { for (let r = 0; r < gold; r++) {
let x = randomi(offset, width - offset) let x = randomi(OFFSET, width - OFFSET)
let y = randomi(offset + my, height - (offset + my)) let y = randomi(OFFSET + my, height - (OFFSET + my))
let size = randomi(1, 4) let size = randomi(1, 4)
objects.push(new Gold(x, y, size)) objects.push(new Gold(x, y, size))
} }
// Add diamonds // Add diamonds
for (let r = 0; r < diamond; r++) { for (let r = 0; r < diamond; r++) {
let x = randomi(offset, width - offset) let x = randomi(OFFSET, width - OFFSET)
let y = randomi(offset + my, height - (offset + my)) let y = randomi(OFFSET + my, height - (OFFSET + my))
objects.push(new Diamond(x, y)) objects.push(new Diamond(x, y))
} }
// Add loot // Add loot
for (let r = 0; r < loot; r++) { for (let r = 0; r < loot; r++) {
let x = randomi(offset, width - offset) let x = randomi(OFFSET, width - OFFSET)
let y = randomi(offset + my, height - (offset + my)) let y = randomi(OFFSET + my, height - (OFFSET + my))
objects.push(new Lootbag(x, y)) objects.push(new Lootbag(x, y))
} }
// Add TNT
for (let r = 0; r < tnt; r++) {
let x = randomi(OFFSET, width - OFFSET)
let y = randomi(OFFSET + my + 10, height - (OFFSET + my) - 10)
objects.push(new TNTBarrel(x, y))
}
let n = new Level(index, objects) let n = new Level(index, objects)
return n return n
} }

View File

@ -127,6 +127,13 @@ class Hook extends GameObject {
} }
if (firstIntersect) { if (firstIntersect) {
if (firstIntersect.explode) {
let obj = firstIntersect.explode(level)
this.obj = obj
this.md = 0
return
}
this.obj = firstIntersect this.obj = firstIntersect
this.md = 0 this.md = 0
return return

View File

@ -19,3 +19,7 @@ export function intersectRect (r1, r2) {
r2.y > r1.h + r1.y || r2.y > r1.h + r1.y ||
r2.h + r2.y < r1.y) r2.h + r2.y < r1.y)
} }
export function distanceTo (o1, o2) {
return Math.sqrt(Math.pow(o2.x - o1.x, 2) + Math.pow(o2.y - o1.y, 2))
}

BIN
static/barrel_piece.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

BIN
static/explosion.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.6 KiB

BIN
static/tnt.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.2 KiB