Skip to content

Commit 74bd3aa

Browse files
committed
docs: adjust aviator demo agian
1 parent 0beec44 commit 74bd3aa

File tree

6 files changed

+21
-27
lines changed

6 files changed

+21
-27
lines changed

apps/kitchen-sink/src/app/misc/aviator/airplane/airplane.ts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {
2+
afterNextRender,
23
ChangeDetectionStrategy,
34
Component,
45
CUSTOM_ELEMENTS_SCHEMA,
@@ -122,15 +123,9 @@ export class Airplane {
122123
constructor() {
123124
const gameStore = inject(GameStore);
124125

125-
effect(
126-
() => {
127-
const airplane = this.airplaneRef().nativeElement;
128-
if (!airplane) return;
129-
130-
gameStore.airplane.set(airplane);
131-
},
132-
{ allowSignalWrites: true },
133-
);
126+
afterNextRender(() => {
127+
gameStore.airplaneRef = this.airplaneRef;
128+
});
134129

135130
effect(() => {
136131
const suspensionGeometry = this.suspensionGeometryRef()?.nativeElement;

apps/kitchen-sink/src/app/misc/aviator/airplane/pilot.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,16 +115,14 @@ export class Pilot {
115115
});
116116

117117
effect(() => {
118-
const sideGeometries = this.sideGeometryRefs();
119-
for (const sideGeometry of sideGeometries) {
118+
for (const sideGeometry of this.sideGeometryRefs()) {
120119
sideGeometry.nativeElement.applyMatrix4(new Matrix4().makeTranslation(-6, 0, 0));
121120
}
122121
});
123122

124123
let angleHairs = 0;
125124
injectBeforeRender(({ delta }) => {
126125
const topHair = this.topHairRef().nativeElement;
127-
if (!topHair) return;
128126

129127
for (let i = 0; i < topHair.children.length; i++) {
130128
const child = topHair.children[i];

apps/kitchen-sink/src/app/misc/aviator/airplane/propeller.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,15 @@ export class Propeller {
6060
const propellerGeometry = this.propellerGeometryRef()?.nativeElement;
6161
if (!propellerGeometry) return;
6262

63-
propellerGeometry.attributes['position'].array[4 * 3 + 1] -= 5;
64-
propellerGeometry.attributes['position'].array[4 * 3 + 2] += 5;
65-
propellerGeometry.attributes['position'].array[5 * 3 + 1] -= 5;
66-
propellerGeometry.attributes['position'].array[5 * 3 + 2] -= 5;
67-
propellerGeometry.attributes['position'].array[6 * 3 + 1] += 5;
68-
propellerGeometry.attributes['position'].array[6 * 3 + 2] += 5;
69-
propellerGeometry.attributes['position'].array[7 * 3 + 1] += 5;
70-
propellerGeometry.attributes['position'].array[7 * 3 + 2] -= 5;
63+
const positions = propellerGeometry.attributes['position'].array;
64+
positions[4 * 3 + 1] -= 5;
65+
positions[4 * 3 + 2] += 5;
66+
positions[5 * 3 + 1] -= 5;
67+
positions[5 * 3 + 2] -= 5;
68+
positions[6 * 3 + 1] += 5;
69+
positions[6 * 3 + 2] += 5;
70+
positions[7 * 3 + 1] += 5;
71+
positions[7 * 3 + 2] -= 5;
7172
});
7273

7374
injectBeforeRender(({ delta }) => {

apps/kitchen-sink/src/app/misc/aviator/collectible/coin.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,15 @@ export class Coin extends Collectible {
5555
super();
5656
injectBeforeRender(({ delta }) => {
5757
const coin = this.coinRef().nativeElement;
58-
if (!coin) return;
5958

6059
this.rotateAroundSea(coin, delta);
61-
6260
coin.rotation.x += Math.random() * 0.1;
6361
coin.rotation.y += Math.random() * 0.1;
6462

65-
const airplane = this.gameStore.airplane();
63+
const airplaneRef = this.gameStore.airplaneRef;
64+
if (!airplaneRef) return;
65+
66+
const airplane = airplaneRef().nativeElement;
6667
if (!airplane) return;
6768

6869
if (this.collide(airplane, coin, COIN_DISTANCE_TOLERANCE)) {

apps/kitchen-sink/src/app/misc/aviator/game.store.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import { Injectable, signal } from '@angular/core';
1+
import { ElementRef, Injectable, Signal, signal } from '@angular/core';
22
import { Object3D } from 'three';
33

44
@Injectable()
55
export class GameStore {
66
coins = signal(0);
7-
airplane = signal<Object3D | null>(null);
7+
airplaneRef?: Signal<ElementRef<Object3D>>;
88

99
// NOTE: these aren't signals because they are game state used in the animation loop.
1010
state = {

apps/kitchen-sink/src/app/misc/aviator/sky/sky.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,8 @@ export class Sky {
4040
private skyRef = viewChild.required<ElementRef<Object3D>>('sky');
4141
private clouds = viewChildren(Cloud);
4242
private cloudObjects = computed(() => {
43-
const clouds = this.clouds();
4443
const objects: Object3D[] = [];
45-
for (const cloud of clouds) {
44+
for (const cloud of this.clouds()) {
4645
objects.push(cloud.cloudRef().nativeElement);
4746
}
4847
return objects;

0 commit comments

Comments
 (0)