Skip to content

Commit

Permalink
feat: add F35
Browse files Browse the repository at this point in the history
  • Loading branch information
vigneshd332 committed Jan 26, 2024
1 parent 0b01f24 commit 3bf3ea4
Show file tree
Hide file tree
Showing 8 changed files with 118 additions and 23 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ P.S You're free to use anything from this code, but make sure to give attributio

<ul>
<li><a href="https://skfb.ly/6sYKy">Star Wars: XQ6 Platform</a> by Daniel, <a href="https://skfb.ly/69vYB">Floating Platform</a> by Escoly and <a href="https://skfb.ly/XuQC">Space ship</a> by light_ua is licensed under <a href="http://creativecommons.org/licenses/by/4.0/">Creative Commons Attribution.</a></li>
<li><a href="https://skfb.ly/otLTC">"Sizzler SS-N-27 Kalibr 3M-54"</a> by mmickel is licensed under Creative Commons Attribution (http://creativecommons.org/licenses/by/4.0/).</li>
<li><a href="https://skfb.ly/otLTC">"Sizzler SS-N-27 Kalibr 3M-54"</a> by mmickel is licensed under Creative Commons Attribution.</li>
<li>Icons from <a href="https://www.flaticon.com/free-icons/keyboard" title="keyboard icons">Flaticon.</a></li>
<li><a href="https://skfb.ly/oFSnI">"Sukhoi Su-27 Flanker"</a> by TGSuperTop is licensed under Creative Commons Attribution (http://creativecommons.org/licenses/by/4.0/).</li>
<li><a href="https://skfb.ly/oFSnI">"Sukhoi Su-27 Flanker"</a> by TGSuperTop is licensed under Creative Commons Attribution.</li>
<li><a href="https://skfb.ly/6tF7y">"F-35"</a> by PetriParkkinen is licensed under Creative Commons Attribution.</li>
</ul>
Binary file added public/models/f35/f-35.glb
Binary file not shown.
9 changes: 9 additions & 0 deletions src/world/loaders/AircraftLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,12 @@ export async function loadFlankerModel(): Promise<THREE.Group> {
});
});
}

export async function loadF35Model(): Promise<THREE.Group> {
return new Promise((resolve) => {
const loader = new GLTFLoader();
loader.load("models/f35/f-35.glb", (gltf: GLTF) => {
resolve(gltf.scene);
});
});
}
1 change: 1 addition & 0 deletions src/world/loaders/LoadScene.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export async function loadSceneAssets(
spawn.rotation,
spawn.final_takeoff_offset,
spawn.launch_delay,
spawn.vtol,
spawn.post_takeoff_action,
spawn.pre_takeoff_action
)
Expand Down
59 changes: 41 additions & 18 deletions src/world/objects/Aircraft.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,22 @@ export enum AircraftState {
dead,
}

const AircraftStateActions: { [key in AircraftState]: () => void } = {
[AircraftState.takeoff]: function (this: Aircraft) {
// Launch
this.launch();
},
[AircraftState.takeoff_climb]: function (this: Aircraft) {
this.vtol ? this.vtolTakeOff() : this.correctTakeOffAltitude();
},
[AircraftState.fly]: function (this: Aircraft) {
this.post_takeoff_action();
},
[AircraftState.landing]: function () {},
[AircraftState.firing]: function () {},
[AircraftState.dead]: function () {},
};

