adds player change support

main
koehr 5 years ago
parent c3d0a69148
commit 3ead03ef72

@ -1,5 +1,5 @@
export const type = {
air: {type: 'air', hp: 0, walkable: true},
air: {type: 'air', hp: Infinity, walkable: true},
grass: {type: 'grass', hp: 1, walkable: false},
tree_top_left: {type: 'tree_top_left', hp: 5, walkable: true},
@ -33,8 +33,7 @@ export const type = {
stone_gravel: {type: 'stone_gravel', hp: 5, walkable: false},
stone: {type: 'stone', hp: 10, walkable: false},
bedrock: {type: 'bedrock', hp: 25, walkable: false},
cave: {type: 'cave', hp: 0, walkable: true},
player: {type: 'player', hp: 10, background: 'air'}
cave: {type: 'cave', hp: Infinity, walkable: true}
}
export const level = {

@ -1,5 +1,3 @@
import SeedRng from 'seedrandom'
import FastSimplexNoise from 'fast-simplex-noise'
import {type as T, level as L, probability as P} from './def'
export default class BlockGen {

@ -4,7 +4,7 @@ import FastSimplexNoise from 'fast-simplex-noise'
import {type as T, level as L} from './def'
import BlockGen from './first-iteration'
import BlockExt from './second-iteration'
import Player from './third-iteration'
import PlayerChanges from './third-iteration'
export default class Level {
constructor (width, height, seed = 'super random seed') {
@ -15,7 +15,14 @@ export default class Level {
this._grid = new Array(this._h)
this.blockGen = new BlockGen(noiseGen)
this.blockExt = new BlockExt(noiseGen)
this.player = new Player(this._grid)
this.playerChanges = new PlayerChanges()
}
change (level, column, newBlock) {
if (newBlock.hp <= 0) {
newBlock = level > L.rock ? { ...T.cave } : { ...T.air }
}
this.playerChanges.apply(level, column, newBlock)
}
grid (x, y) {
@ -23,14 +30,14 @@ export default class Level {
return this._grid
}
generate (x, y, w, h) {
for (let i = 0; i < h; i++) {
generate (column, y, width, height) {
for (let i = 0; i < height; i++) {
const level = y + i
const column = x
const row = Array(w)
const row = Array(width)
const previousRow = this._grid[i - 1] || Array()
this.blockGen.level(level, column, row, previousRow)
this.blockExt.level(level, column, row, previousRow)
this.playerChanges.level(level, column, row)
this._grid[i] = row
}
}

@ -1,8 +1,23 @@
import {type as T} from './def'
export default class PlayerChanges {
constructor () {
this.changes = {}
}
getKey (level, column) {
return `${column}.${level}`
}
apply (level, column, newBlock) {
const key = this.getKey(level, column)
this.changes[key] = newBlock
console.log('applied', level, column, newBlock, this.changes)
}
export default class Player {
constructor (grid) {
this._player_changes = []
this._grid = grid
level (level, column, row) {
for (let i = 0; i < row.length; i++) {
const key = this.getKey(level - 1, column + i)
const change = this.changes[key]
if (change) row[i] = change
}
}
}

Loading…
Cancel
Save