small serve script, refresh chunks when enabling grid, some renames

This commit is contained in:
Evert Prants 2020-01-11 20:13:09 +02:00
parent 71eaa328a4
commit 8eb371c090
Signed by: evert
GPG Key ID: 1688DA83D222D0B5
7 changed files with 36 additions and 15 deletions

View File

@ -2,10 +2,9 @@
"name": "tilegame", "name": "tilegame",
"version": "0.0.0", "version": "0.0.0",
"description": "Tile game", "description": "Tile game",
"main": "index.js",
"scripts": { "scripts": {
"test": "echo \"Error: no test specified\" && exit 1", "test": "echo \"Error: no test specified\" && exit 1",
"serve": "node .", "serve": "node serve.js",
"build": "webpack -p", "build": "webpack -p",
"watch": "webpack -w --mode=development" "watch": "webpack -w --mode=development"
}, },
@ -24,5 +23,8 @@
"standard": "^12.0.1", "standard": "^12.0.1",
"webpack": "^4.41.5", "webpack": "^4.41.5",
"webpack-command": "^0.4.2" "webpack-command": "^0.4.2"
},
"optionalDependencies": {
"express": "^4.17.1"
} }
} }

11
serve.js Executable file
View File

@ -0,0 +1,11 @@
#!/usr/bin/env node
const express = require('express')
const path = require('path')
const app = express()
app.use('/assets', express.static(path.join(__dirname, 'assets')))
app.use('/', express.static(path.join(__dirname, 'dist')))
app.listen(3000, function () {
console.log('server listening on 0.0.0.0:3000')
})

View File

@ -129,7 +129,6 @@ class ItemEntity extends PhysicsEntity {
this.istr = this.name + ' ' + (this.count + ent.count) this.istr = this.name + ' ' + (this.count + ent.count)
this._life = 0 this._life = 0
ent.dead = true ent.dead = true
continue
} }
} }

View File

