-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
101 lines (81 loc) · 4.45 KB
/
test.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
//! GHOST MOVEMENT original
//* move all the ghosts randomly
ghosts.forEach(ghost => moveGhost(ghost))
//* write the function to move the ghosts
function moveGhost(ghost) {
const directions = [-1, +1, width, -width]
let direction = directions[Math.floor(Math.random() * directions.length)]
ghost.timerID = setInterval(function(){
//if the next square your ghost is going to go in does NOT contain a wall and a ghost, you can go there
if (!squares[ghost.currentIndex + direction].classList.contains("wall") && !squares[ghost.currentIndex + direction].classList.contains("ghost")) {
//you can go here
//remove all ghost related classes
squares[ghost.currentIndex].classList.remove(ghost.className, "ghost", "scared-ghost")
//change the currentindex to the new safe square
ghost.currentIndex += direction
//redraw the ghost in the new safe space
squares[ghost.currentIndex].classList.add(ghost.className, "ghost")
//else find a new direction to try
} else direction = directions[Math.floor(Math.random() * directions.length)]
//if the ghost is currently scared, change their color and behaviour
if (ghost.isScared) {
squares[ghost.currentIndex].classList.add("scared-ghost")
}
//if the ghost is scared and pacman runs towards it
if (ghost.isScared && squares[ghost.currentIndex].classList.contains("pacman")) {
squares[ghost.currentIndex].classList.remove(ghost.className, "ghost", "scared-ghost")
ghost.currentIndex = ghost.startIndex
score += 100
squares[ghost.currentIndex].classList.add(ghost.className, "ghost")
}
checkForGameOver()
}, ghost.speed)
}
______________________________________________________________________________________________________________________
//! GHOST LOGIC
//*add logic to ghost movement to hunt for pacman
//*get coordinates of ghosts and pacman
function getCoordinates (index) {
return [index % width, Math.floor(index / width)]
}
console.log(getCoordinates(502))
//^ GHOST MOVEMENT BASED ON LOGIC
function moveGhost(ghost) {
const directions = [-1, +1, +width, -width]
let direction = directions[Math.floor(Math.random() * directions.length)]
//* ghosttimerid is zero
let ghostTimerID = NaN
ghostTimerID = setInterval(function() {
if (!squares[ghost.currentIndex + direction].classList.contains("wall")) {
//* remove ghost class
squares[ghost.currentIndex].classList.remove(ghost.className, "ghost", "scared-ghost")
//* check if the new space is closer to pacman
const [ghostX, ghostY] = getCoordinates(ghost.currentIndex)
console.log(ghostX)
const [pacmanX, pacmanY] = getCoordinates(pacmanCurrentIndex)
const [ghostNewX, ghostNewY] = getCoordinates(ghost.currentIndex + direction)
function isXCordCloser() {
if ((ghostNewX - pacmanX) > (ghostX - pacmanX)) {
return true
} else return false
}
function isYCordCloser() {
if ((ghostNewY - pacmanY) > (ghostY - pacmanY)) {
return true
} else return false
}
if (isXCordCloser () || isYCordCloser()) {
ghost.currentIndex += direction
squares(ghost.currentIndex).classList.add(ghost.className, "ghost", "scared-ghost")
} else {
squares(ghost.currentIndex).classList.add(ghost.className, "ghost", "scared-ghost")
directions[Math.floor(Math.random() * directions.length)]
}
// ghost.currentIndex += direction
squares[ghost.currentIndex].classList.add(ghost.className, "ghost", "scared-ghost")
} else direction = directions[Math.floor(Math.random() * directions.length)]
//* stop game when pacman is present in the same square as ghost, essentially ghost eats pacman
if (squares[ghost.currentIndex].classList.contains("pac-man")) clearInterval(ghostTimerID)
}, 300)
}
moveGhostLogic()