Skip to content

Commit

Permalink
Add more error messages (hackclub#1025)
Browse files Browse the repository at this point in the history
* Start adding more error messages

Related to hackclub#972

* Add some nicer messages to setLegend()

* Start adding more error messages

Related to hackclub#972

* Add some nicer messages to setLegend()

* One more addition

* Also check array length in setPushables



Co-authored-by: @snoglobe <[email protected]>

---------

Co-authored-by: @snoglobe <[email protected]>
Co-authored-by: Lucas <[email protected]>
  • Loading branch information
3 people authored May 12, 2023
1 parent c28edb3 commit 75188c6
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
24 changes: 19 additions & 5 deletions src/lib/engine/1-base/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { palette } from './palette'
import type { Text } from './text'

// Tagged tempalate literal factory go brrr
// Tagged template literal factory go brrr
const _makeTag = <T>(cb: (string: string) => T) => {
return (strings: string[], ...interps: string[]) => {
if (typeof strings === 'string') {
Expand Down Expand Up @@ -65,7 +65,7 @@ export function baseEngine() {

set type(newType) {
const legendDict = Object.fromEntries(gameState.legend)
if (!(newType in legendDict)) throw new Error(`"${newType}" not in legend.`)
if (!(newType in legendDict)) throw new Error(`"${newType}" isn\'t in the legend.`)
this.remove()
addSprite(this._x, this._y, newType)
}
Expand Down Expand Up @@ -181,11 +181,14 @@ export function baseEngine() {

const setMap = (string: string): void => {
if (!string) throw new Error('Tried to set empty map.')

if (string.constructor == Object) throw new Error('setMap() takes a string, not a dict.') // https://stackoverflow.com/a/51285298
if (Array.isArray(string)) throw new Error('It looks like you passed an array into setMap(). Did you mean to use something like setMap(levels[level]) instead of setMap(levels)?')

const rows = string.trim().split("\n").map(x => x.trim())
const rowLengths = rows.map(x => x.length)
const isRect = _allEqual(rowLengths)
if (!isRect) throw new Error('Level must be rect.')
if (!isRect) throw new Error('Level must be rectangular.')
const w = rows[0]?.length ?? 0
const h = rows.length
gameState.dimensions.width = w
Expand Down Expand Up @@ -259,8 +262,19 @@ export function baseEngine() {
return tiles
}

const setSolids = (arr: string[]): void => { gameState.solids = arr }
const setPushables = (map: Record<string, string[]>): void => { gameState.pushable = map }
const setSolids = (arr: string[]): void => {
if (!Array.isArray(arr)) throw new Error('The sprites passed into setSolids() need to be an array.')
gameState.solids = arr
}
const setPushables = (map: Record<string, string[]>): void => {
for (const key in map) {
if(key.length != 1) {
throw new Error('Your sprite name must be wrapped in [] brackets here.');
}
_checkLegend(key)
}
gameState.pushable = map
}

const api = {
setMap,
Expand Down
4 changes: 4 additions & 0 deletions src/lib/engine/2-web/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ export function webEngine(canvas: HTMLCanvasElement) {
let animationId = window.requestAnimationFrame(_gameloop)

const setLegend = (...bitmaps: [string, string][]): void => {
if (bitmaps.length == 0) throw new Error('There needs to be at least one sprite in the legend.')

if (!Array.isArray(bitmaps[0])) throw new Error('The sprites passed into setLegend each need to be in square brackets, like setLegend([player, bitmap`...`]).')

bitmaps.forEach(([ key ]) => {
if (key === '.') throw new Error(`Can't reassign "." bitmap`)
if (key.length !== 1) throw new Error(`Bitmaps must have one character names`)
Expand Down
2 changes: 1 addition & 1 deletion src/lib/upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export const uploadState = signal<UploadState>('IDLE')

const getPort = async (): Promise<SerialPort> => {
if (!navigator.serial) {
const msg = 'Your browser does not support the Web Serial API. Please try again in a recent version of Chrome.'
const msg = 'Your browser does not support the Web Serial API. Please try again in a recent version of a Chromium-based browser, such as Chrome, Edge, or Arc.'
alert(msg)
throw new Error(msg)
}
Expand Down

0 comments on commit 75188c6

Please sign in to comment.