Skip to content

Commit

Permalink
Make switchGear method public and add collision detection for gear sw…
Browse files Browse the repository at this point in the history
…itching (#46)
  • Loading branch information
MiguelRipoll23 authored Dec 9, 2024
1 parent d176f0c commit cc6d4d0
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/objects/gear-stick-object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export class GearStickObject extends BaseGameObject {
return this.currentGear;
}

private switchGear(): void {
public switchGear(): void {
this.currentGear = this.currentGear === "F" ? "R" : "F";
}

Expand Down
17 changes: 17 additions & 0 deletions src/objects/local-car-object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { ObjectType } from "../enums/object-type.js";
import { CarObject } from "./car-object.js";
import { GearStickObject } from "./gear-stick-object.js";
import { JoystickObject } from "./joystick-object.js";
import { WorldBackgroundObject } from "./backgrounds/world-background-object.js";

export class LocalCarObject extends CarObject {
private readonly joystickObject: JoystickObject;
Expand Down Expand Up @@ -48,6 +49,10 @@ export class LocalCarObject extends CarObject {

public override update(deltaTimeStamp: DOMHighResTimeStamp): void {
if (this.active) {
if (this.isCollidingWithBounds()) {
this.gearStickObject.switchGear();
}

this.handleControls();
}

Expand All @@ -66,6 +71,18 @@ export class LocalCarObject extends CarObject {
this.setTypeId(ObjectType.RemoteCar);
}

private isCollidingWithBounds(): boolean {
const collidingObjects = this.getCollidingObjects();

for (const object of collidingObjects) {
if (object instanceof WorldBackgroundObject) {
return true;
}
}

return false;
}

private handleControls(): void {
if (!this.joystickObject || !this.gearStickObject) return;

Expand Down

0 comments on commit cc6d4d0

Please sign in to comment.