Skip to content

Commit b1d2008

Browse files
committed
feat: add purple-mushroom
1 parent 3c05c33 commit b1d2008

13 files changed

Lines changed: 18604 additions & 18716 deletions

model/character.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,20 @@ export type IObj = IBox & ILoader & {
3232
image(): ImageBitmap
3333
}
3434

35-
export type ItemType = "apple" | "green-apple" | "mushroom"
35+
export type ItemType = "apple" | "green-apple" | "mushroom" | "purple-mushroom"
3636

3737
export type IItem = IObj & {
3838
id: string | null
3939
type: ItemType
4040
}
4141

42+
export type PropType = "chair" | "table"
43+
44+
export type IProp = IObj & {
45+
id: string | null
46+
type: PropType
47+
}
48+
4249
export type IFieldTester = {
4350
get(i: number, j: number): {
4451
canEnter: boolean

model/prop.ts

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import { loadImage } from "../util/load.ts"
2+
import { CELL_SIZE } from "../util/constants.ts"
3+
import type { IProp, PropType } from "./character.ts"
4+
5+
const fallbackImage = await fetch(
6+
// TODO(kt3k): Update
7+
"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAADRJREFUOE9jZKAQMFKon2FoGPAfzZsoribGC0PQALxORo92bGEwDAwgKXUTkw7wGjjwBgAAiwgIEW1Cnt4AAAAASUVORK5CYII=",
8+
).then((res) => res.blob()).then((blob) => createImageBitmap(blob))
9+
10+
export class Prop implements IProp {
11+
/** The unique identifier of the item. Only items which are spawned from block map have ids. */
12+
readonly id: string | null
13+
readonly i: number
14+
readonly j: number
15+
readonly type: PropType
16+
readonly src: string
17+
readonly w = CELL_SIZE
18+
readonly h = CELL_SIZE
19+
#image: ImageBitmap | undefined
20+
21+
/**
22+
* @param i The column of the grid coordinate
23+
* @param j The row of the grid coordinate
24+
* @param type The type of item
25+
* @param src The path to the asset image
26+
*/
27+
constructor(
28+
id: string | null,
29+
i: number,
30+
j: number,
31+
type: PropType,
32+
src: string,
33+
) {
34+
this.id = id
35+
this.i = i
36+
this.j = j
37+
this.type = type
38+
this.src = src
39+
}
40+
41+
async loadAssets() {
42+
this.#image = await loadImage(this.src)
43+
}
44+
45+
get assetsReady(): boolean {
46+
return !!this.#image
47+
}
48+
49+
image(): ImageBitmap {
50+
return this.#image ?? fallbackImage
51+
}
52+
53+
get x(): number {
54+
return this.i * CELL_SIZE
55+
}
56+
get y(): number {
57+
return this.j * CELL_SIZE
58+
}
59+
}

player/main-character.ts

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,31 @@ import {
1010
} from "../model/character.ts"
1111
import * as signal from "../util/signal.ts"
1212
import { bindToggleFullscreenOnce } from "../util/fullscreen.ts"
13+
import { seed } from "../util/random.ts"
1314

1415
export class MainCharacter extends Character {
1516
#lastMoveTypes: MoveType[] = []
16-
#nextActionQueue: (NextAction | "speed-up" | "speed-reset")[] = []
17+
#nextActionQueue: (NextAction | "speed-2x" | "speed-4x" | "speed-reset")[] =
18+
[]
1719
#speedUpTimer: number | undefined = undefined
1820
override getNextAction(
1921
fieldTester: IFieldTester,
2022
collisionChecker: CollisionChecker,
2123
): NextAction {
2224
if (this.#nextActionQueue.length > 0) {
2325
const nextAction = this.#nextActionQueue.shift()!
24-
if (nextAction === "speed-up") {
26+
if (nextAction === "speed-2x") {
2527
this.speed = 2
2628
this.#speedUpTimer = setTimeout(() => {
2729
this.#nextActionQueue.push("speed-reset", "jump")
2830
}, 15000)
2931
return this.getNextAction(fieldTester, collisionChecker)
32+
} else if (nextAction === "speed-4x") {
33+
this.speed = 4
34+
this.#speedUpTimer = setTimeout(() => {
35+
this.#nextActionQueue.push("speed-reset", "jump")
36+
}, 15000)
37+
return this.getNextAction(fieldTester, collisionChecker)
3038
} else if (nextAction === "speed-reset") {
3139
if (this.#speedUpTimer) {
3240
clearTimeout(this.#speedUpTimer)
@@ -84,7 +92,19 @@ export class MainCharacter extends Character {
8492
}
8593
case "mushroom": {
8694
itemContainer.collect(this.i, this.j)
87-
this.#nextActionQueue.push("speed-reset", "jump", "jump", "speed-up")
95+
this.#nextActionQueue = []
96+
this.#nextActionQueue.push("speed-reset", "jump", "speed-2x")
97+
break
98+
}
99+
case "purple-mushroom": {
100+
itemContainer.collect(this.i, this.j)
101+
const { choice } = seed(`${this.i},${this.j}`)
102+
this.#nextActionQueue = []
103+
this.#nextActionQueue.push("speed-reset", "jump", "jump", "speed-4x")
104+
for (const _ of Array(30)) {
105+
this.#nextActionQueue.push(choice([UP, DOWN, LEFT, RIGHT]))
106+
}
107+
this.#nextActionQueue.push("speed-reset", "jump")
88108
break
89109
}
90110
}

static/item/mushroom.png

19 Bytes
Loading

static/item/purple-mushroom.png

270 Bytes
Loading

0 commit comments

Comments
 (0)