export class Aircraft {
aircraft: THREE.Group;
launch_time: number;
Expand All @@ -17,6 +33,7 @@ export class Aircraft {
initial_rotation: THREE.Vector3;
state: AircraftState;
velocity: number;
vtol: boolean;
pre_takeoff_action: (this: Aircraft) => void;
post_takeoff_action: (this: Aircraft) => void;

Expand All @@ -28,6 +45,7 @@ export class Aircraft {
_rotation: THREE.Vector3,
_final_takeoff_offset: number,
_launch_delay: number = 0,
vtol: boolean = false,
_post_takeoff_action_callback: (this: Aircraft) => void = () => {},
_pre_takeoff_action_callback: (this: Aircraft) => void = () => {}
) {
Expand All @@ -39,53 +57,58 @@ export class Aircraft {

this.aircraft = model;
this.launch_time = new Date().getTime() + _launch_delay * 1000;
this.state = AircraftState.takeoff;
this.state = vtol ? AircraftState.takeoff_climb : AircraftState.takeoff;
this.final_takeoff_offset = _final_takeoff_offset;
this.current_takeoff_offset = 0;
this.initial_rotation = _rotation;
this.velocity = 0.1;
this.vtol = vtol;
this.pre_takeoff_action = _pre_takeoff_action_callback;
this.post_takeoff_action = _post_takeoff_action_callback;
}

launch(): boolean {
launch(): void {
if (this.current_takeoff_offset > this.final_takeoff_offset) {
if (this.state === AircraftState.takeoff)
this.state = AircraftState.takeoff_climb;
return true;
}
this.aircraft.translateZ(this.velocity);
if (this.velocity < 15) this.velocity += 0.05;
this.current_takeoff_offset += 0.1;
if (this.current_takeoff_offset > 0.8 * this.final_takeoff_offset)
this.aircraft.rotateX(-0.005);
return false;
}

correctTakeOffAltitude(): boolean {
if (this.state === AircraftState.takeoff) return false;
correctTakeOffAltitude(): void {
// Takeoff Climb
if (this.aircraft.position.y < 3000) {
this.aircraft.translateZ(this.velocity);
return;
}
// Reversing takeoff rotation
if (this.aircraft.position.y >= 3000 && this.aircraft.rotation.x < 0) {
this.state = AircraftState.fly;
this.aircraft.rotateX(0.005);
return false;
this.aircraft.translateZ(this.velocity);
return;
}
this.state = AircraftState.fly;
}

vtolTakeOff(): void {
if (this.aircraft.position.y >= 3000) this.state = AircraftState.fly;
if (this.aircraft.position.y >= 1000) {
if (this.velocity < 10) this.velocity += 0.05;
this.aircraft.translateZ(this.velocity);
}
return true;
this.aircraft.translateY(2);
}

update() {
// Launch Delay
if (new Date().getTime() < this.launch_time) return;
// Pre Takeoff Action
this.pre_takeoff_action();
// Take Off
if (!this.launch()) return;
// Climb to Altitude
this.aircraft.translateZ(this.velocity);
// Level Off at Altitude
if (!this.correctTakeOffAltitude()) return;
if (this.state === AircraftState.takeoff_climb) return;
// Post Takeoff Action
this.post_takeoff_action();
// State Action
AircraftStateActions[this.state].call(this);
}
}
62 changes: 61 additions & 1 deletion src/world/spawns/Aircraft.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import * as THREE from "three";
import { Aircraft } from "../objects/Aircraft";
import { loadFlankerModel } from "../loaders/AircraftLoader";
import { loadF35Model, loadFlankerModel } from "../loaders/AircraftLoader";

export async function getFriendlyCarrierAircraftSpawns(): Promise<
CarrierAircraftSpawn[]
> {
const flankerModel = await loadFlankerModel();
const f35Model = await loadF35Model();

return [
{
Expand All @@ -15,11 +16,13 @@ export async function getFriendlyCarrierAircraftSpawns(): Promise<
rotation: new THREE.Vector3(0, Math.PI / 5, 0),
final_takeoff_offset: 35,
launch_delay: 20,
vtol: false,
post_takeoff_action: function (this: Aircraft) {
if (this.aircraft.rotation.z < this.initial_rotation.z + 0.8) {
this.aircraft.rotateZ(0.005);
}
this.aircraft.rotation.y -= 0.001;
this.aircraft.translateZ(this.velocity);
},
pre_takeoff_action: function () {},
},
Expand All @@ -30,11 +33,13 @@ export async function getFriendlyCarrierAircraftSpawns(): Promise<
rotation: new THREE.Vector3(0, Math.PI / 5, 0),
final_takeoff_offset: 23,
launch_delay: 30,
vtol: false,
post_takeoff_action: function (this: Aircraft) {
if (this.aircraft.rotation.z < this.initial_rotation.z + 0.8) {
this.aircraft.rotateZ(0.005);
}
this.aircraft.rotation.y -= 0.001;
this.aircraft.translateZ(this.velocity);
},
pre_takeoff_action: function () {},
},
Expand All @@ -45,11 +50,13 @@ export async function getFriendlyCarrierAircraftSpawns(): Promise<
rotation: new THREE.Vector3(0, Math.PI / 8, 0),
final_takeoff_offset: 23,
launch_delay: 7,
vtol: false,
post_takeoff_action: function (this: Aircraft) {
if (this.aircraft.rotation.z > this.initial_rotation.z - 0.8) {
this.aircraft.rotateZ(-0.005);
}
this.aircraft.rotation.y += 0.001;
this.aircraft.translateZ(this.velocity);
},
pre_takeoff_action: function () {},
},
Expand All @@ -60,11 +67,64 @@ export async function getFriendlyCarrierAircraftSpawns(): Promise<
rotation: new THREE.Vector3(0, Math.PI / 6, 0),
final_takeoff_offset: 35,
launch_delay: 40,
vtol: false,
post_takeoff_action: function (this: Aircraft) {
if (this.aircraft.rotation.z > this.initial_rotation.z - 0.8) {
this.aircraft.rotateZ(-0.005);
}
this.aircraft.rotation.y += 0.001;
this.aircraft.translateZ(this.velocity);
},
pre_takeoff_action: function () {},
},
{
model: f35Model,
position: new THREE.Vector3(3900, 250, -2200),
scale: new THREE.Vector3(1000, 1000, 1000),
rotation: new THREE.Vector3(0, Math.PI / 6, 0),
final_takeoff_offset: 0,
launch_delay: 7,
vtol: true,
post_takeoff_action: function (this: Aircraft) {
if (this.aircraft.rotation.z > this.initial_rotation.z - 0.8) {
this.aircraft.rotateZ(-0.005);
}
this.aircraft.rotation.y += 0.001;
this.aircraft.translateZ(this.velocity);
},
pre_takeoff_action: function () {},
},
{
model: f35Model,
position: new THREE.Vector3(4000, 250, -2900),
scale: new THREE.Vector3(1000, 1000, 1000),
rotation: new THREE.Vector3(0, Math.PI / 6, 0),
final_takeoff_offset: 0,
launch_delay: 20,
vtol: true,
post_takeoff_action: function (this: Aircraft) {
if (this.aircraft.rotation.z < this.initial_rotation.z + 0.8) {
this.aircraft.rotateZ(+0.005);
}
this.aircraft.rotation.y -= 0.001;
this.aircraft.translateZ(this.velocity);
},
pre_takeoff_action: function () {},
},
{
model: f35Model,
position: new THREE.Vector3(4400, 250, -2200),
scale: new THREE.Vector3(1000, 1000, 1000),
rotation: new THREE.Vector3(0, Math.PI / 6, 0),
final_takeoff_offset: 0,
launch_delay: 35,
vtol: true,
post_takeoff_action: function (this: Aircraft) {
if (this.aircraft.rotation.z > this.initial_rotation.z - 0.8) {
this.aircraft.rotateZ(-0.005);
}
this.aircraft.rotation.y += 0.001;
this.aircraft.translateZ(this.velocity);
},
pre_takeoff_action: function () {},
},
Expand Down
1 change: 1 addition & 0 deletions src/world/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ interface CarrierAircraftSpawn {
rotation: THREE.Vector3;
final_takeoff_offset: number;
launch_delay: number;
vtol: boolean;
pre_takeoff_action: (this: Aircraft) => void;
post_takeoff_action: (this: Aircraft) => void;
}
Expand Down
4 changes: 2 additions & 2 deletions src/world/world.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ export default async function init(setLoaded: (loaded: boolean) => void) {

THREE.DefaultLoadingManager.onProgress = function (_item, loaded, _) {
const progressBar = document.getElementById("LoaderText") as Element;
progressBar.innerHTML = `${loaded} / 54 objects loaded...`;
if (loaded === 54) setLoaded(true);
progressBar.innerHTML = `${loaded} / 57 objects loaded...`;
if (loaded === 57) setLoaded(true);
};

scene = new THREE.Scene();
Expand Down

0 comments on commit 3bf3ea4

Please sign in to comment.