Skip to content

Commit 5bd25b7

Browse files
authored
Update animation.tsx
1 parent 9e8af41 commit 5bd25b7

1 file changed

Lines changed: 68 additions & 100 deletions

File tree

src/components/animation.tsx

Lines changed: 68 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -1,128 +1,96 @@
1-
'use client'
1+
"use client"
22

3-
import React, { useEffect, useRef } from 'react';
4-
import Matter from 'matter-js';
5-
6-
7-
// Create an engine
8-
const { Engine, Render, Runner, World, Bodies,Mouse, MouseConstraint } = Matter;
9-
10-
declare global {
11-
interface Window {
12-
engine: Matter.Engine
13-
runner: Matter.Runner
14-
}
15-
}
16-
17-
export default function MatterSimulation(){
18-
const canvasRef = useRef<HTMLCanvasElement | null>(null); // Tentukan tipe ref
3+
import React, { useEffect, useRef,useState } from "react";
4+
import Matter from "matter-js";
195

6+
const MatterSimulation = () => {
7+
const sceneRef = useRef(null); // Ref untuk container canvas
8+
const [score, setScore] = useState(0); // State untuk skor
209

2110
useEffect(() => {
22-
if (!canvasRef.current) return; // Pastikan canvasRef.current ada
11+
// Modul yang diperlukan dari Matter.js
12+
const { Engine, Render, Runner, World, Bodies, Mouse, MouseConstraint, Events } = Matter;
2313

14+
// Buat engine
2415
const engine = Engine.create();
2516
const { world } = engine;
2617

27-
// Create a renderer
18+
// Buat renderer
2819
const render = Render.create({
29-
canvas: canvasRef.current,
30-
engine,
20+
element: sceneRef.current,
21+
engine: engine,
22+
canvas: document.createElement("canvas"),
3123
options: {
32-
width: window.innerWidth,
33-
height: window.innerHeight,
34-
wireframes: false, // Set to true for wireframe mode
35-
background: '#f4f4f4',
24+
width: 800,
25+
height: 600,
26+
wireframes: false,
27+
background: "#f4f4f4",
3628
},
3729
});
3830

39-
// Add a ground
40-
const ground = Bodies.rectangle(window.innerWidth / 2, window.innerHeight - 50, window.innerWidth, 100, {
41-
isStatic: true,
42-
render: {
43-
fillStyle: '#555',
44-
},
45-
});
31+
// Tambahkan canvas ke DOM
32+
sceneRef.current.appendChild(render.canvas);
4633

47-
// Add a ball
48-
const ball = Bodies.circle(window.innerWidth / 2, 200, 50, {
49-
restitution: 0.8,
50-
render: {
51-
fillStyle: '#ff0000',
52-
},
53-
});
34+
// Buat objek fisika
35+
const ground = Bodies.rectangle(400, 590, 810, 60, { isStatic: true });
36+
const leftWall = Bodies.rectangle(0, 300, 60, 600, { isStatic: true });
37+
const rightWall = Bodies.rectangle(800, 300, 60, 600, { isStatic: true });
5438

55-
// Add bodies to the world
56-
World.add(world, [ground, ball]);
39+
const ball = Bodies.circle(400, 100, 40, { restitution: 0.9 });
5740

58-
// Run the engine and renderer
59-
Engine.run(engine);
60-
Render.run(render);
41+
// Tambahkan objek ke dunia
42+
World.add(world, [ground, leftWall, rightWall, ball]);
6143

62-
// Add a runner to keep the simulation running
63-
const runner = Runner.create();
64-
Runner.run(runner, engine);
44+
// Setup mouse interaction
45+
const mouse = Mouse.create(render.canvas);
46+
const mouseConstraint = MouseConstraint.create(engine, {
47+
mouse: mouse,
48+
constraint: {
49+
stiffness: 0.2,
50+
render: {
51+
visible: false, // Sembunyikan garis constraint
52+
},
53+
},
54+
});
6555

66-
// Handle window resizing
67-
const handleResize = () => {
68-
render.canvas.width = window.innerWidth;
69-
render.canvas.height = window.innerHeight;
56+
// Tambahkan mouse constraint ke dunia
57+
World.add(world, mouseConstraint);
7058

71-
// Update the ground's position and size
72-
Matter.Body.setPosition(ground, {
73-
x: window.innerWidth / 2,
74-
y: window.innerHeight - 50,
59+
// Event listener untuk tumbukan
60+
Events.on(engine, "collisionStart", (event) => {
61+
const pairs = event.pairs;
62+
pairs.forEach((pair) => {
63+
if (pair.bodyA === ball || pair.bodyB === ball) {
64+
setScore((prevScore) => prevScore + 1); // Tambahkan skor
65+
}
7566
});
76-
Matter.Body.setVertices(ground, [
77-
{ x: 0, y: window.innerHeight - 50 },
78-
{ x: window.innerWidth, y: window.innerHeight - 50 },
79-
{ x: window.innerWidth, y: window.innerHeight },
80-
{ x: 0, y: window.innerHeight },
81-
]);
82-
};
83-
// Add a stack of boxes
84-
for (let i = 0; i < 5; i++) {
85-
const box = Bodies.rectangle(300 + i * 60, 100, 50, 50, {
86-
render: {
87-
fillStyle: '#00ff00',
88-
},
89-
});
90-
World.add(world, box);
91-
}
92-
93-
94-
95-
const mouse = Mouse.create(render.canvas);
96-
const mouseConstraint = MouseConstraint.create(engine, {
97-
mouse,
98-
constraint: {
99-
stiffness: 0.2,
100-
render: {
101-
visible: false,
102-
},
103-
},
104-
});
105-
106-
World.add(world, mouseConstraint);
107-
render.mouse = mouse;
108-
window.addEventListener('resize', handleResize);
109-
110-
// Cleanup
67+
});
68+
69+
// Jalankan renderer dan engine
70+
Render.run(render);
71+
Runner.run(Runner.create(), engine);
72+
73+
// Cleanup saat komponen di-unmount
11174
return () => {
112-
window.removeEventListener('resize', handleResize);
11375
Render.stop(render);
114-
Runner.stop(runner);
115-
World.clear(world, false);
76+
World.clear(world);
11677
Engine.clear(engine);
117-
render.canvas.remove();
118-
render.textures = {};
78+
sceneRef.current.removeChild(render.canvas);
11979
};
12080
}, []);
121-
122-
return(
123-
<div className="container mx-auto">
124-
<canvas ref={canvasRef} />
81+
return (
82+
<div className="text-4xl font-bold text-gray-800 text-center text-shadow-lg shadow-gray-500">
83+
<h1>Pukul bola jangan sampai menyentuh dinding hitam</h1>
84+
<p>Score : {score}</p>
85+
<p>{score > 12? "kamu kalah": "ayo semangat!!"}</p>
86+
{score > 12 ? window.location.reload(true): " " }
87+
<div ref={sceneRef}></div>
12588
</div>
126-
);
89+
);
90+
12791
};
12892

93+
export default MatterSimulation
94+
95+
96+

0 commit comments

Comments
 (0)