Skip to content

Commit 6533e96

Browse files
committed
feat: splash purple color when get purple mushroom
1 parent f1fa1d6 commit 6533e96

7 files changed

Lines changed: 170 additions & 65 deletions

File tree

game/field.ts

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import {
2121
} from "../model/field-block.ts"
2222
import { loadImage } from "../util/load.ts"
2323
import { RectScope } from "../util/rect-scope.ts"
24+
import { DIRS, nextGrid } from "../util/dir.ts"
2425

2526
/** The items on the field */
2627
export class FieldItems implements IStepper, ILoader {
@@ -638,3 +639,58 @@ export class Field implements IField {
638639
return this.#initialActivateReady
639640
}
640641
}
642+
643+
export function splashColor(
644+
field: IField,
645+
i: number,
646+
j: number,
647+
hue: number,
648+
sat: number,
649+
light: number,
650+
alpha: number = 0.40,
651+
radius: number = 2,
652+
rng: () => number = Math.random,
653+
): void {
654+
if (radius < 1) {
655+
return
656+
}
657+
colorCell(i, j, hue, sat, light, alpha, field, rng)
658+
if (radius < 2) {
659+
return
660+
}
661+
for (const dir of DIRS) {
662+
for (let dist = 2; dist <= radius; dist++) {
663+
const [i_, j_] = nextGrid(i, j, dir, dist - 1)
664+
if (!field.canEnterStatic(i_, j_)) break
665+
setTimeout(() => {
666+
colorCell(
667+
i_,
668+
j_,
669+
hue,
670+
sat,
671+
light,
672+
alpha * 0.6 ** dist,
673+
field,
674+
rng,
675+
)
676+
}, dist * 32)
677+
}
678+
}
679+
}
680+
681+
function colorCell(
682+
i: number,
683+
j: number,
684+
hue: number,
685+
sat: number,
686+
light: number,
687+
alpha: number,
688+
field: IField,
689+
rng: () => number,
690+
): void {
691+
field.colorCell(
692+
i,
693+
j,
694+
`hsla(${hue}, ${sat}%, ${light}%, ${alpha + rng() * 0.15 - 0.05})`,
695+
)
696+
}

game/game-screen.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ import { RectScope } from "../util/rect-scope.ts"
88

99
import { Field } from "./field.ts"
1010

11-
const I = 72
12-
const J = 18
11+
/** The starting X coordinate */
12+
const I = 114
13+
/** The starting Y coordinate */
14+
const J = 164
1315

1416
/**
1517
* The area which is visible to the user

game/main-character.ts

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { DIRS, DOWN, LEFT, RIGHT, UP } from "../util/dir.ts"
22
import { loadImage } from "../util/load.ts"
33
import { Input, inputQueue } from "./ui/input.ts"
44
import { Character, Move, spawnCharacter } from "../model/character.ts"
5+
import { splashColor } from "./field.ts"
56
import type { IField, MovePlan } from "../model/types.ts"
67
import * as signal from "../util/signal.ts"
78
import { bindToggleFullscreenOnce } from "../util/fullscreen.ts"
@@ -20,7 +21,7 @@ function colorCell(
2021
field.colorCell(
2122
i,
2223
j,
23-
`hsla(${hue}, ${sat}%, ${light}%, ${alpha + rng() * 0.1})`,
24+
`hsla(${hue}, ${sat}%, ${light}%, ${alpha + rng() * 0.15 - 0.05})`,
2425
)
2526
}
2627

@@ -72,23 +73,21 @@ export class MainCharacter extends Character {
7273
}
7374

7475
const { rng } = seed(this.i + " " + this.j)
75-
7676
const hue = 333.3
7777
const sat = 59.4
78-
const light = 38.6
79-
80-
colorCell(this.i, this.j, hue, sat, light, 0.40, field, rng)
81-
82-
for (const dir of DIRS) {
83-
for (let dist = 1; dist <= 3; dist++) {
84-
const [i, j] = this.nextGrid(dir, dist)
85-
if (!field.canEnterStatic(i, j)) break
86-
setTimeout(() => {
87-
colorCell(i, j, hue, sat, light, 0.24 - dist * 0.06, field, rng)
88-
}, dist * 32)
89-
}
90-
}
91-
78+
const light = 32
79+
const alpha = 0.40
80+
splashColor(
81+
field,
82+
this.i,
83+
this.j,
84+
hue,
85+
sat,
86+
light,
87+
alpha,
88+
4,
89+
rng,
90+
)
9291
const count = signal.appleCount.get()
9392
signal.appleCount.update(count + 1)
9493
break
@@ -121,6 +120,14 @@ export class MainCharacter extends Character {
121120
{ type: "speed", change: "4x" },
122121
)
123122
for (const _ of Array(30)) {
123+
this.enqueueAction({
124+
type: "splash",
125+
hue: 280,
126+
sat: 40,
127+
light: 30,
128+
alpha: 0.2,
129+
radius: 3,
130+
})
124131
this.enqueueAction({ type: "go", dir: this.dir })
125132
}
126133
this.enqueueAction(

model/character.ts

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@ import {
22
DIRS,
33
DOWN,
44
LEFT,
5+
nextGrid,
56
opposite,
67
RIGHT,
78
turnLeft,
89
turnRight,
910
UP,
1011
} from "../util/dir.ts"
12+
import { splashColor } from "../game/field.ts"
1113
import { CELL_SIZE } from "../util/constants.ts"
1214
import { seed } from "../util/random.ts"
1315
import type {
@@ -294,17 +296,7 @@ export abstract class Character implements IActor {
294296

295297
/** Returns the next grid coordinates of the 1 cell next of the character to the given direction */
296298
nextGrid(dir: Dir, distance = 1): [i: number, j: number] {
297-
switch (dir) {
298-
case UP:
299-
return [this.#i, this.#j - distance]
300-
case DOWN:
301-
return [this.#i, this.#j + distance]
302-
case LEFT:
303-
return [this.#i - distance, this.#j]
304-
case RIGHT:
305-
return [this.#i + distance, this.#j]
306-
}
307-
throw new Error(`Unknown direction: ${dir}`)
299+
return nextGrid(this.#i, this.#j, dir, distance)
308300
}
309301

310302
/** Returns true if the character can go to the given direction */
@@ -417,6 +409,21 @@ export abstract class Character implements IActor {
417409
} else {
418410
return this.#getNextMovePlanWrap(field)
419411
}
412+
} else if (nextAction.type === "splash") {
413+
const { i, j } = this
414+
const { rng } = seed(`${i}${j}`)
415+
splashColor(
416+
field,
417+
i,
418+
j,
419+
nextAction.hue,
420+
nextAction.sat,
421+
nextAction.light,
422+
nextAction.alpha,
423+
nextAction.radius,
424+
rng,
425+
)
426+
return this.#getNextMovePlanWrap(field)
420427
}
421428
return nextAction
422429
}

model/types.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,3 +102,11 @@ export type Action =
102102
| "back"
103103
}
104104
| { readonly type: "wait"; readonly until: number }
105+
| {
106+
readonly type: "splash"
107+
readonly hue: number
108+
readonly sat: number
109+
readonly light: number
110+
readonly alpha: number
111+
readonly radius: number
112+
}

0 commit comments

Comments
 (0)