-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgamelib.js
executable file
·140 lines (123 loc) · 4.34 KB
/
gamelib.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
'use strict';
var assetCnt = 0;
var frameCnt = 0;
/*
* Easing Functions - inspired from http://gizma.com/easing/
* only considering the t value for the range [0, 1] => [0, 1]
*/
var EasingFunctions = {
// no easing, no acceleration
linear: function (t) { return t },
// accelerating from zero velocity
easeInQuad: function (t) { return t*t },
// decelerating to zero velocity
easeOutQuad: function (t) { return t*(2-t) },
// acceleration until halfway, then deceleration
easeInOutQuad: function (t) { return t<.5 ? 2*t*t : -1+(4-2*t)*t },
// accelerating from zero velocity
easeInCubic: function (t) { return t*t*t },
// decelerating to zero velocity
easeOutCubic: function (t) { return (--t)*t*t+1 },
// acceleration until halfway, then deceleration
easeInOutCubic: function (t) { return t<.5 ? 4*t*t*t : (t-1)*(2*t-2)*(2*t-2)+1 },
// accelerating from zero velocity
easeInQuart: function (t) { return t*t*t*t },
// decelerating to zero velocity
easeOutQuart: function (t) { return 1-(--t)*t*t*t },
// acceleration until halfway, then deceleration
easeInOutQuart: function (t) { return t<.5 ? 8*t*t*t*t : 1-8*(--t)*t*t*t },
// accelerating from zero velocity
easeInQuint: function (t) { return t*t*t*t*t },
// decelerating to zero velocity
easeOutQuint: function (t) { return 1+(--t)*t*t*t*t },
// acceleration until halfway, then deceleration
easeInOutQuint: function (t) { return t<.5 ? 16*t*t*t*t*t : 1+16*(--t)*t*t*t*t }
}
function remainderWithPrec(number, decimals) {
return (number-Math.floor(number)).toFixed(decimals);
}
function clampMe(number, min, max){
if (max<min)
{
min = max + (max=min, 0);
}
return number < max ? (number > min ? number : min) : max;
}
function loadImg(path){
assetCnt++;
const img = new Image();
img.onload = ()=> { assetCnt--; console.log(`loaded ${path}`) };
img.src = path;
return img;
}
function mobileFullscreen()
{
var canvas = document.getElementById("gamearea");
//const isMobileDevice = window.navigator.userAgent.toLowerCase().includes("mobi");
if (1 )//|| isMobileDevice)
{
function launchIntoFullscreen(element) {
if(element.requestFullscreen) {
element.requestFullscreen();
} else if(element.mozRequestFullScreen) {
element.mozRequestFullScreen();
} else if(element.webkitRequestFullscreen) {
element.webkitRequestFullscreen();
} else if(element.msRequestFullscreen) {
element.msRequestFullscreen();
}
}
canvas.addEventListener("click", function() {
launchIntoFullscreen(canvas);}
);
}
}
function clearCanvas(color)
{
var canvas = document.getElementById("gamearea");
var ctx = canvas.getContext('2d');
ctx.fillStyle = color;
ctx.fillRect(0, 0, canvas.width, canvas.height);
}
function drawImage(context, img, x, y, width, height, deg, flip, flop, center) {
context.save();
if(typeof width === "undefined") width = img.width;
if(typeof height === "undefined") height = img.height;
if(typeof center === "undefined") center = false;
// Set rotation point to center of image, instead of top/left
if(center) {
x -= width/2;
y -= height/2;
}
// Set the origin to the center of the image
context.translate(x + width/2, y + height/2);
// Rotate the canvas around the origin
if (deg) {
let rad = 2 * Math.PI - deg * Math.PI / 180;
context.rotate(rad);
}
let flipScale = 1, flopScale = 1;
// Flip/flop the canvas
if(flip) flipScale = -1;
if(flop) flopScale = -1;
if(flipScale == -1 || flopScale == -1) {
context.scale(flipScale, flopScale);
}
// Draw the image
context.drawImage(img, -width/2, -height/2, width, height);
context.restore();
}
function loadMoveAnim(name, no_of_frames)
{
var animFrames = new Object();
animFrames.running=[];
animFrames.idle=[];
//animFrames.idle.push(loadImg(`../../assets/img/${name}.png`));
for (var i=1; i<no_of_frames+1; i++){
animFrames.idle.push(loadImg(`assets/img/${name}_s${i}.png`));
}
for (var i=1; i<no_of_frames+1; i++){
animFrames.running.push(loadImg(`assets/img/${name}_m${i}.png`));
}
return animFrames;
}