@ -49,11 +49,9 @@ class HeightMap {
// octaves - Number of noise layers // octaves - Number of noise layers
// period - Distance above which we start to see similarities. The higher, the longer "hills" will be on a terrain. // period - Distance above which we start to see similarities. The higher, the longer "hills" will be on a terrain.
// lacunarity - Controls period change across octaves. 2 is usually a good value to address all detail levels. // lacunarity - Controls period change across octaves. 2 is usually a good value to address all detail levels.
constructor (offsetX, offsetY, size, seed, amplitude = 30, persistence = 0.5, octaves = 5, period = 80, lacunarity = 2) { constructor (offsetY, seed, amplitude = 30, persistence = 0.5, octaves = 5, period = 80, lacunarity = 2) {
this.ix = offsetX
this.iy = offsetY this.iy = offsetY
this.seed = seed this.seed = seed
this.size = size
this.noise = new PerlinNoise(seedrandom(seed)) this.noise = new PerlinNoise(seedrandom(seed))
@ -65,7 +63,7 @@ class HeightMap {
} }
getNoise (zx) { getNoise (zx) {
let x = ((this.size * this.ix) + zx) / this.period let x = zx / this.period
let amp = 1.0 let amp = 1.0
let max = 1.0 let max = 1.0

View File

@ -16,9 +16,9 @@ let frameCount = 0
let fps = 0 let fps = 0
let vp = new Viewport(0, 0) let vp = new Viewport(0, 0)
let p = new Player(800, 1200, 32, 64) let p = new Player(1000, 1000, 32, 64)
let height = new HeightMap(0, 32, 16, 0) let height = new HeightMap(32, 0)
const chunkSize = 32 const chunkSize = 32
const tileSize = 16 const tileSize = 16
@ -74,7 +74,7 @@ function start () {
world.chunks = [] world.chunks = []
}) })
Debug.addCheckbox(Debug, 'drawGrid', function (argument) { Debug.addCheckbox(Debug, 'drawGrid', function (argument) {
world.chunks = [] world.redraw()
}) })
playing = true playing = true
gameLoop() gameLoop()

View File

@ -8,9 +8,9 @@ class Player extends PhysicsEntity {
constructor (x, y, w, h) { constructor (x, y, w, h) {
super(x, y, w, h) super(x, y, w, h)
this.speed = 8 this.speed = 4
this.gravity = 1 this.gravity = 0.5
this.jumpPower = 20 this.jumpPower = 10
this.inv = new Inventory(9) this.inv = new Inventory(9)
this.itemPickUpDistance = 40 this.itemPickUpDistance = 40

View File

@ -152,7 +152,7 @@ class TileLayer {
} }
} }
class TilePhysicsLayer extends TileLayer { class TileCollisionLayer extends TileLayer {
constructor (size = 16, tileSize = 16) { constructor (size = 16, tileSize = 16) {
super(null, 'col', size, tileSize) super(null, 'col', size, tileSize)
this.empty = false this.empty = false
@ -232,7 +232,7 @@ class Chunk {
this.layers = [] this.layers = []
let bgLayer = new TileLayer(tileMap, 'bg', this.size, this.tile) let bgLayer = new TileLayer(tileMap, 'bg', this.size, this.tile)
let fgLayer = new TileLayer(tileMap, 'fg', this.size, this.tile) let fgLayer = new TileLayer(tileMap, 'fg', this.size, this.tile)
let clLayer = new TilePhysicsLayer(this.size, this.tile) let clLayer = new TileCollisionLayer(this.size, this.tile)
for (let i = 0; i < this.size * this.size; i++) { for (let i = 0; i < this.size * this.size; i++) {
let tileCoords = fgLayer.toXY(i) let tileCoords = fgLayer.toXY(i)
let tileAbs = this.toAbs(tileCoords) let tileAbs = this.toAbs(tileCoords)
@ -402,6 +402,7 @@ class World {
update (dt, vp) { update (dt, vp) {
this._active = [] this._active = []
let posPoint = vp.chunkIn(this.chunkSize * this.tileSize) let posPoint = vp.chunkIn(this.chunkSize * this.tileSize)
// Load chunks if necessary
for (let x = posPoint.x - 3; x < posPoint.x + 4; x++) { for (let x = posPoint.x - 3; x < posPoint.x + 4; x++) {
for (let y = posPoint.y - 2; y < posPoint.y + 3; y++) { for (let y = posPoint.y - 2; y < posPoint.y + 3; y++) {
if (x < 0 || y < 0 || x >= this.width || y >= this.height) continue if (x < 0 || y < 0 || x >= this.width || y >= this.height) continue
@ -444,6 +445,7 @@ class World {
} }
} }
// Get a position in the world grid from an absolute position
gridPosition (pos) { gridPosition (pos) {
let a = this.chunkSize * this.tileSize let a = this.chunkSize * this.tileSize
let chunk = { x: Math.floor(pos.x / a), y: Math.floor(pos.y / a) } let chunk = { x: Math.floor(pos.x / a), y: Math.floor(pos.y / a) }
@ -454,6 +456,14 @@ class World {
return { chunk: this.getChunk(chunk.x, chunk.y), tile } return { chunk: this.getChunk(chunk.x, chunk.y), tile }
} }
// Forces all loaded chunks to re-draw themselves
redraw () {
for (let i in this.chunks) {
this.chunks[i].dirty = true
}
}
// Draw all loaded in-view chunks and special layers
draw (vp) { draw (vp) {
this._lastDrawCount = 0 this._lastDrawCount = 0
this._lastUpdateCount = 0 this._lastUpdateCount = 0
@ -474,6 +484,7 @@ class World {
} }
} }
// Terrain collision test
collide (obj) { collide (obj) {
if (!this._active.length) return null if (!this._active.length) return null
for (let i in this._active) { for (let i in this._active) {