one step forward, one step back

This commit is contained in:
2025-02-09 05:23:30 -07:00
parent 3d4596f8fb
commit 68eec35ea2
11 changed files with 797 additions and 49 deletions

55
train/cars.ts Normal file
View File

@@ -0,0 +1,55 @@
import { Vector } from "https://jsr.io/@bearmetal/doodler/0.0.4/geometry/vector.ts";
import { TrainCar } from "./train.ts";
import { ResourceManager } from "../lib/resources.ts";
import { getContextItem } from "../lib/context.ts";
export class Tender extends TrainCar {
constructor() {
const resources = getContextItem<ResourceManager>("resources");
super(25, resources.get<HTMLImageElement>("engine-sprites")!, 40, 20, {
at: new Vector(80, 0),
width: 40,
height: 20,
});
}
}
export class Tank extends TrainCar {
constructor() {
const resources = getContextItem<ResourceManager>("resources");
super(50, resources.get<HTMLImageElement>("engine-sprites")!, 70, 20, {
at: new Vector(80, 20),
width: 70,
height: 20,
});
}
}
export class YellowDumpCar extends TrainCar {
constructor() {
const resources = getContextItem<ResourceManager>("resources");
super(50, resources.get<HTMLImageElement>("engine-sprites")!, 70, 20, {
at: new Vector(80, 40),
width: 70,
height: 20,
});
}
}
export class GrayDumpCar extends TrainCar {
constructor() {
const resources = getContextItem<ResourceManager>("resources");
super(50, resources.get<HTMLImageElement>("engine-sprites")!, 70, 20, {
at: new Vector(80, 60),
width: 70,
height: 20,
});
}
}
export class NullCar extends TrainCar {
constructor() {
const resources = getContextItem<ResourceManager>("resources");
super(50, resources.get<HTMLImageElement>("engine-sprites")!, 70, 20, {
at: new Vector(80, 80),
width: 70,
height: 20,
});
}
}

55
train/engines.ts Normal file
View File

@@ -0,0 +1,55 @@
import { Vector } from "@bearmetal/doodler";
import { TrainCar } from "./train.ts";
import { getContextItem } from "../lib/context.ts";
import { ResourceManager } from "../lib/resources.ts";
export class RedEngine extends TrainCar {
constructor() {
const resources = getContextItem<ResourceManager>("resources");
super(55, resources.get<HTMLImageElement>("engine-sprites")!, 80, 20, {
at: new Vector(0, 60),
width: 80,
height: 20,
});
}
}
export class PurpleEngine extends TrainCar {
constructor() {
const resources = getContextItem<ResourceManager>("resources");
super(55, resources.get<HTMLImageElement>("engine-sprites")!, 80, 20, {
at: new Vector(0, 60),
width: 80,
height: 20,
});
}
}
export class GreenEngine extends TrainCar {
constructor() {
const resources = getContextItem<ResourceManager>("resources");
super(55, resources.get<HTMLImageElement>("engine-sprites")!, 80, 20, {
at: new Vector(0, 40),
width: 80,
height: 20,
});
}
}
export class GrayEngine extends TrainCar {
constructor() {
const resources = getContextItem<ResourceManager>("resources");
super(55, resources.get<HTMLImageElement>("engine-sprites")!, 80, 20, {
at: new Vector(0, 20),
width: 80,
height: 20,
});
}
}
export class BlueEngine extends TrainCar {
constructor() {
const resources = getContextItem<ResourceManager>("resources");
super(55, resources.get<HTMLImageElement>("engine-sprites")!, 80, 20, {
at: new Vector(0, 0),
width: 80,
height: 20,
});
}
}

View File

@@ -1,47 +1,49 @@
import { ComplexPath, PathSegment } from "../math/path.ts";
import { Follower } from "../physics/follower.ts";
import { Mover } from "../physics/mover.ts";
import { Spline, Track } from "../track.ts";
import { getContextItem } from "../lib/context.ts";
import { Doodler, Vector } from "@bearmetal/doodler";
import { Spline, TrackSegment } from "../track/system.ts";
import { ResourceManager } from "../lib/resources.ts";
export class Train {
nodes: Vector[] = [];
cars: TrainCar[] = [];
path: Spline<Track>;
path: Spline<TrackSegment>;
t: number;
engineLength = 40;
spacing = 30;
speed = 0;
speed = 10;
constructor(track: Spline<Track>, cars: TrainCar[] = []) {
constructor(track: Spline<TrackSegment>, cars: TrainCar[]) {
this.path = track;
this.t = 0;
this.nodes.push(this.path.followEvenPoints(this.t));
this.nodes.push(this.path.followEvenPoints(this.t - this.real2Track(40)));
const engineSprites = document.getElementById(
"engine-sprites",
)! as HTMLImageElement;
this.cars.push(
new TrainCar(
55,
engineSprites,
80,
20,
{ at: new Vector(0, 60), width: 80, height: 20 },
),
new TrainCar(
25,
engineSprites,
40,
20,
{ at: new Vector(80, 0), width: 40, height: 20 },
),
);
const resources = getContextItem<ResourceManager>("resources");
const engineSprites = resources.get<HTMLImageElement>("engine-sprites")!;
console.log(engineSprites);
this.cars = cars;
// this.cars.push(
// new TrainCar(
// 55,
// engineSprites,
// 80,
// 20,
// { at: new Vector(0, 60), width: 80, height: 20 },
// ),
// new TrainCar(
// 25,
// engineSprites,
// 40,
// 20,
// { at: new Vector(80, 0), width: 40, height: 20 },
// ),
// );
this.cars[0].points = this.nodes.map((n) => n) as [Vector, Vector];
this.cars[1].points = this.nodes.map((n) => n) as [Vector, Vector];
let currentOffset = 40;
@@ -66,14 +68,17 @@ export class Train {
currentOffset += car.length;
b.set(this.path.followEvenPoints(this.t - currentOffset));
currentOffset += this.spacing;
car.draw();
// car.draw();
}
// this.draw();
}
// draw() {
// const doodler = getContextItem<Doodler>("doodler");
// this.path.draw();
// for (const [i, node] of this.nodes.entries()) {
// doodler.drawCircle(node.point, 10, { color: 'purple', weight: 3 })
// // doodler.drawCircle(node, 10, { color: "purple", weight: 3 });
// doodler.fillCircle(node, 2, { color: "purple" });
// // const next = this.nodes[i + 1];
// // if (next) {
// // const to = Vector.sub(node.point, next.point);