Skip to content

Commit

Permalink
refactor(game): improve gate manipulation by using stockableGate + im…
Browse files Browse the repository at this point in the history
…prove particles WIP
  • Loading branch information
mathieuher committed Dec 5, 2024
1 parent 4b3d2de commit 3195487
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 51 deletions.
2 changes: 1 addition & 1 deletion src/app/game/actors/gate-detector.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Actor, CollisionStartEvent, CollisionType, type PreCollisionEvent, type Vector, vec } from 'excalibur';
import { Actor, type CollisionStartEvent, CollisionType, type Vector, vec } from 'excalibur';
import { Skier } from './skier';

export class GateDetector extends Actor {
Expand Down
8 changes: 2 additions & 6 deletions src/app/game/actors/skier.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Actor, CollisionType, type Engine, GpuParticleEmitter, type ParticleEmitter, vec } from 'excalibur';
import { Actor, CollisionType, type Engine, type ParticleEmitter, Scene, vec } from 'excalibur';
import { Config } from '../config';
import { Resources } from '../resources';
import type { Race } from '../scenes/race';
Expand Down Expand Up @@ -223,7 +223,6 @@ export class Skier extends Actor {
private emitSlidingParticles(speedPercentage: number, slidingIntensity: number, skierAction: SkierActions): void {
this.particlesEmitter.pos.y = 2.5;
this.particlesEmitter.radius = 6;
this.particlesEmitter.particle.life = 2500;
this.particlesEmitter.particle.minSpeed = 10;
this.particlesEmitter.particle.maxSpeed = 50;
if (skierAction === SkierActions.SLIDE_LEFT) {
Expand All @@ -241,7 +240,6 @@ export class Skier extends Actor {
private emitCarvingParticles(speedPercentage: number, carvingIntensity: number, skierAction: SkierActions): void {
this.particlesEmitter.pos.y = -1;
this.particlesEmitter.radius = 1;
this.particlesEmitter.particle.life = 2000;
this.particlesEmitter.particle.minSpeed = 0;
this.particlesEmitter.particle.maxSpeed = 0;
this.particlesEmitter.particle.maxAngle = 1;
Expand All @@ -253,7 +251,6 @@ export class Skier extends Actor {
private emitBrakingParticles(speedPercentage: number): void {
this.particlesEmitter.pos.y = -10;
this.particlesEmitter.radius = 6;
this.particlesEmitter.particle.life = 2000;
this.particlesEmitter.particle.minSpeed = 10;
this.particlesEmitter.particle.maxSpeed = 50;
this.particlesEmitter.particle.maxAngle = 6;
Expand All @@ -265,13 +262,12 @@ export class Skier extends Actor {
private emitRidingParticles(speedPercentage: number): void {
this.particlesEmitter.pos.y = 0;
this.particlesEmitter.radius = 8;
this.particlesEmitter.particle.life = 500;
this.particlesEmitter.particle.minSpeed = 0;
this.particlesEmitter.particle.maxSpeed = 0;
this.particlesEmitter.particle.maxAngle = 6;
this.particlesEmitter.particle.minAngle = 3.4;
this.particlesEmitter.pos.x = 0;
this.particlesEmitter.emitParticles(speedPercentage);
// this.particlesEmitter.emitParticles(speedPercentage * 0.01);
}

private hasBreakingIntention(engine: Engine): boolean {
Expand Down
22 changes: 11 additions & 11 deletions src/app/game/models/track.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Gate } from '../actors/gate';
import type { StockableGate } from './stockable-gate';
import { StockableTrack } from './stockable-track';
import type { TrackStyles } from './track-styles.enum';

Expand All @@ -8,9 +8,16 @@ export class Track {
public builderVersion?: number;
public style: TrackStyles;
public date: Date;
public gates: Gate[];
public gates: StockableGate[];

constructor(id?: number, builderVersion?: number, name?: string, style?: TrackStyles, date?: Date, gates?: Gate[]) {
constructor(
id?: number,
builderVersion?: number,
name?: string,
style?: TrackStyles,
date?: Date,
gates?: StockableGate[]
) {
this.id = id;
this.builderVersion = builderVersion;
this.name = name!;
Expand All @@ -24,14 +31,7 @@ export class Track {
}

public toStockable(): StockableTrack {
return new StockableTrack(
this.id,
this.builderVersion,
this.name,
this.style,
this.date,
this.gates.map(gate => gate.getStockableGate())
);
return new StockableTrack(this.id, this.builderVersion, this.name, this.style, this.date, this.gates);
}

private static formatTrackName(name: string): string {
Expand Down
9 changes: 3 additions & 6 deletions src/app/game/scenes/race.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,9 +278,8 @@ export class Race extends Scene {
}

private buildTrack(track: Track): void {
const gates = track.gates.map(gate => {
const stockableGate = gate.getStockableGate();
return new Gate(
for (const stockableGate of track.gates) {
const gate = new Gate(
TrackBuilder.getGatesConfig(track.style),
vec(stockableGate.x, stockableGate.y),
stockableGate.width,
Expand All @@ -289,9 +288,7 @@ export class Race extends Scene {
stockableGate.isFinal,
stockableGate.sectorNumber
);
});
for (const gate of gates) {
this.gates?.push(gate);
this.gates.push(gate);
this.add(gate);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/app/game/utils/particles-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export class ParticlesBuilder {
minAngle: 3.4,
maxAngle: 6,
opacity: 0.7,
life: 2000,
life: 1000,
maxSize: 5,
minSize: 5,
startSize: 5,
Expand Down Expand Up @@ -47,6 +47,6 @@ export class ParticlesBuilder {
radius: 1,
emitRate: 1,
isEmitting: false
})
});
}
}
40 changes: 15 additions & 25 deletions src/app/game/utils/track-builder.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { type Vector, vec } from 'excalibur';
import { Gate } from '../actors/gate';
import { Config } from '../config';
import { Track } from '../models/track';
import type { StockableTrack } from '../models/stockable-track';
import { TrackStyles } from '../models/track-styles.enum';
import type { GatesConfig } from '../models/gates-config';
import { StockableGate } from '../models/stockable-gate';

export class TrackBuilder {
/**
Expand All @@ -24,11 +24,11 @@ export class TrackBuilder {
let nextGatePosition = TrackBuilder.getNextGatePosition(nextGateWidth, gatesConfig);

for (let index = 1; index < numberOfGates; index++) {
const gate = new Gate(
gatesConfig,
nextGatePosition,
nextGateWidth,
const gate = new StockableGate(
nextGatePosition.x,
nextGatePosition.y,
TrackBuilder.getGateColor(index, trackStyle),
nextGateWidth,
index,
false,
sectorGateNumbers.indexOf(index) + 1
Expand All @@ -50,27 +50,13 @@ export class TrackBuilder {
*/
public static buildTrack(stockableTrack: StockableTrack): Track {
('TrackBuilder - Rebuilding an existing track');
const gates: Gate[] = [];
for (const stockableGate of stockableTrack.gates) {
gates.push(
new Gate(
TrackBuilder.getGatesConfig(stockableTrack.style),
vec(stockableGate.x, stockableGate.y),
stockableGate.width,
stockableGate.color,
stockableGate.gateNumber,
stockableGate.isFinal,
stockableGate.sectorNumber
)
);
}
return new Track(
stockableTrack.id,
stockableTrack.builderVersion,
stockableTrack.name,
stockableTrack.style,
stockableTrack.date,
gates
stockableTrack.gates
);
}

Expand Down Expand Up @@ -102,12 +88,16 @@ export class TrackBuilder {
return gatesConfig.minWidth + Math.random() * (gatesConfig.maxWidth - gatesConfig.minWidth);
}

private static generateFinalGate(verticalPosition: number, gateNumber: number, gatesConfig: GatesConfig): Gate {
return new Gate(
gatesConfig,
vec(Config.FINAL_GATE_POSITION, verticalPosition),
Config.FINAL_GATE_WIDTH,
private static generateFinalGate(
verticalPosition: number,
gateNumber: number,
gatesConfig: GatesConfig
): StockableGate {
return new StockableGate(
Config.FINAL_GATE_POSITION,
verticalPosition,
'red',
Config.FINAL_GATE_WIDTH,
gateNumber,
true,
Config.SECTORS_PER_RACE + 1
Expand Down

0 comments on commit 3195487

Please sign in to comment.