From ebf62613ef5d4e111406e9d831d8f63ddeaeda85 Mon Sep 17 00:00:00 2001 From: cha0s Date: Fri, 26 Jul 2024 19:28:28 -0500 Subject: [PATCH] feat: collision filtering --- app/ecs/components/collider.js | 25 +++++++++++++++++++++---- app/ecs/schema.test.js | 25 +++++++++++++++++++++---- public/assets/magic-swords/start.js | 1 + 3 files changed, 43 insertions(+), 8 deletions(-) diff --git a/app/ecs/components/collider.js b/app/ecs/components/collider.js index de74c66..13f7981 100644 --- a/app/ecs/components/collider.js +++ b/app/ecs/components/collider.js @@ -188,8 +188,18 @@ export default class Collider extends Component { } for (const i in aabbs) { const aabb = aabbs[i]; + const body = this.bodies[i]; for (const j in otherAabbs) { const otherAabb = otherAabbs[j]; + const otherBody = other.bodies[j]; + if (body.group === otherBody.group && body.group < 0) { + continue; + } + if (body.group !== otherBody.group || 0 === body.group) { + if (0 === (otherBody.mask & body.bits) || 0 === (body.mask & otherBody.bits)) { + continue; + } + } // todo accuracy if (intersects(aabb, otherAabb)) { intersections.push({ @@ -244,6 +254,14 @@ export default class Collider extends Component { } } async load(instance) { + for (const i in instance.bodies) { + instance.bodies[i] = { + ...this.constructor.schema.constructor.defaultValue( + this.constructor.schema.specification.concrete.properties.bodies.concrete.subtype, + ), + ...instance.bodies[i], + }; + } instance.updateAabbs(); // heavy handed... if ('undefined' !== typeof window) { @@ -270,15 +288,14 @@ export default class Collider extends Component { subtype: { type: 'object', properties: { + bits: {defaultValue: 0x00000001, type: 'uint32'}, impassable: {type: 'uint8'}, + group: {type: 'int32'}, + mask: {defaultValue: 0xFFFFFFFF, type: 'uint32'}, points: { type: 'array', subtype: vector2d('int16'), }, - tags: { - type: 'array', - subtype: {type: 'string'}, - }, unstoppable: {type: 'uint8'}, }, }, diff --git a/app/ecs/schema.test.js b/app/ecs/schema.test.js index 366028e..98f38b4 100644 --- a/app/ecs/schema.test.js +++ b/app/ecs/schema.test.js @@ -2,11 +2,12 @@ import {expect, test} from 'vitest'; import Schema from './schema.js'; +const compare = (specification, value) => { + expect(new Schema(specification).defaultValue()) + .to.deep.equal(value); +}; + test('defaults values', () => { - const compare = (specification, value) => { - expect(new Schema(specification).defaultValue()) - .to.deep.equal(value); - }; [ 'uint8', 'int8', @@ -54,6 +55,22 @@ test('defaults values', () => { ); }); +test.todo('defaults nested values', () => { + compare( + { + type: 'array', + subtype: { + type: 'object', + properties: { + foo: {defaultValue: 'bar', type: 'string'}, + }, + }, + }, + [{}], + [{foo: 'bar'}], + ); +}); + test('validates a schema', () => { [ 'uint8', diff --git a/public/assets/magic-swords/start.js b/public/assets/magic-swords/start.js index 7e068ca..1267826 100644 --- a/public/assets/magic-swords/start.js +++ b/public/assets/magic-swords/start.js @@ -14,6 +14,7 @@ for (let i = 0; i < N; ++i) { Collider: { bodies: [ { + group: -1, points: [ {x: -2.5, y: -2.5}, {x: 14, y: -2.5},