summaryrefslogtreecommitdiff
path: root/client/src/logic/logic.ts
blob: d9ac6c875f3a57f53b195069db8b084109041d6d (plain)
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
import { genItems, getMap } from "../map.js";
import { updatePlayers } from "./players.js"
import { updateUI } from "./ui.js"
import { updateMovement } from "./movement.js"
import { updateItems } from "./items.js" 
import { GameState, Input } from "../types.js";
import { updateGhosts } from "./ai.js";

export const InitialState: GameState = {
    started: false,
    input: {},
    players: [],
    ghosts: [undefined, undefined, undefined, undefined],
    items: {},
    mapId: undefined,
    frame: 0,
    rng: 0
}

export const random = (state: GameState): number => {
    return state.rng = (state.rng * 926659 + 4294967291) % 16381
}

export const onLogic = (
    pastData: GameState = InitialState, 
    input: Input = { players: {} },
    frame: number
) => {
    
    let data = structuredClone(pastData)
    data.frame = frame
    random(data)
    
    let startPressed = updatePlayers(data, input);

    if (data.started) {
        updateMovement(data)
        updateItems(data)
        updateGhosts(data)
    } else {
        updateUI(data)
    }
    
    if (startPressed && !data.started) {
        initMap(data, 0)
        data.started = true;	
    }

    return data

}

const initMap = (gameData: GameState, mapId: number) => {

    document.getElementById("lobby").style.display = "none"

    gameData.mapId = mapId

    let map = getMap(mapId)
    // if (!map) {
    //     let {width, height, data} = decompressMap(maps[mapId])
    //     map = genMap(width, height, data, mapId)
    // }
    
    gameData.items = genItems(map)
}