-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
135 lines (112 loc) · 3.27 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
import { Bodies, Body, Engine, Events, Render, Runner, World } from "matter-js"
import { FRUITS_BASE } from "./fruits.js"
import './style.css'
let engine = Engine.create()
let render = Render.create({
engine,
element: document.body,
options: {
wireframes: false,
background: "#f7f4c8",
width: 620,
height: 850,
},
})
let world = engine.world
let leftWall = Bodies.rectangle(15, 395, 30, 790, {
isStatic: true,
render: { fillStyle: "#e6b143" },
})
let rightWall = Bodies.rectangle(605, 395, 30, 790, {
isStatic: true,
render: { fillStyle: "#e6b143" },
})
let ground = Bodies.rectangle(310, 820, 620, 60, {
isStatic: true,
render: { fillStyle: "#e6b143" },
})
let topLine = Bodies.rectangle(310, 150, 620, 2, {
name: "topLine",
isSensor: true,
isStatic: true,
render: { fillStyle: "#e6b143" },
})
World.add(world, [leftWall, rightWall, ground, topLine])
Render.run(render)
Runner.run(engine)
let currentBody = null
let currentFruit = null
let disableAction
function AddFruit() {
let index = Math.floor(Math.random() * 5)
let fruit = FRUITS_BASE[index]
console.log(fruit.color)
let body = Bodies.circle(300, 50, fruit.radius, {
index: index,
isSleeping: true,
render: {
fillStyle: `${fruit.color}`,
},
restitution: 0.2,
})
currentBody = body
currentFruit = fruit
World.add(world, body)
}
window.onkeydown = (event) => {
if (disableAction) {
return
}
switch (event.code) {
case "KeyA":
if (currentBody.position.x - currentFruit.radius > 40) {
Body.setPosition(currentBody, {
x: currentBody.position.x - 10,
y: currentBody.position.y,
})
}
break
case "KeyD":
if (currentBody.position.x + currentFruit.radius < 580) {
Body.setPosition(currentBody, {
x: currentBody.position.x + 10,
y: currentBody.position.y,
})
}
break
case "KeyS":
currentBody.isSleeping = false
disableAction = true
setTimeout(()=>{
AddFruit()
disableAction = false
}, 1000)
break
}
}
Events.on(engine, "collisionStart", (event) => {
event.pairs.forEach((collision) => {
if (collision.bodyA.index === collision.bodyB.index) {
let index = collision.bodyA.index
World.remove(world, [collision.bodyA, collision.bodyB])
let newFruit = FRUITS_BASE[index+1]
let newBody = Bodies.circle(
collision.collision.supports[0].x,
collision.collision.supports[0].y,
newFruit.radius,
{
render: {fillStyle: `${newFruit.color}`},
index: index + 1
}
)
World.add(world, newBody)
if (newBody.index === 10) {
alert('you win')
}
}
if ( !disableAction && (collision.bodyA.name === "topLine" || collision.bodyB.name === "topLine")) {
alert("GameOver")
}
})
})
AddFruit()