forked from Mercer01/hackTheGame
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib.js
More file actions
156 lines (131 loc) · 3.7 KB
/
lib.js
File metadata and controls
156 lines (131 loc) · 3.7 KB
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
150
151
152
153
154
155
156
'use strict';
/**
* ============== CONSTANTS ==============
*/
const GAME_WIDTH = 1920;
const GAME_HEIGHT = 1080;
//Tile and player constants
const GRID_SIZE = 20;
const PLAYER_SIZE = GRID_SIZE * 2;
const PLAYER_SPEED = 5;
//Screen IDs
const MAIN_MENU_SCREEN = 0;
const REGISTR_SCREEN = 1;
const PRESENT_SCREEN = 2;
const DEV_SCREEN = 3
//Key codes
const W_KEY = 87;
const A_KEY = 65;
const S_KEY = 83;
const D_KEY = 68;
/**
* ============== Globals =============
*/
let queue_count = 0;
let current_registering = new Array();
let player = null;
/**
* ============== CANVAS ==============
*/
let canvas = document.getElementById("canvas");
let context = canvas.getContext("2d");
/**
* ============== HELPER FUNCTIONS ==============
*/
//Function to get the mouse position
function getMousePos(event) {
const rect = canvas.getBoundingClientRect();
return {
x: event.clientX - rect.left,
y: event.clientY - rect.top
};
}
function drawPlayButton(x, y, width, height, fillColor) {
context.fillStyle = fillColor;
context.strokeStyle = fillColor;
context.lineWidth = 65;
context.lineJoin = "round"
context.rect(x, y, width, height);
context.fill();
context.stroke();
context.font = '40pt Kremlin Pro Web';
context.fillStyle = '#000000';
context.textAlign = 'center';
context.textBaseline = 'middle';
drawText(x + width / 2, y + height / 2, "Start");
}
//Retursn true if pos is inside of a rect
function isInside(pos, rect) {
return pos.x > rect.x && pos.x < rect.x + rect.width && pos.y < rect.y + rect.height && pos.y > rect.y;
}
function drawCircle(x, y, size) {
context.beginPath();
context.arc(x, y, size, 0, 2 * Math.PI, false);
context.fill();
context.stroke();
}
function drawRect(x, y, w, h, colour = "rgba(0,0,0,1)") {
context.beginPath();
context.fillStyle = colour;
context.fillRect(x, y, w, h);
}
function drawText(x, y, string, size, colour = "rgba(0,0,0,1)") {
context.beginPath();
context.font = size;
context.fillStyle = colour;
context.fillText(string, x, y);
}
function drawImage(name, x, y) {
const image = document.getElementById(name);
context.drawImage(image, x, y);
}
function drawPlayerImage(name, x, y, rotation = 0) {
const image = document.getElementById(name);
context.save();
const midpointX = image.width / 2;
const midpointY = image.height / 2;
const radRotation = rotation * Math.PI / 180;
context.translate(x, y);
context.rotate(radRotation);
context.drawImage(image, -midpointX, -midpointY);
context.restore();
//context.fillStyle = "black";
//context.fillRect(x,y,image.width,image.height);
}
function drawPlayer(name, x, y, rotation = 0) {
const image = document.getElementById(name);
context.save();
const midpointX = image.width / 2;
const midpointY = image.height / 2;
const radRotation = rotation * Math.PI / 180;
context.translate(x, y);
context.rotate(radRotation);
context.drawImage(image, -midpointX, -midpointY);
context.restore();
//context.fillStyle = "black";
//context.fillRect(x,y,image.width,image.height);
}
function drawGridOverlay() {
context.strokeStyle = "black";
//
for (let y = 0; y < 1080; y += 20) {
context.beginPath();
context.moveTo(0, y)
context.lineTo(1960, y);
context.stroke();
}
for (let x = 0; x < 1960; x += 20) {
context.beginPath();
context.moveTo(x, 0)
context.lineTo(x, 1960);
context.stroke();
}
}
function youLost() {
if (player.satisfaction <= 0) return true;
return false;
}
function youWon() {
if (player.food + player.coffee >= 200) return true;
return false;
}