Added SceneryObject, unified classes
+Added SceneryObject class. All objects like Track, TrackObject, Switch inherit from it. *Switches now use applyObject instead of applySwitch. All SceneryObejct based objects can be applied after scenery load
This commit is contained in:
parent
8fd4b4ff86
commit
f8725c0371
27
src/model/scenery-object.js
Normal file
27
src/model/scenery-object.js
Normal file
@ -0,0 +1,27 @@
|
||||
export default class SceneryObject {
|
||||
id;
|
||||
x;
|
||||
y;
|
||||
z;
|
||||
|
||||
constructor(id, x, y, z) {
|
||||
Object.assign(this, {
|
||||
id, x, y, z
|
||||
});
|
||||
}
|
||||
|
||||
static fromText(text) {
|
||||
console.error("SceneryObject.fromText not implemented");
|
||||
return null;
|
||||
}
|
||||
|
||||
applyObject(scenery) {
|
||||
|
||||
}
|
||||
|
||||
getRenderBounds() {
|
||||
return [
|
||||
{ x: this.x, z: this.z }
|
||||
];
|
||||
}
|
||||
}
|
||||
@ -23,7 +23,7 @@ export default class Scenery
|
||||
scenery.addObject(object);
|
||||
});
|
||||
|
||||
scenery.applySwitches();
|
||||
scenery.applyObjects();
|
||||
console.log(scenery);
|
||||
|
||||
return scenery;
|
||||
@ -42,9 +42,13 @@ export default class Scenery
|
||||
}
|
||||
}
|
||||
|
||||
applySwitches() {
|
||||
Object.values(this.objects.switches ?? []).forEach(sw => {
|
||||
sw.applySwitch(this);
|
||||
applyObjects() {
|
||||
Object.keys(this.objects).forEach(category => {
|
||||
Object.values(this.objects[category]).forEach(object => {
|
||||
if(object.applyObject) {
|
||||
object.applyObject(this);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@ -1,11 +1,8 @@
|
||||
import PointTrack from "./point-track.js";
|
||||
import SceneryObject from "./scenery-object.js";
|
||||
|
||||
export default class Switch {
|
||||
id;
|
||||
export default class Switch extends SceneryObject {
|
||||
model;
|
||||
x;
|
||||
y;
|
||||
z;
|
||||
rot;
|
||||
data;
|
||||
id_isolation;
|
||||
@ -20,9 +17,9 @@ export default class Switch {
|
||||
trackB;
|
||||
|
||||
constructor(id, model, x, y, z, rot, data, id_isolation, id_switch, maxspeed, derailspeed) {
|
||||
super(id, x, y, z);
|
||||
Object.assign(this, {
|
||||
id, model,
|
||||
x, y, z,
|
||||
model,
|
||||
rot, data,
|
||||
id_switch, id_isolation,
|
||||
maxspeed, derailspeed
|
||||
@ -49,7 +46,7 @@ export default class Switch {
|
||||
return sw;
|
||||
}
|
||||
|
||||
applySwitch(scenery) {
|
||||
applyObject(scenery) {
|
||||
if(this.applied) return; // already applied
|
||||
|
||||
this.outs = Switch.getOutsFromData(this.data);
|
||||
|
||||
@ -1,9 +1,7 @@
|
||||
export default class TrackObject {
|
||||
id;
|
||||
import SceneryObject from './scenery-object';
|
||||
|
||||
export default class TrackObject extends SceneryObject {
|
||||
prefab_name;
|
||||
x;
|
||||
y;
|
||||
z;
|
||||
rot;
|
||||
track_id;
|
||||
name;
|
||||
@ -12,10 +10,12 @@ export default class TrackObject {
|
||||
track;
|
||||
|
||||
constructor(id, prefab_name, x, y, z, rot, track_id, name) {
|
||||
super(id, x, y, z);
|
||||
Object.assign(this, {
|
||||
id, prefab_name,
|
||||
x, y, z,
|
||||
rot, track_id, name
|
||||
prefab_name,
|
||||
rot,
|
||||
track_id,
|
||||
name
|
||||
});
|
||||
}
|
||||
|
||||
@ -36,12 +36,12 @@ export default class TrackObject {
|
||||
}
|
||||
|
||||
applyObject(scenery) {
|
||||
/* TODO */
|
||||
}
|
||||
const track = scenery.getObject('tracks', this.track_id);
|
||||
if (!track) {
|
||||
console.warn(`TrackObject ${this.id} cannot be applied: track ${this.track_id} not found.`);
|
||||
return;
|
||||
}
|
||||
|
||||
getRenderBounds() {
|
||||
return [
|
||||
{ x: this.x, z: this.z }
|
||||
];
|
||||
this.track = track;
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,8 +1,6 @@
|
||||
export default class Track {
|
||||
id;
|
||||
x;
|
||||
y;
|
||||
z;
|
||||
import SceneryObject from "./scenery-object";
|
||||
|
||||
export default class Track extends SceneryObject {
|
||||
rot;
|
||||
len;
|
||||
r;
|
||||
@ -15,9 +13,8 @@ export default class Track {
|
||||
outs = [];
|
||||
|
||||
constructor(id, x, y, z, rot, len, r, previd, nextid, id_station, id_isolation, maxspeed, derailspeed) {
|
||||
super(id, x, y, z);
|
||||
Object.assign(this, {
|
||||
id,
|
||||
x, y, z,
|
||||
rot, len, r,
|
||||
id_station, id_isolation,
|
||||
maxspeed, derailspeed
|
||||
@ -35,10 +32,4 @@ export default class Track {
|
||||
const dist2 = pow2(x-this.points.x2)+pow2(z-this.points.z2);
|
||||
return (dist1 < dist2) ? [this.points.x1, y, this.points.z1] : [this.points.x2, y, this.points.z2];
|
||||
}
|
||||
|
||||
getRenderBounds() {
|
||||
return [
|
||||
{ x: this.x, z: this.z }
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user