-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Demo for rendering opinionated Tiled maps.
- Loading branch information
1 parent
7a606d6
commit b7edfe1
Showing
20 changed files
with
2,541 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,15 @@ | ||
export function loadLocalImage(data: string) { | ||
export async function loadLocalImage(data: string): Promise<HTMLImageElement> { | ||
const img = new Image(); | ||
const test1 = data.match(/([a-z0-9-_]+).(png|gif|jpg)$/i); | ||
const test2 = data.match(/^data:image\//i); | ||
if (!test1 && !test2) { | ||
throw new Error(`Trying to an load an invalid image ${data}.`); | ||
} | ||
|
||
img.src = data; | ||
return img; | ||
return new Promise((resolve) => { | ||
img.src = data; | ||
img.onload = function() { | ||
resolve(this as HTMLImageElement); | ||
} | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
root = true | ||
|
||
[*.{js,json,ts}] | ||
indent_style = space | ||
indent_size = 2 | ||
max_line_length = 140 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
src/*.js | ||
src/*.js.map | ||
src/assets/sprites/*.png |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# Ignore artifacts: | ||
build | ||
dist | ||
coverage |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"printWidth": 140 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<!doctype html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" | ||
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> | ||
<meta http-equiv="X-UA-Compatible" content="ie=edge"> | ||
<title>GLHF Demo</title> | ||
<style> | ||
body { background-color: dimgrey; } | ||
#game-wrapper { border: 1px solid black; background-color: white; } | ||
</style> | ||
</head> | ||
<body> | ||
|
||
</body> | ||
</html> | ||
<script src="demo.js"></script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{ | ||
"name": "glhf-demo-tiled", | ||
"version": "1.0.0", | ||
"description": "Demo of a Tiled map loaded to Canvas", | ||
"scripts": { | ||
"build": "esbuild ./src/index.ts --bundle --sourcemap --loader:.png=dataurl --outfile=dist/demo.js", | ||
"dev": "esbuild ./src/index.ts --bundle --sourcemap --watch --loader:.png=dataurl --outfile=dist/demo.js --servedir=dist", | ||
"test": "echo \"Error: no test specified for glhf-demo yet.\"" | ||
}, | ||
"author": "Serban Ghita <[email protected]> (https://ghita.org)", | ||
"license": "All code is MIT, the assets are copywritten.", | ||
"devDependencies": { | ||
"@eslint/js": "^9.4.0", | ||
"@types/eslint__js": "^8.42.3", | ||
"esbuild": "0.21.4", | ||
"eslint": "^9.4.0", | ||
"prettier": "3.3.3", | ||
"typescript": "^5.4.5", | ||
"typescript-eslint": "^7.11.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/* eslint-disable @typescript-eslint/no-var-requires */ | ||
import { loadLocalImage } from "@serbanghita-gamedev/assets"; | ||
|
||
export async function loadSprites() { | ||
const SPRITES: { [key: string]: HTMLImageElement } = { | ||
"./assets/sprites/terrain.png": await loadLocalImage(require("./assets/sprites/terrain.png")) | ||
} | ||
|
||
return SPRITES; | ||
} |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import {Component} from "@serbanghita-gamedev/ecs"; | ||
import {TiledMapFile} from "@serbanghita-gamedev/tiled"; | ||
|
||
export type IsTiledMapProps = { | ||
mapFile: TiledMapFile; | ||
} | ||
|
||
export default class IsTiledMap extends Component { | ||
constructor(public properties: IsTiledMapProps) { | ||
super(properties); | ||
|
||
this.init(properties); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// 0. Create the UI and canvas. | ||
import {createWrapperElement, createCanvas} from "@serbanghita-gamedev/renderer"; | ||
import {World} from "@serbanghita-gamedev/ecs"; | ||
import { PreRenderTiledMapSystem } from "./system/PreRenderTiledMapSystem"; | ||
import IsTiledMap from "./component/IsTiledMap"; | ||
import { loadSprites } from "./assets"; | ||
|
||
async function runGame() { | ||
const HTML_WRAPPER = createWrapperElement("game-wrapper", 640, 480); | ||
const CANVAS_FOREGROUND = createCanvas("foreground", 640, 480, "1"); | ||
const CANVAS_BACKGROUND = createCanvas("background", 640, 480, "2"); | ||
HTML_WRAPPER.appendChild(CANVAS_FOREGROUND); | ||
HTML_WRAPPER.appendChild(CANVAS_BACKGROUND); | ||
document.body.appendChild(HTML_WRAPPER); | ||
|
||
const SPRITES = await loadSprites(); | ||
|
||
// Create the current "World" (scene). | ||
const world = new World(); | ||
|
||
world.declarations.components.registerComponent(IsTiledMap); | ||
|
||
const CustomMapEntity = world.createEntity("map"); | ||
CustomMapEntity.addComponent(IsTiledMap, {mapFile: require("./assets/maps/E1MM2.json")}) | ||
|
||
const TiledMapQuery = world.createQuery("TiledMapQuery", { all: [IsTiledMap] }); | ||
world.createSystem(PreRenderTiledMapSystem, TiledMapQuery, CANVAS_BACKGROUND, SPRITES).runOnlyOnce(); | ||
world.systems.forEach((system) => system.update(0)); | ||
} | ||
|
||
runGame().then(() => console.log('Game started ...')); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { System, Query, World } from "@serbanghita-gamedev/ecs"; | ||
import IsTiledMap from "../component/IsTiledMap"; | ||
import { renderTile } from "@serbanghita-gamedev/renderer"; | ||
import TiledMap from "@serbanghita-gamedev/tiled/tiled"; | ||
import { getCtx } from "@serbanghita-gamedev/renderer"; | ||
|
||
export class PreRenderTiledMapSystem extends System | ||
{ | ||
public constructor(public world: World, public query: Query, protected CANVAS_BACKGROUND: HTMLCanvasElement, protected SPRITES: { [key: string]: HTMLImageElement }) { | ||
super(world, query); | ||
} | ||
|
||
private renderToBuffer(tiledMap: TiledMap) { | ||
|
||
if (!this.CANVAS_BACKGROUND) { | ||
throw new Error(`Background canvas ($background) was not created or passed.`); | ||
} | ||
|
||
// Create all the layers. | ||
tiledMap.getRenderLayers().forEach((layer) => { | ||
|
||
// Create all the items existing in the layer. | ||
for (let j = 0; j < layer.data.length; j++) { | ||
|
||
// Don't draw empty cells. | ||
if (layer.data[j] === 0) { | ||
continue; | ||
} | ||
|
||
renderTile( | ||
// getBufferCtx(getObjectProperty(layer.properties, "renderOnLayer")), | ||
getCtx(this.CANVAS_BACKGROUND), | ||
this.SPRITES["./assets/sprites/terrain.png"], | ||
tiledMap.getTileWidth(), | ||
tiledMap.getTileHeight(), | ||
j, | ||
layer.data[j], | ||
tiledMap.getWidthInTiles(), | ||
tiledMap.getHeightInTiles() | ||
); | ||
} | ||
|
||
}); | ||
|
||
} | ||
|
||
public update(now: number): void { | ||
this.query.execute().forEach((entity) => { | ||
console.log(entity); | ||
|
||
const tiledMapComponent = entity.getComponent(IsTiledMap); | ||
const tiledMapFile = tiledMapComponent.properties.mapFile; | ||
|
||
const tileMap = new TiledMap(tiledMapFile); | ||
this.renderToBuffer(tileMap); | ||
|
||
|
||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
{ | ||
"compilerOptions": { | ||
"target": "ES2022", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */ | ||
"module": "commonjs", /* Specify what module code is generated. */ | ||
"esModuleInterop": true, /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */ | ||
"forceConsistentCasingInFileNames": true, /* Ensure that casing is correct in imports. */ | ||
"strict": true, /* Enable all strict type-checking options. */ | ||
"skipLibCheck": true, /* Skip type checking all .d.ts files. */ | ||
"rootDirs": ["src"], | ||
"baseUrl": "src", | ||
"sourceMap": true, | ||
"paths": { | ||
"@serbanghita-gamedev/assets/*": ["../../assets/src/*"], | ||
"@serbanghita-gamedev/bitmask/*": ["../../bitmask/src/*"], | ||
"@serbanghita-gamedev/component/*": ["../../component/src/*"], | ||
"@serbanghita-gamedev/ecs/*": ["../../ecs/src/*"], | ||
"@serbanghita-gamedev/input/*": ["../../input/src/*"], | ||
"@serbanghita-gamedev/renderer/*": ["../../renderer/src/*"], | ||
"@serbanghita-gamedev/matrix/*": ["../../matrix/src/*"], | ||
"@serbanghita-gamedev/tiled/*": ["../../tiled/src/*"], | ||
} | ||
}, | ||
"include": [ | ||
"src/index.ts", | ||
], | ||
"exclude": [ | ||
"node_modules", | ||
"dist" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { defineConfig } from 'vitest/config' | ||
import tsconfigPaths from 'vite-tsconfig-paths' | ||
import * as path from "node:path"; | ||
|
||
export default defineConfig({ | ||
plugins: [tsconfigPaths()], | ||
test: { | ||
alias: { | ||
'@serbanghita-gamedev/assets/': path.join(__dirname, '../assets/'), | ||
'@serbanghita-gamedev/bitmask/': path.join(__dirname, '../bitmask/'), | ||
'@serbanghita-gamedev/component/': path.join(__dirname, '../component/'), | ||
'@serbanghita-gamedev/ecs/': path.join(__dirname, '../ecs/'), | ||
'@serbanghita-gamedev/input/': path.join(__dirname, '../input/'), | ||
'@serbanghita-gamedev/renderer/': path.join(__dirname, '../renderer/'), | ||
'@serbanghita-gamedev/matrix/': path.join(__dirname, '../matrix/'), | ||
'@serbanghita-gamedev/tiled/': path.join(__dirname, '../tiled/') | ||
} | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.