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.addObject(object);
|
||||||
});
|
});
|
||||||
|
|
||||||
scenery.applySwitches();
|
scenery.applyObjects();
|
||||||
console.log(scenery);
|
console.log(scenery);
|
||||||
|
|
||||||
return scenery;
|
return scenery;
|
||||||
@ -42,9 +42,13 @@ export default class Scenery
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
applySwitches() {
|
applyObjects() {
|
||||||
Object.values(this.objects.switches ?? []).forEach(sw => {
|
Object.keys(this.objects).forEach(category => {
|
||||||
sw.applySwitch(this);
|
Object.values(this.objects[category]).forEach(object => {
|
||||||
|
if(object.applyObject) {
|
||||||
|
object.applyObject(this);
|
||||||
|
}
|
||||||
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -1,11 +1,8 @@
|
|||||||
import PointTrack from "./point-track.js";
|
import PointTrack from "./point-track.js";
|
||||||
|
import SceneryObject from "./scenery-object.js";
|
||||||
|
|
||||||
export default class Switch {
|
export default class Switch extends SceneryObject {
|
||||||
id;
|
|
||||||
model;
|
model;
|
||||||
x;
|
|
||||||
y;
|
|
||||||
z;
|
|
||||||
rot;
|
rot;
|
||||||
data;
|
data;
|
||||||
id_isolation;
|
id_isolation;
|
||||||
@ -20,9 +17,9 @@ export default class Switch {
|
|||||||
trackB;
|
trackB;
|
||||||
|
|
||||||
constructor(id, model, x, y, z, rot, data, id_isolation, id_switch, maxspeed, derailspeed) {
|
constructor(id, model, x, y, z, rot, data, id_isolation, id_switch, maxspeed, derailspeed) {
|
||||||
|
super(id, x, y, z);
|
||||||
Object.assign(this, {
|
Object.assign(this, {
|
||||||
id, model,
|
model,
|
||||||
x, y, z,
|
|
||||||
rot, data,
|
rot, data,
|
||||||
id_switch, id_isolation,
|
id_switch, id_isolation,
|
||||||
maxspeed, derailspeed
|
maxspeed, derailspeed
|
||||||
@ -49,7 +46,7 @@ export default class Switch {
|
|||||||
return sw;
|
return sw;
|
||||||
}
|
}
|
||||||
|
|
||||||
applySwitch(scenery) {
|
applyObject(scenery) {
|
||||||
if(this.applied) return; // already applied
|
if(this.applied) return; // already applied
|
||||||
|
|
||||||
this.outs = Switch.getOutsFromData(this.data);
|
this.outs = Switch.getOutsFromData(this.data);
|
||||||
|
|||||||
@ -1,9 +1,7 @@
|
|||||||
export default class TrackObject {
|
import SceneryObject from './scenery-object';
|
||||||
id;
|
|
||||||
|
export default class TrackObject extends SceneryObject {
|
||||||
prefab_name;
|
prefab_name;
|
||||||
x;
|
|
||||||
y;
|
|
||||||
z;
|
|
||||||
rot;
|
rot;
|
||||||
track_id;
|
track_id;
|
||||||
name;
|
name;
|
||||||
@ -12,10 +10,12 @@ export default class TrackObject {
|
|||||||
track;
|
track;
|
||||||
|
|
||||||
constructor(id, prefab_name, x, y, z, rot, track_id, name) {
|
constructor(id, prefab_name, x, y, z, rot, track_id, name) {
|
||||||
|
super(id, x, y, z);
|
||||||
Object.assign(this, {
|
Object.assign(this, {
|
||||||
id, prefab_name,
|
prefab_name,
|
||||||
x, y, z,
|
rot,
|
||||||
rot, track_id, name
|
track_id,
|
||||||
|
name
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -36,12 +36,12 @@ export default class TrackObject {
|
|||||||
}
|
}
|
||||||
|
|
||||||
applyObject(scenery) {
|
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() {
|
this.track = track;
|
||||||
return [
|
|
||||||
{ x: this.x, z: this.z }
|
|
||||||
];
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,8 +1,6 @@
|
|||||||
export default class Track {
|
import SceneryObject from "./scenery-object";
|
||||||
id;
|
|
||||||
x;
|
export default class Track extends SceneryObject {
|
||||||
y;
|
|
||||||
z;
|
|
||||||
rot;
|
rot;
|
||||||
len;
|
len;
|
||||||
r;
|
r;
|
||||||
@ -15,9 +13,8 @@ export default class Track {
|
|||||||
outs = [];
|
outs = [];
|
||||||
|
|
||||||
constructor(id, x, y, z, rot, len, r, previd, nextid, id_station, id_isolation, maxspeed, derailspeed) {
|
constructor(id, x, y, z, rot, len, r, previd, nextid, id_station, id_isolation, maxspeed, derailspeed) {
|
||||||
|
super(id, x, y, z);
|
||||||
Object.assign(this, {
|
Object.assign(this, {
|
||||||
id,
|
|
||||||
x, y, z,
|
|
||||||
rot, len, r,
|
rot, len, r,
|
||||||
id_station, id_isolation,
|
id_station, id_isolation,
|
||||||
maxspeed, derailspeed
|
maxspeed, derailspeed
|
||||||
@ -35,10 +32,4 @@ export default class Track {
|
|||||||
const dist2 = pow2(x-this.points.x2)+pow2(z-this.points.z2);
|
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];
|
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