Show sourcecode
The following files exists in this folder. Click to view.
webbserverprogrammering/GYA/js/
box.js
branch_box_1.js
branch_main_1.js
branch_main_2.js
branch_player_1.js
door.js
global.js
main.js
player.js
world.js
box.js
176 lines UTF-8 Windows (CRLF)
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
function loadBoxes(level) {
// Töm befintliga lådor
boxes = [];
// Loopa igenom din world[level] och skapa nya lådor där det ska finnas
for (let y = 0; y < world[level].length; y++) {
for (let x = 0; x < world[level][y].length; x++) {
if (world[level][y][x] === 4) { // antar 4 betyder "box"
boxes.push({
x: x * tile_size,
y: y * tile_size,
width: tile_size,
height: tile_size,
color: "yellow" // eller vad du vill ha
});
}
}
}
}
function moveBox() {
for (let i = 0; i < boxes.length; i++) {
// Spara lådans gamla position
let oldBoxX = boxes[i].x;
let oldBoxY = boxes[i].y;
// --- Putta lådan om spelaren är intill och rör sig mot lådan ---
let pushed = false;
// Vänster
if (
player.x + player.width > boxes[i].x &&
player.x < boxes[i].x &&
player.y + player.height > boxes[i].y &&
player.y < boxes[i].y + boxes[i].height &&
player.dx > 0
) {
boxes[i].x += player.dx * player.speed;
pushed = true;
}
// Höger
else if (
player.x < boxes[i].x + boxes[i].width &&
player.x + player.width > boxes[i].x + boxes[i].width &&
player.y + player.height > boxes[i].y &&
player.y < boxes[i].y + boxes[i].height &&
player.dx < 0
) {
boxes[i].x += player.dx * player.speed;
pushed = true;
}
// Ovan
else if (
player.y + player.height > boxes[i].y &&
player.y < boxes[i].y &&
player.x + player.width > boxes[i].x &&
player.x < boxes[i].x + boxes[i].width &&
player.dy > 0
) {
boxes[i].y += player.dy * player.speed;
pushed = true;
}
// Under
else if (
player.y < boxes[i].y + boxes[i].height &&
player.y + player.height > boxes[i].y + boxes[i].height &&
player.x + player.width > boxes[i].x &&
player.x < boxes[i].x + boxes[i].width &&
player.dy < 0
) {
boxes[i].y += player.dy * player.speed;
pushed = true;
}
// --- Kollision med världskanten ---
if (
boxes[i].x < 0 ||
boxes[i].x > canvas.canvas.width - boxes[i].width ||
boxes[i].y < 0 ||
boxes[i].y > canvas.canvas.height - boxes[i].height
) {
boxes[i].x = oldBoxX;
boxes[i].y = oldBoxY;
if (pushed) {
if (player.dx > 0) player.x = boxes[i].x - player.width;
if (player.dx < 0) player.x = boxes[i].x + boxes[i].width;
if (player.dy > 0) player.y = boxes[i].y - player.height;
if (player.dy < 0) player.y = boxes[i].y + boxes[i].height;
}
}
// --- Kollision med blockerande block (1, 2, 3) ---
let corners = [
[boxes[i].x, boxes[i].y],
[boxes[i].x + boxes[i].width - 1, boxes[i].y],
[boxes[i].x, boxes[i].y + boxes[i].height - 1],
[boxes[i].x + boxes[i].width - 1, boxes[i].y + boxes[i].height - 1]
];
let blocked = false;
for (let c = 0; c < corners.length; c++) {
let gridX = Math.floor(corners[c][0] / tile_size);
let gridY = Math.floor(corners[c][1] / tile_size);
if (
gridY >= 0 && gridY < world[level].length &&
gridX >= 0 && gridX < world[level][gridY].length
) {
if ([1, 2, 3].includes(world[level][gridY][gridX])) {
blocked = true;
break;
}
}
}
if (blocked) {
boxes[i].x = oldBoxX;
boxes[i].y = oldBoxY;
if (pushed) {
if (player.dx > 0) player.x = boxes[i].x - player.width;
if (player.dx < 0) player.x = boxes[i].x + boxes[i].width;
if (player.dy > 0) player.y = boxes[i].y - player.height;
if (player.dy < 0) player.y = boxes[i].y + boxes[i].height;
}
}
// --- NYTT: Stanna lådan om den knuffas mot en annan låda ---
for (let j = 0; j < boxes.length; j++) {
if (i === j) continue;
let b = boxes[j];
let overlapX = boxes[i].x < b.x + b.width && boxes[i].x + boxes[i].width > b.x;
let overlapY = boxes[i].y < b.y + b.height && boxes[i].y + boxes[i].height > b.y;
if (overlapX && overlapY) {
boxes[i].x = oldBoxX;
boxes[i].y = oldBoxY;
if (pushed) {
if (player.dx > 0) player.x = boxes[i].x - player.width;
if (player.dx < 0) player.x = boxes[i].x + boxes[i].width;
if (player.dy > 0) player.y = boxes[i].y - player.height;
if (player.dy < 0) player.y = boxes[i].y + boxes[i].height;
}
break;
}
}
// --- Hindra spelaren från att gå igenom lådan ---
let overlapX = player.x < boxes[i].x + boxes[i].width && player.x + player.width > boxes[i].x;
let overlapY = player.y < boxes[i].y + boxes[i].height && player.y + player.height > boxes[i].y;
if (overlapX && overlapY && !pushed) {
let dxLeft = Math.abs((player.x + player.width) - boxes[i].x);
let dxRight = Math.abs(player.x - (boxes[i].x + boxes[i].width));
let dyTop = Math.abs((player.y + player.height) - boxes[i].y);
let dyBottom = Math.abs(player.y - (boxes[i].y + boxes[i].height));
let minDist = Math.min(dxLeft, dxRight, dyTop, dyBottom);
if (minDist === dxLeft) {
player.x = boxes[i].x - player.width;
} else if (minDist === dxRight) {
player.x = boxes[i].x + boxes[i].width;
} else if (minDist === dyTop) {
player.y = boxes[i].y - player.height;
} else if (minDist === dyBottom) {
player.y = boxes[i].y + boxes[i].height;
}
}
}
}
// Ritar lådan på spelplanen
function drawBox(canvas) {
for (let i = 0; i < boxes.length; i++) {
canvas.fillStyle = boxes[i].color;
canvas.fillRect(boxes[i].x, boxes[i].y, boxes[i].width, boxes[i].height);
}
}