Skip to content

Commit

Permalink
5x fps
Browse files Browse the repository at this point in the history
  • Loading branch information
j0code committed Mar 23, 2024
1 parent 93259a1 commit 95a7bb2
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/gui/state/ingame.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import WorldGenerator from "../../world/WorldGenerator.js"

// constants
export const blockSize = 80
const gameOffset = new Dim2(0, -2)
export const gameOffset = new Dim2(0, -2)

// other
export let world: World
Expand Down Expand Up @@ -63,7 +63,7 @@ export function tick() {
}

export function draw(g: Graphics) {
if (menuState == MenuState.INGAME) drawGame()
if (menuState == MenuState.INGAME || og.canvas.width != game.width || og.canvas.height != game.height) drawGame()

g.reset()

Expand Down
13 changes: 10 additions & 3 deletions src/world/World.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import Block, { BlockData } from "../block/Block.js"
import Entity, { EntityData } from "../entity/Entity.js"
import Player, { type PlayerData } from "../entity/Player.js"
import { createBlock, createEntity, getFirstBlock } from "../gui/state/ingame.js"
import { blockSize, cam, createBlock, createEntity, gameOffset, getFirstBlock } from "../gui/state/ingame.js"
import Dim2 from "../dim/Dim2.js"
import Graphics from "../Graphics.js"
import { type NamespacedId } from "../util/interfaces.js"
import { game } from "../main.js"
import { isNamespacedId } from "../util/typecheck.js"

// import YSON from "https://j0code.github.io/browserjs-yson/main.mjs"
Expand Down Expand Up @@ -220,13 +221,19 @@ export default class World {
}

draw(g: Graphics) {
// screen edges
const left = Math.floor((-game.width /2 + cam.x*blockSize) / blockSize - gameOffset.x)
const top = Math.ceil(-(-game.height/2 - cam.y*blockSize) / blockSize - gameOffset.y)
const right = Math.ceil((game.width /2 + cam.x*blockSize) / blockSize - gameOffset.x)
const bottom = Math.floor(-(game.height/2 - cam.y*blockSize) / blockSize - gameOffset.y)

for (let z = this.minZ; z <= this.maxZ; z++) {
if (z == 0) { // draw entities behind blocks (e.g. water)
for (const entity of this.getAllEntities()) entity.draw(g, this)
}

for (let y = this.minY; y <= this.maxY; y++) {
for (let x = this.minX; x <= this.maxX; x++) {
for (let y = Math.max(this.minY, bottom); y <= Math.min(this.maxY, top); y++) {
for (let x = Math.max(this.minX, left); x <= Math.min(this.maxX, right); x++) {
const frontBlock = getFirstBlock(this, x, y, undefined, block => block.full)

if (z >= frontBlock.z) this.getBlock(x, y, z)?.draw(g, x, y, z)
Expand Down

0 comments on commit 95a7bb2

Please sign in to comment.