-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmoss.js
229 lines (226 loc) · 5.4 KB
/
moss.js
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/*
Built in Keystrokes
Built in Mouse hover and click
Built in render pipeline
*/
canvas = document.getElementById("myCanvas");
c = canvas.getContext("2d");
function hits(x,y,width,height,xx,yy,ww,hh){
if(x < xx + ww &&
x + width > xx &&
y < yy + hh &&
y + height > yy){
return true;
}
else{
return false;
}
}
function random(max){
return Math.floor(Math.random() * max);
}
function createCanvas(width,height){
canvas.width = width;
canvas.height = height;
this.stroke = true;
}
function image(img,x,y,w,h,opacity=1){
c.globalAlpha = opacity;
c.drawImage(img,x,y,w,h);
c.globalAlpha = 1;
}
function bg(color,opacity = 1){
c.globalAlpha = opacity;
c.fillStyle = color;
c.fillRect(0,0,canvas.width,canvas.height);
c.fillStyle = "#ffffff";
}
function rect(x,y,w,h,opacity = 1){
c.globalAlpha = opacity;
c.fillRect(x,y,w,h);
if(this.stroke){
c.strokeRect(x,y,w,h);
}
}
function circle(x,y,r,opacity = 1){
c.globalAlpha = opacity;
c.beginPath();
if(this.stroke){
c.arc(x,y,r,0,Math.PI *2,);
c.lineWidth += 10;
c.stroke();
c.lineWidth -= 10;
}
else{
c.arc(x,y,r,0,Math.PI *2,);
}
c.fill();
c.globalAlpha = 1;
}
function ellipse(x,y,w,h,r=0,opacity=1){
c.globalAlpha = opacity;
c.beginPath()
if(this.stroke){
c.ellipse(x, y, w, h, r, 0, 2 * Math.PI);
c.stroke();
}
else{
c.ellipse(x, y, w, h, r, 0, 2 * Math.PI);
}
c.fill()
c.globalAlpha = 1;
}
function text(text,x,y,size,font,opacity=1){
c.globalAlpha = opacity;
c.font = 'bold '+size+'px '+font+'';
c.fillText(text,x,y);
}
function line(x,y,xx,yy,opacity=1){
c.globalAlpha = opacity;
c.beginPath();
c.moveTo(x, y);
c.lineTo(xx,yy);
c.stroke();
}
function selectColor(color){
c.fillStyle = color;
}
function strokeColor(color){
c.strokeStyle = color;
}
function noStroke(){
this.stroke = false;
}
function strokeActive(){
this.stroke = true;
}
function strokeSize(size){
c.lineWidth = size;
}
function setCookie(cname, cvalue, exdays) {
const d = new Date();
d.setTime(d.getTime() + (exdays*24*60*60*1000));
let expires = "expires="+ d.toUTCString();
document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
}
function getCookie(cname) {
let name = cname + "=";
let decodedCookie = decodeURIComponent(document.cookie);
let ca = decodedCookie.split(';');
for(let i = 0; i <ca.length; i++) {
let c = ca[i];
while (c.charAt(0) == ' ') {
c = c.substring(1);
}
if (c.indexOf(name) == 0) {
return c.substring(name.length, c.length);
}
}
return "";
}
// Provide TWO arguments both canvas, and evt
function getMousePos(canvas, event) {
var rect = canvas.getBoundingClientRect();
return {
x: event.clientX - rect.left,
y: event.clientY - rect.top
};
}
let mouse = {}
function mouseWillMove(){
document.getElementById("myCanvas").addEventListener('mousemove',function(evt){
mouse = getMousePos(document.getElementById("myCanvas"),evt);
});
};mouseWillMove();
function mouseClicked(func){
document.getElementById("myCanvas").addEventListener('click',function(evt){
func();
});
}
// Provide TWO arguments both mousePos (from getMousePos), and an object array using {} (requires x, y, width, height )
function isInside(pos,rt){
return pos.x > rt.x && pos.x < rt.x + rt.width && pos.y <rt.y+rt.height && pos.y > rt.y;
}
let leftKeyPressed = false;
let rightKeyPressed = false;
let upKeyPressed = false;
let downKeyPressed = false;
let key = "none";
function keyPressed(evt){
if(evt.keyCode == 37||evt.keyCode==65){
key = "pressed"
leftKeyPressed = true;
}
else if(evt.keyCode == 39||evt.keyCode==68){
key = "pressed"
rightKeyPressed = true;
}
else if(evt.keyCode == 38||evt.keyCode==87){
key = "pressed"
upKeyPressed = true;
}
else if(evt.keyCode == 40||evt.keyCode==83){
key = "pressed"
downKeyPressed = true;
}
}
function keyReleased(evt){
if(evt.keyCode == 37||evt.keyCode==65){
leftKeyPressed = false;
}
else if(evt.keyCode == 39||evt.keyCode==68){
rightKeyPressed = false;
}
else if(evt.keyCode == 38||evt.keyCode==87){
upKeyPressed = false;
}
else if(evt.keyCode == 40||evt.keyCode==83){
downKeyPressed = false;
}
}
let allowMultiMove = true;
function playerMove(){
if(!downKeyPressed&&!upKeyPressed&&!leftKeyPressed&&!rightKeyPressed){
key = "none"
}
if(allowMultiMove){
if(leftKeyPressed){
left()
}
if(rightKeyPressed){
right()
}
if(upKeyPressed){
up()
}
if(downKeyPressed){
down()
}
}else{
if(leftKeyPressed){
left()
}
else if(rightKeyPressed){
right()
}
else if(upKeyPressed){
up()
}
else if(downKeyPressed){
down()
}
}
}
function oneKeyAtATime(){
allowMultiMove = false;
}
function useKeys(){
requestAnimationFrame(useKeys)
document.addEventListener('keydown',keyPressed);
document.addEventListener('keyup',keyReleased);
playerMove();
}
function main(){
requestAnimationFrame(main)
render()
};main();