From 57ce707874044070de86ea874a59d60bff6a928f Mon Sep 17 00:00:00 2001 From: James Chen Date: Sun, 12 Jan 2025 19:35:58 +0800 Subject: [PATCH] [v3.8.6] Optimize code size, box2d-js backend. --- EngineErrorMap.md | 4 +++ .../physics-2d/box2d/joints/distance-joint.ts | 1 + cocos/physics-2d/box2d/joints/fixed-joint.ts | 1 + cocos/physics-2d/box2d/joints/hinge-joint.ts | 1 + cocos/physics-2d/box2d/joints/joint-2d.ts | 1 + cocos/physics-2d/box2d/joints/mouse-joint.ts | 1 + .../physics-2d/box2d/joints/relative-joint.ts | 1 + cocos/physics-2d/box2d/joints/slider-joint.ts | 1 + cocos/physics-2d/box2d/joints/spring-joint.ts | 1 + cocos/physics-2d/box2d/joints/wheel-joint.ts | 1 + cocos/physics-2d/box2d/physics-contact.ts | 3 +- cocos/physics-2d/box2d/physics-world.ts | 1 + .../platform/physics-aabb-query-callback.ts | 1 + .../platform/physics-contact-listener.ts | 35 +++++++++++-------- .../box2d/platform/physics-debug-draw.ts | 9 ++--- .../platform/physics-ray-cast-callback.ts | 1 + cocos/physics-2d/box2d/rigid-body.ts | 1 + cocos/physics-2d/box2d/shapes/box-shape-2d.ts | 1 + .../box2d/shapes/circle-shape-2d.ts | 1 + .../box2d/shapes/polygon-shape-2d.ts | 5 +-- cocos/physics-2d/box2d/shapes/shape-2d.ts | 3 +- 21 files changed, 52 insertions(+), 22 deletions(-) diff --git a/EngineErrorMap.md b/EngineErrorMap.md index d7472a18381..821a326ab5c 100644 --- a/EngineErrorMap.md +++ b/EngineErrorMap.md @@ -3888,3 +3888,7 @@ Can't find the spriteFrame of tilesets %s ### 16407 Spline error: invalid mode + +### 16408 + +[Physics2D] b2PolygonShape failed to decompose polygon into convex polygons, node name: %s diff --git a/cocos/physics-2d/box2d/joints/distance-joint.ts b/cocos/physics-2d/box2d/joints/distance-joint.ts index a35b3ceec7e..41f7fc78c5b 100644 --- a/cocos/physics-2d/box2d/joints/distance-joint.ts +++ b/cocos/physics-2d/box2d/joints/distance-joint.ts @@ -28,6 +28,7 @@ import { b2Joint } from './joint-2d'; import { DistanceJoint2D } from '../../framework'; import { PHYSICS_2D_PTM_RATIO } from '../../framework/physics-types'; +/** @mangle */ export class b2DistanceJoint extends b2Joint implements IDistanceJoint { setMaxLength (v: number): void { if (this._b2joint) { diff --git a/cocos/physics-2d/box2d/joints/fixed-joint.ts b/cocos/physics-2d/box2d/joints/fixed-joint.ts index b42cb968b82..3d4d9c7f593 100644 --- a/cocos/physics-2d/box2d/joints/fixed-joint.ts +++ b/cocos/physics-2d/box2d/joints/fixed-joint.ts @@ -28,6 +28,7 @@ import { b2Joint } from './joint-2d'; import { FixedJoint2D } from '../../framework'; import { PHYSICS_2D_PTM_RATIO } from '../../framework/physics-types'; +/** @mangle */ export class b2FixedJoint extends b2Joint implements IFixedJoint { setFrequency (v: number): void { if (this._b2joint) { diff --git a/cocos/physics-2d/box2d/joints/hinge-joint.ts b/cocos/physics-2d/box2d/joints/hinge-joint.ts index ab66f2e02c1..b834b075793 100644 --- a/cocos/physics-2d/box2d/joints/hinge-joint.ts +++ b/cocos/physics-2d/box2d/joints/hinge-joint.ts @@ -29,6 +29,7 @@ import { b2Joint } from './joint-2d'; import { PHYSICS_2D_PTM_RATIO } from '../../framework/physics-types'; import { toRadian } from '../../../core'; +/** @mangle */ export class b2HingeJoint extends b2Joint implements IHingeJoint { enableLimit (v: boolean): void { if (this._b2joint) { diff --git a/cocos/physics-2d/box2d/joints/joint-2d.ts b/cocos/physics-2d/box2d/joints/joint-2d.ts index 1f62c14142c..987bfd87d20 100644 --- a/cocos/physics-2d/box2d/joints/joint-2d.ts +++ b/cocos/physics-2d/box2d/joints/joint-2d.ts @@ -27,6 +27,7 @@ import { IJoint2D } from '../../spec/i-physics-joint'; import { Joint2D, PhysicsSystem2D, RigidBody2D } from '../../framework'; import { b2PhysicsWorld } from '../physics-world'; +/** @mangle */ export class b2Joint implements IJoint2D { get impl (): b2.Joint | null { return this._b2joint; diff --git a/cocos/physics-2d/box2d/joints/mouse-joint.ts b/cocos/physics-2d/box2d/joints/mouse-joint.ts index b4a50af7c55..e526ed476d0 100644 --- a/cocos/physics-2d/box2d/joints/mouse-joint.ts +++ b/cocos/physics-2d/box2d/joints/mouse-joint.ts @@ -35,6 +35,7 @@ import { find } from '../../../scene-graph'; const tempB2Vec2 = new b2.Vec2(); +/** @mangle */ export class b2MouseJoint extends b2Joint implements IMouseJoint { _touchPoint = new Vec2(); _isTouched = false; diff --git a/cocos/physics-2d/box2d/joints/relative-joint.ts b/cocos/physics-2d/box2d/joints/relative-joint.ts index aaee57c899b..ae69f14e020 100644 --- a/cocos/physics-2d/box2d/joints/relative-joint.ts +++ b/cocos/physics-2d/box2d/joints/relative-joint.ts @@ -29,6 +29,7 @@ import { RelativeJoint2D } from '../../framework'; import { PHYSICS_2D_PTM_RATIO } from '../../framework/physics-types'; import { toRadian } from '../../../core'; +/** @mangle */ export class b2RelativeJoint extends b2Joint implements IRelativeJoint { setMaxForce (v: number): void { if (this._b2joint) { diff --git a/cocos/physics-2d/box2d/joints/slider-joint.ts b/cocos/physics-2d/box2d/joints/slider-joint.ts index 7de7655c977..45f59d747a6 100644 --- a/cocos/physics-2d/box2d/joints/slider-joint.ts +++ b/cocos/physics-2d/box2d/joints/slider-joint.ts @@ -29,6 +29,7 @@ import { SliderJoint2D } from '../../framework'; import { PHYSICS_2D_PTM_RATIO } from '../../framework/physics-types'; import { toRadian } from '../../../core'; +/** @mangle */ export class b2SliderJoint extends b2Joint implements ISliderJoint { // limit enableLimit (v: boolean): void { diff --git a/cocos/physics-2d/box2d/joints/spring-joint.ts b/cocos/physics-2d/box2d/joints/spring-joint.ts index 00ce8588797..c5b55d45101 100644 --- a/cocos/physics-2d/box2d/joints/spring-joint.ts +++ b/cocos/physics-2d/box2d/joints/spring-joint.ts @@ -28,6 +28,7 @@ import { b2Joint } from './joint-2d'; import { SpringJoint2D } from '../../framework'; import { PHYSICS_2D_PTM_RATIO } from '../../framework/physics-types'; +/** @mangle */ export class b2SpringJoint extends b2Joint implements ISpringJoint { setDampingRatio (v: number): void { if (this._b2joint) { diff --git a/cocos/physics-2d/box2d/joints/wheel-joint.ts b/cocos/physics-2d/box2d/joints/wheel-joint.ts index 6c8a951503b..d4608449c74 100644 --- a/cocos/physics-2d/box2d/joints/wheel-joint.ts +++ b/cocos/physics-2d/box2d/joints/wheel-joint.ts @@ -29,6 +29,7 @@ import { b2Joint } from './joint-2d'; import { PHYSICS_2D_PTM_RATIO } from '../../framework/physics-types'; import { toRadian } from '../../../core'; +/** @mangle */ export class b2WheelJoint extends b2Joint implements IWheelJoint { setDampingRatio (v: number): void { if (this._b2joint) { diff --git a/cocos/physics-2d/box2d/physics-contact.ts b/cocos/physics-2d/box2d/physics-contact.ts index 07e691c7aa5..bd90e1ea4da 100644 --- a/cocos/physics-2d/box2d/physics-contact.ts +++ b/cocos/physics-2d/box2d/physics-contact.ts @@ -30,7 +30,7 @@ import { b2Shape2D } from './shapes/shape-2d'; import { IPhysics2DContact, IPhysics2DImpulse, IPhysics2DManifoldPoint, IPhysics2DWorldManifold } from '../spec/i-physics-contact'; export type b2ContactExtends = b2.Contact & { - m_userData: any + m_userData: any; } const pools: PhysicsContact[] = []; @@ -66,6 +66,7 @@ const impulse: IPhysics2DImpulse = { tangentImpulses: [] as number[], }; +/** @mangle */ export class PhysicsContact implements IPhysics2DContact { static get (b2contact: b2ContactExtends): PhysicsContact { let c = pools.pop(); diff --git a/cocos/physics-2d/box2d/physics-world.ts b/cocos/physics-2d/box2d/physics-world.ts index d8da3d0fb30..67bc585d24e 100644 --- a/cocos/physics-2d/box2d/physics-world.ts +++ b/cocos/physics-2d/box2d/physics-world.ts @@ -48,6 +48,7 @@ const tempB2AABB = new b2.AABB(); const testResults: Collider2D[] = []; +/** @mangle */ export class b2PhysicsWorld implements IPhysicsWorld { protected _world: b2.World; protected _bodies: b2RigidBody2D[] = []; diff --git a/cocos/physics-2d/box2d/platform/physics-aabb-query-callback.ts b/cocos/physics-2d/box2d/platform/physics-aabb-query-callback.ts index 050a068fc57..80e2e9b1ae9 100644 --- a/cocos/physics-2d/box2d/platform/physics-aabb-query-callback.ts +++ b/cocos/physics-2d/box2d/platform/physics-aabb-query-callback.ts @@ -25,6 +25,7 @@ import b2 from '@cocos/box2d'; import { Vec2 } from '../../../core'; +/** @mangle */ export class PhysicsAABBQueryCallback extends b2.QueryCallback { _point = new b2.Vec2(); _isPoint = false; diff --git a/cocos/physics-2d/box2d/platform/physics-contact-listener.ts b/cocos/physics-2d/box2d/platform/physics-contact-listener.ts index ff70a56753e..db62f1d4276 100644 --- a/cocos/physics-2d/box2d/platform/physics-contact-listener.ts +++ b/cocos/physics-2d/box2d/platform/physics-contact-listener.ts @@ -24,32 +24,39 @@ import b2 from '@cocos/box2d'; import { js } from '../../../core'; +import type { b2ContactExtends } from '../physics-contact'; +type BeginContactCallback = (contact: b2ContactExtends) => void; +type EndContactCallback = (contact: b2ContactExtends) => void; +type PreSolveCallback = (contact: b2ContactExtends, oldManifold: b2.Manifold) => void; +type PostSolveCallback = (contact: b2ContactExtends, impulse: b2.ContactImpulse) => void; + +/** @mangle */ export class PhysicsContactListener extends b2.ContactListener { _contactFixtures: b2.Fixture[] = []; - _BeginContact: Function | null = null; - _EndContact: Function | null = null; - _PreSolve: Function | null = null; - _PostSolve: Function | null = null; + _BeginContact: BeginContactCallback | null = null; + _EndContact: EndContactCallback | null = null; + _PreSolve: PreSolveCallback | null = null; + _PostSolve: PostSolveCallback | null = null; - setBeginContact (cb): void { + setBeginContact (cb: BeginContactCallback): void { this._BeginContact = cb; } - setEndContact (cb): void { + setEndContact (cb: EndContactCallback): void { this._EndContact = cb; } - setPreSolve (cb): void { + setPreSolve (cb: PreSolveCallback): void { this._PreSolve = cb; } - setPostSolve (cb): void { + setPostSolve (cb: PostSolveCallback): void { this._PostSolve = cb; } - BeginContact (contact: b2.Contact): void { + BeginContact (contact: b2ContactExtends): void { if (!this._BeginContact) return; const fixtureA = contact.GetFixtureA(); @@ -64,30 +71,30 @@ export class PhysicsContactListener extends b2.ContactListener { } } - EndContact (contact: b2.Contact): void { + EndContact (contact: b2ContactExtends): void { if (this._EndContact && (contact as any)._shouldReport) { (contact as any)._shouldReport = false; this._EndContact(contact); } } - PreSolve (contact: b2.Contact, oldManifold: b2.Manifold): void { + PreSolve (contact: b2ContactExtends, oldManifold: b2.Manifold): void { if (this._PreSolve && (contact as any)._shouldReport) { this._PreSolve(contact, oldManifold); } } - PostSolve (contact: b2.Contact, impulse: b2.ContactImpulse): void { + PostSolve (contact: b2ContactExtends, impulse: b2.ContactImpulse): void { if (this._PostSolve && (contact as any)._shouldReport) { this._PostSolve(contact, impulse); } } - registerContactFixture (fixture): void { + registerContactFixture (fixture: b2.Fixture): void { this._contactFixtures.push(fixture); } - unregisterContactFixture (fixture): void { + unregisterContactFixture (fixture: b2.Fixture): void { js.array.remove(this._contactFixtures, fixture); } } diff --git a/cocos/physics-2d/box2d/platform/physics-debug-draw.ts b/cocos/physics-2d/box2d/platform/physics-debug-draw.ts index 547b31922e1..69b57d96f49 100644 --- a/cocos/physics-2d/box2d/platform/physics-debug-draw.ts +++ b/cocos/physics-2d/box2d/platform/physics-debug-draw.ts @@ -33,6 +33,7 @@ const _tmp_color = new Color(); const GREEN_COLOR = Color.GREEN; const RED_COLOR = Color.RED; +/** @mangle */ export class PhysicsDebugDraw extends b2.Draw { _drawer: Graphics | null = null; @@ -44,11 +45,11 @@ export class PhysicsDebugDraw extends b2.Draw { this._drawer = drawer; } - _DrawPolygon (vertices, vertexCount): void { + _DrawPolygon (vertices: Vec2[], vertexCount: number): void { const drawer = this._drawer!; for (let i = 0; i < vertexCount; i++) { - b2.Transform.MulXV(this._xf, vertices[i] as Vec2, _tmp_vec2); + b2.Transform.MulXV(this._xf, vertices[i], _tmp_vec2); const x = _tmp_vec2.x * PHYSICS_2D_PTM_RATIO; const y = _tmp_vec2.y * PHYSICS_2D_PTM_RATIO; if (i === 0) drawer.moveTo(x, y); @@ -60,13 +61,13 @@ export class PhysicsDebugDraw extends b2.Draw { drawer.close(); } - DrawPolygon (vertices, vertexCount, color): void { + DrawPolygon (vertices: Vec2[], vertexCount: number, color: Color): void { this._applyStrokeColor(color); this._DrawPolygon(vertices, vertexCount); this._drawer!.stroke(); } - DrawSolidPolygon (vertices, vertexCount, color): void { + DrawSolidPolygon (vertices: Vec2[], vertexCount: number, color: Color): void { this._applyFillColor(color); this._DrawPolygon(vertices, vertexCount); this._drawer!.fill(); diff --git a/cocos/physics-2d/box2d/platform/physics-ray-cast-callback.ts b/cocos/physics-2d/box2d/platform/physics-ray-cast-callback.ts index ae79ad64ad3..99fadeecf30 100644 --- a/cocos/physics-2d/box2d/platform/physics-ray-cast-callback.ts +++ b/cocos/physics-2d/box2d/platform/physics-ray-cast-callback.ts @@ -26,6 +26,7 @@ import b2 from '@cocos/box2d'; import { Vec2 } from '../../../core'; import { ERaycast2DType } from '../../framework'; +/** @mangle */ export class PhysicsRayCastCallback extends b2.RayCastCallback { _type = ERaycast2DType.Closest; _fixtures: b2.Fixture[] = []; diff --git a/cocos/physics-2d/box2d/rigid-body.ts b/cocos/physics-2d/box2d/rigid-body.ts index ccdcf8352ef..f64cdd6c6d5 100644 --- a/cocos/physics-2d/box2d/rigid-body.ts +++ b/cocos/physics-2d/box2d/rigid-body.ts @@ -39,6 +39,7 @@ const tempVec3 = new Vec3(); const tempVec2_1 = new b2.Vec2(); +/** @mangle */ export class b2RigidBody2D implements IRigidBody2D { get impl (): b2.Body | null { return this._body; diff --git a/cocos/physics-2d/box2d/shapes/box-shape-2d.ts b/cocos/physics-2d/box2d/shapes/box-shape-2d.ts index f12c85c9fe3..3642fcc02c7 100644 --- a/cocos/physics-2d/box2d/shapes/box-shape-2d.ts +++ b/cocos/physics-2d/box2d/shapes/box-shape-2d.ts @@ -31,6 +31,7 @@ import { Vec2, Rect } from '../../../core'; const tempAabb = new Rect(); +/** @mangle */ export class b2BoxShape extends b2Shape2D implements IBoxShape { _worldPoints: Vec2[] = [new Vec2(), new Vec2(), new Vec2(), new Vec2()]; get worldPoints (): Readonly[] { diff --git a/cocos/physics-2d/box2d/shapes/circle-shape-2d.ts b/cocos/physics-2d/box2d/shapes/circle-shape-2d.ts index 8079ce3eb24..a3defee7dc2 100644 --- a/cocos/physics-2d/box2d/shapes/circle-shape-2d.ts +++ b/cocos/physics-2d/box2d/shapes/circle-shape-2d.ts @@ -29,6 +29,7 @@ import { PHYSICS_2D_PTM_RATIO } from '../../framework/physics-types'; import { ICircleShape } from '../../spec/i-physics-shape'; import { Vec2 } from '../../../core'; +/** @mangle */ export class b2CircleShape extends b2Shape2D implements ICircleShape { get worldRadius (): number { return (this._shapes[0] as b2.CircleShape).m_radius * PHYSICS_2D_PTM_RATIO; diff --git a/cocos/physics-2d/box2d/shapes/polygon-shape-2d.ts b/cocos/physics-2d/box2d/shapes/polygon-shape-2d.ts index 0a9097add6e..e958e4f06e7 100644 --- a/cocos/physics-2d/box2d/shapes/polygon-shape-2d.ts +++ b/cocos/physics-2d/box2d/shapes/polygon-shape-2d.ts @@ -28,8 +28,9 @@ import * as PolygonPartition from '../../framework/utils/polygon-partition'; import { PolygonCollider2D } from '../../framework'; import { PHYSICS_2D_PTM_RATIO } from '../../framework/physics-types'; import { IPolygonShape } from '../../spec/i-physics-shape'; -import { Vec2 } from '../../../core'; +import { logID, Vec2 } from '../../../core'; +/** @mangle */ export class b2PolygonShape extends b2Shape2D implements IPolygonShape { _worldPoints: Vec2[] = []; get worldPoints (): Readonly[] { @@ -61,7 +62,7 @@ export class b2PolygonShape extends b2Shape2D implements IPolygonShape { const polys = PolygonPartition.ConvexPartition(points); if (!polys) { - console.log('[Physics2D] b2PolygonShape failed to decompose polygon into convex polygons, node name: ', comp.node.name); + logID(16408, comp.node.name); return shapes; } diff --git a/cocos/physics-2d/box2d/shapes/shape-2d.ts b/cocos/physics-2d/box2d/shapes/shape-2d.ts index ca98f6bdf7a..fc261d5bab2 100644 --- a/cocos/physics-2d/box2d/shapes/shape-2d.ts +++ b/cocos/physics-2d/box2d/shapes/shape-2d.ts @@ -34,7 +34,7 @@ const tempFilter = new b2.Filter(); const lowerBound = new b2.Vec2(); const upperBound = new b2.Vec2(); -function getFilter (shape: b2Shape2D): any { +function getFilter (shape: b2Shape2D): b2.Filter { const comp = shape.collider; if (comp.body) { tempFilter.categoryBits = comp.group === PhysicsGroup.DEFAULT ? comp.body.group : comp.group; @@ -45,6 +45,7 @@ function getFilter (shape: b2Shape2D): any { return tempFilter; } +/** @mangle */ export class b2Shape2D implements IBaseShape { protected _shapes: b2.Shape[] = []; protected _fixtures: b2.Fixture[] = [];