Skip to content

Commit 87115fc

Browse files
committed
fix hitbox and holding of the iron ith touch controls
1 parent f43d9b4 commit 87115fc

3 files changed

Lines changed: 33 additions & 12 deletions

File tree

src/AnkleHitbox.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
import {
2+
Box3,
23
BoxGeometry,
34
Mesh,
45
MeshBasicMaterial,
56
type OrthographicCamera,
67
type PerspectiveCamera,
7-
Raycaster,
8-
type Vector2,
8+
Vector3,
99
} from "three";
1010
import { ANKLE_MONITOR_OFFSET, BOZO_POSITION } from "./constants";
1111
import type { SolderingIron } from "./SolderingIron";
1212

1313
export class AnkleHitbox extends Mesh {
1414
public health: number = 100;
1515
public isBeingHit = false;
16-
private raycaster = new Raycaster();
16+
private hitbox: Box3;
17+
1718
constructor(
1819
private camera: OrthographicCamera | PerspectiveCamera,
1920
private solderingIron: SolderingIron,
@@ -32,13 +33,19 @@ export class AnkleHitbox extends Mesh {
3233
BOZO_POSITION.z + ANKLE_MONITOR_OFFSET.z,
3334
);
3435
this.renderOrder = 2;
36+
37+
// Create and cache the hitbox
38+
this.hitbox = new Box3().setFromObject(this);
3539
}
36-
public update(deltaTime: number, mouse: Vector2) {
40+
public update(deltaTime: number) {
3741
if (this.solderingIron.isSoldering) {
38-
this.raycaster.setFromCamera(mouse, this.camera);
39-
const intersects = this.raycaster.intersectObject(this);
42+
// Create a small hitbox for the tip of the iron
43+
const ironTipHitbox = new Box3().setFromCenterAndSize(
44+
this.solderingIron.position,
45+
new Vector3(10, 10, 10), // A small 10x10x10 box
46+
);
4047

41-
if (intersects.length > 0) {
48+
if (this.hitbox.intersectsBox(ironTipHitbox)) {
4249
this.isBeingHit = true;
4350
this.health -= 10 * deltaTime;
4451

src/GameManager.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ export class GameManager {
4040
private clock: Clock;
4141
private raycaster: Raycaster;
4242
private mouse: Vector2;
43+
public inputType: "mouse" | "touch" = "mouse";
4344

4445
private state: GameState = "LOADING";
4546
private canInteract: boolean = true;
@@ -500,6 +501,7 @@ export class GameManager {
500501
private onMouseMove(event: MouseEvent) {
501502
if (this.state === "LOADING") return;
502503

504+
this.inputType = "mouse";
503505
this.unlockAudio();
504506

505507
this.updateMousePosition(event.clientX, event.clientY);
@@ -523,6 +525,7 @@ export class GameManager {
523525
event.preventDefault(); // Prevent scrolling
524526
if (this.state === "LOADING") return;
525527

528+
this.inputType = "touch";
526529
this.unlockAudio();
527530

528531
if (event.touches.length > 0) {
@@ -637,8 +640,8 @@ export class GameManager {
637640
if (this.state === "INTRO_ANIMATION") {
638641
this.introAnimation.update(deltaTime);
639642
} else if (this.state === "PLAYING") {
640-
this.solderingIron.update(deltaTime, this.mouse);
641-
this.ankleHitbox.update(deltaTime, this.mouse);
643+
this.solderingIron.update(deltaTime, this.mouse, this.inputType);
644+
this.ankleHitbox.update(deltaTime);
642645
this.xandao.update(deltaTime);
643646
this.bossHealthBar.update(this.ankleHitbox.health);
644647

src/SolderingIron.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,12 @@ export class SolderingIron extends Sprite {
4646
this.smokeSystem = new SmokeSystem();
4747
}
4848

49-
public update(deltaTime: number, mouse: Vector2) {
50-
this.updatePosition(mouse);
49+
public update(
50+
deltaTime: number,
51+
mouse: Vector2,
52+
inputType: "mouse" | "touch",
53+
) {
54+
this.updatePosition(mouse, inputType);
5155
this.smokeSystem.update(deltaTime);
5256

5357
// Only smoke if we are soldering AND hitting the target (sound is playing)
@@ -58,10 +62,17 @@ export class SolderingIron extends Sprite {
5862
}
5963
}
6064

61-
private updatePosition(mouse: Vector2) {
65+
private updatePosition(mouse: Vector2, inputType: "mouse" | "touch") {
6266
const mouse3D = new Vector3(mouse.x, mouse.y, 0.5);
6367
mouse3D.unproject(this.camera);
6468
this.position.copy(mouse3D);
69+
70+
if (inputType === "touch") {
71+
// Offset the iron above the touch point
72+
this.position.x -= 140;
73+
this.position.y += 130;
74+
}
75+
6576
// z=4.8 to be in front of mute button (z=4.5) but behind camera (z=5)
6677
this.position.z = 4.8;
6778
}

0 commit comments

Comments
 (0)