Skip to content

Commit

Permalink
remove box, jumper and wheel
Browse files Browse the repository at this point in the history
  • Loading branch information
lazygyu committed Nov 22, 2024
1 parent 10da519 commit 26a018d
Show file tree
Hide file tree
Showing 11 changed files with 12 additions and 358 deletions.
3 changes: 2 additions & 1 deletion .prettierrc
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
"trailingComma": "es5",
"tabWidth": 2,
"semi": true,
"singleQuote": true
"singleQuote": true,
"printWidth": 120
}
3 changes: 1 addition & 2 deletions eslint.config.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import typescriptEslint from 'typescript-eslint';
import eslintPluginPrettier from 'eslint-plugin-prettier';
import eslintCongigPrettier from 'eslint-config-prettier';

export default typescriptEslint.config(typescriptEslint.configs.recommended, {
files: ['**/*.ts'],
// Prettier 규칙을 ESLint 통합
plugins: { prettier: eslintPluginPrettier },
plugins: ['prettier'],
extends: [
eslintCongigPrettier, // ESLint와 Prettier 충돌 방지
],
Expand Down
9 changes: 0 additions & 9 deletions src/IPhysics.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import type { StageDef } from './data/maps';
import type { WheelState } from './types/WheelState';
import type { BoxState } from './types/BoxState';
import type { JumperState } from './types/JumperState';
import { MapEntityState } from './types/MapEntity.type';

export interface IPhysics {
Expand All @@ -21,12 +18,6 @@ export interface IPhysics {

getMarblePosition(id: number): { x: number; y: number };

getWheels(): WheelState[];

getBoxes(): BoxState[];

getJumpers(): JumperState[];

getEntities(): MapEntityState[];

impact(id: number): void;
Expand Down
57 changes: 0 additions & 57 deletions src/data/maps.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,10 @@
import { MapEntity } from '../types/MapEntity.type';

type WallDef = [x: number, y: number];
type BoxDef = [
x: number,
y: number,
angle: number,
width: number,
height: number,
];
type WheelDef = [
x: number,
y: number,
power: number,
centerX?: number,
centerY?: number,
size?: number,
];
type JumperDef = [x: number, y: number, size: number, temporary?: boolean];

export type StageDef = {
title: string;
walls: WallDef[][];
boxes: BoxDef[];
wheels: WheelDef[];
jumpers?: JumperDef[];
entities?: MapEntity[];
goalY: number;
zoomY: number;
Expand Down Expand Up @@ -413,8 +394,6 @@ export const stages: StageDef[] = [
props: { density: 1, angularVelocity: -1.2, restitution: 0 },
},
],
boxes: [],
wheels: [],
},
{
title: 'BubblePop',
Expand Down Expand Up @@ -888,9 +867,6 @@ export const stages: StageDef[] = [
props: { density: 1, angularVelocity: 0, restitution: 1.5, life: 1 },
},
],
wheels: [],
boxes: [],
jumpers: [],
},
{
title: 'Pot of greed',
Expand Down Expand Up @@ -1135,38 +1111,5 @@ export const stages: StageDef[] = [
props: { density: 1, angularVelocity: 10, restitution: 0 },
},
],
boxes: [
// x, y, angle, width, height
// [13, 20, rad(45), 3, 3],
// [13, 55, rad(45), 3, 3],
//
// [8, 37, rad(45), 2, 2],
// [18, 37, rad(45), 2, 2],
],
jumpers: [],
wheels: [
// [11, 12, -3],
// [15, 12, 3],
//
// [8, 104, -10, 0, 0, 1],
// [6, 103, -10, 0, 0, 1.5],
// [4, 100, -10, 0, 0, 1.5],
// [3.5, 95, -10],
// [3, 90, -10],
// [2.75, 85, -10],
// [2.5, 80, -10],
// [2.25, 75, -10],
// [2, 70, -10],
//
// [18, 104, 10, 0, 0, 1],
// [20, 103, 10, 0, 0, 1.5],
// [22, 100, 10, 0, 0, 1.5],
// [22.5, 95, 10],
// [23, 90, 10],
// [23.25, 85, 10],
// [23.5, 80, 10],
// [23.75, 75, 10],
// [24, 70, 10],
],
},
];
55 changes: 0 additions & 55 deletions src/minimap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ import { DefaultEntityColor, initialZoom } from './data/constants';
import { UIObject } from './UIObject';
import { bound } from './utils/bound.decorator';
import { Rect } from './types/rect.type';
import { WheelState } from './types/WheelState';
import { BoxState } from './types/BoxState';
import { JumperState } from './types/JumperState';
import { VectorLike } from './types/VectorLike';
import { MapEntityState } from './types/MapEntity.type';

