Skip to content

Commit

Permalink
add <world>.center.[x|z] option, closes #20, supersedes #25
Browse files Browse the repository at this point in the history
  • Loading branch information
granny committed Feb 13, 2024
1 parent b679f27 commit 2c4f758
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,19 @@ public final class WorldConfig extends AbstractConfig {
The display position for the link box""")
public String UI_LINK = "bottomright";

@Key("center.x")
@Comment("""
The x coordinate for the map to load at.
A value of -1 will default to world spawn.""")
public int CENTER_X = -1;

@Key("center.z")
@Comment("""
The z coordinate for the map to load at.
A value of -1 will default to world spawn.""")
public int CENTER_Z = -1;


@Key("zoom.default")
@Comment("""
The default zoom when loading the map in browser.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ public void run() {
spawn.put("x", point.x());
spawn.put("z", point.z());

Map<String, Object> center = new LinkedHashMap<>();
center.put("x", config.CENTER_X);
center.put("z", config.CENTER_Z);

Map<String, Object> zoom = new LinkedHashMap<>();
zoom.put("default", config.ZOOM_DEFAULT);
zoom.put("maxOut", config.ZOOM_MAX_OUT);
Expand All @@ -124,6 +128,7 @@ public void run() {
settings.put("name", world.getName().replace(":", "-"));
settings.put("tileUpdateInterval", 10);
settings.put("spawn", spawn);
settings.put("center", center);
settings.put("zoom", zoom);
settings.put("ui", ui);

Expand Down
30 changes: 30 additions & 0 deletions webmap/src/settings/WorldSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export class WorldSettings {

private _tileUpdateInterval: number = 5;
private _spawn: Spawn = new Spawn(0, 0);
private _center: Center = new Center(-1, -1);
private _zoom: Zoom = new Zoom(0, 3, 2);
private _ui: UI = new UI()

Expand Down Expand Up @@ -59,6 +60,14 @@ export class WorldSettings {
this._spawn = spawn;
}

get center(): Center {
return this._center;
}

set center(center: Center) {
this._center = center;
}

get zoom(): Zoom {
return this._zoom;
}
Expand Down Expand Up @@ -97,6 +106,27 @@ export class Spawn {
}
}

/**
* Represents a world's center.
*/
export class Center {
private readonly _x: number;
private readonly _z: number;

constructor(x: number, z: number) {
this._x = x;
this._z = z;
}

get x(): number {
return this._x;
}

get z(): number {
return this._z;
}
}

/**
* Represents UI settings.
*/
Expand Down
7 changes: 6 additions & 1 deletion webmap/src/world/World.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {Pl3xMap} from "../Pl3xMap";
import {MarkerLayer} from "../layergroup/MarkerLayer";
import {BlockInfo} from "../palette/BlockInfo";
import {Label} from "../settings/Lang";
import {Spawn, WorldSettings, Zoom} from "../settings/WorldSettings";
import {Center, Spawn, WorldSettings, Zoom} from "../settings/WorldSettings";
import {DoubleTileLayer} from "../tilelayer/DoubleTileLayer";
import {WorldManager} from "./WorldManager";
import {fireCustomEvent, getBytes, getJSON} from "../util/Util";
Expand Down Expand Up @@ -63,6 +63,7 @@ export class World {

// copy settings values
this.settings.spawn = settings.spawn;
this.settings.center = settings.center;
this.settings.zoom = settings.zoom;
this.settings.tileUpdateInterval = settings.tileUpdateInterval;
this.settings.ui = settings.ui;
Expand Down Expand Up @@ -198,6 +199,10 @@ export class World {
return this.settings.spawn;
}

get center(): Center {
return this.settings.center;
}

get zoom(): Zoom {
return this.settings.zoom;
}
Expand Down
6 changes: 4 additions & 2 deletions webmap/src/world/WorldManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,11 @@ export class WorldManager {

world.resetRenderer(renderer);

const xCoord = world.center.x === -1 ? world.spawn.x : world.center.x;
const zCoord = world.center.z === -1 ? world.spawn.z : world.center.z;
this._pl3xmap.map.centerOn(
resetCoords ? world.spawn.x : getUrlParam('x', world.spawn.x),
resetCoords ? world.spawn.z : getUrlParam('z', world.spawn.z),
resetCoords ? xCoord : getUrlParam('x', xCoord),
resetCoords ? zCoord : getUrlParam('z', zCoord),
resetCoords ? world.zoom.default : getUrlParam('zoom', world.zoom.default)
);

Expand Down

0 comments on commit 2c4f758

Please sign in to comment.