summaryrefslogtreecommitdiff
path: root/client/src/main.ts
blob: a6cc3babfccabbebd2f2eaae1ef8584e381bc465 (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
68
69
70
71
72
73
74
75
import { Game } from "./net/game.js";
import { InitialState, onLogic } from "./logic/logic.js";
import { startGraphicsUpdater } from "./renderer.js";
import { GameKeyMap, Frame, Key } from "./types.js";

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

join.onsubmit = async function(event) {
    event.preventDefault()
     
    const room_code = this.elements.room_code.value.trim()
    const player_name = this.elements.name.value.trim()

    if (room_code == '') {
        alert('Please enter a room code')
        return
    }

    if (player_name == '') {
        alert('Please enter a player name')
        return
    }

    join.style.display = "none"

    startGame(room_code, player_name)
}

const updateGraphics = startGraphicsUpdater()

const onLoad = (startData: Frame) => {

    if (startData.data.started) {
        alert('Room has already started')
        return false
    }

    let players = Object.values(startData.data.players).filter(p => { return p !== null && p.name !== undefined })
    if (players.length >= 4) {
        alert('Room is full')
        return false
    }

    lobby.style.display = ""

    return true
}

const onFrame = (data: Frame, frame: number) => {
    
	updateGraphics(data ? data.data : InitialState, frame);

}


const startGame = (code: string, name: string) => {

    const game = new Game(3000)

    game.start(
        code,
        GameKeyMap,        
        onLoad,
        onFrame,
        onLogic,
        {
            start: false,
            key: Key.NOTHING,
            name
        }
    )

}