Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function rnd(nMax) {
- return Math.floor(Math.random() * nMax);
- }
- const RENDERER_WIDTH = 800;
- const RENDERER_HEIGHT = 600;
- const app = new PIXI.Application(
- RENDERER_WIDTH,
- RENDERER_HEIGHT,
- {backgroundColor : 0x1099bb}
- );
- document.body.appendChild(app.view);
- const INITIAL_BUNNIES_AMOUNT = 10;
- const bunnies = [];
- function makeBunny(width = 20, height = 32) {
- const bunny = PIXI.Sprite.fromImage('examples/assets/bunny.png');
- bunny.anchor.set(0.5);
- bunny.width = width;
- bunny.height = height;
- bunny.body = new PIXI.Rectangle(0, 0, width, height);
- bunny.velocity = new PIXI.Point(0, 0);
- bunnies.push(bunny);
- return bunny;
- }
- function initialize() {
- for (let i = 0; i < INITIAL_BUNNIES_AMOUNT; i++) {
- const bunny = makeBunny();
- bunny.body.x = rnd(app.view.width);
- bunny.body.y = rnd(app.view.height);
- bunny.velocity = new PIXI.Point(rnd(10), rnd(10));
- app.stage.addChild(bunny);
- }
- }
- initialize();
- app.ticker.add(function(delta) {
- bunnies.forEach((b, i) => {
- b.body.x += b.velocity.x;
- b.body.y += b.velocity.y;
- const collisions = bunnies.filter((b2, j) => {
- if (i === j || b2.checked) return;
- const bunnyA_x1 = b.body.x;
- const bunnyA_x2 = bunnyA_x1 + b.body.width;
- const bunnyA_y1 = b.body.y;
- const bunnyA_y2 = bunnyA_y1 + b.body.height;
- const bunnyB_x1 = b2.body.x;
- const bunnyB_x2 = bunnyB_x1 + b2.body.width;
- const bunnyB_y1 = b2.body.y;
- const bunnyB_y2 = bunnyB_y1 + b2.body.height;
- return bunnyA_x1 < bunnyB_x2
- && bunnyA_x2 > bunnyB_x1
- && bunnyA_y1 < bunnyB_y2
- && bunnyA_y2 > bunnyB_y1
- });
- collisions.forEach(c => {
- const velocity = new PIXI.Point(
- b.velocity.x + c.velocity.x,
- b.velocity.y + c.velocity.y
- );
- b.velocity = velocity;
- c.velocity = velocity;
- b.checked = c.checked = true;
- });
- if (b.body.x >= RENDERER_WIDTH || b.body.x <= 0) {
- b.velocity.x *= -1;
- }
- if (b.body.y >= RENDERER_HEIGHT || b.body.y <= 0) {
- b.velocity.y *= -1;
- }
- b.x = b.body.x;
- b.y = b.body.y;
- });
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement