-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.js
183 lines (154 loc) · 5.31 KB
/
game.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
const c = document.getElementById("myCanvas");
const ctx = c.getContext("2d");
let adown = false;
let ddown = false;
let currentAnim = 'stand';
const multi = 8; //block size
const groundLevel = 4;
const jumpPower = 300;
const stepPower = 50;
function buttonEvent(key, state) {
if (key === 65) { //a button
adown = (state === 'down');
Player.powerX = (state === 'down') ? (-1 * stepPower) : (ddown) ? stepPower : 0;
}
if (key === 68) { //d button
ddown = (state === 'down');
Player.powerX = (state === 'down') ? stepPower : (adown) ? (-1 * stepPower) : 0;
}
if ((key === 32) && (state === 'down') && !Player.fall) { //spce button - jump
Player.powerY = jumpPower;
}
}
window.onkeydown = function(e) {
const kk = e.keyCode;
buttonEvent(kk,'down');
}
window.onkeyup = function(e) {
const kk = e.keyCode;
buttonEvent(kk,'up');
}
const getXCoord = (x, halfX) => (x * multi) - (halfX * multi);
const getYCoord = (y, halfY) => (600 - (y * multi)) - (halfY * multi);
function drawRect(x, y, halfX, halfY) {
const xCoord = getXCoord(x, halfX) - Player.offsetX;
const yCoord = getYCoord(y, halfY) - Player.offsetY;
ctx.rect(xCoord, yCoord, (halfX*2) * multi, (halfY*2) * multi);
ctx.stroke();
}
function drawPlayer() {
const xCoord = getXCoord(Player.baseX, Player.halfX);
const yCoord = getYCoord(Player.baseY, Player.halfY);
const xCoordNew = getXCoord(Player.x, Player.halfX);
const yCoordNew = getYCoord(Player.y, Player.halfY);
Player.offsetX = xCoordNew - xCoord;
Player.offsetY = yCoordNew - yCoord;
const image = findImageByName(currentAnim);
ctx.drawImage(image.element, xCoord, yCoord);
}
function Render() {
ctx.clearRect(0, 0, 600, 600);
ctx.beginPath();
currentAnim = adown ? 'right' : ddown ? 'left' : 'front';
drawPlayer();
Map.forEach(o => drawRect(o.x,o.y, o.halfX, o.halfY)); //draw bricks
}
function isCollide(other) {
if ( Math.abs(Player.x - other.x) >= Player.halfX + other.halfX ) return false;
if ( Math.abs(Player.y - other.y) >= Player.halfY + other.halfY ) return false;
return true;
}
function getCollideObject() {
return Map.find(brick => isCollide(brick));
}
async function loadImage(img) {
const decoded = new Image();
decoded.src = img.file;
await decoded.decode();
return {element: decoded, name:img.name};
}
function findImageByName(name) {
return ImageLibrary.find(img => (img.name === name));
}
//load images
(() => {
images.forEach(image => {
loadImage(image).then(loaded => ImageLibrary.push(loaded));
});
})();
//physics cycle
const t = setInterval(function() {
let collideObj;
let moveHorisontal = 0;
let moveVertical = 0;
if (ImageLibrary.length !== images.length) return false; //not loaded
if (Player.powerX !== 0) {
moveHorisontal = Player.powerX / 4;
}
if (moveHorisontal !== 0) {
Player.x = Player.x + (moveHorisontal/10);
collideObj = getCollideObject();
if (collideObj) {
Player.x = Player.oldX;
} else {
Player.oldX = Player.x;
}
}
if (Player.powerY === 0) {
Player.powerY = -50;
}
if (Player.powerY < -200) { //max falling speed
Player.powerY = -200;
}
moveVertical = Player.powerY / 4;
Player.y = Player.y + (moveVertical/10);
//check obs and ground crossing
Player.fall = true;
if (Player.y < groundLevel) { //мы уже приземлились
Player.y = groundLevel;
Player.powerY = 0;
Player.fall = false;
}
collideObj = getCollideObject();
if (collideObj) {
//мы уже приземлились
//если мы падаем вниз
if (moveVertical < 0) {
Player.y = collideObj.y + collideObj.halfY + Player.halfY; //тут должны быть координаты граунда или обьекта
Player.fall = false;
} else {
//головой об потолок
Player.y = collideObj.y - collideObj.halfY - Player.halfY;
Player.fall = true;
}
Player.powerY = 0;
}
if (Player.fall) {
Player.powerY = Player.powerY - 50;
}
//move moving parts
Map.forEach(brick => {
if (!brick.moving) return;
if(brick.movingParams.axis === 'x') {
const distance = (brick.movingParams.right) ? 0.5 : -0.5;
brick.x = brick.x + distance;
if ((collideObj === brick) && !Player.fall) {
Player.x = Player.x + distance;
}
if ((brick.x < brick.movingParams.startX) || (brick.x > brick.movingParams.finishX)) {
brick.movingParams.right = !brick.movingParams.right; //change moving direction
}
}
if(brick.movingParams.axis === 'y') {
const distance = (brick.movingParams.up) ? 0.5 : -0.5;
brick.y = brick.y + distance;
if ((collideObj === brick) && !Player.fall) {
Player.y = Player.y + distance;
}
if ((brick.y < brick.movingParams.startY) || (brick.y > brick.movingParams.finishY)) {
brick.movingParams.up = !brick.movingParams.up; //change moving direction
}
}
})
Render();
}, 50);