-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
145 lines (117 loc) · 3.85 KB
/
main.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
let c = document.getElementById('canvas');
let ctx = c.getContext('2d')
let tile = 50
//make map
let map = [[1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0],
[0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0],
[0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0],
[0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0],
[0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0],
[0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 2]
];
//collision box as new array to be populated when the map is draw with object and status
let collBox = [];
let mapLength = map[0].length;
let mapHeight = map.length;
//obstacle tiles
let myTile = new Image()
myTile.src = 'assets/tile1.png'
//the kitchen floor
let floor = new Image()
floor.src = 'assets/kitchen.jpg'
//mouse
let mouse = new Image();
mouse.src = 'assets/mouse2.jpg'
//cat
let cat = new Image()
cat.src = 'assets/cat.png'
//sound
let miau = new Audio()
miau.src = 'assets/miau.wav'
let victoryMiau = new Audio();
victoryMiau.src = 'assets/Nyan_cat.mp3'
//2 iesirea
function drawMap(m){
for (let i=0; i < m.length; i++){
collBox.push([]) //empty array into map
for (let j=0; j < m[i].length; j++){
if(m[i][j] === 1){
ctx.beginPath();
// ctx.fillStyle = "#000";
// ctx.fillRect(j*tile, i*tile, tile, tile)
ctx.drawImage(myTile, j*tile, i*tile, tile, tile);
}else if(m[i][j] === 2){
ctx.beginPath();
// ctx.fillStyle = "pink";
// ctx.fillRect(j*tile, i*tile, tile, tile)
ctx.drawImage(mouse, j*tile, i*tile, tile, tile)
}else{
ctx.beginPath();
ctx.drawImage(floor,j*tile, i*tile, tile, tile)
}
collBox[i].push({x:j*tile, y:i*tile, status: m[i][j] === 1 ? 1 : (m[i][j] === 2 ? 2 : 0)})
}
}
}
function drawPlayer(x,y){
ctx.beginPath();
// ctx.fillStyle = "red";
// ctx.fillRect(x, y, tile, tile)
ctx.drawImage(cat, x, y, tile, tile)
}
//object for player
let player = {
x: 0,
y: 0,
newX: 0,
newY: 0
}
//move Player acc to col and row
function move(x,y){
ctx.clearRect(0,0, mapLength*tile, mapHeight*tile); //clear canvas in PX!
drawMap(map); //redraw Map
drawPlayer(x,y); //drawPlayer
player.x = player.newX;
player.y = player.newY
//to save the values when you move
}
//using collBox iteration if 1 2 or 0
function checkCollision(){
for (let i=0; i< mapHeight; i++){
for (let j=0; j< mapLength; j++){
console.log(collBox[i][j.status])
let b = collBox[i][j]
if (player.newX === b.x && player.newY === b.y){
if(b.status === 1){
miau.play()
console.log('Hit rock')
}else if(b.status === 2){
console.log('WIN')
move(player.newX, player.newY)
document.querySelector('.winner-game').style.display = 'block';
victoryMiau.play()
}else {
move(player.newX, player.newY);}
}
else if(player.newX < 0 || player.newX >= mapLength*tile || player.newY < 0 || player.newY >= mapHeight*tile){
console.log('hit wall')
}
}
}
}
//PRESS IN WINDOW
window.addEventListener('keydown', function(event){
if (event.keyCode === 38) {player.newX = player.x ; player.newY = player.y - tile; console.log('UP')}
if (event.keyCode === 37) {player.newX = player.x - tile; player.newY = player.y; console.log('LEFT')}
if (event.keyCode === 39) {player.newX = player.x + tile; player.newY = player.y;console.log('RIGHT')}
if (event.keyCode === 40) {player.newX = player.x ; player.newY = player.y + tile; console.log('DOWN')}
checkCollision()
//add collision check+sound
} )
window.onload = function(){
drawMap(map)
drawPlayer(0,0)
console.log(collBox)
}