-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ts
75 lines (60 loc) · 2.01 KB
/
main.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import { Developer, Player, Boss } from "@/objects"
import { GameWindow, Intro, Windows } from "@/ui"
import _ from "lodash"
import { Game, GameStorage, Plan } from "./game"
import { Vector } from "./geometry"
import * as MazeGenerator from "@/generator"
import * as Logging from "@/utils/logging"
const MAZE_SIZE: Vector.t = { y: 25, x: 80 }
const logger = Logging.make("main")
logger("Starting the game")
Logging.setIsEnabled((name: string) =>
_.includes(
["main", "player", "game", "fellow_developer", "game_window", "plan", "generator"],
name,
),
)
export function main() {
const plan: Plan.Plan = Plan.generatePlan(0)
Plan.dump(plan)
const game: Game.Game = Game.make(MAZE_SIZE, plan)
MazeGenerator.maze(MAZE_SIZE, game)
const boss: Boss.Boss = Boss.make(
game.map.getRandomLocation((map, position) => {
const objs = map.at(position)
return (
objs.length > 0 &&
objs.every(
(obj) =>
obj.type === "wall" &&
obj.position.y > 1 &&
obj.position.y < game.map.height - 1,
)
)
}),
)
game.map.add([boss])
game.player = Player.make(game.map.getRandomEmptyLocation())
game.map.add([game.player])
game.developer = Developer.make()
game.map.add([game.developer])
window.addEventListener("keydown", (event) => {
logger(`keydown ${event.key}`)
const focused = Windows.focused()
if (focused && focused.keydown) {
focused.keydown(focused, event)
}
})
Windows.show(new GameWindow.GameWindow(game, localStorage))
Windows.show(Windows.center(new Intro.Window()))
}
const localStorage: GameStorage.GameStorage = {
save(json: string): void {
window.localStorage.setItem("map", json)
},
load(): string | null {
const objectsStorage = window.localStorage.getItem("map")
return objectsStorage
},
}
main()