aboutsummaryrefslogtreecommitdiff
path: root/src/math/static/main.js
blob: d66f48c991d94cc1ad6d8dc5edbd70dfa1934857 (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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// The callbacks to be called each frame on update, 
// and on draw
var updateCallback, drawCallback, mouseDownCallback, mouseUpCallback;
// The interval object
var gameInterval;
let width = 800;
let height = 600;
let fps = 30;

window.onload = function () {
    canvas = document.getElementById("canvas");
    ctx = canvas.getContext("2d");
    canvas.width = width;
    canvas.height = height;
    document.addEventListener("keydown", keyPush);
    document.addEventListener("mousedown", mouseDown);
    document.addEventListener("mouseup", mouseUp);
    init();
}

function switchState(game) {
    updateCallback = game.updateCallback;
    drawCallback = game.drawCallback;
    mouseDownCallback = game.mouseDownCallback;
    mouseUpCallback = game.mouseUpCallback; 
}

function game() {
    updateCallback();
    drawCallback();
}

function init() {
    switchState(menu);
    gameInterval = setInterval(game, 1000 / fps);

    let buttonX = (width / 5) * 1 // 2/5 the width
    let buttonWidth = (width / 5) * 3
    let buttonHeight = 40
    menu.buttons.forEach((gameData, i) => {
        gameData.x = buttonX;
        gameData.y = (buttonHeight + 10) * i + 200;
        gameData.width = buttonWidth;
        gameData.height = buttonHeight;
    })
    menu.buttons.push({
        x: 680,
        y: 530,
        width: 100,
        height: buttonHeight, 
        name: "Help",
        game: help,
    })
}

var menu = {
    "buttons": [
        {
            "name": "Nim",
            "game": nim,
        },
        // {
        //     "name": "Solitaire",
        //     "game": solitaire,
        // },
        {
            "name": "Kings Corner",
            "game": kings_corner,
        },
        {
            "name": "Black Hole",
            "game": black_hole,
        },
    ]
};
menu.updateCallback = function () {

}
menu.drawCallback = function () {
    font(48);
    ctx.fillStyle = "#99b3ff";
    ctx.fillRect(0, 0, width, height);

    menu.buttons.forEach((gameData, i) => {
        button(gameData.x, gameData.y, gameData.width, gameData.height, gameData.name, gameData.isClicked);
    })
}

menu.mouseDownCallback = function (e) {
    let newGame = buttonAt(e.x, e.y, menu.buttons);
    if(newGame){
        newGame.isClicked = true;
    }
}

menu.mouseUpCallback = function (e) {
    let newGame = buttonAt(e.x, e.y, menu.buttons);
    if(newGame && newGame.isClicked){
        switchState(newGame.game)
    }
    menu.buttons.forEach((gameData, i) => {
        gameData.isClicked = false;
    })
}

function buttonAt(x, y, buttons) {
    return buttons.find((gameData, i) => {
        if (gameData.x < x && x < gameData.x + gameData.width
            && gameData.y < y && y < gameData.y + gameData.height) {
            return true;
        }
    })
}

function font(size) {
    ctx.font = size + "px Courier";
}

function keyPush(e) {

}

function mouseDown(e) {
    mouseDownCallback(e);
}

function mouseUp(e) {
    mouseUpCallback(e);
}

function button(x, y, w, h, text, isClicked){
    if(isClicked){
        ctx.fillStyle = "grey"
    } else {
        ctx.fillStyle = "darkgrey"
    }
    ctx.fillRect(x, y, w, h);
    ctx.strokeStyle = "black"
    ctx.beginPath();
    ctx.moveTo(x, y);
    ctx.lineTo(x+w, y);
    ctx.lineTo(x+w, y+h);
    ctx.lineTo(x, y+h);
    ctx.lineTo(x, y);
    ctx.stroke(); 
    font(36);
    ctx.fillStyle = "black"
    ctx.fillText(text, x+5, y+h-8)
}