Expand Down Expand Up @@ -77,9 +74,6 @@ export class Minimap implements UIObject {

this.ctx.lineWidth = 3 / (params.camera.zoom + initialZoom);
this.drawWalls(params);
this.drawWheels(params.wheels);
this.drawBoxes(params.boxes);
this.drawJumpers(params.jumpers);
this.drawEntities(params.entities);
this.drawMarbles(params);
this.drawViewport(params);
Expand Down Expand Up @@ -126,55 +120,6 @@ export class Minimap implements UIObject {
this.ctx.restore();
}

private drawWheels(wheels: WheelState[]) {
this.ctx.save();
this.ctx.fillStyle = '#94d5ed';
wheels.forEach((wheel) => {
this.ctx.save();
this.ctx.translate(wheel.x, wheel.y);
this.ctx.rotate(wheel.angle);
this.ctx.fillRect(-wheel.size, -0.05, wheel.size * 2, 0.1);
this.ctx.restore();
});
this.ctx.restore();
}

private drawBoxes(boxes: BoxState[]) {
this.ctx.save();
this.ctx.fillStyle = '#94d5ed';
this.ctx.strokeStyle = '#94d5ed';

boxes.forEach((box) => {
this.ctx.save();
this.ctx.translate(box.x, box.y);
this.ctx.rotate(box.angle);
this.ctx.fillRect(-box.width / 2, -box.height / 2, box.width, box.height);
this.ctx.strokeRect(
-box.width / 2,
-box.height / 2,
box.width,
box.height,
);
this.ctx.restore();
});
this.ctx.restore();
}

private drawJumpers(jumpers: JumperState[]) {
this.ctx.save();
this.ctx.fillStyle = 'yellow';
this.ctx.strokeStyle = 'yellow';
jumpers.forEach((jumper) => {
this.ctx.save();
this.ctx.translate(jumper.x, jumper.y);
this.ctx.beginPath();
this.ctx.arc(0, 0, jumper.radius, 0, Math.PI * 2, false);
this.ctx.stroke();
this.ctx.restore();
});
this.ctx.restore();
}

private drawEntities(entities: MapEntityState[]) {
this.ctx.save();
entities.forEach((entity) => {
Expand Down
137 changes: 0 additions & 137 deletions src/physics-box2d.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import { IPhysics } from './IPhysics';
import { StageDef } from './data/maps';
import { BoxState } from './types/BoxState';
import { JumperState } from './types/JumperState';
import { WheelState } from './types/WheelState';
import Box2DFactory from 'box2d-wasm';
import { MapEntity, MapEntityState } from './types/MapEntity.type';

Expand All @@ -13,9 +10,6 @@ export class Box2dPhysics implements IPhysics {

private marbleMap: { [id: number]: Box2D.b2Body } = {};
private walls: Box2D.b2Body[] = [];
private boxes: ({ body: Box2D.b2Body } & BoxState)[] = [];
private wheels: { body: Box2D.b2Body; size: number }[] = [];
private jumpers: ({ body: Box2D.b2Body } & JumperState)[] = [];
private entities: ({ body: Box2D.b2Body } & MapEntityState)[] = [];

private deleteCandidates: Box2D.b2Body[] = [];
Expand All @@ -29,9 +23,6 @@ export class Box2dPhysics implements IPhysics {

clear(): void {
this.clearWalls();
this.clearWheels();
this.clearBoxes();
this.clearJumpers();
this.clearEntities();
}

Expand All @@ -44,9 +35,6 @@ export class Box2dPhysics implements IPhysics {

createStage(stage: StageDef): void {
this.createWalls(stage.walls);
this.createWheels(stage.wheels);
this.createBoxes(stage.boxes);
this.createJumpers(stage.jumpers);
this.createEntities(stage.entities);
}

Expand Down Expand Up @@ -109,68 +97,6 @@ export class Box2dPhysics implements IPhysics {
this.entities = [];
}

createJumpers(jumpers: StageDef['jumpers']) {
if (!jumpers) return;

jumpers.forEach((jumperDef) => {
const [x, y, size, temporary] = jumperDef;
const bodyDef = new this.Box2D.b2BodyDef();
bodyDef.set_type(this.Box2D.b2_staticBody);
const body = this.world.CreateBody(bodyDef);
const shape = new this.Box2D.b2CircleShape();
shape.set_m_radius(size);

const fixtureDef = new this.Box2D.b2FixtureDef();
fixtureDef.set_density(1);
fixtureDef.set_restitution(1.5);
fixtureDef.set_shape(shape);

body.CreateFixture(fixtureDef);
body.SetTransform(new this.Box2D.b2Vec2(x, y), 0);
this.jumpers.push({ x, y, radius: size, body, isTemporary: !!temporary });
});
}

clearJumpers() {
this.jumpers.forEach((jumper) => {
this.world.DestroyBody(jumper.body);
});
this.jumpers = [];
}

createWheels(wheels: StageDef['wheels']) {
wheels.forEach((wheelDef) => {
const pos = { x: wheelDef[0], y: wheelDef[1] };
const power = wheelDef[2];
const size = wheelDef[5] ?? 2;

const bodyDef = new this.Box2D.b2BodyDef();
bodyDef.set_type(this.Box2D.b2_kinematicBody);
// bodyDef.set_position(new this.Box2D.b2Vec2(pos.x, pos.y));

const body = this.world.CreateBody(bodyDef);

const shape = new this.Box2D.b2PolygonShape();
shape.SetAsBox(size, 0.1);

const fixtureDef = new this.Box2D.b2FixtureDef();
fixtureDef.set_density(1);
fixtureDef.set_shape(shape);

body.CreateFixture(fixtureDef);
body.SetAngularVelocity(power);
body.SetTransform(new this.Box2D.b2Vec2(pos.x, pos.y), 0);

this.wheels.push({ body, size });
});
}

clearWheels() {
this.wheels.forEach((wheel) => {
this.world.DestroyBody(wheel.body);
});
this.wheels = [];
}

createWalls(walls: StageDef['walls']) {
walls.forEach((positions) => {
Expand Down Expand Up @@ -199,39 +125,6 @@ export class Box2dPhysics implements IPhysics {
this.walls = [];
}

createBoxes(boxes: StageDef['boxes']) {
boxes.forEach((boxDef) => {
const pos = { x: boxDef[0], y: boxDef[1] };
const width = boxDef[3] ?? 0.5;
const height = boxDef[4] ?? 0.25;
const rotation = boxDef[2] ?? 0;

const def = new this.Box2D.b2BodyDef();
def.set_type(this.Box2D.b2_staticBody);
const body = this.world.CreateBody(def);

const shape = new this.Box2D.b2PolygonShape();
shape.SetAsBox(width, height, 0, rotation);
body.CreateFixture(shape, 1);
body.SetTransform(new this.Box2D.b2Vec2(pos.x, pos.y), 0);
this.boxes.push({
body,
width: width * 2,
height: height * 2,
x: pos.x,
y: pos.y,
angle: rotation,
});
});
}

clearBoxes() {
this.boxes.forEach((box) => {
this.world.DestroyBody(box.body);
});
this.boxes = [];
}

createMarble(id: number, x: number, y: number): void {
const circleShape = new this.Box2D.b2CircleShape();
circleShape.set_m_radius(0.25);
Expand Down Expand Up @@ -274,26 +167,6 @@ export class Box2dPhysics implements IPhysics {
}
}

getWheels(): WheelState[] {
return this.wheels.map((wheel) => {
const pos = wheel.body.GetPosition();
return {
x: pos.x,
y: pos.y,
size: wheel.size,
angle: wheel.body.GetAngle(),
};
});
}

getBoxes(): BoxState[] {
return this.boxes;
}

getJumpers(): JumperState[] {
return this.jumpers;
}

getEntities(): MapEntityState[] {
return this.entities.map((entity) => {
return {
Expand Down Expand Up @@ -352,15 +225,5 @@ export class Box2dPhysics implements IPhysics {
}
}
}

for (let i = this.jumpers.length - 1; i >= 0; i--) {
const jumper = this.jumpers[i];
const edge = jumper.body.GetContactList();
if (edge.contact && edge.contact.IsTouching()) {
this.deleteCandidates.push(
...this.jumpers.splice(i, 1).map((j) => j.body),
);
}
}
}
}
Loading

0 comments on commit 26a018d

Please sign in to comment.