update() {
// Będzie nadpisana w klasach pochodnych
}
draw() {
ctx.fillStyle = this.color;
ctx.fillRect(this.x * this.width, this.y * this.height, this.width, this.height);
}
// Sprawdzenie, czy można przenieść cząsteczkę do danej pozycji
canMoveTo(x, y) {
if (x < 0 || x >= cols || y >= rows) return false;
return grid[x][y] === null;
}
// Przeniesienie cząsteczki w nową pozycję w siatce
moveTo(newX, newY) {
grid[this.x][this.y] = null;
this.x = newX;
this.y = newY;
grid[this.x][this.y] = this;
}
}
// Klasa Sand dziedziczy z Particle
class Sand extends Particle {
constructor(x, y, width, height) {
super(x, y, width, height);
this.colors = ['#C8AE9E', '#D7BEB0', '#DFCDBC', '#C5B1A7'];
this.color = this.colors[Math.floor(Math.random() * this.colors.length)];
}
code
update() {
// Spadanie w dół
if (this.canMoveTo(this.x, this.y + 1)) {
this.moveTo(this.x, this.y + 1);
} else {
// Próba przesunięcia w lewo lub prawo
const directions = Math.random() < 0.5 ? ['left', 'right'] : ['right', 'left'];
for (let dir of directions) {
const newX = dir === 'left' ? this.x - 1 : this.x + 1;
if (this.canMoveTo(newX, this.y + 1)) {
this.moveTo(newX, this.y + 1);
break;
}
}
}
}
Assistant
}
class Concrate extends Particle {
}
// Klasa Water dziedziczy z Particle
class Water extends Particle {
constructor(x, y, width, height) {
super(x, y, width, height);
this.colors = ['#77C3EC', '#89CFF0', '#9DD9F3', '#B8E2F2'];
this.color = this.colors[Math.floor(Math.random() * this.colors.length)];
}
code
update() {
// Spadanie w dół
if (this.canMoveTo(this.x, this.y + 1)) {
this.moveTo(this.x, this.y + 1);
} else {
// Losowe kierunki boczne
const directions = ['left', 'right'];
// Próbujemy przesunąć w lewo lub w prawo
for (let dir of directions) {
const newX = dir === 'left' ? this.x - 1 : this.x + 1;
if (this.canMoveTo(newX, this.y)) {
this.moveTo(newX, this.y);
break;
}
}
// Jeśli nie udało się przesunąć na boki, próbujemy schować się w głąb
if (Math.random() < 0.1) { // Szansa na losowe przesunięcie
const newY = this.y + 1;
if (this.canMoveTo(this.x, newY)) {
this.moveTo(this.x, newY);
}
}
}
}
}
let gridActivated = false
function drawGrid() {
if (gridActivated) {
let x = 0
let y = 0
for (let i = 0; i <= 20; i++) {
x += 25
ctx.strokeStyle = '#FF0000'
ctx.beginPath();
ctx.moveTo(x, 0)
ctx.lineTo(x, 600)
ctx.stroke()
}
code
for (let i = 0; i <= 20; i++) {
y += 25
ctx.strokeStyle = '#FF0000'
ctx.beginPath();
ctx.moveTo(0, y)
ctx.lineTo(800, y)
ctx.stroke()
}
}
}
let isPressed = false;
let particlePos = [0, 0];
let sandParticles = [];
let waterParticles = [];
let currentTool = 'sand'; // Domyślny narzędzie
// Obsługa przycisków do wyboru narzędzia
document.getElementById('sand-button').addEventListener('click', () => {
currentTool = 'sand';
});
// Upewnij się, że indeksy są w granicach siatki
particlePos[0] = Math.max(0, Math.min(cols - 1, particlePos[0]));
particlePos[1] = Math.max(0, Math.min(rows - 1, particlePos[1]));
}
javascript
// Dodajemy nowe tablice dla prochu strzelniczego i iskier
let gunpowderParticles = [];
let sparkParticles = [];
// Dodajemy nasłuchiwacze dla nowych przycisków narzędzi
document.getElementById('gunpowder-button').addEventListener('click', () => {
currentTool = 'gunpowder';
});
document.getElementById('spark-button').addEventListener('click', () => {
currentTool = 'spark';
});
// Definiujemy klasę Gunpowder
class Gunpowder extends Particle {
constructor(x, y, width, height) {
super(x, y, width, height);
this.color = '#333333'; // Kolor reprezentujący proch
}
update() {
// Proch jest statyczny, brak dodatkowej logiki
}
}
// Definiujemy klasę Spark
class Spark extends Particle {
constructor(x, y, width, height) {
super(x, y, width, height);
this.color = '#FFA500'; // Kolor iskry
this.vx = 0; // Prędkość pozioma
this.vy = -1; // Prędkość pionowa (ruch w górę)
}
update() {
let newX = this.x + this.vx;
let newY = this.y + this.vy;
// Sprawdzamy, czy nowa pozycja jest w granicach siatki
if (newX < 0 || newX >= cols || newY < 0 || newY >= rows) {
// Usuwamy iskrę, jeśli wychodzi poza siatkę
grid[this.x][this.y] = null;
sparkParticles.splice(sparkParticles.indexOf(this), 1);
return;
}
// Jeśli iskra trafia na proch, spalamy proch
if (grid[newX][newY] instanceof Gunpowder) {
grid[newX][newY] = null;
let index = gunpowderParticles.findIndex(p => p.x === newX && p.y === newY);
if (index !== -1) {
gunpowderParticles.splice(index, 1);
}
// Usuwamy iskrę po spaleniu prochu
grid[this.x][this.y] = null;
sparkParticles.splice(sparkParticles.indexOf(this), 1);
return;
}
if (grid[newX][newY] === null) {
// Przenosimy iskrę do nowej pozycji
grid[this.x][this.y] = null;
this.x = newX;
this.y = newY;
grid[this.x][this.y] = this;
} else {
// Usuwamy iskrę w przypadku kolizji z inną cząsteczką
grid[this.x][this.y] = null;
sparkParticles.splice(sparkParticles.indexOf(this), 1);
}
}
}
// Modyfikujemy funkcję main, aby uwzględniała proch i iskrę
function main() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
if (isPressed) {
const gridX = particlePos[0];
const gridY = particlePos[1];
if (grid[gridX][gridY] === null) {
let particle;
if (currentTool === 'sand') {
particle = new Sand(gridX, gridY, gridSize, gridSize);
sandParticles.push(particle);
} else if (currentTool === 'water') {
particle = new Water(gridX, gridY, gridSize, gridSize);
waterParticles.push(particle);
} else if (currentTool === 'gunpowder') {
particle = new Gunpowder(gridX, gridY, gridSize, gridSize);
gunpowderParticles.push(particle);
} else if (currentTool === 'spark') {
particle = new Spark(gridX, gridY, gridSize, gridSize);
sparkParticles.push(particle);
}
grid[gridX][gridY] = particle;
}
}
// Aktualizacja i rysowanie piasku
for (let i = sandParticles.length - 1; i >= 0; i--) {
const sand = sandParticles[i];
sand.update();
sand.draw();
}
// Aktualizacja i rysowanie wody
for (let i = waterParticles.length - 1; i >= 0; i--) {
const water = waterParticles[i];
water.update();
water.draw();
}
// Aktualizacja i rysowanie prochu strzelniczego
for (let i = gunpowderParticles.length - 1; i >= 0; i--) {
const gunpowder = gunpowderParticles[i];
gunpowder.update();
gunpowder.draw();
}
// Aktualizacja i rysowanie iskier
for (let i = sparkParticles.length - 1; i >= 0; i--) {
const spark = sparkParticles[i];
spark.update();
spark.draw();
}
drawGrid()
requestAnimationFrame(main);
}
main();
// Modyfikujemy funkcję addParticle, aby obsługiwała nowe narzędzia
function addParticle(e) {
const rect = canvas.getBoundingClientRect();
const mouseX = e.clientX - rect.left;
const mouseY = e.clientY - rect.top;
particlePos[0] = Math.floor(mouseX / gridSize);
particlePos[1] = Math.floor(mouseY / gridSize);
// Upewniamy się, że indeksy są w granicach siatki
particlePos[0] = Math.max(0, Math.min(cols - 1, particlePos[0]));
particlePos[1] = Math.max(0, Math.min(rows - 1, particlePos[1]));
const gridX = particlePos[0];
const gridY = particlePos[1];
if (currentTool === 'sand') {
if (grid[gridX][gridY] === null) {
const particle = new Sand(gridX, gridY, gridSize, gridSize);
sandParticles.push(particle);
grid[gridX][gridY] = particle;
}
} else if (currentTool === 'water') {
if (grid[gridX][gridY] === null) {
const particle = new Water(gridX, gridY, gridSize, gridSize);
waterParticles.push(particle);
grid[gridX][gridY] = particle;
}
} else if (currentTool === 'gunpowder') {
if (grid[gridX][gridY] === null) {
const particle = new Gunpowder(gridX, gridY, gridSize, gridSize);
gunpowderParticles.push(particle);
grid[gridX][gridY] = particle;
}
} else if (currentTool === 'spark') {
if (grid[gridX][gridY] === null) {
const spark = new Spark(gridX, gridY, gridSize, gridSize);
sparkParticles.push(spark);
grid[gridX][gridY] = spark;
}
}
}
Instrukcje Dodatkowe:
Dodaj Przyciski do HTML:
Upewnij się, że w Twoim pliku HTML znajdują się przyciski z identyfikatorami gunpowder-button oraz spark-button. Przykład:
Stylizacja Nowych Cząsteczek:
Możesz dostosować kolory oraz dodatkowe właściwości nowych cząsteczek Gunpowder i Spark według własnych preferencji, modyfikując odpowiednio wartości this.color.
Testowanie Funkcjonalności:
Po dodaniu powyższego kodu oraz przycisków, uruchom swoją aplikację i sprawdź, czy możesz dodawać proch strzelniczy oraz iskrę. Kliknięcie iskrą na proch powinno powodować jego spalenie.
Optymalizacja i Rozszerzenia:
W zależności od potrzeb, możesz rozszerzyć logikę dla Gunpowder i Spark, na przykład dodając efekty wizualne po spaleniu prochu lub różne kierunki ruchu dla iskier.